fib_trie.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Robert Olsson <robert.olsson@its.uu.se> Uppsala Universitet
  5. * & Swedish University of Agricultural Sciences.
  6. *
  7. * Jens Laas <jens.laas@data.slu.se> Swedish University of
  8. * Agricultural Sciences.
  9. *
  10. * Hans Liss <hans.liss@its.uu.se> Uppsala Universitet
  11. *
  12. * This work is based on the LPC-trie which is originally described in:
  13. *
  14. * An experimental study of compression methods for dynamic tries
  15. * Stefan Nilsson and Matti Tikkanen. Algorithmica, 33(1):19-33, 2002.
  16. * https://www.csc.kth.se/~snilsson/software/dyntrie2/
  17. *
  18. * IP-address lookup using LC-tries. Stefan Nilsson and Gunnar Karlsson
  19. * IEEE Journal on Selected Areas in Communications, 17(6):1083-1092, June 1999
  20. *
  21. * Code from fib_hash has been reused which includes the following header:
  22. *
  23. * INET An implementation of the TCP/IP protocol suite for the LINUX
  24. * operating system. INET is implemented using the BSD Socket
  25. * interface as the means of communication with the user level.
  26. *
  27. * IPv4 FIB: lookup engine and maintenance routines.
  28. *
  29. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  30. *
  31. * Substantial contributions to this work comes from:
  32. *
  33. * David S. Miller, <davem@davemloft.net>
  34. * Stephen Hemminger <shemminger@osdl.org>
  35. * Paul E. McKenney <paulmck@us.ibm.com>
  36. * Patrick McHardy <kaber@trash.net>
  37. */
  38. #include <linux/cache.h>
  39. #include <linux/uaccess.h>
  40. #include <linux/bitops.h>
  41. #include <linux/types.h>
  42. #include <linux/kernel.h>
  43. #include <linux/mm.h>
  44. #include <linux/string.h>
  45. #include <linux/socket.h>
  46. #include <linux/sockios.h>
  47. #include <linux/errno.h>
  48. #include <linux/in.h>
  49. #include <linux/inet.h>
  50. #include <linux/inetdevice.h>
  51. #include <linux/netdevice.h>
  52. #include <linux/if_arp.h>
  53. #include <linux/proc_fs.h>
  54. #include <linux/rcupdate.h>
  55. #include <linux/rcupdate_wait.h>
  56. #include <linux/skbuff.h>
  57. #include <linux/netlink.h>
  58. #include <linux/init.h>
  59. #include <linux/list.h>
  60. #include <linux/slab.h>
  61. #include <linux/export.h>
  62. #include <linux/vmalloc.h>
  63. #include <linux/notifier.h>
  64. #include <net/net_namespace.h>
  65. #include <net/inet_dscp.h>
  66. #include <net/ip.h>
  67. #include <net/protocol.h>
  68. #include <net/route.h>
  69. #include <net/tcp.h>
  70. #include <net/sock.h>
  71. #include <net/ip_fib.h>
  72. #include <net/fib_notifier.h>
  73. #include <trace/events/fib.h>
  74. #include "fib_lookup.h"
  75. static int call_fib_entry_notifier(struct notifier_block *nb,
  76. enum fib_event_type event_type, u32 dst,
  77. int dst_len, struct fib_alias *fa,
  78. struct netlink_ext_ack *extack)
  79. {
  80. struct fib_entry_notifier_info info = {
  81. .info.extack = extack,
  82. .dst = dst,
  83. .dst_len = dst_len,
  84. .fi = fa->fa_info,
  85. .dscp = fa->fa_dscp,
  86. .type = fa->fa_type,
  87. .tb_id = fa->tb_id,
  88. };
  89. return call_fib4_notifier(nb, event_type, &info.info);
  90. }
  91. static int call_fib_entry_notifiers(struct net *net,
  92. enum fib_event_type event_type, u32 dst,
  93. int dst_len, struct fib_alias *fa,
  94. struct netlink_ext_ack *extack)
  95. {
  96. struct fib_entry_notifier_info info = {
  97. .info.extack = extack,
  98. .dst = dst,
  99. .dst_len = dst_len,
  100. .fi = fa->fa_info,
  101. .dscp = fa->fa_dscp,
  102. .type = fa->fa_type,
  103. .tb_id = fa->tb_id,
  104. };
  105. return call_fib4_notifiers(net, event_type, &info.info);
  106. }
  107. #define MAX_STAT_DEPTH 32
  108. #define KEYLENGTH (8*sizeof(t_key))
  109. #define KEY_MAX ((t_key)~0)
  110. typedef unsigned int t_key;
  111. #define IS_TRIE(n) ((n)->pos >= KEYLENGTH)
  112. #define IS_TNODE(n) ((n)->bits)
  113. #define IS_LEAF(n) (!(n)->bits)
  114. struct key_vector {
  115. t_key key;
  116. unsigned char pos; /* 2log(KEYLENGTH) bits needed */
  117. unsigned char bits; /* 2log(KEYLENGTH) bits needed */
  118. unsigned char slen;
  119. union {
  120. /* This list pointer if valid if (pos | bits) == 0 (LEAF) */
  121. struct hlist_head leaf;
  122. /* This array is valid if (pos | bits) > 0 (TNODE) */
  123. DECLARE_FLEX_ARRAY(struct key_vector __rcu *, tnode);
  124. };
  125. };
  126. struct tnode {
  127. struct rcu_head rcu;
  128. t_key empty_children; /* KEYLENGTH bits needed */
  129. t_key full_children; /* KEYLENGTH bits needed */
  130. struct key_vector __rcu *parent;
  131. struct key_vector kv[1];
  132. #define tn_bits kv[0].bits
  133. };
  134. #define TNODE_SIZE(n) offsetof(struct tnode, kv[0].tnode[n])
  135. #define LEAF_SIZE TNODE_SIZE(1)
  136. #ifdef CONFIG_IP_FIB_TRIE_STATS
  137. struct trie_use_stats {
  138. unsigned int gets;
  139. unsigned int backtrack;
  140. unsigned int semantic_match_passed;
  141. unsigned int semantic_match_miss;
  142. unsigned int null_node_hit;
  143. unsigned int resize_node_skipped;
  144. };
  145. #endif
  146. struct trie_stat {
  147. unsigned int totdepth;
  148. unsigned int maxdepth;
  149. unsigned int tnodes;
  150. unsigned int leaves;
  151. unsigned int nullpointers;
  152. unsigned int prefixes;
  153. unsigned int nodesizes[MAX_STAT_DEPTH];
  154. };
  155. struct trie {
  156. struct key_vector kv[1];
  157. #ifdef CONFIG_IP_FIB_TRIE_STATS
  158. struct trie_use_stats __percpu *stats;
  159. #endif
  160. };
  161. static struct key_vector *resize(struct trie *t, struct key_vector *tn);
  162. static unsigned int tnode_free_size;
  163. /*
  164. * synchronize_rcu after call_rcu for outstanding dirty memory; it should be
  165. * especially useful before resizing the root node with PREEMPT_NONE configs;
  166. * the value was obtained experimentally, aiming to avoid visible slowdown.
  167. */
  168. unsigned int sysctl_fib_sync_mem = 512 * 1024;
  169. unsigned int sysctl_fib_sync_mem_min = 64 * 1024;
  170. unsigned int sysctl_fib_sync_mem_max = 64 * 1024 * 1024;
  171. static struct kmem_cache *fn_alias_kmem __ro_after_init;
  172. static struct kmem_cache *trie_leaf_kmem __ro_after_init;
  173. static inline struct tnode *tn_info(struct key_vector *kv)
  174. {
  175. return container_of(kv, struct tnode, kv[0]);
  176. }
  177. /* caller must hold RTNL */
  178. #define node_parent(tn) rtnl_dereference(tn_info(tn)->parent)
  179. #define get_child(tn, i) rtnl_dereference((tn)->tnode[i])
  180. /* caller must hold RCU read lock or RTNL */
  181. #define node_parent_rcu(tn) rcu_dereference_rtnl(tn_info(tn)->parent)
  182. #define get_child_rcu(tn, i) rcu_dereference_rtnl((tn)->tnode[i])
  183. /* wrapper for rcu_assign_pointer */
  184. static inline void node_set_parent(struct key_vector *n, struct key_vector *tp)
  185. {
  186. if (n)
  187. rcu_assign_pointer(tn_info(n)->parent, tp);
  188. }
  189. #define NODE_INIT_PARENT(n, p) RCU_INIT_POINTER(tn_info(n)->parent, p)
  190. /* This provides us with the number of children in this node, in the case of a
  191. * leaf this will return 0 meaning none of the children are accessible.
  192. */
  193. static inline unsigned long child_length(const struct key_vector *tn)
  194. {
  195. return (1ul << tn->bits) & ~(1ul);
  196. }
  197. #define get_cindex(key, kv) (((key) ^ (kv)->key) >> (kv)->pos)
  198. static inline unsigned long get_index(t_key key, struct key_vector *kv)
  199. {
  200. unsigned long index = key ^ kv->key;
  201. if ((BITS_PER_LONG <= KEYLENGTH) && (KEYLENGTH == kv->pos))
  202. return 0;
  203. return index >> kv->pos;
  204. }
  205. /* To understand this stuff, an understanding of keys and all their bits is
  206. * necessary. Every node in the trie has a key associated with it, but not
  207. * all of the bits in that key are significant.
  208. *
  209. * Consider a node 'n' and its parent 'tp'.
  210. *
  211. * If n is a leaf, every bit in its key is significant. Its presence is
  212. * necessitated by path compression, since during a tree traversal (when
  213. * searching for a leaf - unless we are doing an insertion) we will completely
  214. * ignore all skipped bits we encounter. Thus we need to verify, at the end of
  215. * a potentially successful search, that we have indeed been walking the
  216. * correct key path.
  217. *
  218. * Note that we can never "miss" the correct key in the tree if present by
  219. * following the wrong path. Path compression ensures that segments of the key
  220. * that are the same for all keys with a given prefix are skipped, but the
  221. * skipped part *is* identical for each node in the subtrie below the skipped
  222. * bit! trie_insert() in this implementation takes care of that.
  223. *
  224. * if n is an internal node - a 'tnode' here, the various parts of its key
  225. * have many different meanings.
  226. *
  227. * Example:
  228. * _________________________________________________________________
  229. * | i | i | i | i | i | i | i | N | N | N | S | S | S | S | S | C |
  230. * -----------------------------------------------------------------
  231. * 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
  232. *
  233. * _________________________________________________________________
  234. * | C | C | C | u | u | u | u | u | u | u | u | u | u | u | u | u |
  235. * -----------------------------------------------------------------
  236. * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
  237. *
  238. * tp->pos = 22
  239. * tp->bits = 3
  240. * n->pos = 13
  241. * n->bits = 4
  242. *
  243. * First, let's just ignore the bits that come before the parent tp, that is
  244. * the bits from (tp->pos + tp->bits) to 31. They are *known* but at this
  245. * point we do not use them for anything.
  246. *
  247. * The bits from (tp->pos) to (tp->pos + tp->bits - 1) - "N", above - are the
  248. * index into the parent's child array. That is, they will be used to find
  249. * 'n' among tp's children.
  250. *
  251. * The bits from (n->pos + n->bits) to (tp->pos - 1) - "S" - are skipped bits
  252. * for the node n.
  253. *
  254. * All the bits we have seen so far are significant to the node n. The rest
  255. * of the bits are really not needed or indeed known in n->key.
  256. *
  257. * The bits from (n->pos) to (n->pos + n->bits - 1) - "C" - are the index into
  258. * n's child array, and will of course be different for each child.
  259. *
  260. * The rest of the bits, from 0 to (n->pos -1) - "u" - are completely unknown
  261. * at this point.
  262. */
  263. static const int halve_threshold = 25;
  264. static const int inflate_threshold = 50;
  265. static const int halve_threshold_root = 15;
  266. static const int inflate_threshold_root = 30;
  267. static void __alias_free_mem(struct rcu_head *head)
  268. {
  269. struct fib_alias *fa = container_of(head, struct fib_alias, rcu);
  270. kmem_cache_free(fn_alias_kmem, fa);
  271. }
  272. static inline void alias_free_mem_rcu(struct fib_alias *fa)
  273. {
  274. call_rcu(&fa->rcu, __alias_free_mem);
  275. }
  276. #define TNODE_VMALLOC_MAX \
  277. ilog2((SIZE_MAX - TNODE_SIZE(0)) / sizeof(struct key_vector *))
  278. static void __node_free_rcu(struct rcu_head *head)
  279. {
  280. struct tnode *n = container_of(head, struct tnode, rcu);
  281. if (!n->tn_bits)
  282. kmem_cache_free(trie_leaf_kmem, n);
  283. else
  284. kvfree(n);
  285. }
  286. #define node_free(n) call_rcu(&tn_info(n)->rcu, __node_free_rcu)
  287. static struct tnode *tnode_alloc(int bits)
  288. {
  289. size_t size;
  290. /* verify bits is within bounds */
  291. if (bits > TNODE_VMALLOC_MAX)
  292. return NULL;
  293. /* determine size and verify it is non-zero and didn't overflow */
  294. size = TNODE_SIZE(1ul << bits);
  295. if (size <= PAGE_SIZE)
  296. return kzalloc(size, GFP_KERNEL);
  297. else
  298. return vzalloc(size);
  299. }
  300. static inline void empty_child_inc(struct key_vector *n)
  301. {
  302. tn_info(n)->empty_children++;
  303. if (!tn_info(n)->empty_children)
  304. tn_info(n)->full_children++;
  305. }
  306. static inline void empty_child_dec(struct key_vector *n)
  307. {
  308. if (!tn_info(n)->empty_children)
  309. tn_info(n)->full_children--;
  310. tn_info(n)->empty_children--;
  311. }
  312. static struct key_vector *leaf_new(t_key key, struct fib_alias *fa)
  313. {
  314. struct key_vector *l;
  315. struct tnode *kv;
  316. kv = kmem_cache_alloc(trie_leaf_kmem, GFP_KERNEL);
  317. if (!kv)
  318. return NULL;
  319. /* initialize key vector */
  320. l = kv->kv;
  321. l->key = key;
  322. l->pos = 0;
  323. l->bits = 0;
  324. l->slen = fa->fa_slen;
  325. /* link leaf to fib alias */
  326. INIT_HLIST_HEAD(&l->leaf);
  327. hlist_add_head(&fa->fa_list, &l->leaf);
  328. return l;
  329. }
  330. static struct key_vector *tnode_new(t_key key, int pos, int bits)
  331. {
  332. unsigned int shift = pos + bits;
  333. struct key_vector *tn;
  334. struct tnode *tnode;
  335. /* verify bits and pos their msb bits clear and values are valid */
  336. BUG_ON(!bits || (shift > KEYLENGTH));
  337. tnode = tnode_alloc(bits);
  338. if (!tnode)
  339. return NULL;
  340. pr_debug("AT %p s=%zu %zu\n", tnode, TNODE_SIZE(0),
  341. sizeof(struct key_vector *) << bits);
  342. if (bits == KEYLENGTH)
  343. tnode->full_children = 1;
  344. else
  345. tnode->empty_children = 1ul << bits;
  346. tn = tnode->kv;
  347. tn->key = (shift < KEYLENGTH) ? (key >> shift) << shift : 0;
  348. tn->pos = pos;
  349. tn->bits = bits;
  350. tn->slen = pos;
  351. return tn;
  352. }
  353. /* Check whether a tnode 'n' is "full", i.e. it is an internal node
  354. * and no bits are skipped. See discussion in dyntree paper p. 6
  355. */
  356. static inline int tnode_full(struct key_vector *tn, struct key_vector *n)
  357. {
  358. return n && ((n->pos + n->bits) == tn->pos) && IS_TNODE(n);
  359. }
  360. /* Add a child at position i overwriting the old value.
  361. * Update the value of full_children and empty_children.
  362. */
  363. static void put_child(struct key_vector *tn, unsigned long i,
  364. struct key_vector *n)
  365. {
  366. struct key_vector *chi = get_child(tn, i);
  367. int isfull, wasfull;
  368. BUG_ON(i >= child_length(tn));
  369. /* update emptyChildren, overflow into fullChildren */
  370. if (!n && chi)
  371. empty_child_inc(tn);
  372. if (n && !chi)
  373. empty_child_dec(tn);
  374. /* update fullChildren */
  375. wasfull = tnode_full(tn, chi);
  376. isfull = tnode_full(tn, n);
  377. if (wasfull && !isfull)
  378. tn_info(tn)->full_children--;
  379. else if (!wasfull && isfull)
  380. tn_info(tn)->full_children++;
  381. if (n && (tn->slen < n->slen))
  382. tn->slen = n->slen;
  383. rcu_assign_pointer(tn->tnode[i], n);
  384. }
  385. static void update_children(struct key_vector *tn)
  386. {
  387. unsigned long i;
  388. /* update all of the child parent pointers */
  389. for (i = child_length(tn); i;) {
  390. struct key_vector *inode = get_child(tn, --i);
  391. if (!inode)
  392. continue;
  393. /* Either update the children of a tnode that
  394. * already belongs to us or update the child
  395. * to point to ourselves.
  396. */
  397. if (node_parent(inode) == tn)
  398. update_children(inode);
  399. else
  400. node_set_parent(inode, tn);
  401. }
  402. }
  403. static inline void put_child_root(struct key_vector *tp, t_key key,
  404. struct key_vector *n)
  405. {
  406. if (IS_TRIE(tp))
  407. rcu_assign_pointer(tp->tnode[0], n);
  408. else
  409. put_child(tp, get_index(key, tp), n);
  410. }
  411. static inline void tnode_free_init(struct key_vector *tn)
  412. {
  413. tn_info(tn)->rcu.next = NULL;
  414. }
  415. static inline void tnode_free_append(struct key_vector *tn,
  416. struct key_vector *n)
  417. {
  418. tn_info(n)->rcu.next = tn_info(tn)->rcu.next;
  419. tn_info(tn)->rcu.next = &tn_info(n)->rcu;
  420. }
  421. static void tnode_free(struct key_vector *tn)
  422. {
  423. struct callback_head *head = &tn_info(tn)->rcu;
  424. while (head) {
  425. head = head->next;
  426. tnode_free_size += TNODE_SIZE(1ul << tn->bits);
  427. node_free(tn);
  428. tn = container_of(head, struct tnode, rcu)->kv;
  429. }
  430. if (tnode_free_size >= READ_ONCE(sysctl_fib_sync_mem)) {
  431. tnode_free_size = 0;
  432. synchronize_net();
  433. }
  434. }
  435. static struct key_vector *replace(struct trie *t,
  436. struct key_vector *oldtnode,
  437. struct key_vector *tn)
  438. {
  439. struct key_vector *tp = node_parent(oldtnode);
  440. unsigned long i;
  441. /* setup the parent pointer out of and back into this node */
  442. NODE_INIT_PARENT(tn, tp);
  443. put_child_root(tp, tn->key, tn);
  444. /* update all of the child parent pointers */
  445. update_children(tn);
  446. /* all pointers should be clean so we are done */
  447. tnode_free(oldtnode);
  448. /* resize children now that oldtnode is freed */
  449. for (i = child_length(tn); i;) {
  450. struct key_vector *inode = get_child(tn, --i);
  451. /* resize child node */
  452. if (tnode_full(tn, inode))
  453. tn = resize(t, inode);
  454. }
  455. return tp;
  456. }
  457. static struct key_vector *inflate(struct trie *t,
  458. struct key_vector *oldtnode)
  459. {
  460. struct key_vector *tn;
  461. unsigned long i;
  462. t_key m;
  463. pr_debug("In inflate\n");
  464. tn = tnode_new(oldtnode->key, oldtnode->pos - 1, oldtnode->bits + 1);
  465. if (!tn)
  466. goto notnode;
  467. /* prepare oldtnode to be freed */
  468. tnode_free_init(oldtnode);
  469. /* Assemble all of the pointers in our cluster, in this case that
  470. * represents all of the pointers out of our allocated nodes that
  471. * point to existing tnodes and the links between our allocated
  472. * nodes.
  473. */
  474. for (i = child_length(oldtnode), m = 1u << tn->pos; i;) {
  475. struct key_vector *inode = get_child(oldtnode, --i);
  476. struct key_vector *node0, *node1;
  477. unsigned long j, k;
  478. /* An empty child */
  479. if (!inode)
  480. continue;
  481. /* A leaf or an internal node with skipped bits */
  482. if (!tnode_full(oldtnode, inode)) {
  483. put_child(tn, get_index(inode->key, tn), inode);
  484. continue;
  485. }
  486. /* drop the node in the old tnode free list */
  487. tnode_free_append(oldtnode, inode);
  488. /* An internal node with two children */
  489. if (inode->bits == 1) {
  490. put_child(tn, 2 * i + 1, get_child(inode, 1));
  491. put_child(tn, 2 * i, get_child(inode, 0));
  492. continue;
  493. }
  494. /* We will replace this node 'inode' with two new
  495. * ones, 'node0' and 'node1', each with half of the
  496. * original children. The two new nodes will have
  497. * a position one bit further down the key and this
  498. * means that the "significant" part of their keys
  499. * (see the discussion near the top of this file)
  500. * will differ by one bit, which will be "0" in
  501. * node0's key and "1" in node1's key. Since we are
  502. * moving the key position by one step, the bit that
  503. * we are moving away from - the bit at position
  504. * (tn->pos) - is the one that will differ between
  505. * node0 and node1. So... we synthesize that bit in the
  506. * two new keys.
  507. */
  508. node1 = tnode_new(inode->key | m, inode->pos, inode->bits - 1);
  509. if (!node1)
  510. goto nomem;
  511. node0 = tnode_new(inode->key, inode->pos, inode->bits - 1);
  512. tnode_free_append(tn, node1);
  513. if (!node0)
  514. goto nomem;
  515. tnode_free_append(tn, node0);
  516. /* populate child pointers in new nodes */
  517. for (k = child_length(inode), j = k / 2; j;) {
  518. put_child(node1, --j, get_child(inode, --k));
  519. put_child(node0, j, get_child(inode, j));
  520. put_child(node1, --j, get_child(inode, --k));
  521. put_child(node0, j, get_child(inode, j));
  522. }
  523. /* link new nodes to parent */
  524. NODE_INIT_PARENT(node1, tn);
  525. NODE_INIT_PARENT(node0, tn);
  526. /* link parent to nodes */
  527. put_child(tn, 2 * i + 1, node1);
  528. put_child(tn, 2 * i, node0);
  529. }
  530. /* setup the parent pointers into and out of this node */
  531. return replace(t, oldtnode, tn);
  532. nomem:
  533. /* all pointers should be clean so we are done */
  534. tnode_free(tn);
  535. notnode:
  536. return NULL;
  537. }
  538. static struct key_vector *halve(struct trie *t,
  539. struct key_vector *oldtnode)
  540. {
  541. struct key_vector *tn;
  542. unsigned long i;
  543. pr_debug("In halve\n");
  544. tn = tnode_new(oldtnode->key, oldtnode->pos + 1, oldtnode->bits - 1);
  545. if (!tn)
  546. goto notnode;
  547. /* prepare oldtnode to be freed */
  548. tnode_free_init(oldtnode);
  549. /* Assemble all of the pointers in our cluster, in this case that
  550. * represents all of the pointers out of our allocated nodes that
  551. * point to existing tnodes and the links between our allocated
  552. * nodes.
  553. */
  554. for (i = child_length(oldtnode); i;) {
  555. struct key_vector *node1 = get_child(oldtnode, --i);
  556. struct key_vector *node0 = get_child(oldtnode, --i);
  557. struct key_vector *inode;
  558. /* At least one of the children is empty */
  559. if (!node1 || !node0) {
  560. put_child(tn, i / 2, node1 ? : node0);
  561. continue;
  562. }
  563. /* Two nonempty children */
  564. inode = tnode_new(node0->key, oldtnode->pos, 1);
  565. if (!inode)
  566. goto nomem;
  567. tnode_free_append(tn, inode);
  568. /* initialize pointers out of node */
  569. put_child(inode, 1, node1);
  570. put_child(inode, 0, node0);
  571. NODE_INIT_PARENT(inode, tn);
  572. /* link parent to node */
  573. put_child(tn, i / 2, inode);
  574. }
  575. /* setup the parent pointers into and out of this node */
  576. return replace(t, oldtnode, tn);
  577. nomem:
  578. /* all pointers should be clean so we are done */
  579. tnode_free(tn);
  580. notnode:
  581. return NULL;
  582. }
  583. static struct key_vector *collapse(struct trie *t,
  584. struct key_vector *oldtnode)
  585. {
  586. struct key_vector *n, *tp;
  587. unsigned long i;
  588. /* scan the tnode looking for that one child that might still exist */
  589. for (n = NULL, i = child_length(oldtnode); !n && i;)
  590. n = get_child(oldtnode, --i);
  591. /* compress one level */
  592. tp = node_parent(oldtnode);
  593. put_child_root(tp, oldtnode->key, n);
  594. node_set_parent(n, tp);
  595. /* drop dead node */
  596. node_free(oldtnode);
  597. return tp;
  598. }
  599. static unsigned char update_suffix(struct key_vector *tn)
  600. {
  601. unsigned char slen = tn->pos;
  602. unsigned long stride, i;
  603. unsigned char slen_max;
  604. /* only vector 0 can have a suffix length greater than or equal to
  605. * tn->pos + tn->bits, the second highest node will have a suffix
  606. * length at most of tn->pos + tn->bits - 1
  607. */
  608. slen_max = min_t(unsigned char, tn->pos + tn->bits - 1, tn->slen);
  609. /* search though the list of children looking for nodes that might
  610. * have a suffix greater than the one we currently have. This is
  611. * why we start with a stride of 2 since a stride of 1 would
  612. * represent the nodes with suffix length equal to tn->pos
  613. */
  614. for (i = 0, stride = 0x2ul ; i < child_length(tn); i += stride) {
  615. struct key_vector *n = get_child(tn, i);
  616. if (!n || (n->slen <= slen))
  617. continue;
  618. /* update stride and slen based on new value */
  619. stride <<= (n->slen - slen);
  620. slen = n->slen;
  621. i &= ~(stride - 1);
  622. /* stop searching if we have hit the maximum possible value */
  623. if (slen >= slen_max)
  624. break;
  625. }
  626. tn->slen = slen;
  627. return slen;
  628. }
  629. /* From "Implementing a dynamic compressed trie" by Stefan Nilsson of
  630. * the Helsinki University of Technology and Matti Tikkanen of Nokia
  631. * Telecommunications, page 6:
  632. * "A node is doubled if the ratio of non-empty children to all
  633. * children in the *doubled* node is at least 'high'."
  634. *
  635. * 'high' in this instance is the variable 'inflate_threshold'. It
  636. * is expressed as a percentage, so we multiply it with
  637. * child_length() and instead of multiplying by 2 (since the
  638. * child array will be doubled by inflate()) and multiplying
  639. * the left-hand side by 100 (to handle the percentage thing) we
  640. * multiply the left-hand side by 50.
  641. *
  642. * The left-hand side may look a bit weird: child_length(tn)
  643. * - tn->empty_children is of course the number of non-null children
  644. * in the current node. tn->full_children is the number of "full"
  645. * children, that is non-null tnodes with a skip value of 0.
  646. * All of those will be doubled in the resulting inflated tnode, so
  647. * we just count them one extra time here.
  648. *
  649. * A clearer way to write this would be:
  650. *
  651. * to_be_doubled = tn->full_children;
  652. * not_to_be_doubled = child_length(tn) - tn->empty_children -
  653. * tn->full_children;
  654. *
  655. * new_child_length = child_length(tn) * 2;
  656. *
  657. * new_fill_factor = 100 * (not_to_be_doubled + 2*to_be_doubled) /
  658. * new_child_length;
  659. * if (new_fill_factor >= inflate_threshold)
  660. *
  661. * ...and so on, tho it would mess up the while () loop.
  662. *
  663. * anyway,
  664. * 100 * (not_to_be_doubled + 2*to_be_doubled) / new_child_length >=
  665. * inflate_threshold
  666. *
  667. * avoid a division:
  668. * 100 * (not_to_be_doubled + 2*to_be_doubled) >=
  669. * inflate_threshold * new_child_length
  670. *
  671. * expand not_to_be_doubled and to_be_doubled, and shorten:
  672. * 100 * (child_length(tn) - tn->empty_children +
  673. * tn->full_children) >= inflate_threshold * new_child_length
  674. *
  675. * expand new_child_length:
  676. * 100 * (child_length(tn) - tn->empty_children +
  677. * tn->full_children) >=
  678. * inflate_threshold * child_length(tn) * 2
  679. *
  680. * shorten again:
  681. * 50 * (tn->full_children + child_length(tn) -
  682. * tn->empty_children) >= inflate_threshold *
  683. * child_length(tn)
  684. *
  685. */
  686. static inline bool should_inflate(struct key_vector *tp, struct key_vector *tn)
  687. {
  688. unsigned long used = child_length(tn);
  689. unsigned long threshold = used;
  690. /* Keep root node larger */
  691. threshold *= IS_TRIE(tp) ? inflate_threshold_root : inflate_threshold;
  692. used -= tn_info(tn)->empty_children;
  693. used += tn_info(tn)->full_children;
  694. /* if bits == KEYLENGTH then pos = 0, and will fail below */
  695. return (used > 1) && tn->pos && ((50 * used) >= threshold);
  696. }
  697. static inline bool should_halve(struct key_vector *tp, struct key_vector *tn)
  698. {
  699. unsigned long used = child_length(tn);
  700. unsigned long threshold = used;
  701. /* Keep root node larger */
  702. threshold *= IS_TRIE(tp) ? halve_threshold_root : halve_threshold;
  703. used -= tn_info(tn)->empty_children;
  704. /* if bits == KEYLENGTH then used = 100% on wrap, and will fail below */
  705. return (used > 1) && (tn->bits > 1) && ((100 * used) < threshold);
  706. }
  707. static inline bool should_collapse(struct key_vector *tn)
  708. {
  709. unsigned long used = child_length(tn);
  710. used -= tn_info(tn)->empty_children;
  711. /* account for bits == KEYLENGTH case */
  712. if ((tn->bits == KEYLENGTH) && tn_info(tn)->full_children)
  713. used -= KEY_MAX;
  714. /* One child or none, time to drop us from the trie */
  715. return used < 2;
  716. }
  717. #define MAX_WORK 10
  718. static struct key_vector *resize(struct trie *t, struct key_vector *tn)
  719. {
  720. #ifdef CONFIG_IP_FIB_TRIE_STATS
  721. struct trie_use_stats __percpu *stats = t->stats;
  722. #endif
  723. struct key_vector *tp = node_parent(tn);
  724. unsigned long cindex = get_index(tn->key, tp);
  725. int max_work = MAX_WORK;
  726. pr_debug("In tnode_resize %p inflate_threshold=%d threshold=%d\n",
  727. tn, inflate_threshold, halve_threshold);
  728. /* track the tnode via the pointer from the parent instead of
  729. * doing it ourselves. This way we can let RCU fully do its
  730. * thing without us interfering
  731. */
  732. BUG_ON(tn != get_child(tp, cindex));
  733. /* Double as long as the resulting node has a number of
  734. * nonempty nodes that are above the threshold.
  735. */
  736. while (should_inflate(tp, tn) && max_work) {
  737. tp = inflate(t, tn);
  738. if (!tp) {
  739. #ifdef CONFIG_IP_FIB_TRIE_STATS
  740. this_cpu_inc(stats->resize_node_skipped);
  741. #endif
  742. break;
  743. }
  744. max_work--;
  745. tn = get_child(tp, cindex);
  746. }
  747. /* update parent in case inflate failed */
  748. tp = node_parent(tn);
  749. /* Return if at least one inflate is run */
  750. if (max_work != MAX_WORK)
  751. return tp;
  752. /* Halve as long as the number of empty children in this
  753. * node is above threshold.
  754. */
  755. while (should_halve(tp, tn) && max_work) {
  756. tp = halve(t, tn);
  757. if (!tp) {
  758. #ifdef CONFIG_IP_FIB_TRIE_STATS
  759. this_cpu_inc(stats->resize_node_skipped);
  760. #endif
  761. break;
  762. }
  763. max_work--;
  764. tn = get_child(tp, cindex);
  765. }
  766. /* Only one child remains */
  767. if (should_collapse(tn))
  768. return collapse(t, tn);
  769. /* update parent in case halve failed */
  770. return node_parent(tn);
  771. }
  772. static void node_pull_suffix(struct key_vector *tn, unsigned char slen)
  773. {
  774. unsigned char node_slen = tn->slen;
  775. while ((node_slen > tn->pos) && (node_slen > slen)) {
  776. slen = update_suffix(tn);
  777. if (node_slen == slen)
  778. break;
  779. tn = node_parent(tn);
  780. node_slen = tn->slen;
  781. }
  782. }
  783. static void node_push_suffix(struct key_vector *tn, unsigned char slen)
  784. {
  785. while (tn->slen < slen) {
  786. tn->slen = slen;
  787. tn = node_parent(tn);
  788. }
  789. }
  790. /* rcu_read_lock needs to be hold by caller from readside */
  791. static struct key_vector *fib_find_node(struct trie *t,
  792. struct key_vector **tp, u32 key)
  793. {
  794. struct key_vector *pn, *n = t->kv;
  795. unsigned long index = 0;
  796. do {
  797. pn = n;
  798. n = get_child_rcu(n, index);
  799. if (!n)
  800. break;
  801. index = get_cindex(key, n);
  802. /* This bit of code is a bit tricky but it combines multiple
  803. * checks into a single check. The prefix consists of the
  804. * prefix plus zeros for the bits in the cindex. The index
  805. * is the difference between the key and this value. From
  806. * this we can actually derive several pieces of data.
  807. * if (index >= (1ul << bits))
  808. * we have a mismatch in skip bits and failed
  809. * else
  810. * we know the value is cindex
  811. *
  812. * This check is safe even if bits == KEYLENGTH due to the
  813. * fact that we can only allocate a node with 32 bits if a
  814. * long is greater than 32 bits.
  815. */
  816. if (index >= (1ul << n->bits)) {
  817. n = NULL;
  818. break;
  819. }
  820. /* keep searching until we find a perfect match leaf or NULL */
  821. } while (IS_TNODE(n));
  822. *tp = pn;
  823. return n;
  824. }
  825. /* Return the first fib alias matching DSCP with
  826. * priority less than or equal to PRIO.
  827. * If 'find_first' is set, return the first matching
  828. * fib alias, regardless of DSCP and priority.
  829. */
  830. static struct fib_alias *fib_find_alias(struct hlist_head *fah, u8 slen,
  831. dscp_t dscp, u32 prio, u32 tb_id,
  832. bool find_first)
  833. {
  834. struct fib_alias *fa;
  835. if (!fah)
  836. return NULL;
  837. hlist_for_each_entry(fa, fah, fa_list) {
  838. /* Avoid Sparse warning when using dscp_t in inequalities */
  839. u8 __fa_dscp = inet_dscp_to_dsfield(fa->fa_dscp);
  840. u8 __dscp = inet_dscp_to_dsfield(dscp);
  841. if (fa->fa_slen < slen)
  842. continue;
  843. if (fa->fa_slen != slen)
  844. break;
  845. if (fa->tb_id > tb_id)
  846. continue;
  847. if (fa->tb_id != tb_id)
  848. break;
  849. if (find_first)
  850. return fa;
  851. if (__fa_dscp > __dscp)
  852. continue;
  853. if (fa->fa_info->fib_priority >= prio || __fa_dscp < __dscp)
  854. return fa;
  855. }
  856. return NULL;
  857. }
  858. static struct fib_alias *
  859. fib_find_matching_alias(struct net *net, const struct fib_rt_info *fri)
  860. {
  861. u8 slen = KEYLENGTH - fri->dst_len;
  862. struct key_vector *l, *tp;
  863. struct fib_table *tb;
  864. struct fib_alias *fa;
  865. struct trie *t;
  866. tb = fib_get_table(net, fri->tb_id);
  867. if (!tb)
  868. return NULL;
  869. t = (struct trie *)tb->tb_data;
  870. l = fib_find_node(t, &tp, be32_to_cpu(fri->dst));
  871. if (!l)
  872. return NULL;
  873. hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
  874. if (fa->fa_slen == slen && fa->tb_id == fri->tb_id &&
  875. fa->fa_dscp == fri->dscp && fa->fa_info == fri->fi &&
  876. fa->fa_type == fri->type)
  877. return fa;
  878. }
  879. return NULL;
  880. }
  881. void fib_alias_hw_flags_set(struct net *net, const struct fib_rt_info *fri)
  882. {
  883. u8 fib_notify_on_flag_change;
  884. struct fib_alias *fa_match;
  885. struct sk_buff *skb;
  886. int err;
  887. rcu_read_lock();
  888. fa_match = fib_find_matching_alias(net, fri);
  889. if (!fa_match)
  890. goto out;
  891. /* These are paired with the WRITE_ONCE() happening in this function.
  892. * The reason is that we are only protected by RCU at this point.
  893. */
  894. if (READ_ONCE(fa_match->offload) == fri->offload &&
  895. READ_ONCE(fa_match->trap) == fri->trap &&
  896. READ_ONCE(fa_match->offload_failed) == fri->offload_failed)
  897. goto out;
  898. WRITE_ONCE(fa_match->offload, fri->offload);
  899. WRITE_ONCE(fa_match->trap, fri->trap);
  900. fib_notify_on_flag_change = READ_ONCE(net->ipv4.sysctl_fib_notify_on_flag_change);
  901. /* 2 means send notifications only if offload_failed was changed. */
  902. if (fib_notify_on_flag_change == 2 &&
  903. READ_ONCE(fa_match->offload_failed) == fri->offload_failed)
  904. goto out;
  905. WRITE_ONCE(fa_match->offload_failed, fri->offload_failed);
  906. if (!fib_notify_on_flag_change)
  907. goto out;
  908. skb = nlmsg_new(fib_nlmsg_size(fa_match->fa_info), GFP_ATOMIC);
  909. if (!skb) {
  910. err = -ENOBUFS;
  911. goto errout;
  912. }
  913. err = fib_dump_info(skb, 0, 0, RTM_NEWROUTE, fri, 0);
  914. if (err < 0) {
  915. /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
  916. WARN_ON(err == -EMSGSIZE);
  917. kfree_skb(skb);
  918. goto errout;
  919. }
  920. rtnl_notify(skb, net, 0, RTNLGRP_IPV4_ROUTE, NULL, GFP_ATOMIC);
  921. goto out;
  922. errout:
  923. rtnl_set_sk_err(net, RTNLGRP_IPV4_ROUTE, err);
  924. out:
  925. rcu_read_unlock();
  926. }
  927. EXPORT_SYMBOL_GPL(fib_alias_hw_flags_set);
  928. static void trie_rebalance(struct trie *t, struct key_vector *tn)
  929. {
  930. while (!IS_TRIE(tn))
  931. tn = resize(t, tn);
  932. }
  933. static int fib_insert_node(struct trie *t, struct key_vector *tp,
  934. struct fib_alias *new, t_key key)
  935. {
  936. struct key_vector *n, *l;
  937. l = leaf_new(key, new);
  938. if (!l)
  939. goto noleaf;
  940. /* retrieve child from parent node */
  941. n = get_child(tp, get_index(key, tp));
  942. /* Case 2: n is a LEAF or a TNODE and the key doesn't match.
  943. *
  944. * Add a new tnode here
  945. * first tnode need some special handling
  946. * leaves us in position for handling as case 3
  947. */
  948. if (n) {
  949. struct key_vector *tn;
  950. tn = tnode_new(key, __fls(key ^ n->key), 1);
  951. if (!tn)
  952. goto notnode;
  953. /* initialize routes out of node */
  954. NODE_INIT_PARENT(tn, tp);
  955. put_child(tn, get_index(key, tn) ^ 1, n);
  956. /* start adding routes into the node */
  957. put_child_root(tp, key, tn);
  958. node_set_parent(n, tn);
  959. /* parent now has a NULL spot where the leaf can go */
  960. tp = tn;
  961. }
  962. /* Case 3: n is NULL, and will just insert a new leaf */
  963. node_push_suffix(tp, new->fa_slen);
  964. NODE_INIT_PARENT(l, tp);
  965. put_child_root(tp, key, l);
  966. trie_rebalance(t, tp);
  967. return 0;
  968. notnode:
  969. node_free(l);
  970. noleaf:
  971. return -ENOMEM;
  972. }
  973. static int fib_insert_alias(struct trie *t, struct key_vector *tp,
  974. struct key_vector *l, struct fib_alias *new,
  975. struct fib_alias *fa, t_key key)
  976. {
  977. if (!l)
  978. return fib_insert_node(t, tp, new, key);
  979. if (fa) {
  980. hlist_add_before_rcu(&new->fa_list, &fa->fa_list);
  981. } else {
  982. struct fib_alias *last;
  983. hlist_for_each_entry(last, &l->leaf, fa_list) {
  984. if (new->fa_slen < last->fa_slen)
  985. break;
  986. if ((new->fa_slen == last->fa_slen) &&
  987. (new->tb_id > last->tb_id))
  988. break;
  989. fa = last;
  990. }
  991. if (fa)
  992. hlist_add_behind_rcu(&new->fa_list, &fa->fa_list);
  993. else
  994. hlist_add_head_rcu(&new->fa_list, &l->leaf);
  995. }
  996. /* if we added to the tail node then we need to update slen */
  997. if (l->slen < new->fa_slen) {
  998. l->slen = new->fa_slen;
  999. node_push_suffix(tp, new->fa_slen);
  1000. }
  1001. return 0;
  1002. }
  1003. static void fib_remove_alias(struct trie *t, struct key_vector *tp,
  1004. struct key_vector *l, struct fib_alias *old);
  1005. /* Caller must hold RTNL. */
  1006. int fib_table_insert(struct net *net, struct fib_table *tb,
  1007. struct fib_config *cfg, struct netlink_ext_ack *extack)
  1008. {
  1009. struct trie *t = (struct trie *)tb->tb_data;
  1010. struct fib_alias *fa, *new_fa;
  1011. struct key_vector *l, *tp;
  1012. u16 nlflags = NLM_F_EXCL;
  1013. struct fib_info *fi;
  1014. u8 plen = cfg->fc_dst_len;
  1015. u8 slen = KEYLENGTH - plen;
  1016. dscp_t dscp;
  1017. u32 key;
  1018. int err;
  1019. key = ntohl(cfg->fc_dst);
  1020. pr_debug("Insert table=%u %08x/%d\n", tb->tb_id, key, plen);
  1021. fi = fib_create_info(cfg, extack);
  1022. if (IS_ERR(fi)) {
  1023. err = PTR_ERR(fi);
  1024. goto err;
  1025. }
  1026. dscp = cfg->fc_dscp;
  1027. l = fib_find_node(t, &tp, key);
  1028. fa = l ? fib_find_alias(&l->leaf, slen, dscp, fi->fib_priority,
  1029. tb->tb_id, false) : NULL;
  1030. /* Now fa, if non-NULL, points to the first fib alias
  1031. * with the same keys [prefix,dscp,priority], if such key already
  1032. * exists or to the node before which we will insert new one.
  1033. *
  1034. * If fa is NULL, we will need to allocate a new one and
  1035. * insert to the tail of the section matching the suffix length
  1036. * of the new alias.
  1037. */
  1038. if (fa && fa->fa_dscp == dscp &&
  1039. fa->fa_info->fib_priority == fi->fib_priority) {
  1040. struct fib_alias *fa_first, *fa_match;
  1041. err = -EEXIST;
  1042. if (cfg->fc_nlflags & NLM_F_EXCL)
  1043. goto out;
  1044. nlflags &= ~NLM_F_EXCL;
  1045. /* We have 2 goals:
  1046. * 1. Find exact match for type, scope, fib_info to avoid
  1047. * duplicate routes
  1048. * 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it
  1049. */
  1050. fa_match = NULL;
  1051. fa_first = fa;
  1052. hlist_for_each_entry_from(fa, fa_list) {
  1053. if ((fa->fa_slen != slen) ||
  1054. (fa->tb_id != tb->tb_id) ||
  1055. (fa->fa_dscp != dscp))
  1056. break;
  1057. if (fa->fa_info->fib_priority != fi->fib_priority)
  1058. break;
  1059. if (fa->fa_type == cfg->fc_type &&
  1060. fa->fa_info == fi) {
  1061. fa_match = fa;
  1062. break;
  1063. }
  1064. }
  1065. if (cfg->fc_nlflags & NLM_F_REPLACE) {
  1066. struct fib_info *fi_drop;
  1067. u8 state;
  1068. nlflags |= NLM_F_REPLACE;
  1069. fa = fa_first;
  1070. if (fa_match) {
  1071. if (fa == fa_match)
  1072. err = 0;
  1073. goto out;
  1074. }
  1075. err = -ENOBUFS;
  1076. new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
  1077. if (!new_fa)
  1078. goto out;
  1079. fi_drop = fa->fa_info;
  1080. new_fa->fa_dscp = fa->fa_dscp;
  1081. new_fa->fa_info = fi;
  1082. new_fa->fa_type = cfg->fc_type;
  1083. state = fa->fa_state;
  1084. new_fa->fa_state = state & ~FA_S_ACCESSED;
  1085. new_fa->fa_slen = fa->fa_slen;
  1086. new_fa->tb_id = tb->tb_id;
  1087. new_fa->fa_default = -1;
  1088. new_fa->offload = 0;
  1089. new_fa->trap = 0;
  1090. new_fa->offload_failed = 0;
  1091. hlist_replace_rcu(&fa->fa_list, &new_fa->fa_list);
  1092. if (fib_find_alias(&l->leaf, fa->fa_slen, 0, 0,
  1093. tb->tb_id, true) == new_fa) {
  1094. enum fib_event_type fib_event;
  1095. fib_event = FIB_EVENT_ENTRY_REPLACE;
  1096. err = call_fib_entry_notifiers(net, fib_event,
  1097. key, plen,
  1098. new_fa, extack);
  1099. if (err) {
  1100. hlist_replace_rcu(&new_fa->fa_list,
  1101. &fa->fa_list);
  1102. goto out_free_new_fa;
  1103. }
  1104. }
  1105. rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen,
  1106. tb->tb_id, &cfg->fc_nlinfo, nlflags);
  1107. alias_free_mem_rcu(fa);
  1108. fib_release_info(fi_drop);
  1109. if (state & FA_S_ACCESSED)
  1110. rt_cache_flush(cfg->fc_nlinfo.nl_net);
  1111. goto succeeded;
  1112. }
  1113. /* Error if we find a perfect match which
  1114. * uses the same scope, type, and nexthop
  1115. * information.
  1116. */
  1117. if (fa_match)
  1118. goto out;
  1119. if (cfg->fc_nlflags & NLM_F_APPEND)
  1120. nlflags |= NLM_F_APPEND;
  1121. else
  1122. fa = fa_first;
  1123. }
  1124. err = -ENOENT;
  1125. if (!(cfg->fc_nlflags & NLM_F_CREATE))
  1126. goto out;
  1127. nlflags |= NLM_F_CREATE;
  1128. err = -ENOBUFS;
  1129. new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
  1130. if (!new_fa)
  1131. goto out;
  1132. new_fa->fa_info = fi;
  1133. new_fa->fa_dscp = dscp;
  1134. new_fa->fa_type = cfg->fc_type;
  1135. new_fa->fa_state = 0;
  1136. new_fa->fa_slen = slen;
  1137. new_fa->tb_id = tb->tb_id;
  1138. new_fa->fa_default = -1;
  1139. new_fa->offload = 0;
  1140. new_fa->trap = 0;
  1141. new_fa->offload_failed = 0;
  1142. /* Insert new entry to the list. */
  1143. err = fib_insert_alias(t, tp, l, new_fa, fa, key);
  1144. if (err)
  1145. goto out_free_new_fa;
  1146. /* The alias was already inserted, so the node must exist. */
  1147. l = l ? l : fib_find_node(t, &tp, key);
  1148. if (WARN_ON_ONCE(!l)) {
  1149. err = -ENOENT;
  1150. goto out_free_new_fa;
  1151. }
  1152. if (fib_find_alias(&l->leaf, new_fa->fa_slen, 0, 0, tb->tb_id, true) ==
  1153. new_fa) {
  1154. enum fib_event_type fib_event;
  1155. fib_event = FIB_EVENT_ENTRY_REPLACE;
  1156. err = call_fib_entry_notifiers(net, fib_event, key, plen,
  1157. new_fa, extack);
  1158. if (err)
  1159. goto out_remove_new_fa;
  1160. }
  1161. if (!plen)
  1162. tb->tb_num_default++;
  1163. rt_cache_flush(cfg->fc_nlinfo.nl_net);
  1164. rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen, new_fa->tb_id,
  1165. &cfg->fc_nlinfo, nlflags);
  1166. succeeded:
  1167. return 0;
  1168. out_remove_new_fa:
  1169. fib_remove_alias(t, tp, l, new_fa);
  1170. out_free_new_fa:
  1171. kmem_cache_free(fn_alias_kmem, new_fa);
  1172. out:
  1173. fib_release_info(fi);
  1174. err:
  1175. return err;
  1176. }
  1177. static inline t_key prefix_mismatch(t_key key, struct key_vector *n)
  1178. {
  1179. t_key prefix = n->key;
  1180. return (key ^ prefix) & (prefix | -prefix);
  1181. }
  1182. bool fib_lookup_good_nhc(const struct fib_nh_common *nhc, int fib_flags,
  1183. const struct flowi4 *flp)
  1184. {
  1185. if (nhc->nhc_flags & RTNH_F_DEAD)
  1186. return false;
  1187. if (ip_ignore_linkdown(nhc->nhc_dev) &&
  1188. nhc->nhc_flags & RTNH_F_LINKDOWN &&
  1189. !(fib_flags & FIB_LOOKUP_IGNORE_LINKSTATE))
  1190. return false;
  1191. if (flp->flowi4_oif && flp->flowi4_oif != nhc->nhc_oif)
  1192. return false;
  1193. return true;
  1194. }
  1195. /* should be called with rcu_read_lock */
  1196. int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
  1197. struct fib_result *res, int fib_flags)
  1198. {
  1199. struct trie *t = (struct trie *) tb->tb_data;
  1200. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1201. struct trie_use_stats __percpu *stats = t->stats;
  1202. #endif
  1203. const t_key key = ntohl(flp->daddr);
  1204. struct key_vector *n, *pn;
  1205. struct fib_alias *fa;
  1206. unsigned long index;
  1207. t_key cindex;
  1208. pn = t->kv;
  1209. cindex = 0;
  1210. n = get_child_rcu(pn, cindex);
  1211. if (!n) {
  1212. trace_fib_table_lookup(tb->tb_id, flp, NULL, -EAGAIN);
  1213. return -EAGAIN;
  1214. }
  1215. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1216. this_cpu_inc(stats->gets);
  1217. #endif
  1218. /* Step 1: Travel to the longest prefix match in the trie */
  1219. for (;;) {
  1220. index = get_cindex(key, n);
  1221. /* This bit of code is a bit tricky but it combines multiple
  1222. * checks into a single check. The prefix consists of the
  1223. * prefix plus zeros for the "bits" in the prefix. The index
  1224. * is the difference between the key and this value. From
  1225. * this we can actually derive several pieces of data.
  1226. * if (index >= (1ul << bits))
  1227. * we have a mismatch in skip bits and failed
  1228. * else
  1229. * we know the value is cindex
  1230. *
  1231. * This check is safe even if bits == KEYLENGTH due to the
  1232. * fact that we can only allocate a node with 32 bits if a
  1233. * long is greater than 32 bits.
  1234. */
  1235. if (index >= (1ul << n->bits))
  1236. break;
  1237. /* we have found a leaf. Prefixes have already been compared */
  1238. if (IS_LEAF(n))
  1239. goto found;
  1240. /* only record pn and cindex if we are going to be chopping
  1241. * bits later. Otherwise we are just wasting cycles.
  1242. */
  1243. if (n->slen > n->pos) {
  1244. pn = n;
  1245. cindex = index;
  1246. }
  1247. n = get_child_rcu(n, index);
  1248. if (unlikely(!n))
  1249. goto backtrace;
  1250. }
  1251. /* Step 2: Sort out leaves and begin backtracing for longest prefix */
  1252. for (;;) {
  1253. /* record the pointer where our next node pointer is stored */
  1254. struct key_vector __rcu **cptr = n->tnode;
  1255. /* This test verifies that none of the bits that differ
  1256. * between the key and the prefix exist in the region of
  1257. * the lsb and higher in the prefix.
  1258. */
  1259. if (unlikely(prefix_mismatch(key, n)) || (n->slen == n->pos))
  1260. goto backtrace;
  1261. /* exit out and process leaf */
  1262. if (unlikely(IS_LEAF(n)))
  1263. break;
  1264. /* Don't bother recording parent info. Since we are in
  1265. * prefix match mode we will have to come back to wherever
  1266. * we started this traversal anyway
  1267. */
  1268. while ((n = rcu_dereference(*cptr)) == NULL) {
  1269. backtrace:
  1270. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1271. if (!n)
  1272. this_cpu_inc(stats->null_node_hit);
  1273. #endif
  1274. /* If we are at cindex 0 there are no more bits for
  1275. * us to strip at this level so we must ascend back
  1276. * up one level to see if there are any more bits to
  1277. * be stripped there.
  1278. */
  1279. while (!cindex) {
  1280. t_key pkey = pn->key;
  1281. /* If we don't have a parent then there is
  1282. * nothing for us to do as we do not have any
  1283. * further nodes to parse.
  1284. */
  1285. if (IS_TRIE(pn)) {
  1286. trace_fib_table_lookup(tb->tb_id, flp,
  1287. NULL, -EAGAIN);
  1288. return -EAGAIN;
  1289. }
  1290. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1291. this_cpu_inc(stats->backtrack);
  1292. #endif
  1293. /* Get Child's index */
  1294. pn = node_parent_rcu(pn);
  1295. cindex = get_index(pkey, pn);
  1296. }
  1297. /* strip the least significant bit from the cindex */
  1298. cindex &= cindex - 1;
  1299. /* grab pointer for next child node */
  1300. cptr = &pn->tnode[cindex];
  1301. }
  1302. }
  1303. found:
  1304. /* this line carries forward the xor from earlier in the function */
  1305. index = key ^ n->key;
  1306. /* Step 3: Process the leaf, if that fails fall back to backtracing */
  1307. hlist_for_each_entry_rcu(fa, &n->leaf, fa_list) {
  1308. struct fib_info *fi = fa->fa_info;
  1309. struct fib_nh_common *nhc;
  1310. int nhsel, err;
  1311. if ((BITS_PER_LONG > KEYLENGTH) || (fa->fa_slen < KEYLENGTH)) {
  1312. if (index >= (1ul << fa->fa_slen))
  1313. continue;
  1314. }
  1315. if (fa->fa_dscp && !fib_dscp_masked_match(fa->fa_dscp, flp))
  1316. continue;
  1317. /* Paired with WRITE_ONCE() in fib_release_info() */
  1318. if (READ_ONCE(fi->fib_dead))
  1319. continue;
  1320. if (fa->fa_info->fib_scope < flp->flowi4_scope)
  1321. continue;
  1322. fib_alias_accessed(fa);
  1323. err = fib_props[fa->fa_type].error;
  1324. if (unlikely(err < 0)) {
  1325. out_reject:
  1326. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1327. this_cpu_inc(stats->semantic_match_passed);
  1328. #endif
  1329. trace_fib_table_lookup(tb->tb_id, flp, NULL, err);
  1330. return err;
  1331. }
  1332. if (fi->fib_flags & RTNH_F_DEAD)
  1333. continue;
  1334. if (unlikely(fi->nh)) {
  1335. if (nexthop_is_blackhole(fi->nh)) {
  1336. err = fib_props[RTN_BLACKHOLE].error;
  1337. goto out_reject;
  1338. }
  1339. nhc = nexthop_get_nhc_lookup(fi->nh, fib_flags, flp,
  1340. &nhsel);
  1341. if (nhc)
  1342. goto set_result;
  1343. goto miss;
  1344. }
  1345. for (nhsel = 0; nhsel < fib_info_num_path(fi); nhsel++) {
  1346. nhc = fib_info_nhc(fi, nhsel);
  1347. if (!fib_lookup_good_nhc(nhc, fib_flags, flp))
  1348. continue;
  1349. set_result:
  1350. if (!(fib_flags & FIB_LOOKUP_NOREF))
  1351. refcount_inc(&fi->fib_clntref);
  1352. res->prefix = htonl(n->key);
  1353. res->prefixlen = KEYLENGTH - fa->fa_slen;
  1354. res->nh_sel = nhsel;
  1355. res->nhc = nhc;
  1356. res->type = fa->fa_type;
  1357. res->scope = fi->fib_scope;
  1358. res->dscp = fa->fa_dscp;
  1359. res->fi = fi;
  1360. res->table = tb;
  1361. res->fa_head = &n->leaf;
  1362. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1363. this_cpu_inc(stats->semantic_match_passed);
  1364. #endif
  1365. trace_fib_table_lookup(tb->tb_id, flp, nhc, err);
  1366. return err;
  1367. }
  1368. }
  1369. miss:
  1370. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1371. this_cpu_inc(stats->semantic_match_miss);
  1372. #endif
  1373. goto backtrace;
  1374. }
  1375. EXPORT_SYMBOL_GPL(fib_table_lookup);
  1376. static void fib_remove_alias(struct trie *t, struct key_vector *tp,
  1377. struct key_vector *l, struct fib_alias *old)
  1378. {
  1379. /* record the location of the previous list_info entry */
  1380. struct hlist_node **pprev = old->fa_list.pprev;
  1381. struct fib_alias *fa = hlist_entry(pprev, typeof(*fa), fa_list.next);
  1382. /* remove the fib_alias from the list */
  1383. hlist_del_rcu(&old->fa_list);
  1384. /* if we emptied the list this leaf will be freed and we can sort
  1385. * out parent suffix lengths as a part of trie_rebalance
  1386. */
  1387. if (hlist_empty(&l->leaf)) {
  1388. if (tp->slen == l->slen)
  1389. node_pull_suffix(tp, tp->pos);
  1390. put_child_root(tp, l->key, NULL);
  1391. node_free(l);
  1392. trie_rebalance(t, tp);
  1393. return;
  1394. }
  1395. /* only access fa if it is pointing at the last valid hlist_node */
  1396. if (*pprev)
  1397. return;
  1398. /* update the trie with the latest suffix length */
  1399. l->slen = fa->fa_slen;
  1400. node_pull_suffix(tp, fa->fa_slen);
  1401. }
  1402. static void fib_notify_alias_delete(struct net *net, u32 key,
  1403. struct hlist_head *fah,
  1404. struct fib_alias *fa_to_delete,
  1405. struct netlink_ext_ack *extack)
  1406. {
  1407. struct fib_alias *fa_next, *fa_to_notify;
  1408. u32 tb_id = fa_to_delete->tb_id;
  1409. u8 slen = fa_to_delete->fa_slen;
  1410. enum fib_event_type fib_event;
  1411. /* Do not notify if we do not care about the route. */
  1412. if (fib_find_alias(fah, slen, 0, 0, tb_id, true) != fa_to_delete)
  1413. return;
  1414. /* Determine if the route should be replaced by the next route in the
  1415. * list.
  1416. */
  1417. fa_next = hlist_entry_safe(fa_to_delete->fa_list.next,
  1418. struct fib_alias, fa_list);
  1419. if (fa_next && fa_next->fa_slen == slen && fa_next->tb_id == tb_id) {
  1420. fib_event = FIB_EVENT_ENTRY_REPLACE;
  1421. fa_to_notify = fa_next;
  1422. } else {
  1423. fib_event = FIB_EVENT_ENTRY_DEL;
  1424. fa_to_notify = fa_to_delete;
  1425. }
  1426. call_fib_entry_notifiers(net, fib_event, key, KEYLENGTH - slen,
  1427. fa_to_notify, extack);
  1428. }
  1429. /* Caller must hold RTNL. */
  1430. int fib_table_delete(struct net *net, struct fib_table *tb,
  1431. struct fib_config *cfg, struct netlink_ext_ack *extack)
  1432. {
  1433. struct trie *t = (struct trie *) tb->tb_data;
  1434. struct fib_alias *fa, *fa_to_delete;
  1435. struct key_vector *l, *tp;
  1436. u8 plen = cfg->fc_dst_len;
  1437. u8 slen = KEYLENGTH - plen;
  1438. dscp_t dscp;
  1439. u32 key;
  1440. key = ntohl(cfg->fc_dst);
  1441. l = fib_find_node(t, &tp, key);
  1442. if (!l)
  1443. return -ESRCH;
  1444. dscp = cfg->fc_dscp;
  1445. fa = fib_find_alias(&l->leaf, slen, dscp, 0, tb->tb_id, false);
  1446. if (!fa)
  1447. return -ESRCH;
  1448. pr_debug("Deleting %08x/%d dsfield=0x%02x t=%p\n", key, plen,
  1449. inet_dscp_to_dsfield(dscp), t);
  1450. fa_to_delete = NULL;
  1451. hlist_for_each_entry_from(fa, fa_list) {
  1452. struct fib_info *fi = fa->fa_info;
  1453. if ((fa->fa_slen != slen) ||
  1454. (fa->tb_id != tb->tb_id) ||
  1455. (fa->fa_dscp != dscp))
  1456. break;
  1457. if ((!cfg->fc_type || fa->fa_type == cfg->fc_type) &&
  1458. (cfg->fc_scope == RT_SCOPE_NOWHERE ||
  1459. fa->fa_info->fib_scope == cfg->fc_scope) &&
  1460. (!cfg->fc_prefsrc ||
  1461. fi->fib_prefsrc == cfg->fc_prefsrc) &&
  1462. (!cfg->fc_protocol ||
  1463. fi->fib_protocol == cfg->fc_protocol) &&
  1464. fib_nh_match(net, cfg, fi, extack) == 0 &&
  1465. fib_metrics_match(cfg, fi)) {
  1466. fa_to_delete = fa;
  1467. break;
  1468. }
  1469. }
  1470. if (!fa_to_delete)
  1471. return -ESRCH;
  1472. fib_notify_alias_delete(net, key, &l->leaf, fa_to_delete, extack);
  1473. rtmsg_fib(RTM_DELROUTE, htonl(key), fa_to_delete, plen, tb->tb_id,
  1474. &cfg->fc_nlinfo, 0);
  1475. if (!plen)
  1476. tb->tb_num_default--;
  1477. fib_remove_alias(t, tp, l, fa_to_delete);
  1478. if (fa_to_delete->fa_state & FA_S_ACCESSED)
  1479. rt_cache_flush(cfg->fc_nlinfo.nl_net);
  1480. fib_release_info(fa_to_delete->fa_info);
  1481. alias_free_mem_rcu(fa_to_delete);
  1482. return 0;
  1483. }
  1484. /* Scan for the next leaf starting at the provided key value */
  1485. static struct key_vector *leaf_walk_rcu(struct key_vector **tn, t_key key)
  1486. {
  1487. struct key_vector *pn, *n = *tn;
  1488. unsigned long cindex;
  1489. /* this loop is meant to try and find the key in the trie */
  1490. do {
  1491. /* record parent and next child index */
  1492. pn = n;
  1493. cindex = (key > pn->key) ? get_index(key, pn) : 0;
  1494. if (cindex >> pn->bits)
  1495. break;
  1496. /* descend into the next child */
  1497. n = get_child_rcu(pn, cindex++);
  1498. if (!n)
  1499. break;
  1500. /* guarantee forward progress on the keys */
  1501. if (IS_LEAF(n) && (n->key >= key))
  1502. goto found;
  1503. } while (IS_TNODE(n));
  1504. /* this loop will search for the next leaf with a greater key */
  1505. while (!IS_TRIE(pn)) {
  1506. /* if we exhausted the parent node we will need to climb */
  1507. if (cindex >= (1ul << pn->bits)) {
  1508. t_key pkey = pn->key;
  1509. pn = node_parent_rcu(pn);
  1510. cindex = get_index(pkey, pn) + 1;
  1511. continue;
  1512. }
  1513. /* grab the next available node */
  1514. n = get_child_rcu(pn, cindex++);
  1515. if (!n)
  1516. continue;
  1517. /* no need to compare keys since we bumped the index */
  1518. if (IS_LEAF(n))
  1519. goto found;
  1520. /* Rescan start scanning in new node */
  1521. pn = n;
  1522. cindex = 0;
  1523. }
  1524. *tn = pn;
  1525. return NULL; /* Root of trie */
  1526. found:
  1527. /* if we are at the limit for keys just return NULL for the tnode */
  1528. *tn = pn;
  1529. return n;
  1530. }
  1531. static void fib_trie_free(struct fib_table *tb)
  1532. {
  1533. struct trie *t = (struct trie *)tb->tb_data;
  1534. struct key_vector *pn = t->kv;
  1535. unsigned long cindex = 1;
  1536. struct hlist_node *tmp;
  1537. struct fib_alias *fa;
  1538. /* walk trie in reverse order and free everything */
  1539. for (;;) {
  1540. struct key_vector *n;
  1541. if (!(cindex--)) {
  1542. t_key pkey = pn->key;
  1543. if (IS_TRIE(pn))
  1544. break;
  1545. n = pn;
  1546. pn = node_parent(pn);
  1547. /* drop emptied tnode */
  1548. put_child_root(pn, n->key, NULL);
  1549. node_free(n);
  1550. cindex = get_index(pkey, pn);
  1551. continue;
  1552. }
  1553. /* grab the next available node */
  1554. n = get_child(pn, cindex);
  1555. if (!n)
  1556. continue;
  1557. if (IS_TNODE(n)) {
  1558. /* record pn and cindex for leaf walking */
  1559. pn = n;
  1560. cindex = 1ul << n->bits;
  1561. continue;
  1562. }
  1563. hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
  1564. hlist_del_rcu(&fa->fa_list);
  1565. alias_free_mem_rcu(fa);
  1566. }
  1567. put_child_root(pn, n->key, NULL);
  1568. node_free(n);
  1569. }
  1570. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1571. free_percpu(t->stats);
  1572. #endif
  1573. kfree(tb);
  1574. }
  1575. struct fib_table *fib_trie_unmerge(struct fib_table *oldtb)
  1576. {
  1577. struct trie *ot = (struct trie *)oldtb->tb_data;
  1578. struct key_vector *l, *tp = ot->kv;
  1579. struct fib_table *local_tb;
  1580. struct fib_alias *fa;
  1581. struct trie *lt;
  1582. t_key key = 0;
  1583. if (oldtb->tb_data == oldtb->__data)
  1584. return oldtb;
  1585. local_tb = fib_trie_table(RT_TABLE_LOCAL, NULL);
  1586. if (!local_tb)
  1587. return NULL;
  1588. lt = (struct trie *)local_tb->tb_data;
  1589. while ((l = leaf_walk_rcu(&tp, key)) != NULL) {
  1590. struct key_vector *local_l = NULL, *local_tp;
  1591. hlist_for_each_entry(fa, &l->leaf, fa_list) {
  1592. struct fib_alias *new_fa;
  1593. if (local_tb->tb_id != fa->tb_id)
  1594. continue;
  1595. /* clone fa for new local table */
  1596. new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
  1597. if (!new_fa)
  1598. goto out;
  1599. memcpy(new_fa, fa, sizeof(*fa));
  1600. /* insert clone into table */
  1601. if (!local_l)
  1602. local_l = fib_find_node(lt, &local_tp, l->key);
  1603. if (fib_insert_alias(lt, local_tp, local_l, new_fa,
  1604. NULL, l->key)) {
  1605. kmem_cache_free(fn_alias_kmem, new_fa);
  1606. goto out;
  1607. }
  1608. }
  1609. /* stop loop if key wrapped back to 0 */
  1610. key = l->key + 1;
  1611. if (key < l->key)
  1612. break;
  1613. }
  1614. return local_tb;
  1615. out:
  1616. fib_trie_free(local_tb);
  1617. return NULL;
  1618. }
  1619. /* Caller must hold RTNL */
  1620. void fib_table_flush_external(struct fib_table *tb)
  1621. {
  1622. struct trie *t = (struct trie *)tb->tb_data;
  1623. struct key_vector *pn = t->kv;
  1624. unsigned long cindex = 1;
  1625. struct hlist_node *tmp;
  1626. struct fib_alias *fa;
  1627. /* walk trie in reverse order */
  1628. for (;;) {
  1629. unsigned char slen = 0;
  1630. struct key_vector *n;
  1631. if (!(cindex--)) {
  1632. t_key pkey = pn->key;
  1633. /* cannot resize the trie vector */
  1634. if (IS_TRIE(pn))
  1635. break;
  1636. /* update the suffix to address pulled leaves */
  1637. if (pn->slen > pn->pos)
  1638. update_suffix(pn);
  1639. /* resize completed node */
  1640. pn = resize(t, pn);
  1641. cindex = get_index(pkey, pn);
  1642. continue;
  1643. }
  1644. /* grab the next available node */
  1645. n = get_child(pn, cindex);
  1646. if (!n)
  1647. continue;
  1648. if (IS_TNODE(n)) {
  1649. /* record pn and cindex for leaf walking */
  1650. pn = n;
  1651. cindex = 1ul << n->bits;
  1652. continue;
  1653. }
  1654. hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
  1655. /* if alias was cloned to local then we just
  1656. * need to remove the local copy from main
  1657. */
  1658. if (tb->tb_id != fa->tb_id) {
  1659. hlist_del_rcu(&fa->fa_list);
  1660. alias_free_mem_rcu(fa);
  1661. continue;
  1662. }
  1663. /* record local slen */
  1664. slen = fa->fa_slen;
  1665. }
  1666. /* update leaf slen */
  1667. n->slen = slen;
  1668. if (hlist_empty(&n->leaf)) {
  1669. put_child_root(pn, n->key, NULL);
  1670. node_free(n);
  1671. }
  1672. }
  1673. }
  1674. /* Caller must hold RTNL. */
  1675. int fib_table_flush(struct net *net, struct fib_table *tb, bool flush_all)
  1676. {
  1677. struct trie *t = (struct trie *)tb->tb_data;
  1678. struct nl_info info = { .nl_net = net };
  1679. struct key_vector *pn = t->kv;
  1680. unsigned long cindex = 1;
  1681. struct hlist_node *tmp;
  1682. struct fib_alias *fa;
  1683. int found = 0;
  1684. /* walk trie in reverse order */
  1685. for (;;) {
  1686. unsigned char slen = 0;
  1687. struct key_vector *n;
  1688. if (!(cindex--)) {
  1689. t_key pkey = pn->key;
  1690. /* cannot resize the trie vector */
  1691. if (IS_TRIE(pn))
  1692. break;
  1693. /* update the suffix to address pulled leaves */
  1694. if (pn->slen > pn->pos)
  1695. update_suffix(pn);
  1696. /* resize completed node */
  1697. pn = resize(t, pn);
  1698. cindex = get_index(pkey, pn);
  1699. continue;
  1700. }
  1701. /* grab the next available node */
  1702. n = get_child(pn, cindex);
  1703. if (!n)
  1704. continue;
  1705. if (IS_TNODE(n)) {
  1706. /* record pn and cindex for leaf walking */
  1707. pn = n;
  1708. cindex = 1ul << n->bits;
  1709. continue;
  1710. }
  1711. hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
  1712. struct fib_info *fi = fa->fa_info;
  1713. if (!fi || tb->tb_id != fa->tb_id ||
  1714. (!(fi->fib_flags & RTNH_F_DEAD) &&
  1715. !fib_props[fa->fa_type].error)) {
  1716. slen = fa->fa_slen;
  1717. continue;
  1718. }
  1719. /* Do not flush error routes if network namespace is
  1720. * not being dismantled
  1721. */
  1722. if (!flush_all && fib_props[fa->fa_type].error) {
  1723. slen = fa->fa_slen;
  1724. continue;
  1725. }
  1726. fib_notify_alias_delete(net, n->key, &n->leaf, fa,
  1727. NULL);
  1728. if (fi->pfsrc_removed)
  1729. rtmsg_fib(RTM_DELROUTE, htonl(n->key), fa,
  1730. KEYLENGTH - fa->fa_slen, tb->tb_id, &info, 0);
  1731. hlist_del_rcu(&fa->fa_list);
  1732. fib_release_info(fa->fa_info);
  1733. alias_free_mem_rcu(fa);
  1734. found++;
  1735. }
  1736. /* update leaf slen */
  1737. n->slen = slen;
  1738. if (hlist_empty(&n->leaf)) {
  1739. put_child_root(pn, n->key, NULL);
  1740. node_free(n);
  1741. }
  1742. }
  1743. pr_debug("trie_flush found=%d\n", found);
  1744. return found;
  1745. }
  1746. /* derived from fib_trie_free */
  1747. static void __fib_info_notify_update(struct net *net, struct fib_table *tb,
  1748. struct nl_info *info)
  1749. {
  1750. struct trie *t = (struct trie *)tb->tb_data;
  1751. struct key_vector *pn = t->kv;
  1752. unsigned long cindex = 1;
  1753. struct fib_alias *fa;
  1754. for (;;) {
  1755. struct key_vector *n;
  1756. if (!(cindex--)) {
  1757. t_key pkey = pn->key;
  1758. if (IS_TRIE(pn))
  1759. break;
  1760. pn = node_parent(pn);
  1761. cindex = get_index(pkey, pn);
  1762. continue;
  1763. }
  1764. /* grab the next available node */
  1765. n = get_child(pn, cindex);
  1766. if (!n)
  1767. continue;
  1768. if (IS_TNODE(n)) {
  1769. /* record pn and cindex for leaf walking */
  1770. pn = n;
  1771. cindex = 1ul << n->bits;
  1772. continue;
  1773. }
  1774. hlist_for_each_entry(fa, &n->leaf, fa_list) {
  1775. struct fib_info *fi = fa->fa_info;
  1776. if (!fi || !fi->nh_updated || fa->tb_id != tb->tb_id)
  1777. continue;
  1778. rtmsg_fib(RTM_NEWROUTE, htonl(n->key), fa,
  1779. KEYLENGTH - fa->fa_slen, tb->tb_id,
  1780. info, NLM_F_REPLACE);
  1781. }
  1782. }
  1783. }
  1784. void fib_info_notify_update(struct net *net, struct nl_info *info)
  1785. {
  1786. unsigned int h;
  1787. for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
  1788. struct hlist_head *head = &net->ipv4.fib_table_hash[h];
  1789. struct fib_table *tb;
  1790. hlist_for_each_entry_rcu(tb, head, tb_hlist,
  1791. lockdep_rtnl_is_held())
  1792. __fib_info_notify_update(net, tb, info);
  1793. }
  1794. }
  1795. static int fib_leaf_notify(struct key_vector *l, struct fib_table *tb,
  1796. struct notifier_block *nb,
  1797. struct netlink_ext_ack *extack)
  1798. {
  1799. struct fib_alias *fa;
  1800. int last_slen = -1;
  1801. int err;
  1802. hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
  1803. struct fib_info *fi = fa->fa_info;
  1804. if (!fi)
  1805. continue;
  1806. /* local and main table can share the same trie,
  1807. * so don't notify twice for the same entry.
  1808. */
  1809. if (tb->tb_id != fa->tb_id)
  1810. continue;
  1811. if (fa->fa_slen == last_slen)
  1812. continue;
  1813. last_slen = fa->fa_slen;
  1814. err = call_fib_entry_notifier(nb, FIB_EVENT_ENTRY_REPLACE,
  1815. l->key, KEYLENGTH - fa->fa_slen,
  1816. fa, extack);
  1817. if (err)
  1818. return err;
  1819. }
  1820. return 0;
  1821. }
  1822. static int fib_table_notify(struct fib_table *tb, struct notifier_block *nb,
  1823. struct netlink_ext_ack *extack)
  1824. {
  1825. struct trie *t = (struct trie *)tb->tb_data;
  1826. struct key_vector *l, *tp = t->kv;
  1827. t_key key = 0;
  1828. int err;
  1829. while ((l = leaf_walk_rcu(&tp, key)) != NULL) {
  1830. err = fib_leaf_notify(l, tb, nb, extack);
  1831. if (err)
  1832. return err;
  1833. key = l->key + 1;
  1834. /* stop in case of wrap around */
  1835. if (key < l->key)
  1836. break;
  1837. }
  1838. return 0;
  1839. }
  1840. int fib_notify(struct net *net, struct notifier_block *nb,
  1841. struct netlink_ext_ack *extack)
  1842. {
  1843. unsigned int h;
  1844. int err;
  1845. for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
  1846. struct hlist_head *head = &net->ipv4.fib_table_hash[h];
  1847. struct fib_table *tb;
  1848. hlist_for_each_entry_rcu(tb, head, tb_hlist) {
  1849. err = fib_table_notify(tb, nb, extack);
  1850. if (err)
  1851. return err;
  1852. }
  1853. }
  1854. return 0;
  1855. }
  1856. static void __trie_free_rcu(struct rcu_head *head)
  1857. {
  1858. struct fib_table *tb = container_of(head, struct fib_table, rcu);
  1859. #ifdef CONFIG_IP_FIB_TRIE_STATS
  1860. struct trie *t = (struct trie *)tb->tb_data;
  1861. if (tb->tb_data == tb->__data)
  1862. free_percpu(t->stats);
  1863. #endif /* CONFIG_IP_FIB_TRIE_STATS */
  1864. kfree(tb);
  1865. }
  1866. void fib_free_table(struct fib_table *tb)
  1867. {
  1868. call_rcu(&tb->rcu, __trie_free_rcu);
  1869. }
  1870. static int fn_trie_dump_leaf(struct key_vector *l, struct fib_table *tb,
  1871. struct sk_buff *skb, struct netlink_callback *cb,
  1872. struct fib_dump_filter *filter)
  1873. {
  1874. unsigned int flags = NLM_F_MULTI;
  1875. __be32 xkey = htonl(l->key);
  1876. int i, s_i, i_fa, s_fa, err;
  1877. struct fib_alias *fa;
  1878. if (filter->filter_set ||
  1879. !filter->dump_exceptions || !filter->dump_routes)
  1880. flags |= NLM_F_DUMP_FILTERED;
  1881. s_i = cb->args[4];
  1882. s_fa = cb->args[5];
  1883. i = 0;
  1884. /* rcu_read_lock is hold by caller */
  1885. hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
  1886. struct fib_info *fi = fa->fa_info;
  1887. if (i < s_i)
  1888. goto next;
  1889. i_fa = 0;
  1890. if (tb->tb_id != fa->tb_id)
  1891. goto next;
  1892. if (filter->filter_set) {
  1893. if (filter->rt_type && fa->fa_type != filter->rt_type)
  1894. goto next;
  1895. if ((filter->protocol &&
  1896. fi->fib_protocol != filter->protocol))
  1897. goto next;
  1898. if (filter->dev &&
  1899. !fib_info_nh_uses_dev(fi, filter->dev))
  1900. goto next;
  1901. }
  1902. if (filter->dump_routes) {
  1903. if (!s_fa) {
  1904. struct fib_rt_info fri;
  1905. fri.fi = fi;
  1906. fri.tb_id = tb->tb_id;
  1907. fri.dst = xkey;
  1908. fri.dst_len = KEYLENGTH - fa->fa_slen;
  1909. fri.dscp = fa->fa_dscp;
  1910. fri.type = fa->fa_type;
  1911. fri.offload = READ_ONCE(fa->offload);
  1912. fri.trap = READ_ONCE(fa->trap);
  1913. fri.offload_failed = READ_ONCE(fa->offload_failed);
  1914. err = fib_dump_info(skb,
  1915. NETLINK_CB(cb->skb).portid,
  1916. cb->nlh->nlmsg_seq,
  1917. RTM_NEWROUTE, &fri, flags);
  1918. if (err < 0)
  1919. goto stop;
  1920. }
  1921. i_fa++;
  1922. }
  1923. if (filter->dump_exceptions) {
  1924. err = fib_dump_info_fnhe(skb, cb, tb->tb_id, fi,
  1925. &i_fa, s_fa, flags);
  1926. if (err < 0)
  1927. goto stop;
  1928. }
  1929. next:
  1930. i++;
  1931. }
  1932. cb->args[4] = i;
  1933. return skb->len;
  1934. stop:
  1935. cb->args[4] = i;
  1936. cb->args[5] = i_fa;
  1937. return err;
  1938. }
  1939. /* rcu_read_lock needs to be hold by caller from readside */
  1940. int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
  1941. struct netlink_callback *cb, struct fib_dump_filter *filter)
  1942. {
  1943. struct trie *t = (struct trie *)tb->tb_data;
  1944. struct key_vector *l, *tp = t->kv;
  1945. /* Dump starting at last key.
  1946. * Note: 0.0.0.0/0 (ie default) is first key.
  1947. */
  1948. int count = cb->args[2];
  1949. t_key key = cb->args[3];
  1950. /* First time here, count and key are both always 0. Count > 0
  1951. * and key == 0 means the dump has wrapped around and we are done.
  1952. */
  1953. if (count && !key)
  1954. return 0;
  1955. while ((l = leaf_walk_rcu(&tp, key)) != NULL) {
  1956. int err;
  1957. err = fn_trie_dump_leaf(l, tb, skb, cb, filter);
  1958. if (err < 0) {
  1959. cb->args[3] = key;
  1960. cb->args[2] = count;
  1961. return err;
  1962. }
  1963. ++count;
  1964. key = l->key + 1;
  1965. memset(&cb->args[4], 0,
  1966. sizeof(cb->args) - 4*sizeof(cb->args[0]));
  1967. /* stop loop if key wrapped back to 0 */
  1968. if (key < l->key)
  1969. break;
  1970. }
  1971. cb->args[3] = key;
  1972. cb->args[2] = count;
  1973. return 0;
  1974. }
  1975. void __init fib_trie_init(void)
  1976. {
  1977. fn_alias_kmem = kmem_cache_create("ip_fib_alias",
  1978. sizeof(struct fib_alias),
  1979. 0, SLAB_PANIC | SLAB_ACCOUNT, NULL);
  1980. trie_leaf_kmem = kmem_cache_create("ip_fib_trie",
  1981. LEAF_SIZE,
  1982. 0, SLAB_PANIC | SLAB_ACCOUNT, NULL);
  1983. }
  1984. struct fib_table *fib_trie_table(u32 id, struct fib_table *alias)
  1985. {
  1986. struct fib_table *tb;
  1987. struct trie *t;
  1988. size_t sz = sizeof(*tb);
  1989. if (!alias)
  1990. sz += sizeof(struct trie);
  1991. tb = kzalloc(sz, GFP_KERNEL);
  1992. if (!tb)
  1993. return NULL;
  1994. tb->tb_id = id;
  1995. tb->tb_num_default = 0;
  1996. tb->tb_data = (alias ? alias->__data : tb->__data);
  1997. if (alias)
  1998. return tb;
  1999. t = (struct trie *) tb->tb_data;
  2000. t->kv[0].pos = KEYLENGTH;
  2001. t->kv[0].slen = KEYLENGTH;
  2002. #ifdef CONFIG_IP_FIB_TRIE_STATS
  2003. t->stats = alloc_percpu(struct trie_use_stats);
  2004. if (!t->stats) {
  2005. kfree(tb);
  2006. tb = NULL;
  2007. }
  2008. #endif
  2009. return tb;
  2010. }
  2011. #ifdef CONFIG_PROC_FS
  2012. /* Depth first Trie walk iterator */
  2013. struct fib_trie_iter {
  2014. struct seq_net_private p;
  2015. struct fib_table *tb;
  2016. struct key_vector *tnode;
  2017. unsigned int index;
  2018. unsigned int depth;
  2019. };
  2020. static struct key_vector *fib_trie_get_next(struct fib_trie_iter *iter)
  2021. {
  2022. unsigned long cindex = iter->index;
  2023. struct key_vector *pn = iter->tnode;
  2024. t_key pkey;
  2025. pr_debug("get_next iter={node=%p index=%d depth=%d}\n",
  2026. iter->tnode, iter->index, iter->depth);
  2027. while (!IS_TRIE(pn)) {
  2028. while (cindex < child_length(pn)) {
  2029. struct key_vector *n = get_child_rcu(pn, cindex++);
  2030. if (!n)
  2031. continue;
  2032. if (IS_LEAF(n)) {
  2033. iter->tnode = pn;
  2034. iter->index = cindex;
  2035. } else {
  2036. /* push down one level */
  2037. iter->tnode = n;
  2038. iter->index = 0;
  2039. ++iter->depth;
  2040. }
  2041. return n;
  2042. }
  2043. /* Current node exhausted, pop back up */
  2044. pkey = pn->key;
  2045. pn = node_parent_rcu(pn);
  2046. cindex = get_index(pkey, pn) + 1;
  2047. --iter->depth;
  2048. }
  2049. /* record root node so further searches know we are done */
  2050. iter->tnode = pn;
  2051. iter->index = 0;
  2052. return NULL;
  2053. }
  2054. static struct key_vector *fib_trie_get_first(struct fib_trie_iter *iter,
  2055. struct trie *t)
  2056. {
  2057. struct key_vector *n, *pn;
  2058. if (!t)
  2059. return NULL;
  2060. pn = t->kv;
  2061. n = rcu_dereference(pn->tnode[0]);
  2062. if (!n)
  2063. return NULL;
  2064. if (IS_TNODE(n)) {
  2065. iter->tnode = n;
  2066. iter->index = 0;
  2067. iter->depth = 1;
  2068. } else {
  2069. iter->tnode = pn;
  2070. iter->index = 0;
  2071. iter->depth = 0;
  2072. }
  2073. return n;
  2074. }
  2075. static void trie_collect_stats(struct trie *t, struct trie_stat *s)
  2076. {
  2077. struct key_vector *n;
  2078. struct fib_trie_iter iter;
  2079. memset(s, 0, sizeof(*s));
  2080. rcu_read_lock();
  2081. for (n = fib_trie_get_first(&iter, t); n; n = fib_trie_get_next(&iter)) {
  2082. if (IS_LEAF(n)) {
  2083. struct fib_alias *fa;
  2084. s->leaves++;
  2085. s->totdepth += iter.depth;
  2086. if (iter.depth > s->maxdepth)
  2087. s->maxdepth = iter.depth;
  2088. hlist_for_each_entry_rcu(fa, &n->leaf, fa_list)
  2089. ++s->prefixes;
  2090. } else {
  2091. s->tnodes++;
  2092. if (n->bits < MAX_STAT_DEPTH)
  2093. s->nodesizes[n->bits]++;
  2094. s->nullpointers += tn_info(n)->empty_children;
  2095. }
  2096. }
  2097. rcu_read_unlock();
  2098. }
  2099. /*
  2100. * This outputs /proc/net/fib_triestats
  2101. */
  2102. static void trie_show_stats(struct seq_file *seq, struct trie_stat *stat)
  2103. {
  2104. unsigned int i, max, pointers, bytes, avdepth;
  2105. if (stat->leaves)
  2106. avdepth = stat->totdepth*100 / stat->leaves;
  2107. else
  2108. avdepth = 0;
  2109. seq_printf(seq, "\tAver depth: %u.%02d\n",
  2110. avdepth / 100, avdepth % 100);
  2111. seq_printf(seq, "\tMax depth: %u\n", stat->maxdepth);
  2112. seq_printf(seq, "\tLeaves: %u\n", stat->leaves);
  2113. bytes = LEAF_SIZE * stat->leaves;
  2114. seq_printf(seq, "\tPrefixes: %u\n", stat->prefixes);
  2115. bytes += sizeof(struct fib_alias) * stat->prefixes;
  2116. seq_printf(seq, "\tInternal nodes: %u\n\t", stat->tnodes);
  2117. bytes += TNODE_SIZE(0) * stat->tnodes;
  2118. max = MAX_STAT_DEPTH;
  2119. while (max > 0 && stat->nodesizes[max-1] == 0)
  2120. max--;
  2121. pointers = 0;
  2122. for (i = 1; i < max; i++)
  2123. if (stat->nodesizes[i] != 0) {
  2124. seq_printf(seq, " %u: %u", i, stat->nodesizes[i]);
  2125. pointers += (1<<i) * stat->nodesizes[i];
  2126. }
  2127. seq_putc(seq, '\n');
  2128. seq_printf(seq, "\tPointers: %u\n", pointers);
  2129. bytes += sizeof(struct key_vector *) * pointers;
  2130. seq_printf(seq, "Null ptrs: %u\n", stat->nullpointers);
  2131. seq_printf(seq, "Total size: %u kB\n", (bytes + 1023) / 1024);
  2132. }
  2133. #ifdef CONFIG_IP_FIB_TRIE_STATS
  2134. static void trie_show_usage(struct seq_file *seq,
  2135. const struct trie_use_stats __percpu *stats)
  2136. {
  2137. struct trie_use_stats s = { 0 };
  2138. int cpu;
  2139. /* loop through all of the CPUs and gather up the stats */
  2140. for_each_possible_cpu(cpu) {
  2141. const struct trie_use_stats *pcpu = per_cpu_ptr(stats, cpu);
  2142. s.gets += pcpu->gets;
  2143. s.backtrack += pcpu->backtrack;
  2144. s.semantic_match_passed += pcpu->semantic_match_passed;
  2145. s.semantic_match_miss += pcpu->semantic_match_miss;
  2146. s.null_node_hit += pcpu->null_node_hit;
  2147. s.resize_node_skipped += pcpu->resize_node_skipped;
  2148. }
  2149. seq_printf(seq, "\nCounters:\n---------\n");
  2150. seq_printf(seq, "gets = %u\n", s.gets);
  2151. seq_printf(seq, "backtracks = %u\n", s.backtrack);
  2152. seq_printf(seq, "semantic match passed = %u\n",
  2153. s.semantic_match_passed);
  2154. seq_printf(seq, "semantic match miss = %u\n", s.semantic_match_miss);
  2155. seq_printf(seq, "null node hit= %u\n", s.null_node_hit);
  2156. seq_printf(seq, "skipped node resize = %u\n\n", s.resize_node_skipped);
  2157. }
  2158. #endif /* CONFIG_IP_FIB_TRIE_STATS */
  2159. static void fib_table_print(struct seq_file *seq, struct fib_table *tb)
  2160. {
  2161. if (tb->tb_id == RT_TABLE_LOCAL)
  2162. seq_puts(seq, "Local:\n");
  2163. else if (tb->tb_id == RT_TABLE_MAIN)
  2164. seq_puts(seq, "Main:\n");
  2165. else
  2166. seq_printf(seq, "Id %d:\n", tb->tb_id);
  2167. }
  2168. static int fib_triestat_seq_show(struct seq_file *seq, void *v)
  2169. {
  2170. struct net *net = seq->private;
  2171. unsigned int h;
  2172. seq_printf(seq,
  2173. "Basic info: size of leaf:"
  2174. " %zd bytes, size of tnode: %zd bytes.\n",
  2175. LEAF_SIZE, TNODE_SIZE(0));
  2176. rcu_read_lock();
  2177. for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
  2178. struct hlist_head *head = &net->ipv4.fib_table_hash[h];
  2179. struct fib_table *tb;
  2180. hlist_for_each_entry_rcu(tb, head, tb_hlist) {
  2181. struct trie *t = (struct trie *) tb->tb_data;
  2182. struct trie_stat stat;
  2183. if (!t)
  2184. continue;
  2185. fib_table_print(seq, tb);
  2186. trie_collect_stats(t, &stat);
  2187. trie_show_stats(seq, &stat);
  2188. #ifdef CONFIG_IP_FIB_TRIE_STATS
  2189. trie_show_usage(seq, t->stats);
  2190. #endif
  2191. }
  2192. cond_resched_rcu();
  2193. }
  2194. rcu_read_unlock();
  2195. return 0;
  2196. }
  2197. static struct key_vector *fib_trie_get_idx(struct seq_file *seq, loff_t pos)
  2198. {
  2199. struct fib_trie_iter *iter = seq->private;
  2200. struct net *net = seq_file_net(seq);
  2201. loff_t idx = 0;
  2202. unsigned int h;
  2203. for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
  2204. struct hlist_head *head = &net->ipv4.fib_table_hash[h];
  2205. struct fib_table *tb;
  2206. hlist_for_each_entry_rcu(tb, head, tb_hlist) {
  2207. struct key_vector *n;
  2208. for (n = fib_trie_get_first(iter,
  2209. (struct trie *) tb->tb_data);
  2210. n; n = fib_trie_get_next(iter))
  2211. if (pos == idx++) {
  2212. iter->tb = tb;
  2213. return n;
  2214. }
  2215. }
  2216. }
  2217. return NULL;
  2218. }
  2219. static void *fib_trie_seq_start(struct seq_file *seq, loff_t *pos)
  2220. __acquires(RCU)
  2221. {
  2222. rcu_read_lock();
  2223. return fib_trie_get_idx(seq, *pos);
  2224. }
  2225. static void *fib_trie_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2226. {
  2227. struct fib_trie_iter *iter = seq->private;
  2228. struct net *net = seq_file_net(seq);
  2229. struct fib_table *tb = iter->tb;
  2230. struct hlist_node *tb_node;
  2231. unsigned int h;
  2232. struct key_vector *n;
  2233. ++*pos;
  2234. /* next node in same table */
  2235. n = fib_trie_get_next(iter);
  2236. if (n)
  2237. return n;
  2238. /* walk rest of this hash chain */
  2239. h = tb->tb_id & (FIB_TABLE_HASHSZ - 1);
  2240. while ((tb_node = rcu_dereference(hlist_next_rcu(&tb->tb_hlist)))) {
  2241. tb = hlist_entry(tb_node, struct fib_table, tb_hlist);
  2242. n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
  2243. if (n)
  2244. goto found;
  2245. }
  2246. /* new hash chain */
  2247. while (++h < FIB_TABLE_HASHSZ) {
  2248. struct hlist_head *head = &net->ipv4.fib_table_hash[h];
  2249. hlist_for_each_entry_rcu(tb, head, tb_hlist) {
  2250. n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
  2251. if (n)
  2252. goto found;
  2253. }
  2254. }
  2255. return NULL;
  2256. found:
  2257. iter->tb = tb;
  2258. return n;
  2259. }
  2260. static void fib_trie_seq_stop(struct seq_file *seq, void *v)
  2261. __releases(RCU)
  2262. {
  2263. rcu_read_unlock();
  2264. }
  2265. static void seq_indent(struct seq_file *seq, int n)
  2266. {
  2267. while (n-- > 0)
  2268. seq_puts(seq, " ");
  2269. }
  2270. static inline const char *rtn_scope(char *buf, size_t len, enum rt_scope_t s)
  2271. {
  2272. switch (s) {
  2273. case RT_SCOPE_UNIVERSE: return "universe";
  2274. case RT_SCOPE_SITE: return "site";
  2275. case RT_SCOPE_LINK: return "link";
  2276. case RT_SCOPE_HOST: return "host";
  2277. case RT_SCOPE_NOWHERE: return "nowhere";
  2278. default:
  2279. snprintf(buf, len, "scope=%d", s);
  2280. return buf;
  2281. }
  2282. }
  2283. static const char *const rtn_type_names[__RTN_MAX] = {
  2284. [RTN_UNSPEC] = "UNSPEC",
  2285. [RTN_UNICAST] = "UNICAST",
  2286. [RTN_LOCAL] = "LOCAL",
  2287. [RTN_BROADCAST] = "BROADCAST",
  2288. [RTN_ANYCAST] = "ANYCAST",
  2289. [RTN_MULTICAST] = "MULTICAST",
  2290. [RTN_BLACKHOLE] = "BLACKHOLE",
  2291. [RTN_UNREACHABLE] = "UNREACHABLE",
  2292. [RTN_PROHIBIT] = "PROHIBIT",
  2293. [RTN_THROW] = "THROW",
  2294. [RTN_NAT] = "NAT",
  2295. [RTN_XRESOLVE] = "XRESOLVE",
  2296. };
  2297. static inline const char *rtn_type(char *buf, size_t len, unsigned int t)
  2298. {
  2299. if (t < __RTN_MAX && rtn_type_names[t])
  2300. return rtn_type_names[t];
  2301. snprintf(buf, len, "type %u", t);
  2302. return buf;
  2303. }
  2304. /* Pretty print the trie */
  2305. static int fib_trie_seq_show(struct seq_file *seq, void *v)
  2306. {
  2307. const struct fib_trie_iter *iter = seq->private;
  2308. struct key_vector *n = v;
  2309. if (IS_TRIE(node_parent_rcu(n)))
  2310. fib_table_print(seq, iter->tb);
  2311. if (IS_TNODE(n)) {
  2312. __be32 prf = htonl(n->key);
  2313. seq_indent(seq, iter->depth-1);
  2314. seq_printf(seq, " +-- %pI4/%zu %u %u %u\n",
  2315. &prf, KEYLENGTH - n->pos - n->bits, n->bits,
  2316. tn_info(n)->full_children,
  2317. tn_info(n)->empty_children);
  2318. } else {
  2319. __be32 val = htonl(n->key);
  2320. struct fib_alias *fa;
  2321. seq_indent(seq, iter->depth);
  2322. seq_printf(seq, " |-- %pI4\n", &val);
  2323. hlist_for_each_entry_rcu(fa, &n->leaf, fa_list) {
  2324. char buf1[32], buf2[32];
  2325. seq_indent(seq, iter->depth + 1);
  2326. seq_printf(seq, " /%zu %s %s",
  2327. KEYLENGTH - fa->fa_slen,
  2328. rtn_scope(buf1, sizeof(buf1),
  2329. fa->fa_info->fib_scope),
  2330. rtn_type(buf2, sizeof(buf2),
  2331. fa->fa_type));
  2332. if (fa->fa_dscp)
  2333. seq_printf(seq, " tos=%d",
  2334. inet_dscp_to_dsfield(fa->fa_dscp));
  2335. seq_putc(seq, '\n');
  2336. }
  2337. }
  2338. return 0;
  2339. }
  2340. static const struct seq_operations fib_trie_seq_ops = {
  2341. .start = fib_trie_seq_start,
  2342. .next = fib_trie_seq_next,
  2343. .stop = fib_trie_seq_stop,
  2344. .show = fib_trie_seq_show,
  2345. };
  2346. struct fib_route_iter {
  2347. struct seq_net_private p;
  2348. struct fib_table *main_tb;
  2349. struct key_vector *tnode;
  2350. loff_t pos;
  2351. t_key key;
  2352. };
  2353. static struct key_vector *fib_route_get_idx(struct fib_route_iter *iter,
  2354. loff_t pos)
  2355. {
  2356. struct key_vector *l, **tp = &iter->tnode;
  2357. t_key key;
  2358. /* use cached location of previously found key */
  2359. if (iter->pos > 0 && pos >= iter->pos) {
  2360. key = iter->key;
  2361. } else {
  2362. iter->pos = 1;
  2363. key = 0;
  2364. }
  2365. pos -= iter->pos;
  2366. while ((l = leaf_walk_rcu(tp, key)) && (pos-- > 0)) {
  2367. key = l->key + 1;
  2368. iter->pos++;
  2369. l = NULL;
  2370. /* handle unlikely case of a key wrap */
  2371. if (!key)
  2372. break;
  2373. }
  2374. if (l)
  2375. iter->key = l->key; /* remember it */
  2376. else
  2377. iter->pos = 0; /* forget it */
  2378. return l;
  2379. }
  2380. static void *fib_route_seq_start(struct seq_file *seq, loff_t *pos)
  2381. __acquires(RCU)
  2382. {
  2383. struct fib_route_iter *iter = seq->private;
  2384. struct fib_table *tb;
  2385. struct trie *t;
  2386. rcu_read_lock();
  2387. tb = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN);
  2388. if (!tb)
  2389. return NULL;
  2390. iter->main_tb = tb;
  2391. t = (struct trie *)tb->tb_data;
  2392. iter->tnode = t->kv;
  2393. if (*pos != 0)
  2394. return fib_route_get_idx(iter, *pos);
  2395. iter->pos = 0;
  2396. iter->key = KEY_MAX;
  2397. return SEQ_START_TOKEN;
  2398. }
  2399. static void *fib_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2400. {
  2401. struct fib_route_iter *iter = seq->private;
  2402. struct key_vector *l = NULL;
  2403. t_key key = iter->key + 1;
  2404. ++*pos;
  2405. /* only allow key of 0 for start of sequence */
  2406. if ((v == SEQ_START_TOKEN) || key)
  2407. l = leaf_walk_rcu(&iter->tnode, key);
  2408. if (l) {
  2409. iter->key = l->key;
  2410. iter->pos++;
  2411. } else {
  2412. iter->pos = 0;
  2413. }
  2414. return l;
  2415. }
  2416. static void fib_route_seq_stop(struct seq_file *seq, void *v)
  2417. __releases(RCU)
  2418. {
  2419. rcu_read_unlock();
  2420. }
  2421. static unsigned int fib_flag_trans(int type, __be32 mask, struct fib_info *fi)
  2422. {
  2423. unsigned int flags = 0;
  2424. if (type == RTN_UNREACHABLE || type == RTN_PROHIBIT)
  2425. flags = RTF_REJECT;
  2426. if (fi) {
  2427. const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
  2428. if (nhc->nhc_gw.ipv4)
  2429. flags |= RTF_GATEWAY;
  2430. }
  2431. if (mask == htonl(0xFFFFFFFF))
  2432. flags |= RTF_HOST;
  2433. flags |= RTF_UP;
  2434. return flags;
  2435. }
  2436. /*
  2437. * This outputs /proc/net/route.
  2438. * The format of the file is not supposed to be changed
  2439. * and needs to be same as fib_hash output to avoid breaking
  2440. * legacy utilities
  2441. */
  2442. static int fib_route_seq_show(struct seq_file *seq, void *v)
  2443. {
  2444. struct fib_route_iter *iter = seq->private;
  2445. struct fib_table *tb = iter->main_tb;
  2446. struct fib_alias *fa;
  2447. struct key_vector *l = v;
  2448. __be32 prefix;
  2449. if (v == SEQ_START_TOKEN) {
  2450. seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
  2451. "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
  2452. "\tWindow\tIRTT");
  2453. return 0;
  2454. }
  2455. prefix = htonl(l->key);
  2456. hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
  2457. struct fib_info *fi = fa->fa_info;
  2458. __be32 mask = inet_make_mask(KEYLENGTH - fa->fa_slen);
  2459. unsigned int flags = fib_flag_trans(fa->fa_type, mask, fi);
  2460. if ((fa->fa_type == RTN_BROADCAST) ||
  2461. (fa->fa_type == RTN_MULTICAST))
  2462. continue;
  2463. if (fa->tb_id != tb->tb_id)
  2464. continue;
  2465. seq_setwidth(seq, 127);
  2466. if (fi) {
  2467. struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
  2468. __be32 gw = 0;
  2469. if (nhc->nhc_gw_family == AF_INET)
  2470. gw = nhc->nhc_gw.ipv4;
  2471. seq_printf(seq,
  2472. "%s\t%08X\t%08X\t%04X\t%d\t%u\t"
  2473. "%d\t%08X\t%d\t%u\t%u",
  2474. nhc->nhc_dev ? nhc->nhc_dev->name : "*",
  2475. prefix, gw, flags, 0, 0,
  2476. fi->fib_priority,
  2477. mask,
  2478. (fi->fib_advmss ?
  2479. fi->fib_advmss + 40 : 0),
  2480. fi->fib_window,
  2481. fi->fib_rtt >> 3);
  2482. } else {
  2483. seq_printf(seq,
  2484. "*\t%08X\t%08X\t%04X\t%d\t%u\t"
  2485. "%d\t%08X\t%d\t%u\t%u",
  2486. prefix, 0, flags, 0, 0, 0,
  2487. mask, 0, 0, 0);
  2488. }
  2489. seq_pad(seq, '\n');
  2490. }
  2491. return 0;
  2492. }
  2493. static const struct seq_operations fib_route_seq_ops = {
  2494. .start = fib_route_seq_start,
  2495. .next = fib_route_seq_next,
  2496. .stop = fib_route_seq_stop,
  2497. .show = fib_route_seq_show,
  2498. };
  2499. int __net_init fib_proc_init(struct net *net)
  2500. {
  2501. if (!proc_create_net("fib_trie", 0444, net->proc_net, &fib_trie_seq_ops,
  2502. sizeof(struct fib_trie_iter)))
  2503. goto out1;
  2504. if (!proc_create_net_single("fib_triestat", 0444, net->proc_net,
  2505. fib_triestat_seq_show, NULL))
  2506. goto out2;
  2507. if (!proc_create_net("route", 0444, net->proc_net, &fib_route_seq_ops,
  2508. sizeof(struct fib_route_iter)))
  2509. goto out3;
  2510. return 0;
  2511. out3:
  2512. remove_proc_entry("fib_triestat", net->proc_net);
  2513. out2:
  2514. remove_proc_entry("fib_trie", net->proc_net);
  2515. out1:
  2516. return -ENOMEM;
  2517. }
  2518. void __net_exit fib_proc_exit(struct net *net)
  2519. {
  2520. remove_proc_entry("fib_trie", net->proc_net);
  2521. remove_proc_entry("fib_triestat", net->proc_net);
  2522. remove_proc_entry("route", net->proc_net);
  2523. }
  2524. #endif /* CONFIG_PROC_FS */