blob: 9ac481a10d37dbff04910af538ab0a624fd2808c [file] [log] [blame]
Robert Olsson19baf832005-06-21 12:43:18 -07001/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version
5 * 2 of the License, or (at your option) any later version.
6 *
7 * Robert Olsson <robert.olsson@its.uu.se> Uppsala Universitet
8 * & Swedish University of Agricultural Sciences.
9 *
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090010 * Jens Laas <jens.laas@data.slu.se> Swedish University of
Robert Olsson19baf832005-06-21 12:43:18 -070011 * Agricultural Sciences.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090012 *
Robert Olsson19baf832005-06-21 12:43:18 -070013 * Hans Liss <hans.liss@its.uu.se> Uppsala Universitet
14 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015 * This work is based on the LPC-trie which is originally described in:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090016 *
Robert Olsson19baf832005-06-21 12:43:18 -070017 * An experimental study of compression methods for dynamic tries
18 * Stefan Nilsson and Matti Tikkanen. Algorithmica, 33(1):19-33, 2002.
Justin P. Mattock631dd1a2010-10-18 11:03:14 +020019 * http://www.csc.kth.se/~snilsson/software/dyntrie2/
Robert Olsson19baf832005-06-21 12:43:18 -070020 *
21 *
22 * IP-address lookup using LC-tries. Stefan Nilsson and Gunnar Karlsson
23 * IEEE Journal on Selected Areas in Communications, 17(6):1083-1092, June 1999
24 *
Robert Olsson19baf832005-06-21 12:43:18 -070025 *
26 * Code from fib_hash has been reused which includes the following header:
27 *
28 *
29 * INET An implementation of the TCP/IP protocol suite for the LINUX
30 * operating system. INET is implemented using the BSD Socket
31 * interface as the means of communication with the user level.
32 *
33 * IPv4 FIB: lookup engine and maintenance routines.
34 *
35 *
36 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
37 *
38 * This program is free software; you can redistribute it and/or
39 * modify it under the terms of the GNU General Public License
40 * as published by the Free Software Foundation; either version
41 * 2 of the License, or (at your option) any later version.
Robert Olssonfd966252005-12-22 11:25:10 -080042 *
43 * Substantial contributions to this work comes from:
44 *
45 * David S. Miller, <davem@davemloft.net>
46 * Stephen Hemminger <shemminger@osdl.org>
47 * Paul E. McKenney <paulmck@us.ibm.com>
48 * Patrick McHardy <kaber@trash.net>
Robert Olsson19baf832005-06-21 12:43:18 -070049 */
50
Jens Låås80b71b82009-08-28 23:57:15 -070051#define VERSION "0.409"
Robert Olsson19baf832005-06-21 12:43:18 -070052
Robert Olsson19baf832005-06-21 12:43:18 -070053#include <asm/uaccess.h>
54#include <asm/system.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070055#include <linux/bitops.h>
Robert Olsson19baf832005-06-21 12:43:18 -070056#include <linux/types.h>
57#include <linux/kernel.h>
Robert Olsson19baf832005-06-21 12:43:18 -070058#include <linux/mm.h>
59#include <linux/string.h>
60#include <linux/socket.h>
61#include <linux/sockios.h>
62#include <linux/errno.h>
63#include <linux/in.h>
64#include <linux/inet.h>
Stephen Hemmingercd8787a2006-01-03 14:38:34 -080065#include <linux/inetdevice.h>
Robert Olsson19baf832005-06-21 12:43:18 -070066#include <linux/netdevice.h>
67#include <linux/if_arp.h>
68#include <linux/proc_fs.h>
Robert Olsson2373ce12005-08-25 13:01:29 -070069#include <linux/rcupdate.h>
Robert Olsson19baf832005-06-21 12:43:18 -070070#include <linux/skbuff.h>
71#include <linux/netlink.h>
72#include <linux/init.h>
73#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090074#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020075#include <net/net_namespace.h>
Robert Olsson19baf832005-06-21 12:43:18 -070076#include <net/ip.h>
77#include <net/protocol.h>
78#include <net/route.h>
79#include <net/tcp.h>
80#include <net/sock.h>
81#include <net/ip_fib.h>
82#include "fib_lookup.h"
83
Robert Olsson06ef9212006-03-20 21:35:01 -080084#define MAX_STAT_DEPTH 32
Robert Olsson19baf832005-06-21 12:43:18 -070085
Robert Olsson19baf832005-06-21 12:43:18 -070086#define KEYLENGTH (8*sizeof(t_key))
Robert Olsson19baf832005-06-21 12:43:18 -070087
Robert Olsson19baf832005-06-21 12:43:18 -070088typedef unsigned int t_key;
89
90#define T_TNODE 0
91#define T_LEAF 1
92#define NODE_TYPE_MASK 0x1UL
Robert Olsson2373ce12005-08-25 13:01:29 -070093#define NODE_TYPE(node) ((node)->parent & NODE_TYPE_MASK)
94
Olof Johansson91b9a272005-08-09 20:24:39 -070095#define IS_TNODE(n) (!(n->parent & T_LEAF))
96#define IS_LEAF(n) (n->parent & T_LEAF)
Robert Olsson19baf832005-06-21 12:43:18 -070097
David S. Millerb299e4f2011-02-02 20:48:10 -080098struct rt_trie_node {
Olof Johansson91b9a272005-08-09 20:24:39 -070099 unsigned long parent;
Eric Dumazet8d965442008-01-13 22:31:44 -0800100 t_key key;
Robert Olsson19baf832005-06-21 12:43:18 -0700101};
102
103struct leaf {
Olof Johansson91b9a272005-08-09 20:24:39 -0700104 unsigned long parent;
Eric Dumazet8d965442008-01-13 22:31:44 -0800105 t_key key;
Robert Olsson19baf832005-06-21 12:43:18 -0700106 struct hlist_head list;
Robert Olsson2373ce12005-08-25 13:01:29 -0700107 struct rcu_head rcu;
Robert Olsson19baf832005-06-21 12:43:18 -0700108};
109
110struct leaf_info {
111 struct hlist_node hlist;
Robert Olsson2373ce12005-08-25 13:01:29 -0700112 struct rcu_head rcu;
Robert Olsson19baf832005-06-21 12:43:18 -0700113 int plen;
114 struct list_head falh;
115};
116
117struct tnode {
Olof Johansson91b9a272005-08-09 20:24:39 -0700118 unsigned long parent;
Eric Dumazet8d965442008-01-13 22:31:44 -0800119 t_key key;
Eric Dumazet112d8cf2008-01-12 21:27:41 -0800120 unsigned char pos; /* 2log(KEYLENGTH) bits needed */
121 unsigned char bits; /* 2log(KEYLENGTH) bits needed */
Eric Dumazet8d965442008-01-13 22:31:44 -0800122 unsigned int full_children; /* KEYLENGTH bits needed */
123 unsigned int empty_children; /* KEYLENGTH bits needed */
Stephen Hemminger15be75c2008-04-10 02:56:38 -0700124 union {
125 struct rcu_head rcu;
126 struct work_struct work;
Jarek Poplawskie0f7cb82009-06-15 02:31:29 -0700127 struct tnode *tnode_free;
Stephen Hemminger15be75c2008-04-10 02:56:38 -0700128 };
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700129 struct rt_trie_node __rcu *child[0];
Robert Olsson19baf832005-06-21 12:43:18 -0700130};
131
132#ifdef CONFIG_IP_FIB_TRIE_STATS
133struct trie_use_stats {
134 unsigned int gets;
135 unsigned int backtrack;
136 unsigned int semantic_match_passed;
137 unsigned int semantic_match_miss;
138 unsigned int null_node_hit;
Robert Olsson2f368952005-07-05 15:02:40 -0700139 unsigned int resize_node_skipped;
Robert Olsson19baf832005-06-21 12:43:18 -0700140};
141#endif
142
143struct trie_stat {
144 unsigned int totdepth;
145 unsigned int maxdepth;
146 unsigned int tnodes;
147 unsigned int leaves;
148 unsigned int nullpointers;
Stephen Hemminger93672292008-01-22 21:54:05 -0800149 unsigned int prefixes;
Robert Olsson06ef9212006-03-20 21:35:01 -0800150 unsigned int nodesizes[MAX_STAT_DEPTH];
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700151};
Robert Olsson19baf832005-06-21 12:43:18 -0700152
153struct trie {
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700154 struct rt_trie_node __rcu *trie;
Robert Olsson19baf832005-06-21 12:43:18 -0700155#ifdef CONFIG_IP_FIB_TRIE_STATS
156 struct trie_use_stats stats;
157#endif
Robert Olsson19baf832005-06-21 12:43:18 -0700158};
159
David S. Millerb299e4f2011-02-02 20:48:10 -0800160static void put_child(struct trie *t, struct tnode *tn, int i, struct rt_trie_node *n);
161static void tnode_put_child_reorg(struct tnode *tn, int i, struct rt_trie_node *n,
Stephen Hemmingera07f5f52008-01-22 21:53:36 -0800162 int wasfull);
David S. Millerb299e4f2011-02-02 20:48:10 -0800163static struct rt_trie_node *resize(struct trie *t, struct tnode *tn);
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700164static struct tnode *inflate(struct trie *t, struct tnode *tn);
165static struct tnode *halve(struct trie *t, struct tnode *tn);
Jarek Poplawskie0f7cb82009-06-15 02:31:29 -0700166/* tnodes to free after resize(); protected by RTNL */
167static struct tnode *tnode_free_head;
Jarek Poplawskic3059472009-07-14 08:33:08 +0000168static size_t tnode_free_size;
169
170/*
171 * synchronize_rcu after call_rcu for that many pages; it should be especially
172 * useful before resizing the root node with PREEMPT_NONE configs; the value was
173 * obtained experimentally, aiming to avoid visible slowdown.
174 */
175static const int sync_pages = 128;
Robert Olsson19baf832005-06-21 12:43:18 -0700176
Christoph Lametere18b8902006-12-06 20:33:20 -0800177static struct kmem_cache *fn_alias_kmem __read_mostly;
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -0800178static struct kmem_cache *trie_leaf_kmem __read_mostly;
Robert Olsson19baf832005-06-21 12:43:18 -0700179
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700180/*
181 * caller must hold RTNL
182 */
183static inline struct tnode *node_parent(const struct rt_trie_node *node)
Stephen Hemminger06801912007-08-10 15:22:13 -0700184{
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700185 unsigned long parent;
186
187 parent = rcu_dereference_index_check(node->parent, lockdep_rtnl_is_held());
188
189 return (struct tnode *)(parent & ~NODE_TYPE_MASK);
Eric Dumazetb59cfbf2008-01-18 03:31:36 -0800190}
Stephen Hemminger06801912007-08-10 15:22:13 -0700191
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700192/*
193 * caller must hold RCU read lock or RTNL
194 */
195static inline struct tnode *node_parent_rcu(const struct rt_trie_node *node)
Eric Dumazetb59cfbf2008-01-18 03:31:36 -0800196{
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700197 unsigned long parent;
Eric Dumazetb59cfbf2008-01-18 03:31:36 -0800198
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700199 parent = rcu_dereference_index_check(node->parent, rcu_read_lock_held() ||
200 lockdep_rtnl_is_held());
201
202 return (struct tnode *)(parent & ~NODE_TYPE_MASK);
Stephen Hemminger06801912007-08-10 15:22:13 -0700203}
204
Stephen Hemminger6440cc92008-03-22 17:59:58 -0700205/* Same as rcu_assign_pointer
206 * but that macro() assumes that value is a pointer.
207 */
David S. Millerb299e4f2011-02-02 20:48:10 -0800208static inline void node_set_parent(struct rt_trie_node *node, struct tnode *ptr)
Stephen Hemminger06801912007-08-10 15:22:13 -0700209{
Stephen Hemminger6440cc92008-03-22 17:59:58 -0700210 smp_wmb();
211 node->parent = (unsigned long)ptr | NODE_TYPE(node);
Stephen Hemminger06801912007-08-10 15:22:13 -0700212}
Robert Olsson2373ce12005-08-25 13:01:29 -0700213
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700214/*
215 * caller must hold RTNL
216 */
217static inline struct rt_trie_node *tnode_get_child(const struct tnode *tn, unsigned int i)
Robert Olsson19baf832005-06-21 12:43:18 -0700218{
Eric Dumazetb59cfbf2008-01-18 03:31:36 -0800219 BUG_ON(i >= 1U << tn->bits);
Robert Olsson19baf832005-06-21 12:43:18 -0700220
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700221 return rtnl_dereference(tn->child[i]);
Eric Dumazetb59cfbf2008-01-18 03:31:36 -0800222}
223
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700224/*
225 * caller must hold RCU read lock or RTNL
226 */
227static inline struct rt_trie_node *tnode_get_child_rcu(const struct tnode *tn, unsigned int i)
Eric Dumazetb59cfbf2008-01-18 03:31:36 -0800228{
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700229 BUG_ON(i >= 1U << tn->bits);
Eric Dumazetb59cfbf2008-01-18 03:31:36 -0800230
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700231 return rcu_dereference_rtnl(tn->child[i]);
Robert Olsson19baf832005-06-21 12:43:18 -0700232}
233
Stephen Hemmignerbb435b82005-08-09 20:25:39 -0700234static inline int tnode_child_length(const struct tnode *tn)
Robert Olsson19baf832005-06-21 12:43:18 -0700235{
Olof Johansson91b9a272005-08-09 20:24:39 -0700236 return 1 << tn->bits;
Robert Olsson19baf832005-06-21 12:43:18 -0700237}
238
David S. Miller3b004562011-02-16 14:56:22 -0800239static inline t_key mask_pfx(t_key k, unsigned int l)
Stephen Hemmingerab66b4a2007-08-10 15:22:58 -0700240{
241 return (l == 0) ? 0 : k >> (KEYLENGTH-l) << (KEYLENGTH-l);
242}
243
David S. Miller3b004562011-02-16 14:56:22 -0800244static inline t_key tkey_extract_bits(t_key a, unsigned int offset, unsigned int bits)
Robert Olsson19baf832005-06-21 12:43:18 -0700245{
Olof Johansson91b9a272005-08-09 20:24:39 -0700246 if (offset < KEYLENGTH)
Robert Olsson19baf832005-06-21 12:43:18 -0700247 return ((t_key)(a << offset)) >> (KEYLENGTH - bits);
Olof Johansson91b9a272005-08-09 20:24:39 -0700248 else
Robert Olsson19baf832005-06-21 12:43:18 -0700249 return 0;
250}
251
252static inline int tkey_equals(t_key a, t_key b)
253{
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700254 return a == b;
Robert Olsson19baf832005-06-21 12:43:18 -0700255}
256
257static inline int tkey_sub_equals(t_key a, int offset, int bits, t_key b)
258{
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700259 if (bits == 0 || offset >= KEYLENGTH)
260 return 1;
Olof Johansson91b9a272005-08-09 20:24:39 -0700261 bits = bits > KEYLENGTH ? KEYLENGTH : bits;
262 return ((a ^ b) << offset) >> (KEYLENGTH - bits) == 0;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700263}
Robert Olsson19baf832005-06-21 12:43:18 -0700264
265static inline int tkey_mismatch(t_key a, int offset, t_key b)
266{
267 t_key diff = a ^ b;
268 int i = offset;
269
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700270 if (!diff)
271 return 0;
272 while ((diff << i) >> (KEYLENGTH-1) == 0)
Robert Olsson19baf832005-06-21 12:43:18 -0700273 i++;
274 return i;
275}
276
Robert Olsson19baf832005-06-21 12:43:18 -0700277/*
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900278 To understand this stuff, an understanding of keys and all their bits is
279 necessary. Every node in the trie has a key associated with it, but not
Robert Olsson19baf832005-06-21 12:43:18 -0700280 all of the bits in that key are significant.
281
282 Consider a node 'n' and its parent 'tp'.
283
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900284 If n is a leaf, every bit in its key is significant. Its presence is
285 necessitated by path compression, since during a tree traversal (when
286 searching for a leaf - unless we are doing an insertion) we will completely
287 ignore all skipped bits we encounter. Thus we need to verify, at the end of
288 a potentially successful search, that we have indeed been walking the
Robert Olsson19baf832005-06-21 12:43:18 -0700289 correct key path.
290
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900291 Note that we can never "miss" the correct key in the tree if present by
292 following the wrong path. Path compression ensures that segments of the key
293 that are the same for all keys with a given prefix are skipped, but the
294 skipped part *is* identical for each node in the subtrie below the skipped
295 bit! trie_insert() in this implementation takes care of that - note the
Robert Olsson19baf832005-06-21 12:43:18 -0700296 call to tkey_sub_equals() in trie_insert().
297
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900298 if n is an internal node - a 'tnode' here, the various parts of its key
Robert Olsson19baf832005-06-21 12:43:18 -0700299 have many different meanings.
300
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900301 Example:
Robert Olsson19baf832005-06-21 12:43:18 -0700302 _________________________________________________________________
303 | i | i | i | i | i | i | i | N | N | N | S | S | S | S | S | C |
304 -----------------------------------------------------------------
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900305 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Robert Olsson19baf832005-06-21 12:43:18 -0700306
307 _________________________________________________________________
308 | C | C | C | u | u | u | u | u | u | u | u | u | u | u | u | u |
309 -----------------------------------------------------------------
310 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
311
312 tp->pos = 7
313 tp->bits = 3
314 n->pos = 15
Olof Johansson91b9a272005-08-09 20:24:39 -0700315 n->bits = 4
Robert Olsson19baf832005-06-21 12:43:18 -0700316
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900317 First, let's just ignore the bits that come before the parent tp, that is
318 the bits from 0 to (tp->pos-1). They are *known* but at this point we do
Robert Olsson19baf832005-06-21 12:43:18 -0700319 not use them for anything.
320
321 The bits from (tp->pos) to (tp->pos + tp->bits - 1) - "N", above - are the
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900322 index into the parent's child array. That is, they will be used to find
Robert Olsson19baf832005-06-21 12:43:18 -0700323 'n' among tp's children.
324
325 The bits from (tp->pos + tp->bits) to (n->pos - 1) - "S" - are skipped bits
326 for the node n.
327
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900328 All the bits we have seen so far are significant to the node n. The rest
Robert Olsson19baf832005-06-21 12:43:18 -0700329 of the bits are really not needed or indeed known in n->key.
330
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900331 The bits from (n->pos) to (n->pos + n->bits - 1) - "C" - are the index into
Robert Olsson19baf832005-06-21 12:43:18 -0700332 n's child array, and will of course be different for each child.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900333
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700334
Robert Olsson19baf832005-06-21 12:43:18 -0700335 The rest of the bits, from (n->pos + n->bits) onward, are completely unknown
336 at this point.
337
338*/
339
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700340static inline void check_tnode(const struct tnode *tn)
Robert Olsson19baf832005-06-21 12:43:18 -0700341{
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700342 WARN_ON(tn && tn->pos+tn->bits > 32);
Robert Olsson19baf832005-06-21 12:43:18 -0700343}
344
Denis V. Lunevf5026fa2007-12-13 09:47:57 -0800345static const int halve_threshold = 25;
346static const int inflate_threshold = 50;
Jarek Poplawski345aa032009-07-07 19:39:16 -0700347static const int halve_threshold_root = 15;
Jens Låås80b71b82009-08-28 23:57:15 -0700348static const int inflate_threshold_root = 30;
Robert Olsson2373ce12005-08-25 13:01:29 -0700349
350static void __alias_free_mem(struct rcu_head *head)
351{
352 struct fib_alias *fa = container_of(head, struct fib_alias, rcu);
353 kmem_cache_free(fn_alias_kmem, fa);
354}
355
356static inline void alias_free_mem_rcu(struct fib_alias *fa)
357{
358 call_rcu(&fa->rcu, __alias_free_mem);
359}
360
361static void __leaf_free_rcu(struct rcu_head *head)
362{
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -0800363 struct leaf *l = container_of(head, struct leaf, rcu);
364 kmem_cache_free(trie_leaf_kmem, l);
Robert Olsson2373ce12005-08-25 13:01:29 -0700365}
366
Stephen Hemminger387a5482008-04-10 03:47:34 -0700367static inline void free_leaf(struct leaf *l)
368{
369 call_rcu_bh(&l->rcu, __leaf_free_rcu);
370}
371
Robert Olsson2373ce12005-08-25 13:01:29 -0700372static void __leaf_info_free_rcu(struct rcu_head *head)
373{
374 kfree(container_of(head, struct leaf_info, rcu));
375}
376
377static inline void free_leaf_info(struct leaf_info *leaf)
378{
379 call_rcu(&leaf->rcu, __leaf_info_free_rcu);
380}
381
Eric Dumazet8d965442008-01-13 22:31:44 -0800382static struct tnode *tnode_alloc(size_t size)
Robert Olsson2373ce12005-08-25 13:01:29 -0700383{
Robert Olsson2373ce12005-08-25 13:01:29 -0700384 if (size <= PAGE_SIZE)
Eric Dumazet8d965442008-01-13 22:31:44 -0800385 return kzalloc(size, GFP_KERNEL);
Stephen Hemminger15be75c2008-04-10 02:56:38 -0700386 else
Eric Dumazet7a1c8e52010-11-20 07:46:35 +0000387 return vzalloc(size);
Stephen Hemminger15be75c2008-04-10 02:56:38 -0700388}
Robert Olsson2373ce12005-08-25 13:01:29 -0700389
Stephen Hemminger15be75c2008-04-10 02:56:38 -0700390static void __tnode_vfree(struct work_struct *arg)
391{
392 struct tnode *tn = container_of(arg, struct tnode, work);
393 vfree(tn);
Robert Olsson2373ce12005-08-25 13:01:29 -0700394}
395
396static void __tnode_free_rcu(struct rcu_head *head)
397{
398 struct tnode *tn = container_of(head, struct tnode, rcu);
Eric Dumazet8d965442008-01-13 22:31:44 -0800399 size_t size = sizeof(struct tnode) +
David S. Millerb299e4f2011-02-02 20:48:10 -0800400 (sizeof(struct rt_trie_node *) << tn->bits);
Robert Olsson2373ce12005-08-25 13:01:29 -0700401
402 if (size <= PAGE_SIZE)
403 kfree(tn);
Stephen Hemminger15be75c2008-04-10 02:56:38 -0700404 else {
405 INIT_WORK(&tn->work, __tnode_vfree);
406 schedule_work(&tn->work);
407 }
Robert Olsson2373ce12005-08-25 13:01:29 -0700408}
409
410static inline void tnode_free(struct tnode *tn)
411{
Stephen Hemminger387a5482008-04-10 03:47:34 -0700412 if (IS_LEAF(tn))
413 free_leaf((struct leaf *) tn);
414 else
Robert Olsson550e29b2006-04-04 12:53:35 -0700415 call_rcu(&tn->rcu, __tnode_free_rcu);
Robert Olsson2373ce12005-08-25 13:01:29 -0700416}
417
Jarek Poplawskie0f7cb82009-06-15 02:31:29 -0700418static void tnode_free_safe(struct tnode *tn)
419{
420 BUG_ON(IS_LEAF(tn));
Jarek Poplawski7b855762009-06-18 00:28:51 -0700421 tn->tnode_free = tnode_free_head;
422 tnode_free_head = tn;
Jarek Poplawskic3059472009-07-14 08:33:08 +0000423 tnode_free_size += sizeof(struct tnode) +
David S. Millerb299e4f2011-02-02 20:48:10 -0800424 (sizeof(struct rt_trie_node *) << tn->bits);
Jarek Poplawskie0f7cb82009-06-15 02:31:29 -0700425}
426
427static void tnode_free_flush(void)
428{
429 struct tnode *tn;
430
431 while ((tn = tnode_free_head)) {
432 tnode_free_head = tn->tnode_free;
433 tn->tnode_free = NULL;
434 tnode_free(tn);
435 }
Jarek Poplawskic3059472009-07-14 08:33:08 +0000436
437 if (tnode_free_size >= PAGE_SIZE * sync_pages) {
438 tnode_free_size = 0;
439 synchronize_rcu();
440 }
Jarek Poplawskie0f7cb82009-06-15 02:31:29 -0700441}
442
Robert Olsson19baf832005-06-21 12:43:18 -0700443static struct leaf *leaf_new(void)
444{
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -0800445 struct leaf *l = kmem_cache_alloc(trie_leaf_kmem, GFP_KERNEL);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700446 if (l) {
Robert Olsson2373ce12005-08-25 13:01:29 -0700447 l->parent = T_LEAF;
Robert Olsson19baf832005-06-21 12:43:18 -0700448 INIT_HLIST_HEAD(&l->list);
449 }
450 return l;
451}
452
453static struct leaf_info *leaf_info_new(int plen)
454{
455 struct leaf_info *li = kmalloc(sizeof(struct leaf_info), GFP_KERNEL);
Robert Olsson2373ce12005-08-25 13:01:29 -0700456 if (li) {
457 li->plen = plen;
458 INIT_LIST_HEAD(&li->falh);
Patrick McHardyf0e36f82005-07-05 14:44:55 -0700459 }
Robert Olsson2373ce12005-08-25 13:01:29 -0700460 return li;
Patrick McHardyf0e36f82005-07-05 14:44:55 -0700461}
462
Stephen Hemmingera07f5f52008-01-22 21:53:36 -0800463static struct tnode *tnode_new(t_key key, int pos, int bits)
Robert Olsson19baf832005-06-21 12:43:18 -0700464{
David S. Millerb299e4f2011-02-02 20:48:10 -0800465 size_t sz = sizeof(struct tnode) + (sizeof(struct rt_trie_node *) << bits);
Patrick McHardyf0e36f82005-07-05 14:44:55 -0700466 struct tnode *tn = tnode_alloc(sz);
Robert Olsson19baf832005-06-21 12:43:18 -0700467
Olof Johansson91b9a272005-08-09 20:24:39 -0700468 if (tn) {
Robert Olsson2373ce12005-08-25 13:01:29 -0700469 tn->parent = T_TNODE;
Robert Olsson19baf832005-06-21 12:43:18 -0700470 tn->pos = pos;
471 tn->bits = bits;
472 tn->key = key;
473 tn->full_children = 0;
474 tn->empty_children = 1<<bits;
475 }
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700476
Eric Dumazeta034ee32010-09-09 23:32:28 +0000477 pr_debug("AT %p s=%zu %zu\n", tn, sizeof(struct tnode),
David S. Millerb299e4f2011-02-02 20:48:10 -0800478 sizeof(struct rt_trie_node) << bits);
Robert Olsson19baf832005-06-21 12:43:18 -0700479 return tn;
480}
481
Robert Olsson19baf832005-06-21 12:43:18 -0700482/*
483 * Check whether a tnode 'n' is "full", i.e. it is an internal node
484 * and no bits are skipped. See discussion in dyntree paper p. 6
485 */
486
David S. Millerb299e4f2011-02-02 20:48:10 -0800487static inline int tnode_full(const struct tnode *tn, const struct rt_trie_node *n)
Robert Olsson19baf832005-06-21 12:43:18 -0700488{
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700489 if (n == NULL || IS_LEAF(n))
Robert Olsson19baf832005-06-21 12:43:18 -0700490 return 0;
491
492 return ((struct tnode *) n)->pos == tn->pos + tn->bits;
493}
494
Stephen Hemmingera07f5f52008-01-22 21:53:36 -0800495static inline void put_child(struct trie *t, struct tnode *tn, int i,
David S. Millerb299e4f2011-02-02 20:48:10 -0800496 struct rt_trie_node *n)
Robert Olsson19baf832005-06-21 12:43:18 -0700497{
498 tnode_put_child_reorg(tn, i, n, -1);
499}
500
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700501 /*
Robert Olsson19baf832005-06-21 12:43:18 -0700502 * Add a child at position i overwriting the old value.
503 * Update the value of full_children and empty_children.
504 */
505
David S. Millerb299e4f2011-02-02 20:48:10 -0800506static void tnode_put_child_reorg(struct tnode *tn, int i, struct rt_trie_node *n,
Stephen Hemmingera07f5f52008-01-22 21:53:36 -0800507 int wasfull)
Robert Olsson19baf832005-06-21 12:43:18 -0700508{
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700509 struct rt_trie_node *chi = rtnl_dereference(tn->child[i]);
Robert Olsson19baf832005-06-21 12:43:18 -0700510 int isfull;
511
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700512 BUG_ON(i >= 1<<tn->bits);
513
Robert Olsson19baf832005-06-21 12:43:18 -0700514 /* update emptyChildren */
515 if (n == NULL && chi != NULL)
516 tn->empty_children++;
517 else if (n != NULL && chi == NULL)
518 tn->empty_children--;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700519
Robert Olsson19baf832005-06-21 12:43:18 -0700520 /* update fullChildren */
Olof Johansson91b9a272005-08-09 20:24:39 -0700521 if (wasfull == -1)
Robert Olsson19baf832005-06-21 12:43:18 -0700522 wasfull = tnode_full(tn, chi);
523
524 isfull = tnode_full(tn, n);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700525 if (wasfull && !isfull)
Robert Olsson19baf832005-06-21 12:43:18 -0700526 tn->full_children--;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700527 else if (!wasfull && isfull)
Robert Olsson19baf832005-06-21 12:43:18 -0700528 tn->full_children++;
Olof Johansson91b9a272005-08-09 20:24:39 -0700529
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700530 if (n)
Stephen Hemminger06801912007-08-10 15:22:13 -0700531 node_set_parent(n, tn);
Robert Olsson19baf832005-06-21 12:43:18 -0700532
Robert Olsson2373ce12005-08-25 13:01:29 -0700533 rcu_assign_pointer(tn->child[i], n);
Robert Olsson19baf832005-06-21 12:43:18 -0700534}
535
Jens Låås80b71b82009-08-28 23:57:15 -0700536#define MAX_WORK 10
David S. Millerb299e4f2011-02-02 20:48:10 -0800537static struct rt_trie_node *resize(struct trie *t, struct tnode *tn)
Robert Olsson19baf832005-06-21 12:43:18 -0700538{
539 int i;
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700540 struct tnode *old_tn;
Robert Olssone6308be2005-10-04 13:01:58 -0700541 int inflate_threshold_use;
542 int halve_threshold_use;
Jens Låås80b71b82009-08-28 23:57:15 -0700543 int max_work;
Robert Olsson19baf832005-06-21 12:43:18 -0700544
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900545 if (!tn)
Robert Olsson19baf832005-06-21 12:43:18 -0700546 return NULL;
547
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700548 pr_debug("In tnode_resize %p inflate_threshold=%d threshold=%d\n",
549 tn, inflate_threshold, halve_threshold);
Robert Olsson19baf832005-06-21 12:43:18 -0700550
551 /* No children */
552 if (tn->empty_children == tnode_child_length(tn)) {
Jarek Poplawskie0f7cb82009-06-15 02:31:29 -0700553 tnode_free_safe(tn);
Robert Olsson19baf832005-06-21 12:43:18 -0700554 return NULL;
555 }
556 /* One child */
557 if (tn->empty_children == tnode_child_length(tn) - 1)
Jens Låås80b71b82009-08-28 23:57:15 -0700558 goto one_child;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700559 /*
Robert Olsson19baf832005-06-21 12:43:18 -0700560 * Double as long as the resulting node has a number of
561 * nonempty nodes that are above the threshold.
562 */
563
564 /*
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700565 * From "Implementing a dynamic compressed trie" by Stefan Nilsson of
566 * the Helsinki University of Technology and Matti Tikkanen of Nokia
Robert Olsson19baf832005-06-21 12:43:18 -0700567 * Telecommunications, page 6:
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700568 * "A node is doubled if the ratio of non-empty children to all
Robert Olsson19baf832005-06-21 12:43:18 -0700569 * children in the *doubled* node is at least 'high'."
570 *
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700571 * 'high' in this instance is the variable 'inflate_threshold'. It
572 * is expressed as a percentage, so we multiply it with
573 * tnode_child_length() and instead of multiplying by 2 (since the
574 * child array will be doubled by inflate()) and multiplying
575 * the left-hand side by 100 (to handle the percentage thing) we
Robert Olsson19baf832005-06-21 12:43:18 -0700576 * multiply the left-hand side by 50.
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700577 *
578 * The left-hand side may look a bit weird: tnode_child_length(tn)
579 * - tn->empty_children is of course the number of non-null children
580 * in the current node. tn->full_children is the number of "full"
Robert Olsson19baf832005-06-21 12:43:18 -0700581 * children, that is non-null tnodes with a skip value of 0.
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700582 * All of those will be doubled in the resulting inflated tnode, so
Robert Olsson19baf832005-06-21 12:43:18 -0700583 * we just count them one extra time here.
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700584 *
Robert Olsson19baf832005-06-21 12:43:18 -0700585 * A clearer way to write this would be:
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700586 *
Robert Olsson19baf832005-06-21 12:43:18 -0700587 * to_be_doubled = tn->full_children;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700588 * not_to_be_doubled = tnode_child_length(tn) - tn->empty_children -
Robert Olsson19baf832005-06-21 12:43:18 -0700589 * tn->full_children;
590 *
591 * new_child_length = tnode_child_length(tn) * 2;
592 *
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700593 * new_fill_factor = 100 * (not_to_be_doubled + 2*to_be_doubled) /
Robert Olsson19baf832005-06-21 12:43:18 -0700594 * new_child_length;
595 * if (new_fill_factor >= inflate_threshold)
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700596 *
597 * ...and so on, tho it would mess up the while () loop.
598 *
Robert Olsson19baf832005-06-21 12:43:18 -0700599 * anyway,
600 * 100 * (not_to_be_doubled + 2*to_be_doubled) / new_child_length >=
601 * inflate_threshold
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700602 *
Robert Olsson19baf832005-06-21 12:43:18 -0700603 * avoid a division:
604 * 100 * (not_to_be_doubled + 2*to_be_doubled) >=
605 * inflate_threshold * new_child_length
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700606 *
Robert Olsson19baf832005-06-21 12:43:18 -0700607 * expand not_to_be_doubled and to_be_doubled, and shorten:
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700608 * 100 * (tnode_child_length(tn) - tn->empty_children +
Olof Johansson91b9a272005-08-09 20:24:39 -0700609 * tn->full_children) >= inflate_threshold * new_child_length
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700610 *
Robert Olsson19baf832005-06-21 12:43:18 -0700611 * expand new_child_length:
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700612 * 100 * (tnode_child_length(tn) - tn->empty_children +
Olof Johansson91b9a272005-08-09 20:24:39 -0700613 * tn->full_children) >=
Robert Olsson19baf832005-06-21 12:43:18 -0700614 * inflate_threshold * tnode_child_length(tn) * 2
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700615 *
Robert Olsson19baf832005-06-21 12:43:18 -0700616 * shorten again:
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700617 * 50 * (tn->full_children + tnode_child_length(tn) -
Olof Johansson91b9a272005-08-09 20:24:39 -0700618 * tn->empty_children) >= inflate_threshold *
Robert Olsson19baf832005-06-21 12:43:18 -0700619 * tnode_child_length(tn)
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700620 *
Robert Olsson19baf832005-06-21 12:43:18 -0700621 */
622
623 check_tnode(tn);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700624
Robert Olssone6308be2005-10-04 13:01:58 -0700625 /* Keep root node larger */
626
David S. Millerb299e4f2011-02-02 20:48:10 -0800627 if (!node_parent((struct rt_trie_node *)tn)) {
Jens Låås80b71b82009-08-28 23:57:15 -0700628 inflate_threshold_use = inflate_threshold_root;
629 halve_threshold_use = halve_threshold_root;
Eric Dumazeta034ee32010-09-09 23:32:28 +0000630 } else {
Robert Olssone6308be2005-10-04 13:01:58 -0700631 inflate_threshold_use = inflate_threshold;
Jens Låås80b71b82009-08-28 23:57:15 -0700632 halve_threshold_use = halve_threshold;
633 }
Robert Olssone6308be2005-10-04 13:01:58 -0700634
Jens Låås80b71b82009-08-28 23:57:15 -0700635 max_work = MAX_WORK;
636 while ((tn->full_children > 0 && max_work-- &&
Stephen Hemmingera07f5f52008-01-22 21:53:36 -0800637 50 * (tn->full_children + tnode_child_length(tn)
638 - tn->empty_children)
639 >= inflate_threshold_use * tnode_child_length(tn))) {
Robert Olsson19baf832005-06-21 12:43:18 -0700640
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700641 old_tn = tn;
642 tn = inflate(t, tn);
Stephen Hemmingera07f5f52008-01-22 21:53:36 -0800643
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700644 if (IS_ERR(tn)) {
645 tn = old_tn;
Robert Olsson2f368952005-07-05 15:02:40 -0700646#ifdef CONFIG_IP_FIB_TRIE_STATS
647 t->stats.resize_node_skipped++;
648#endif
649 break;
650 }
Robert Olsson19baf832005-06-21 12:43:18 -0700651 }
652
653 check_tnode(tn);
654
Jens Låås80b71b82009-08-28 23:57:15 -0700655 /* Return if at least one inflate is run */
Eric Dumazeta034ee32010-09-09 23:32:28 +0000656 if (max_work != MAX_WORK)
David S. Millerb299e4f2011-02-02 20:48:10 -0800657 return (struct rt_trie_node *) tn;
Jens Låås80b71b82009-08-28 23:57:15 -0700658
Robert Olsson19baf832005-06-21 12:43:18 -0700659 /*
660 * Halve as long as the number of empty children in this
661 * node is above threshold.
662 */
Robert Olsson2f368952005-07-05 15:02:40 -0700663
Jens Låås80b71b82009-08-28 23:57:15 -0700664 max_work = MAX_WORK;
665 while (tn->bits > 1 && max_work-- &&
Robert Olsson19baf832005-06-21 12:43:18 -0700666 100 * (tnode_child_length(tn) - tn->empty_children) <
Robert Olssone6308be2005-10-04 13:01:58 -0700667 halve_threshold_use * tnode_child_length(tn)) {
Robert Olsson19baf832005-06-21 12:43:18 -0700668
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700669 old_tn = tn;
670 tn = halve(t, tn);
671 if (IS_ERR(tn)) {
672 tn = old_tn;
Robert Olsson2f368952005-07-05 15:02:40 -0700673#ifdef CONFIG_IP_FIB_TRIE_STATS
674 t->stats.resize_node_skipped++;
675#endif
676 break;
677 }
678 }
679
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700680
Robert Olsson19baf832005-06-21 12:43:18 -0700681 /* Only one child remains */
Jens Låås80b71b82009-08-28 23:57:15 -0700682 if (tn->empty_children == tnode_child_length(tn) - 1) {
683one_child:
Robert Olsson19baf832005-06-21 12:43:18 -0700684 for (i = 0; i < tnode_child_length(tn); i++) {
David S. Millerb299e4f2011-02-02 20:48:10 -0800685 struct rt_trie_node *n;
Olof Johansson91b9a272005-08-09 20:24:39 -0700686
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700687 n = rtnl_dereference(tn->child[i]);
Robert Olsson2373ce12005-08-25 13:01:29 -0700688 if (!n)
Olof Johansson91b9a272005-08-09 20:24:39 -0700689 continue;
Olof Johansson91b9a272005-08-09 20:24:39 -0700690
691 /* compress one level */
692
Stephen Hemminger06801912007-08-10 15:22:13 -0700693 node_set_parent(n, NULL);
Jarek Poplawskie0f7cb82009-06-15 02:31:29 -0700694 tnode_free_safe(tn);
Olof Johansson91b9a272005-08-09 20:24:39 -0700695 return n;
Robert Olsson19baf832005-06-21 12:43:18 -0700696 }
Jens Låås80b71b82009-08-28 23:57:15 -0700697 }
David S. Millerb299e4f2011-02-02 20:48:10 -0800698 return (struct rt_trie_node *) tn;
Robert Olsson19baf832005-06-21 12:43:18 -0700699}
700
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700701
702static void tnode_clean_free(struct tnode *tn)
703{
704 int i;
705 struct tnode *tofree;
706
707 for (i = 0; i < tnode_child_length(tn); i++) {
708 tofree = (struct tnode *)rtnl_dereference(tn->child[i]);
709 if (tofree)
710 tnode_free(tofree);
711 }
712 tnode_free(tn);
713}
714
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700715static struct tnode *inflate(struct trie *t, struct tnode *tn)
Robert Olsson19baf832005-06-21 12:43:18 -0700716{
Robert Olsson19baf832005-06-21 12:43:18 -0700717 struct tnode *oldtnode = tn;
718 int olen = tnode_child_length(tn);
719 int i;
720
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700721 pr_debug("In inflate\n");
Robert Olsson19baf832005-06-21 12:43:18 -0700722
723 tn = tnode_new(oldtnode->key, oldtnode->pos, oldtnode->bits + 1);
724
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700725 if (!tn)
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700726 return ERR_PTR(-ENOMEM);
Robert Olsson2f368952005-07-05 15:02:40 -0700727
728 /*
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700729 * Preallocate and store tnodes before the actual work so we
730 * don't get into an inconsistent state if memory allocation
731 * fails. In case of failure we return the oldnode and inflate
Robert Olsson2f368952005-07-05 15:02:40 -0700732 * of tnode is ignored.
733 */
Olof Johansson91b9a272005-08-09 20:24:39 -0700734
735 for (i = 0; i < olen; i++) {
Stephen Hemmingera07f5f52008-01-22 21:53:36 -0800736 struct tnode *inode;
Robert Olsson2f368952005-07-05 15:02:40 -0700737
Stephen Hemmingera07f5f52008-01-22 21:53:36 -0800738 inode = (struct tnode *) tnode_get_child(oldtnode, i);
Robert Olsson2f368952005-07-05 15:02:40 -0700739 if (inode &&
740 IS_TNODE(inode) &&
741 inode->pos == oldtnode->pos + oldtnode->bits &&
742 inode->bits > 1) {
743 struct tnode *left, *right;
Stephen Hemmingerab66b4a2007-08-10 15:22:58 -0700744 t_key m = ~0U << (KEYLENGTH - 1) >> inode->pos;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700745
Robert Olsson2f368952005-07-05 15:02:40 -0700746 left = tnode_new(inode->key&(~m), inode->pos + 1,
747 inode->bits - 1);
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700748 if (!left)
749 goto nomem;
Olof Johansson91b9a272005-08-09 20:24:39 -0700750
Robert Olsson2f368952005-07-05 15:02:40 -0700751 right = tnode_new(inode->key|m, inode->pos + 1,
752 inode->bits - 1);
753
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900754 if (!right) {
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700755 tnode_free(left);
756 goto nomem;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900757 }
Robert Olsson2f368952005-07-05 15:02:40 -0700758
David S. Millerb299e4f2011-02-02 20:48:10 -0800759 put_child(t, tn, 2*i, (struct rt_trie_node *) left);
760 put_child(t, tn, 2*i+1, (struct rt_trie_node *) right);
Robert Olsson2f368952005-07-05 15:02:40 -0700761 }
762 }
763
Olof Johansson91b9a272005-08-09 20:24:39 -0700764 for (i = 0; i < olen; i++) {
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -0800765 struct tnode *inode;
David S. Millerb299e4f2011-02-02 20:48:10 -0800766 struct rt_trie_node *node = tnode_get_child(oldtnode, i);
Olof Johansson91b9a272005-08-09 20:24:39 -0700767 struct tnode *left, *right;
768 int size, j;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700769
Robert Olsson19baf832005-06-21 12:43:18 -0700770 /* An empty child */
771 if (node == NULL)
772 continue;
773
774 /* A leaf or an internal node with skipped bits */
775
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700776 if (IS_LEAF(node) || ((struct tnode *) node)->pos >
Robert Olsson19baf832005-06-21 12:43:18 -0700777 tn->pos + tn->bits - 1) {
Stephen Hemmingera07f5f52008-01-22 21:53:36 -0800778 if (tkey_extract_bits(node->key,
779 oldtnode->pos + oldtnode->bits,
780 1) == 0)
Robert Olsson19baf832005-06-21 12:43:18 -0700781 put_child(t, tn, 2*i, node);
782 else
783 put_child(t, tn, 2*i+1, node);
784 continue;
785 }
786
787 /* An internal node with two children */
788 inode = (struct tnode *) node;
789
790 if (inode->bits == 1) {
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700791 put_child(t, tn, 2*i, rtnl_dereference(inode->child[0]));
792 put_child(t, tn, 2*i+1, rtnl_dereference(inode->child[1]));
Robert Olsson19baf832005-06-21 12:43:18 -0700793
Jarek Poplawskie0f7cb82009-06-15 02:31:29 -0700794 tnode_free_safe(inode);
Olof Johansson91b9a272005-08-09 20:24:39 -0700795 continue;
Robert Olsson19baf832005-06-21 12:43:18 -0700796 }
797
Olof Johansson91b9a272005-08-09 20:24:39 -0700798 /* An internal node with more than two children */
Robert Olsson19baf832005-06-21 12:43:18 -0700799
Olof Johansson91b9a272005-08-09 20:24:39 -0700800 /* We will replace this node 'inode' with two new
801 * ones, 'left' and 'right', each with half of the
802 * original children. The two new nodes will have
803 * a position one bit further down the key and this
804 * means that the "significant" part of their keys
805 * (see the discussion near the top of this file)
806 * will differ by one bit, which will be "0" in
807 * left's key and "1" in right's key. Since we are
808 * moving the key position by one step, the bit that
809 * we are moving away from - the bit at position
810 * (inode->pos) - is the one that will differ between
811 * left and right. So... we synthesize that bit in the
812 * two new keys.
813 * The mask 'm' below will be a single "one" bit at
814 * the position (inode->pos)
815 */
Robert Olsson19baf832005-06-21 12:43:18 -0700816
Olof Johansson91b9a272005-08-09 20:24:39 -0700817 /* Use the old key, but set the new significant
818 * bit to zero.
819 */
Robert Olsson19baf832005-06-21 12:43:18 -0700820
Olof Johansson91b9a272005-08-09 20:24:39 -0700821 left = (struct tnode *) tnode_get_child(tn, 2*i);
822 put_child(t, tn, 2*i, NULL);
Robert Olsson19baf832005-06-21 12:43:18 -0700823
Olof Johansson91b9a272005-08-09 20:24:39 -0700824 BUG_ON(!left);
Robert Olsson2f368952005-07-05 15:02:40 -0700825
Olof Johansson91b9a272005-08-09 20:24:39 -0700826 right = (struct tnode *) tnode_get_child(tn, 2*i+1);
827 put_child(t, tn, 2*i+1, NULL);
Robert Olsson2f368952005-07-05 15:02:40 -0700828
Olof Johansson91b9a272005-08-09 20:24:39 -0700829 BUG_ON(!right);
Robert Olsson2f368952005-07-05 15:02:40 -0700830
Olof Johansson91b9a272005-08-09 20:24:39 -0700831 size = tnode_child_length(left);
832 for (j = 0; j < size; j++) {
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700833 put_child(t, left, j, rtnl_dereference(inode->child[j]));
834 put_child(t, right, j, rtnl_dereference(inode->child[j + size]));
Robert Olsson19baf832005-06-21 12:43:18 -0700835 }
Olof Johansson91b9a272005-08-09 20:24:39 -0700836 put_child(t, tn, 2*i, resize(t, left));
837 put_child(t, tn, 2*i+1, resize(t, right));
838
Jarek Poplawskie0f7cb82009-06-15 02:31:29 -0700839 tnode_free_safe(inode);
Robert Olsson19baf832005-06-21 12:43:18 -0700840 }
Jarek Poplawskie0f7cb82009-06-15 02:31:29 -0700841 tnode_free_safe(oldtnode);
Robert Olsson19baf832005-06-21 12:43:18 -0700842 return tn;
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700843nomem:
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700844 tnode_clean_free(tn);
845 return ERR_PTR(-ENOMEM);
Robert Olsson19baf832005-06-21 12:43:18 -0700846}
847
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700848static struct tnode *halve(struct trie *t, struct tnode *tn)
Robert Olsson19baf832005-06-21 12:43:18 -0700849{
850 struct tnode *oldtnode = tn;
David S. Millerb299e4f2011-02-02 20:48:10 -0800851 struct rt_trie_node *left, *right;
Robert Olsson19baf832005-06-21 12:43:18 -0700852 int i;
853 int olen = tnode_child_length(tn);
854
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700855 pr_debug("In halve\n");
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700856
857 tn = tnode_new(oldtnode->key, oldtnode->pos, oldtnode->bits - 1);
Robert Olsson19baf832005-06-21 12:43:18 -0700858
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700859 if (!tn)
860 return ERR_PTR(-ENOMEM);
Robert Olsson2f368952005-07-05 15:02:40 -0700861
862 /*
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700863 * Preallocate and store tnodes before the actual work so we
864 * don't get into an inconsistent state if memory allocation
865 * fails. In case of failure we return the oldnode and halve
Robert Olsson2f368952005-07-05 15:02:40 -0700866 * of tnode is ignored.
867 */
868
Olof Johansson91b9a272005-08-09 20:24:39 -0700869 for (i = 0; i < olen; i += 2) {
Robert Olsson2f368952005-07-05 15:02:40 -0700870 left = tnode_get_child(oldtnode, i);
871 right = tnode_get_child(oldtnode, i+1);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700872
Robert Olsson2f368952005-07-05 15:02:40 -0700873 /* Two nonempty children */
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700874 if (left && right) {
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700875 struct tnode *newn;
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700876
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700877 newn = tnode_new(left->key, tn->pos + tn->bits, 1);
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700878
879 if (!newn)
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700880 goto nomem;
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700881
David S. Millerb299e4f2011-02-02 20:48:10 -0800882 put_child(t, tn, i/2, (struct rt_trie_node *)newn);
Robert Olsson2f368952005-07-05 15:02:40 -0700883 }
Robert Olsson2f368952005-07-05 15:02:40 -0700884
Robert Olsson2f368952005-07-05 15:02:40 -0700885 }
Robert Olsson19baf832005-06-21 12:43:18 -0700886
Olof Johansson91b9a272005-08-09 20:24:39 -0700887 for (i = 0; i < olen; i += 2) {
888 struct tnode *newBinNode;
889
Robert Olsson19baf832005-06-21 12:43:18 -0700890 left = tnode_get_child(oldtnode, i);
891 right = tnode_get_child(oldtnode, i+1);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700892
Robert Olsson19baf832005-06-21 12:43:18 -0700893 /* At least one of the children is empty */
894 if (left == NULL) {
895 if (right == NULL) /* Both are empty */
896 continue;
897 put_child(t, tn, i/2, right);
Olof Johansson91b9a272005-08-09 20:24:39 -0700898 continue;
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700899 }
Olof Johansson91b9a272005-08-09 20:24:39 -0700900
901 if (right == NULL) {
Robert Olsson19baf832005-06-21 12:43:18 -0700902 put_child(t, tn, i/2, left);
Olof Johansson91b9a272005-08-09 20:24:39 -0700903 continue;
904 }
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700905
Robert Olsson19baf832005-06-21 12:43:18 -0700906 /* Two nonempty children */
Olof Johansson91b9a272005-08-09 20:24:39 -0700907 newBinNode = (struct tnode *) tnode_get_child(tn, i/2);
908 put_child(t, tn, i/2, NULL);
Olof Johansson91b9a272005-08-09 20:24:39 -0700909 put_child(t, newBinNode, 0, left);
910 put_child(t, newBinNode, 1, right);
911 put_child(t, tn, i/2, resize(t, newBinNode));
Robert Olsson19baf832005-06-21 12:43:18 -0700912 }
Jarek Poplawskie0f7cb82009-06-15 02:31:29 -0700913 tnode_free_safe(oldtnode);
Robert Olsson19baf832005-06-21 12:43:18 -0700914 return tn;
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700915nomem:
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700916 tnode_clean_free(tn);
917 return ERR_PTR(-ENOMEM);
Robert Olsson19baf832005-06-21 12:43:18 -0700918}
919
Robert Olsson772cb712005-09-19 15:31:18 -0700920/* readside must use rcu_read_lock currently dump routines
Robert Olsson2373ce12005-08-25 13:01:29 -0700921 via get_fa_head and dump */
922
Robert Olsson772cb712005-09-19 15:31:18 -0700923static struct leaf_info *find_leaf_info(struct leaf *l, int plen)
Robert Olsson19baf832005-06-21 12:43:18 -0700924{
Robert Olsson772cb712005-09-19 15:31:18 -0700925 struct hlist_head *head = &l->list;
Robert Olsson19baf832005-06-21 12:43:18 -0700926 struct hlist_node *node;
927 struct leaf_info *li;
928
Robert Olsson2373ce12005-08-25 13:01:29 -0700929 hlist_for_each_entry_rcu(li, node, head, hlist)
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700930 if (li->plen == plen)
Robert Olsson19baf832005-06-21 12:43:18 -0700931 return li;
Olof Johansson91b9a272005-08-09 20:24:39 -0700932
Robert Olsson19baf832005-06-21 12:43:18 -0700933 return NULL;
934}
935
Stephen Hemmingera07f5f52008-01-22 21:53:36 -0800936static inline struct list_head *get_fa_head(struct leaf *l, int plen)
Robert Olsson19baf832005-06-21 12:43:18 -0700937{
Robert Olsson772cb712005-09-19 15:31:18 -0700938 struct leaf_info *li = find_leaf_info(l, plen);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700939
Olof Johansson91b9a272005-08-09 20:24:39 -0700940 if (!li)
941 return NULL;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700942
Olof Johansson91b9a272005-08-09 20:24:39 -0700943 return &li->falh;
Robert Olsson19baf832005-06-21 12:43:18 -0700944}
945
946static void insert_leaf_info(struct hlist_head *head, struct leaf_info *new)
947{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900948 struct leaf_info *li = NULL, *last = NULL;
949 struct hlist_node *node;
Robert Olsson19baf832005-06-21 12:43:18 -0700950
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900951 if (hlist_empty(head)) {
952 hlist_add_head_rcu(&new->hlist, head);
953 } else {
954 hlist_for_each_entry(li, node, head, hlist) {
955 if (new->plen > li->plen)
956 break;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700957
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900958 last = li;
959 }
960 if (last)
961 hlist_add_after_rcu(&last->hlist, &new->hlist);
962 else
963 hlist_add_before_rcu(&new->hlist, &li->hlist);
964 }
Robert Olsson19baf832005-06-21 12:43:18 -0700965}
966
Robert Olsson2373ce12005-08-25 13:01:29 -0700967/* rcu_read_lock needs to be hold by caller from readside */
968
Robert Olsson19baf832005-06-21 12:43:18 -0700969static struct leaf *
970fib_find_node(struct trie *t, u32 key)
971{
972 int pos;
973 struct tnode *tn;
David S. Millerb299e4f2011-02-02 20:48:10 -0800974 struct rt_trie_node *n;
Robert Olsson19baf832005-06-21 12:43:18 -0700975
976 pos = 0;
Eric Dumazeta034ee32010-09-09 23:32:28 +0000977 n = rcu_dereference_rtnl(t->trie);
Robert Olsson19baf832005-06-21 12:43:18 -0700978
979 while (n != NULL && NODE_TYPE(n) == T_TNODE) {
980 tn = (struct tnode *) n;
Olof Johansson91b9a272005-08-09 20:24:39 -0700981
Robert Olsson19baf832005-06-21 12:43:18 -0700982 check_tnode(tn);
Olof Johansson91b9a272005-08-09 20:24:39 -0700983
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700984 if (tkey_sub_equals(tn->key, pos, tn->pos-pos, key)) {
Olof Johansson91b9a272005-08-09 20:24:39 -0700985 pos = tn->pos + tn->bits;
Stephen Hemmingera07f5f52008-01-22 21:53:36 -0800986 n = tnode_get_child_rcu(tn,
987 tkey_extract_bits(key,
988 tn->pos,
989 tn->bits));
Olof Johansson91b9a272005-08-09 20:24:39 -0700990 } else
Robert Olsson19baf832005-06-21 12:43:18 -0700991 break;
992 }
993 /* Case we have found a leaf. Compare prefixes */
994
Olof Johansson91b9a272005-08-09 20:24:39 -0700995 if (n != NULL && IS_LEAF(n) && tkey_equals(key, n->key))
996 return (struct leaf *)n;
997
Robert Olsson19baf832005-06-21 12:43:18 -0700998 return NULL;
999}
1000
Jarek Poplawski7b855762009-06-18 00:28:51 -07001001static void trie_rebalance(struct trie *t, struct tnode *tn)
Robert Olsson19baf832005-06-21 12:43:18 -07001002{
Robert Olsson19baf832005-06-21 12:43:18 -07001003 int wasfull;
Robert Olsson3ed18d72009-05-21 15:20:59 -07001004 t_key cindex, key;
Stephen Hemminger06801912007-08-10 15:22:13 -07001005 struct tnode *tp;
Robert Olsson19baf832005-06-21 12:43:18 -07001006
Robert Olsson3ed18d72009-05-21 15:20:59 -07001007 key = tn->key;
1008
David S. Millerb299e4f2011-02-02 20:48:10 -08001009 while (tn != NULL && (tp = node_parent((struct rt_trie_node *)tn)) != NULL) {
Robert Olsson19baf832005-06-21 12:43:18 -07001010 cindex = tkey_extract_bits(key, tp->pos, tp->bits);
1011 wasfull = tnode_full(tp, tnode_get_child(tp, cindex));
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001012 tn = (struct tnode *) resize(t, (struct tnode *)tn);
1013
1014 tnode_put_child_reorg((struct tnode *)tp, cindex,
David S. Millerb299e4f2011-02-02 20:48:10 -08001015 (struct rt_trie_node *)tn, wasfull);
Olof Johansson91b9a272005-08-09 20:24:39 -07001016
David S. Millerb299e4f2011-02-02 20:48:10 -08001017 tp = node_parent((struct rt_trie_node *) tn);
Jarek Poplawski008440e2009-06-30 12:47:19 -07001018 if (!tp)
David S. Millerb299e4f2011-02-02 20:48:10 -08001019 rcu_assign_pointer(t->trie, (struct rt_trie_node *)tn);
Jarek Poplawski008440e2009-06-30 12:47:19 -07001020
Jarek Poplawskie0f7cb82009-06-15 02:31:29 -07001021 tnode_free_flush();
Stephen Hemminger06801912007-08-10 15:22:13 -07001022 if (!tp)
Robert Olsson19baf832005-06-21 12:43:18 -07001023 break;
Stephen Hemminger06801912007-08-10 15:22:13 -07001024 tn = tp;
Robert Olsson19baf832005-06-21 12:43:18 -07001025 }
Stephen Hemminger06801912007-08-10 15:22:13 -07001026
Robert Olsson19baf832005-06-21 12:43:18 -07001027 /* Handle last (top) tnode */
Jarek Poplawski7b855762009-06-18 00:28:51 -07001028 if (IS_TNODE(tn))
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001029 tn = (struct tnode *)resize(t, (struct tnode *)tn);
Robert Olsson19baf832005-06-21 12:43:18 -07001030
David S. Millerb299e4f2011-02-02 20:48:10 -08001031 rcu_assign_pointer(t->trie, (struct rt_trie_node *)tn);
Jarek Poplawski7b855762009-06-18 00:28:51 -07001032 tnode_free_flush();
Robert Olsson19baf832005-06-21 12:43:18 -07001033}
1034
Robert Olsson2373ce12005-08-25 13:01:29 -07001035/* only used from updater-side */
1036
Stephen Hemmingerfea86ad2008-01-12 20:57:07 -08001037static struct list_head *fib_insert_node(struct trie *t, u32 key, int plen)
Robert Olsson19baf832005-06-21 12:43:18 -07001038{
1039 int pos, newpos;
1040 struct tnode *tp = NULL, *tn = NULL;
David S. Millerb299e4f2011-02-02 20:48:10 -08001041 struct rt_trie_node *n;
Robert Olsson19baf832005-06-21 12:43:18 -07001042 struct leaf *l;
1043 int missbit;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001044 struct list_head *fa_head = NULL;
Robert Olsson19baf832005-06-21 12:43:18 -07001045 struct leaf_info *li;
1046 t_key cindex;
1047
1048 pos = 0;
Eric Dumazet0a5c0472011-03-31 01:51:35 -07001049 n = rtnl_dereference(t->trie);
Robert Olsson19baf832005-06-21 12:43:18 -07001050
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001051 /* If we point to NULL, stop. Either the tree is empty and we should
1052 * just put a new leaf in if, or we have reached an empty child slot,
Robert Olsson19baf832005-06-21 12:43:18 -07001053 * and we should just put our new leaf in that.
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001054 * If we point to a T_TNODE, check if it matches our key. Note that
1055 * a T_TNODE might be skipping any number of bits - its 'pos' need
Robert Olsson19baf832005-06-21 12:43:18 -07001056 * not be the parent's 'pos'+'bits'!
1057 *
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001058 * If it does match the current key, get pos/bits from it, extract
Robert Olsson19baf832005-06-21 12:43:18 -07001059 * the index from our key, push the T_TNODE and walk the tree.
1060 *
1061 * If it doesn't, we have to replace it with a new T_TNODE.
1062 *
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001063 * If we point to a T_LEAF, it might or might not have the same key
1064 * as we do. If it does, just change the value, update the T_LEAF's
1065 * value, and return it.
Robert Olsson19baf832005-06-21 12:43:18 -07001066 * If it doesn't, we need to replace it with a T_TNODE.
1067 */
1068
1069 while (n != NULL && NODE_TYPE(n) == T_TNODE) {
1070 tn = (struct tnode *) n;
Olof Johansson91b9a272005-08-09 20:24:39 -07001071
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001072 check_tnode(tn);
Olof Johansson91b9a272005-08-09 20:24:39 -07001073
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001074 if (tkey_sub_equals(tn->key, pos, tn->pos-pos, key)) {
Robert Olsson19baf832005-06-21 12:43:18 -07001075 tp = tn;
Olof Johansson91b9a272005-08-09 20:24:39 -07001076 pos = tn->pos + tn->bits;
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001077 n = tnode_get_child(tn,
1078 tkey_extract_bits(key,
1079 tn->pos,
1080 tn->bits));
Robert Olsson19baf832005-06-21 12:43:18 -07001081
Stephen Hemminger06801912007-08-10 15:22:13 -07001082 BUG_ON(n && node_parent(n) != tn);
Olof Johansson91b9a272005-08-09 20:24:39 -07001083 } else
Robert Olsson19baf832005-06-21 12:43:18 -07001084 break;
1085 }
1086
1087 /*
1088 * n ----> NULL, LEAF or TNODE
1089 *
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001090 * tp is n's (parent) ----> NULL or TNODE
Robert Olsson19baf832005-06-21 12:43:18 -07001091 */
1092
Olof Johansson91b9a272005-08-09 20:24:39 -07001093 BUG_ON(tp && IS_LEAF(tp));
Robert Olsson19baf832005-06-21 12:43:18 -07001094
1095 /* Case 1: n is a leaf. Compare prefixes */
1096
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001097 if (n != NULL && IS_LEAF(n) && tkey_equals(key, n->key)) {
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08001098 l = (struct leaf *) n;
Robert Olsson19baf832005-06-21 12:43:18 -07001099 li = leaf_info_new(plen);
Olof Johansson91b9a272005-08-09 20:24:39 -07001100
Stephen Hemmingerfea86ad2008-01-12 20:57:07 -08001101 if (!li)
1102 return NULL;
Robert Olsson19baf832005-06-21 12:43:18 -07001103
1104 fa_head = &li->falh;
1105 insert_leaf_info(&l->list, li);
1106 goto done;
1107 }
Robert Olsson19baf832005-06-21 12:43:18 -07001108 l = leaf_new();
1109
Stephen Hemmingerfea86ad2008-01-12 20:57:07 -08001110 if (!l)
1111 return NULL;
Robert Olsson19baf832005-06-21 12:43:18 -07001112
1113 l->key = key;
1114 li = leaf_info_new(plen);
1115
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001116 if (!li) {
Stephen Hemminger387a5482008-04-10 03:47:34 -07001117 free_leaf(l);
Stephen Hemmingerfea86ad2008-01-12 20:57:07 -08001118 return NULL;
Robert Olssonf835e472005-06-28 15:00:39 -07001119 }
Robert Olsson19baf832005-06-21 12:43:18 -07001120
1121 fa_head = &li->falh;
1122 insert_leaf_info(&l->list, li);
1123
Robert Olsson19baf832005-06-21 12:43:18 -07001124 if (t->trie && n == NULL) {
Olof Johansson91b9a272005-08-09 20:24:39 -07001125 /* Case 2: n is NULL, and will just insert a new leaf */
Robert Olsson19baf832005-06-21 12:43:18 -07001126
David S. Millerb299e4f2011-02-02 20:48:10 -08001127 node_set_parent((struct rt_trie_node *)l, tp);
Robert Olsson19baf832005-06-21 12:43:18 -07001128
Olof Johansson91b9a272005-08-09 20:24:39 -07001129 cindex = tkey_extract_bits(key, tp->pos, tp->bits);
David S. Millerb299e4f2011-02-02 20:48:10 -08001130 put_child(t, (struct tnode *)tp, cindex, (struct rt_trie_node *)l);
Olof Johansson91b9a272005-08-09 20:24:39 -07001131 } else {
1132 /* Case 3: n is a LEAF or a TNODE and the key doesn't match. */
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001133 /*
1134 * Add a new tnode here
Robert Olsson19baf832005-06-21 12:43:18 -07001135 * first tnode need some special handling
1136 */
1137
1138 if (tp)
Olof Johansson91b9a272005-08-09 20:24:39 -07001139 pos = tp->pos+tp->bits;
Robert Olsson19baf832005-06-21 12:43:18 -07001140 else
Olof Johansson91b9a272005-08-09 20:24:39 -07001141 pos = 0;
1142
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001143 if (n) {
Robert Olsson19baf832005-06-21 12:43:18 -07001144 newpos = tkey_mismatch(key, pos, n->key);
1145 tn = tnode_new(n->key, newpos, 1);
Olof Johansson91b9a272005-08-09 20:24:39 -07001146 } else {
Robert Olsson19baf832005-06-21 12:43:18 -07001147 newpos = 0;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001148 tn = tnode_new(key, newpos, 1); /* First tnode */
Robert Olsson19baf832005-06-21 12:43:18 -07001149 }
Robert Olsson19baf832005-06-21 12:43:18 -07001150
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001151 if (!tn) {
Robert Olssonf835e472005-06-28 15:00:39 -07001152 free_leaf_info(li);
Stephen Hemminger387a5482008-04-10 03:47:34 -07001153 free_leaf(l);
Stephen Hemmingerfea86ad2008-01-12 20:57:07 -08001154 return NULL;
Olof Johansson91b9a272005-08-09 20:24:39 -07001155 }
1156
David S. Millerb299e4f2011-02-02 20:48:10 -08001157 node_set_parent((struct rt_trie_node *)tn, tp);
Robert Olsson19baf832005-06-21 12:43:18 -07001158
Olof Johansson91b9a272005-08-09 20:24:39 -07001159 missbit = tkey_extract_bits(key, newpos, 1);
David S. Millerb299e4f2011-02-02 20:48:10 -08001160 put_child(t, tn, missbit, (struct rt_trie_node *)l);
Robert Olsson19baf832005-06-21 12:43:18 -07001161 put_child(t, tn, 1-missbit, n);
1162
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001163 if (tp) {
Robert Olsson19baf832005-06-21 12:43:18 -07001164 cindex = tkey_extract_bits(key, tp->pos, tp->bits);
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001165 put_child(t, (struct tnode *)tp, cindex,
David S. Millerb299e4f2011-02-02 20:48:10 -08001166 (struct rt_trie_node *)tn);
Olof Johansson91b9a272005-08-09 20:24:39 -07001167 } else {
David S. Millerb299e4f2011-02-02 20:48:10 -08001168 rcu_assign_pointer(t->trie, (struct rt_trie_node *)tn);
Robert Olsson19baf832005-06-21 12:43:18 -07001169 tp = tn;
1170 }
1171 }
Olof Johansson91b9a272005-08-09 20:24:39 -07001172
1173 if (tp && tp->pos + tp->bits > 32)
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001174 pr_warning("fib_trie"
1175 " tp=%p pos=%d, bits=%d, key=%0x plen=%d\n",
1176 tp, tp->pos, tp->bits, key, plen);
Olof Johansson91b9a272005-08-09 20:24:39 -07001177
Robert Olsson19baf832005-06-21 12:43:18 -07001178 /* Rebalance the trie */
Robert Olsson2373ce12005-08-25 13:01:29 -07001179
Jarek Poplawski7b855762009-06-18 00:28:51 -07001180 trie_rebalance(t, tp);
Robert Olssonf835e472005-06-28 15:00:39 -07001181done:
Robert Olsson19baf832005-06-21 12:43:18 -07001182 return fa_head;
1183}
1184
Robert Olssond562f1f2007-03-26 14:22:22 -07001185/*
1186 * Caller must hold RTNL.
1187 */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001188int fib_table_insert(struct fib_table *tb, struct fib_config *cfg)
Robert Olsson19baf832005-06-21 12:43:18 -07001189{
1190 struct trie *t = (struct trie *) tb->tb_data;
1191 struct fib_alias *fa, *new_fa;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001192 struct list_head *fa_head = NULL;
Robert Olsson19baf832005-06-21 12:43:18 -07001193 struct fib_info *fi;
Thomas Graf4e902c52006-08-17 18:14:52 -07001194 int plen = cfg->fc_dst_len;
1195 u8 tos = cfg->fc_tos;
Robert Olsson19baf832005-06-21 12:43:18 -07001196 u32 key, mask;
1197 int err;
1198 struct leaf *l;
1199
1200 if (plen > 32)
1201 return -EINVAL;
1202
Thomas Graf4e902c52006-08-17 18:14:52 -07001203 key = ntohl(cfg->fc_dst);
Robert Olsson19baf832005-06-21 12:43:18 -07001204
Patrick McHardy2dfe55b2006-08-10 23:08:33 -07001205 pr_debug("Insert table=%u %08x/%d\n", tb->tb_id, key, plen);
Robert Olsson19baf832005-06-21 12:43:18 -07001206
Olof Johansson91b9a272005-08-09 20:24:39 -07001207 mask = ntohl(inet_make_mask(plen));
Robert Olsson19baf832005-06-21 12:43:18 -07001208
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001209 if (key & ~mask)
Robert Olsson19baf832005-06-21 12:43:18 -07001210 return -EINVAL;
1211
1212 key = key & mask;
1213
Thomas Graf4e902c52006-08-17 18:14:52 -07001214 fi = fib_create_info(cfg);
1215 if (IS_ERR(fi)) {
1216 err = PTR_ERR(fi);
Robert Olsson19baf832005-06-21 12:43:18 -07001217 goto err;
Thomas Graf4e902c52006-08-17 18:14:52 -07001218 }
Robert Olsson19baf832005-06-21 12:43:18 -07001219
1220 l = fib_find_node(t, key);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001221 fa = NULL;
Robert Olsson19baf832005-06-21 12:43:18 -07001222
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001223 if (l) {
Robert Olsson19baf832005-06-21 12:43:18 -07001224 fa_head = get_fa_head(l, plen);
1225 fa = fib_find_alias(fa_head, tos, fi->fib_priority);
1226 }
1227
1228 /* Now fa, if non-NULL, points to the first fib alias
1229 * with the same keys [prefix,tos,priority], if such key already
1230 * exists or to the node before which we will insert new one.
1231 *
1232 * If fa is NULL, we will need to allocate a new one and
1233 * insert to the head of f.
1234 *
1235 * If f is NULL, no fib node matched the destination key
1236 * and we need to allocate a new one of those as well.
1237 */
1238
Julian Anastasov936f6f82008-01-28 21:18:06 -08001239 if (fa && fa->fa_tos == tos &&
1240 fa->fa_info->fib_priority == fi->fib_priority) {
1241 struct fib_alias *fa_first, *fa_match;
Robert Olsson19baf832005-06-21 12:43:18 -07001242
1243 err = -EEXIST;
Thomas Graf4e902c52006-08-17 18:14:52 -07001244 if (cfg->fc_nlflags & NLM_F_EXCL)
Robert Olsson19baf832005-06-21 12:43:18 -07001245 goto out;
1246
Julian Anastasov936f6f82008-01-28 21:18:06 -08001247 /* We have 2 goals:
1248 * 1. Find exact match for type, scope, fib_info to avoid
1249 * duplicate routes
1250 * 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it
1251 */
1252 fa_match = NULL;
1253 fa_first = fa;
1254 fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
1255 list_for_each_entry_continue(fa, fa_head, fa_list) {
1256 if (fa->fa_tos != tos)
1257 break;
1258 if (fa->fa_info->fib_priority != fi->fib_priority)
1259 break;
1260 if (fa->fa_type == cfg->fc_type &&
Julian Anastasov936f6f82008-01-28 21:18:06 -08001261 fa->fa_info == fi) {
1262 fa_match = fa;
1263 break;
1264 }
1265 }
1266
Thomas Graf4e902c52006-08-17 18:14:52 -07001267 if (cfg->fc_nlflags & NLM_F_REPLACE) {
Robert Olsson19baf832005-06-21 12:43:18 -07001268 struct fib_info *fi_drop;
1269 u8 state;
1270
Julian Anastasov936f6f82008-01-28 21:18:06 -08001271 fa = fa_first;
1272 if (fa_match) {
1273 if (fa == fa_match)
1274 err = 0;
Joonwoo Park67250332008-01-18 03:45:18 -08001275 goto out;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001276 }
Robert Olsson2373ce12005-08-25 13:01:29 -07001277 err = -ENOBUFS;
Christoph Lametere94b1762006-12-06 20:33:17 -08001278 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
Robert Olsson2373ce12005-08-25 13:01:29 -07001279 if (new_fa == NULL)
1280 goto out;
Robert Olsson19baf832005-06-21 12:43:18 -07001281
1282 fi_drop = fa->fa_info;
Robert Olsson2373ce12005-08-25 13:01:29 -07001283 new_fa->fa_tos = fa->fa_tos;
1284 new_fa->fa_info = fi;
Thomas Graf4e902c52006-08-17 18:14:52 -07001285 new_fa->fa_type = cfg->fc_type;
Robert Olsson19baf832005-06-21 12:43:18 -07001286 state = fa->fa_state;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001287 new_fa->fa_state = state & ~FA_S_ACCESSED;
Robert Olsson19baf832005-06-21 12:43:18 -07001288
Robert Olsson2373ce12005-08-25 13:01:29 -07001289 list_replace_rcu(&fa->fa_list, &new_fa->fa_list);
1290 alias_free_mem_rcu(fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001291
1292 fib_release_info(fi_drop);
1293 if (state & FA_S_ACCESSED)
Denis V. Lunev76e6ebf2008-07-05 19:00:44 -07001294 rt_cache_flush(cfg->fc_nlinfo.nl_net, -1);
Milan Kocianb8f55832007-05-23 14:55:06 -07001295 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen,
1296 tb->tb_id, &cfg->fc_nlinfo, NLM_F_REPLACE);
Robert Olsson19baf832005-06-21 12:43:18 -07001297
Olof Johansson91b9a272005-08-09 20:24:39 -07001298 goto succeeded;
Robert Olsson19baf832005-06-21 12:43:18 -07001299 }
1300 /* Error if we find a perfect match which
1301 * uses the same scope, type, and nexthop
1302 * information.
1303 */
Julian Anastasov936f6f82008-01-28 21:18:06 -08001304 if (fa_match)
1305 goto out;
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001306
Thomas Graf4e902c52006-08-17 18:14:52 -07001307 if (!(cfg->fc_nlflags & NLM_F_APPEND))
Julian Anastasov936f6f82008-01-28 21:18:06 -08001308 fa = fa_first;
Robert Olsson19baf832005-06-21 12:43:18 -07001309 }
1310 err = -ENOENT;
Thomas Graf4e902c52006-08-17 18:14:52 -07001311 if (!(cfg->fc_nlflags & NLM_F_CREATE))
Robert Olsson19baf832005-06-21 12:43:18 -07001312 goto out;
1313
1314 err = -ENOBUFS;
Christoph Lametere94b1762006-12-06 20:33:17 -08001315 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
Robert Olsson19baf832005-06-21 12:43:18 -07001316 if (new_fa == NULL)
1317 goto out;
1318
1319 new_fa->fa_info = fi;
1320 new_fa->fa_tos = tos;
Thomas Graf4e902c52006-08-17 18:14:52 -07001321 new_fa->fa_type = cfg->fc_type;
Robert Olsson19baf832005-06-21 12:43:18 -07001322 new_fa->fa_state = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001323 /*
1324 * Insert new entry to the list.
1325 */
1326
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001327 if (!fa_head) {
Stephen Hemmingerfea86ad2008-01-12 20:57:07 -08001328 fa_head = fib_insert_node(t, key, plen);
1329 if (unlikely(!fa_head)) {
1330 err = -ENOMEM;
Robert Olssonf835e472005-06-28 15:00:39 -07001331 goto out_free_new_fa;
Stephen Hemmingerfea86ad2008-01-12 20:57:07 -08001332 }
Robert Olssonf835e472005-06-28 15:00:39 -07001333 }
Robert Olsson19baf832005-06-21 12:43:18 -07001334
David S. Miller21d8c492011-04-14 14:49:37 -07001335 if (!plen)
1336 tb->tb_num_default++;
1337
Robert Olsson2373ce12005-08-25 13:01:29 -07001338 list_add_tail_rcu(&new_fa->fa_list,
1339 (fa ? &fa->fa_list : fa_head));
Robert Olsson19baf832005-06-21 12:43:18 -07001340
Denis V. Lunev76e6ebf2008-07-05 19:00:44 -07001341 rt_cache_flush(cfg->fc_nlinfo.nl_net, -1);
Thomas Graf4e902c52006-08-17 18:14:52 -07001342 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen, tb->tb_id,
Milan Kocianb8f55832007-05-23 14:55:06 -07001343 &cfg->fc_nlinfo, 0);
Robert Olsson19baf832005-06-21 12:43:18 -07001344succeeded:
1345 return 0;
Robert Olssonf835e472005-06-28 15:00:39 -07001346
1347out_free_new_fa:
1348 kmem_cache_free(fn_alias_kmem, new_fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001349out:
1350 fib_release_info(fi);
Olof Johansson91b9a272005-08-09 20:24:39 -07001351err:
Robert Olsson19baf832005-06-21 12:43:18 -07001352 return err;
1353}
1354
Robert Olsson772cb712005-09-19 15:31:18 -07001355/* should be called with rcu_read_lock */
David S. Miller5b470442011-01-31 16:10:03 -08001356static int check_leaf(struct fib_table *tb, struct trie *t, struct leaf *l,
David S. Miller22bd5b92011-03-11 19:54:08 -05001357 t_key key, const struct flowi4 *flp,
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00001358 struct fib_result *res, int fib_flags)
Robert Olsson19baf832005-06-21 12:43:18 -07001359{
Robert Olsson19baf832005-06-21 12:43:18 -07001360 struct leaf_info *li;
1361 struct hlist_head *hhead = &l->list;
1362 struct hlist_node *node;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001363
Robert Olsson2373ce12005-08-25 13:01:29 -07001364 hlist_for_each_entry_rcu(li, node, hhead, hlist) {
David S. Miller3be06862011-03-07 15:01:10 -08001365 struct fib_alias *fa;
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001366 int plen = li->plen;
1367 __be32 mask = inet_make_mask(plen);
1368
Al Viro888454c2006-09-19 13:42:46 -07001369 if (l->key != (key & ntohl(mask)))
Robert Olsson19baf832005-06-21 12:43:18 -07001370 continue;
1371
David S. Miller3be06862011-03-07 15:01:10 -08001372 list_for_each_entry_rcu(fa, &li->falh, fa_list) {
1373 struct fib_info *fi = fa->fa_info;
1374 int nhsel, err;
1375
David S. Miller22bd5b92011-03-11 19:54:08 -05001376 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
David S. Miller3be06862011-03-07 15:01:10 -08001377 continue;
David S. Miller37e826c2011-03-24 18:06:47 -07001378 if (fa->fa_info->fib_scope < flp->flowi4_scope)
David S. Miller3be06862011-03-07 15:01:10 -08001379 continue;
1380 fib_alias_accessed(fa);
1381 err = fib_props[fa->fa_type].error;
1382 if (err) {
1383#ifdef CONFIG_IP_FIB_TRIE_STATS
Julian Anastasov1fbc7842011-03-25 20:33:23 -07001384 t->stats.semantic_match_passed++;
David S. Miller3be06862011-03-07 15:01:10 -08001385#endif
Julian Anastasov1fbc7842011-03-25 20:33:23 -07001386 return err;
David S. Miller3be06862011-03-07 15:01:10 -08001387 }
1388 if (fi->fib_flags & RTNH_F_DEAD)
1389 continue;
1390 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
1391 const struct fib_nh *nh = &fi->fib_nh[nhsel];
1392
1393 if (nh->nh_flags & RTNH_F_DEAD)
1394 continue;
David S. Miller22bd5b92011-03-11 19:54:08 -05001395 if (flp->flowi4_oif && flp->flowi4_oif != nh->nh_oif)
David S. Miller3be06862011-03-07 15:01:10 -08001396 continue;
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001397
Robert Olsson19baf832005-06-21 12:43:18 -07001398#ifdef CONFIG_IP_FIB_TRIE_STATS
David S. Miller3be06862011-03-07 15:01:10 -08001399 t->stats.semantic_match_passed++;
Robert Olsson19baf832005-06-21 12:43:18 -07001400#endif
David S. Miller3be06862011-03-07 15:01:10 -08001401 res->prefixlen = plen;
1402 res->nh_sel = nhsel;
1403 res->type = fa->fa_type;
David S. Miller37e826c2011-03-24 18:06:47 -07001404 res->scope = fa->fa_info->fib_scope;
David S. Miller3be06862011-03-07 15:01:10 -08001405 res->fi = fi;
1406 res->table = tb;
1407 res->fa_head = &li->falh;
1408 if (!(fib_flags & FIB_LOOKUP_NOREF))
1409 atomic_inc(&res->fi->fib_clntref);
1410 return 0;
1411 }
1412 }
1413
1414#ifdef CONFIG_IP_FIB_TRIE_STATS
1415 t->stats.semantic_match_miss++;
1416#endif
Robert Olsson19baf832005-06-21 12:43:18 -07001417 }
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001418
Ben Hutchings2e655572008-07-10 16:52:52 -07001419 return 1;
Robert Olsson19baf832005-06-21 12:43:18 -07001420}
1421
David S. Miller22bd5b92011-03-11 19:54:08 -05001422int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00001423 struct fib_result *res, int fib_flags)
Robert Olsson19baf832005-06-21 12:43:18 -07001424{
1425 struct trie *t = (struct trie *) tb->tb_data;
Ben Hutchings2e655572008-07-10 16:52:52 -07001426 int ret;
David S. Millerb299e4f2011-02-02 20:48:10 -08001427 struct rt_trie_node *n;
Robert Olsson19baf832005-06-21 12:43:18 -07001428 struct tnode *pn;
David S. Miller3b004562011-02-16 14:56:22 -08001429 unsigned int pos, bits;
David S. Miller22bd5b92011-03-11 19:54:08 -05001430 t_key key = ntohl(flp->daddr);
David S. Miller3b004562011-02-16 14:56:22 -08001431 unsigned int chopped_off;
Robert Olsson19baf832005-06-21 12:43:18 -07001432 t_key cindex = 0;
David S. Miller3b004562011-02-16 14:56:22 -08001433 unsigned int current_prefix_length = KEYLENGTH;
Olof Johansson91b9a272005-08-09 20:24:39 -07001434 struct tnode *cn;
Eric Dumazet874ffa82010-10-13 06:56:11 +00001435 t_key pref_mismatch;
Olof Johansson91b9a272005-08-09 20:24:39 -07001436
Robert Olsson2373ce12005-08-25 13:01:29 -07001437 rcu_read_lock();
Robert Olsson19baf832005-06-21 12:43:18 -07001438
Robert Olsson2373ce12005-08-25 13:01:29 -07001439 n = rcu_dereference(t->trie);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001440 if (!n)
Robert Olsson19baf832005-06-21 12:43:18 -07001441 goto failed;
1442
1443#ifdef CONFIG_IP_FIB_TRIE_STATS
1444 t->stats.gets++;
1445#endif
1446
1447 /* Just a leaf? */
1448 if (IS_LEAF(n)) {
David S. Miller5b470442011-01-31 16:10:03 -08001449 ret = check_leaf(tb, t, (struct leaf *)n, key, flp, res, fib_flags);
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001450 goto found;
Robert Olsson19baf832005-06-21 12:43:18 -07001451 }
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001452
Robert Olsson19baf832005-06-21 12:43:18 -07001453 pn = (struct tnode *) n;
1454 chopped_off = 0;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001455
Olof Johansson91b9a272005-08-09 20:24:39 -07001456 while (pn) {
Robert Olsson19baf832005-06-21 12:43:18 -07001457 pos = pn->pos;
1458 bits = pn->bits;
1459
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001460 if (!chopped_off)
Stephen Hemmingerab66b4a2007-08-10 15:22:58 -07001461 cindex = tkey_extract_bits(mask_pfx(key, current_prefix_length),
1462 pos, bits);
Robert Olsson19baf832005-06-21 12:43:18 -07001463
Jarek Poplawskib902e572009-07-14 11:20:32 +00001464 n = tnode_get_child_rcu(pn, cindex);
Robert Olsson19baf832005-06-21 12:43:18 -07001465
1466 if (n == NULL) {
1467#ifdef CONFIG_IP_FIB_TRIE_STATS
1468 t->stats.null_node_hit++;
1469#endif
1470 goto backtrace;
1471 }
1472
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001473 if (IS_LEAF(n)) {
David S. Miller5b470442011-01-31 16:10:03 -08001474 ret = check_leaf(tb, t, (struct leaf *)n, key, flp, res, fib_flags);
Ben Hutchings2e655572008-07-10 16:52:52 -07001475 if (ret > 0)
Olof Johansson91b9a272005-08-09 20:24:39 -07001476 goto backtrace;
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001477 goto found;
Olof Johansson91b9a272005-08-09 20:24:39 -07001478 }
1479
Olof Johansson91b9a272005-08-09 20:24:39 -07001480 cn = (struct tnode *)n;
1481
1482 /*
1483 * It's a tnode, and we can do some extra checks here if we
1484 * like, to avoid descending into a dead-end branch.
1485 * This tnode is in the parent's child array at index
1486 * key[p_pos..p_pos+p_bits] but potentially with some bits
1487 * chopped off, so in reality the index may be just a
1488 * subprefix, padded with zero at the end.
1489 * We can also take a look at any skipped bits in this
1490 * tnode - everything up to p_pos is supposed to be ok,
1491 * and the non-chopped bits of the index (se previous
1492 * paragraph) are also guaranteed ok, but the rest is
1493 * considered unknown.
1494 *
1495 * The skipped bits are key[pos+bits..cn->pos].
1496 */
1497
1498 /* If current_prefix_length < pos+bits, we are already doing
1499 * actual prefix matching, which means everything from
1500 * pos+(bits-chopped_off) onward must be zero along some
1501 * branch of this subtree - otherwise there is *no* valid
1502 * prefix present. Here we can only check the skipped
1503 * bits. Remember, since we have already indexed into the
1504 * parent's child array, we know that the bits we chopped of
1505 * *are* zero.
1506 */
1507
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001508 /* NOTA BENE: Checking only skipped bits
1509 for the new node here */
Olof Johansson91b9a272005-08-09 20:24:39 -07001510
1511 if (current_prefix_length < pos+bits) {
1512 if (tkey_extract_bits(cn->key, current_prefix_length,
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001513 cn->pos - current_prefix_length)
1514 || !(cn->child[0]))
Olof Johansson91b9a272005-08-09 20:24:39 -07001515 goto backtrace;
1516 }
1517
1518 /*
1519 * If chopped_off=0, the index is fully validated and we
1520 * only need to look at the skipped bits for this, the new,
1521 * tnode. What we actually want to do is to find out if
1522 * these skipped bits match our key perfectly, or if we will
1523 * have to count on finding a matching prefix further down,
1524 * because if we do, we would like to have some way of
1525 * verifying the existence of such a prefix at this point.
1526 */
1527
1528 /* The only thing we can do at this point is to verify that
1529 * any such matching prefix can indeed be a prefix to our
1530 * key, and if the bits in the node we are inspecting that
1531 * do not match our key are not ZERO, this cannot be true.
1532 * Thus, find out where there is a mismatch (before cn->pos)
1533 * and verify that all the mismatching bits are zero in the
1534 * new tnode's key.
1535 */
1536
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001537 /*
1538 * Note: We aren't very concerned about the piece of
1539 * the key that precede pn->pos+pn->bits, since these
1540 * have already been checked. The bits after cn->pos
1541 * aren't checked since these are by definition
1542 * "unknown" at this point. Thus, what we want to see
1543 * is if we are about to enter the "prefix matching"
1544 * state, and in that case verify that the skipped
1545 * bits that will prevail throughout this subtree are
1546 * zero, as they have to be if we are to find a
1547 * matching prefix.
Olof Johansson91b9a272005-08-09 20:24:39 -07001548 */
1549
Eric Dumazet874ffa82010-10-13 06:56:11 +00001550 pref_mismatch = mask_pfx(cn->key ^ key, cn->pos);
Olof Johansson91b9a272005-08-09 20:24:39 -07001551
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001552 /*
1553 * In short: If skipped bits in this node do not match
1554 * the search key, enter the "prefix matching"
1555 * state.directly.
Olof Johansson91b9a272005-08-09 20:24:39 -07001556 */
1557 if (pref_mismatch) {
Eric Dumazet874ffa82010-10-13 06:56:11 +00001558 int mp = KEYLENGTH - fls(pref_mismatch);
Olof Johansson91b9a272005-08-09 20:24:39 -07001559
Eric Dumazet874ffa82010-10-13 06:56:11 +00001560 if (tkey_extract_bits(cn->key, mp, cn->pos - mp) != 0)
Olof Johansson91b9a272005-08-09 20:24:39 -07001561 goto backtrace;
1562
1563 if (current_prefix_length >= cn->pos)
1564 current_prefix_length = mp;
1565 }
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001566
Olof Johansson91b9a272005-08-09 20:24:39 -07001567 pn = (struct tnode *)n; /* Descend */
1568 chopped_off = 0;
1569 continue;
1570
Robert Olsson19baf832005-06-21 12:43:18 -07001571backtrace:
1572 chopped_off++;
1573
1574 /* As zero don't change the child key (cindex) */
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001575 while ((chopped_off <= pn->bits)
1576 && !(cindex & (1<<(chopped_off-1))))
Robert Olsson19baf832005-06-21 12:43:18 -07001577 chopped_off++;
Robert Olsson19baf832005-06-21 12:43:18 -07001578
1579 /* Decrease current_... with bits chopped off */
1580 if (current_prefix_length > pn->pos + pn->bits - chopped_off)
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001581 current_prefix_length = pn->pos + pn->bits
1582 - chopped_off;
Olof Johansson91b9a272005-08-09 20:24:39 -07001583
Robert Olsson19baf832005-06-21 12:43:18 -07001584 /*
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001585 * Either we do the actual chop off according or if we have
Robert Olsson19baf832005-06-21 12:43:18 -07001586 * chopped off all bits in this tnode walk up to our parent.
1587 */
1588
Olof Johansson91b9a272005-08-09 20:24:39 -07001589 if (chopped_off <= pn->bits) {
Robert Olsson19baf832005-06-21 12:43:18 -07001590 cindex &= ~(1 << (chopped_off-1));
Olof Johansson91b9a272005-08-09 20:24:39 -07001591 } else {
David S. Millerb299e4f2011-02-02 20:48:10 -08001592 struct tnode *parent = node_parent_rcu((struct rt_trie_node *) pn);
Stephen Hemminger06801912007-08-10 15:22:13 -07001593 if (!parent)
Robert Olsson19baf832005-06-21 12:43:18 -07001594 goto failed;
Olof Johansson91b9a272005-08-09 20:24:39 -07001595
Robert Olsson19baf832005-06-21 12:43:18 -07001596 /* Get Child's index */
Stephen Hemminger06801912007-08-10 15:22:13 -07001597 cindex = tkey_extract_bits(pn->key, parent->pos, parent->bits);
1598 pn = parent;
Robert Olsson19baf832005-06-21 12:43:18 -07001599 chopped_off = 0;
1600
1601#ifdef CONFIG_IP_FIB_TRIE_STATS
1602 t->stats.backtrack++;
1603#endif
1604 goto backtrace;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001605 }
Robert Olsson19baf832005-06-21 12:43:18 -07001606 }
1607failed:
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001608 ret = 1;
Robert Olsson19baf832005-06-21 12:43:18 -07001609found:
Robert Olsson2373ce12005-08-25 13:01:29 -07001610 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07001611 return ret;
1612}
1613
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001614/*
1615 * Remove the leaf and return parent.
1616 */
1617static void trie_leaf_remove(struct trie *t, struct leaf *l)
Robert Olsson19baf832005-06-21 12:43:18 -07001618{
David S. Millerb299e4f2011-02-02 20:48:10 -08001619 struct tnode *tp = node_parent((struct rt_trie_node *) l);
Robert Olsson19baf832005-06-21 12:43:18 -07001620
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001621 pr_debug("entering trie_leaf_remove(%p)\n", l);
Robert Olsson19baf832005-06-21 12:43:18 -07001622
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001623 if (tp) {
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001624 t_key cindex = tkey_extract_bits(l->key, tp->pos, tp->bits);
Robert Olsson19baf832005-06-21 12:43:18 -07001625 put_child(t, (struct tnode *)tp, cindex, NULL);
Jarek Poplawski7b855762009-06-18 00:28:51 -07001626 trie_rebalance(t, tp);
Olof Johansson91b9a272005-08-09 20:24:39 -07001627 } else
Robert Olsson2373ce12005-08-25 13:01:29 -07001628 rcu_assign_pointer(t->trie, NULL);
Robert Olsson19baf832005-06-21 12:43:18 -07001629
Stephen Hemminger387a5482008-04-10 03:47:34 -07001630 free_leaf(l);
Robert Olsson19baf832005-06-21 12:43:18 -07001631}
1632
Robert Olssond562f1f2007-03-26 14:22:22 -07001633/*
1634 * Caller must hold RTNL.
1635 */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001636int fib_table_delete(struct fib_table *tb, struct fib_config *cfg)
Robert Olsson19baf832005-06-21 12:43:18 -07001637{
1638 struct trie *t = (struct trie *) tb->tb_data;
1639 u32 key, mask;
Thomas Graf4e902c52006-08-17 18:14:52 -07001640 int plen = cfg->fc_dst_len;
1641 u8 tos = cfg->fc_tos;
Robert Olsson19baf832005-06-21 12:43:18 -07001642 struct fib_alias *fa, *fa_to_delete;
1643 struct list_head *fa_head;
1644 struct leaf *l;
Olof Johansson91b9a272005-08-09 20:24:39 -07001645 struct leaf_info *li;
1646
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001647 if (plen > 32)
Robert Olsson19baf832005-06-21 12:43:18 -07001648 return -EINVAL;
1649
Thomas Graf4e902c52006-08-17 18:14:52 -07001650 key = ntohl(cfg->fc_dst);
Olof Johansson91b9a272005-08-09 20:24:39 -07001651 mask = ntohl(inet_make_mask(plen));
Robert Olsson19baf832005-06-21 12:43:18 -07001652
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001653 if (key & ~mask)
Robert Olsson19baf832005-06-21 12:43:18 -07001654 return -EINVAL;
1655
1656 key = key & mask;
1657 l = fib_find_node(t, key);
1658
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001659 if (!l)
Robert Olsson19baf832005-06-21 12:43:18 -07001660 return -ESRCH;
1661
1662 fa_head = get_fa_head(l, plen);
1663 fa = fib_find_alias(fa_head, tos, 0);
1664
1665 if (!fa)
1666 return -ESRCH;
1667
Stephen Hemminger0c7770c2005-08-23 21:59:41 -07001668 pr_debug("Deleting %08x/%d tos=%d t=%p\n", key, plen, tos, t);
Robert Olsson19baf832005-06-21 12:43:18 -07001669
1670 fa_to_delete = NULL;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001671 fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
1672 list_for_each_entry_continue(fa, fa_head, fa_list) {
Robert Olsson19baf832005-06-21 12:43:18 -07001673 struct fib_info *fi = fa->fa_info;
1674
1675 if (fa->fa_tos != tos)
1676 break;
1677
Thomas Graf4e902c52006-08-17 18:14:52 -07001678 if ((!cfg->fc_type || fa->fa_type == cfg->fc_type) &&
1679 (cfg->fc_scope == RT_SCOPE_NOWHERE ||
David S. Miller37e826c2011-03-24 18:06:47 -07001680 fa->fa_info->fib_scope == cfg->fc_scope) &&
Julian Anastasov74cb3c12011-03-19 12:13:46 +00001681 (!cfg->fc_prefsrc ||
1682 fi->fib_prefsrc == cfg->fc_prefsrc) &&
Thomas Graf4e902c52006-08-17 18:14:52 -07001683 (!cfg->fc_protocol ||
1684 fi->fib_protocol == cfg->fc_protocol) &&
1685 fib_nh_match(cfg, fi) == 0) {
Robert Olsson19baf832005-06-21 12:43:18 -07001686 fa_to_delete = fa;
1687 break;
1688 }
1689 }
1690
Olof Johansson91b9a272005-08-09 20:24:39 -07001691 if (!fa_to_delete)
1692 return -ESRCH;
Robert Olsson19baf832005-06-21 12:43:18 -07001693
Olof Johansson91b9a272005-08-09 20:24:39 -07001694 fa = fa_to_delete;
Thomas Graf4e902c52006-08-17 18:14:52 -07001695 rtmsg_fib(RTM_DELROUTE, htonl(key), fa, plen, tb->tb_id,
Milan Kocianb8f55832007-05-23 14:55:06 -07001696 &cfg->fc_nlinfo, 0);
Robert Olsson19baf832005-06-21 12:43:18 -07001697
Olof Johansson91b9a272005-08-09 20:24:39 -07001698 l = fib_find_node(t, key);
Robert Olsson772cb712005-09-19 15:31:18 -07001699 li = find_leaf_info(l, plen);
Robert Olsson19baf832005-06-21 12:43:18 -07001700
Robert Olsson2373ce12005-08-25 13:01:29 -07001701 list_del_rcu(&fa->fa_list);
Robert Olsson19baf832005-06-21 12:43:18 -07001702
David S. Miller21d8c492011-04-14 14:49:37 -07001703 if (!plen)
1704 tb->tb_num_default--;
1705
Olof Johansson91b9a272005-08-09 20:24:39 -07001706 if (list_empty(fa_head)) {
Robert Olsson2373ce12005-08-25 13:01:29 -07001707 hlist_del_rcu(&li->hlist);
Olof Johansson91b9a272005-08-09 20:24:39 -07001708 free_leaf_info(li);
Robert Olsson2373ce12005-08-25 13:01:29 -07001709 }
Olof Johansson91b9a272005-08-09 20:24:39 -07001710
1711 if (hlist_empty(&l->list))
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001712 trie_leaf_remove(t, l);
Olof Johansson91b9a272005-08-09 20:24:39 -07001713
1714 if (fa->fa_state & FA_S_ACCESSED)
Denis V. Lunev76e6ebf2008-07-05 19:00:44 -07001715 rt_cache_flush(cfg->fc_nlinfo.nl_net, -1);
Olof Johansson91b9a272005-08-09 20:24:39 -07001716
Robert Olsson2373ce12005-08-25 13:01:29 -07001717 fib_release_info(fa->fa_info);
1718 alias_free_mem_rcu(fa);
Olof Johansson91b9a272005-08-09 20:24:39 -07001719 return 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001720}
1721
Stephen Hemmingeref3660c2008-04-10 03:46:12 -07001722static int trie_flush_list(struct list_head *head)
Robert Olsson19baf832005-06-21 12:43:18 -07001723{
1724 struct fib_alias *fa, *fa_node;
1725 int found = 0;
1726
1727 list_for_each_entry_safe(fa, fa_node, head, fa_list) {
1728 struct fib_info *fi = fa->fa_info;
Robert Olsson19baf832005-06-21 12:43:18 -07001729
Robert Olsson2373ce12005-08-25 13:01:29 -07001730 if (fi && (fi->fib_flags & RTNH_F_DEAD)) {
1731 list_del_rcu(&fa->fa_list);
1732 fib_release_info(fa->fa_info);
1733 alias_free_mem_rcu(fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001734 found++;
1735 }
1736 }
1737 return found;
1738}
1739
Stephen Hemmingeref3660c2008-04-10 03:46:12 -07001740static int trie_flush_leaf(struct leaf *l)
Robert Olsson19baf832005-06-21 12:43:18 -07001741{
1742 int found = 0;
1743 struct hlist_head *lih = &l->list;
1744 struct hlist_node *node, *tmp;
1745 struct leaf_info *li = NULL;
1746
1747 hlist_for_each_entry_safe(li, node, tmp, lih, hlist) {
Stephen Hemmingeref3660c2008-04-10 03:46:12 -07001748 found += trie_flush_list(&li->falh);
Robert Olsson19baf832005-06-21 12:43:18 -07001749
1750 if (list_empty(&li->falh)) {
Robert Olsson2373ce12005-08-25 13:01:29 -07001751 hlist_del_rcu(&li->hlist);
Robert Olsson19baf832005-06-21 12:43:18 -07001752 free_leaf_info(li);
1753 }
1754 }
1755 return found;
1756}
1757
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001758/*
1759 * Scan for the next right leaf starting at node p->child[idx]
1760 * Since we have back pointer, no recursion necessary.
1761 */
David S. Millerb299e4f2011-02-02 20:48:10 -08001762static struct leaf *leaf_walk_rcu(struct tnode *p, struct rt_trie_node *c)
Robert Olsson19baf832005-06-21 12:43:18 -07001763{
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001764 do {
1765 t_key idx;
Robert Olsson19baf832005-06-21 12:43:18 -07001766
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001767 if (c)
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001768 idx = tkey_extract_bits(c->key, p->pos, p->bits) + 1;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001769 else
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001770 idx = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001771
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001772 while (idx < 1u << p->bits) {
1773 c = tnode_get_child_rcu(p, idx++);
Robert Olsson2373ce12005-08-25 13:01:29 -07001774 if (!c)
Olof Johansson91b9a272005-08-09 20:24:39 -07001775 continue;
Robert Olsson19baf832005-06-21 12:43:18 -07001776
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001777 if (IS_LEAF(c)) {
Eric Dumazet0a5c0472011-03-31 01:51:35 -07001778 prefetch(rcu_dereference_rtnl(p->child[idx]));
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001779 return (struct leaf *) c;
Robert Olsson19baf832005-06-21 12:43:18 -07001780 }
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001781
1782 /* Rescan start scanning in new node */
1783 p = (struct tnode *) c;
1784 idx = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001785 }
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001786
1787 /* Node empty, walk back up to parent */
David S. Millerb299e4f2011-02-02 20:48:10 -08001788 c = (struct rt_trie_node *) p;
Eric Dumazeta034ee32010-09-09 23:32:28 +00001789 } while ((p = node_parent_rcu(c)) != NULL);
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001790
1791 return NULL; /* Root of trie */
1792}
1793
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001794static struct leaf *trie_firstleaf(struct trie *t)
1795{
Eric Dumazeta034ee32010-09-09 23:32:28 +00001796 struct tnode *n = (struct tnode *)rcu_dereference_rtnl(t->trie);
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001797
1798 if (!n)
1799 return NULL;
1800
1801 if (IS_LEAF(n)) /* trie is just a leaf */
1802 return (struct leaf *) n;
1803
1804 return leaf_walk_rcu(n, NULL);
1805}
1806
1807static struct leaf *trie_nextleaf(struct leaf *l)
1808{
David S. Millerb299e4f2011-02-02 20:48:10 -08001809 struct rt_trie_node *c = (struct rt_trie_node *) l;
Jarek Poplawskib902e572009-07-14 11:20:32 +00001810 struct tnode *p = node_parent_rcu(c);
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001811
1812 if (!p)
1813 return NULL; /* trie with just one leaf */
1814
1815 return leaf_walk_rcu(p, c);
Robert Olsson19baf832005-06-21 12:43:18 -07001816}
1817
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001818static struct leaf *trie_leafindex(struct trie *t, int index)
1819{
1820 struct leaf *l = trie_firstleaf(t);
1821
Stephen Hemmingerec28cf72008-02-11 21:12:49 -08001822 while (l && index-- > 0)
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001823 l = trie_nextleaf(l);
Stephen Hemmingerec28cf72008-02-11 21:12:49 -08001824
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001825 return l;
1826}
1827
1828
Robert Olssond562f1f2007-03-26 14:22:22 -07001829/*
1830 * Caller must hold RTNL.
1831 */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001832int fib_table_flush(struct fib_table *tb)
Robert Olsson19baf832005-06-21 12:43:18 -07001833{
1834 struct trie *t = (struct trie *) tb->tb_data;
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001835 struct leaf *l, *ll = NULL;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001836 int found = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001837
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001838 for (l = trie_firstleaf(t); l; l = trie_nextleaf(l)) {
Stephen Hemmingeref3660c2008-04-10 03:46:12 -07001839 found += trie_flush_leaf(l);
Robert Olsson19baf832005-06-21 12:43:18 -07001840
1841 if (ll && hlist_empty(&ll->list))
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001842 trie_leaf_remove(t, ll);
Robert Olsson19baf832005-06-21 12:43:18 -07001843 ll = l;
1844 }
1845
1846 if (ll && hlist_empty(&ll->list))
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001847 trie_leaf_remove(t, ll);
Robert Olsson19baf832005-06-21 12:43:18 -07001848
Stephen Hemminger0c7770c2005-08-23 21:59:41 -07001849 pr_debug("trie_flush found=%d\n", found);
Robert Olsson19baf832005-06-21 12:43:18 -07001850 return found;
1851}
1852
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00001853void fib_free_table(struct fib_table *tb)
1854{
1855 kfree(tb);
1856}
1857
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001858static int fn_trie_dump_fa(t_key key, int plen, struct list_head *fah,
1859 struct fib_table *tb,
Robert Olsson19baf832005-06-21 12:43:18 -07001860 struct sk_buff *skb, struct netlink_callback *cb)
1861{
1862 int i, s_i;
1863 struct fib_alias *fa;
Al Viro32ab5f82006-09-26 22:21:45 -07001864 __be32 xkey = htonl(key);
Robert Olsson19baf832005-06-21 12:43:18 -07001865
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001866 s_i = cb->args[5];
Robert Olsson19baf832005-06-21 12:43:18 -07001867 i = 0;
1868
Robert Olsson2373ce12005-08-25 13:01:29 -07001869 /* rcu_read_lock is hold by caller */
1870
1871 list_for_each_entry_rcu(fa, fah, fa_list) {
Robert Olsson19baf832005-06-21 12:43:18 -07001872 if (i < s_i) {
1873 i++;
1874 continue;
1875 }
Robert Olsson19baf832005-06-21 12:43:18 -07001876
1877 if (fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
1878 cb->nlh->nlmsg_seq,
1879 RTM_NEWROUTE,
1880 tb->tb_id,
1881 fa->fa_type,
Thomas Grafbe403ea2006-08-17 18:15:17 -07001882 xkey,
Robert Olsson19baf832005-06-21 12:43:18 -07001883 plen,
1884 fa->fa_tos,
Stephen Hemminger64347f72008-01-22 21:55:01 -08001885 fa->fa_info, NLM_F_MULTI) < 0) {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001886 cb->args[5] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001887 return -1;
Olof Johansson91b9a272005-08-09 20:24:39 -07001888 }
Robert Olsson19baf832005-06-21 12:43:18 -07001889 i++;
1890 }
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001891 cb->args[5] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001892 return skb->len;
1893}
1894
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001895static int fn_trie_dump_leaf(struct leaf *l, struct fib_table *tb,
1896 struct sk_buff *skb, struct netlink_callback *cb)
Robert Olsson19baf832005-06-21 12:43:18 -07001897{
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001898 struct leaf_info *li;
1899 struct hlist_node *node;
1900 int i, s_i;
Robert Olsson19baf832005-06-21 12:43:18 -07001901
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001902 s_i = cb->args[4];
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001903 i = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001904
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001905 /* rcu_read_lock is hold by caller */
1906 hlist_for_each_entry_rcu(li, node, &l->list, hlist) {
1907 if (i < s_i) {
1908 i++;
Robert Olsson19baf832005-06-21 12:43:18 -07001909 continue;
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001910 }
Robert Olsson19baf832005-06-21 12:43:18 -07001911
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001912 if (i > s_i)
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001913 cb->args[5] = 0;
Olof Johansson91b9a272005-08-09 20:24:39 -07001914
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001915 if (list_empty(&li->falh))
Robert Olsson19baf832005-06-21 12:43:18 -07001916 continue;
1917
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001918 if (fn_trie_dump_fa(l->key, li->plen, &li->falh, tb, skb, cb) < 0) {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001919 cb->args[4] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001920 return -1;
1921 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001922 i++;
Robert Olsson19baf832005-06-21 12:43:18 -07001923 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001924
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001925 cb->args[4] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001926 return skb->len;
1927}
1928
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001929int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
1930 struct netlink_callback *cb)
Robert Olsson19baf832005-06-21 12:43:18 -07001931{
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001932 struct leaf *l;
Robert Olsson19baf832005-06-21 12:43:18 -07001933 struct trie *t = (struct trie *) tb->tb_data;
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001934 t_key key = cb->args[2];
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001935 int count = cb->args[3];
Robert Olsson19baf832005-06-21 12:43:18 -07001936
Robert Olsson2373ce12005-08-25 13:01:29 -07001937 rcu_read_lock();
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001938 /* Dump starting at last key.
1939 * Note: 0.0.0.0/0 (ie default) is first key.
1940 */
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001941 if (count == 0)
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001942 l = trie_firstleaf(t);
1943 else {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001944 /* Normally, continue from last key, but if that is missing
1945 * fallback to using slow rescan
1946 */
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001947 l = fib_find_node(t, key);
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001948 if (!l)
1949 l = trie_leafindex(t, count);
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001950 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001951
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001952 while (l) {
1953 cb->args[2] = l->key;
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001954 if (fn_trie_dump_leaf(l, tb, skb, cb) < 0) {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001955 cb->args[3] = count;
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001956 rcu_read_unlock();
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001957 return -1;
Robert Olsson19baf832005-06-21 12:43:18 -07001958 }
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001959
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001960 ++count;
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001961 l = trie_nextleaf(l);
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001962 memset(&cb->args[4], 0,
1963 sizeof(cb->args) - 4*sizeof(cb->args[0]));
Robert Olsson19baf832005-06-21 12:43:18 -07001964 }
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001965 cb->args[3] = count;
Robert Olsson2373ce12005-08-25 13:01:29 -07001966 rcu_read_unlock();
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001967
Robert Olsson19baf832005-06-21 12:43:18 -07001968 return skb->len;
Robert Olsson19baf832005-06-21 12:43:18 -07001969}
1970
David S. Miller5348ba82011-02-01 15:30:56 -08001971void __init fib_trie_init(void)
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001972{
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001973 fn_alias_kmem = kmem_cache_create("ip_fib_alias",
1974 sizeof(struct fib_alias),
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -08001975 0, SLAB_PANIC, NULL);
1976
1977 trie_leaf_kmem = kmem_cache_create("ip_fib_trie",
1978 max(sizeof(struct leaf),
1979 sizeof(struct leaf_info)),
1980 0, SLAB_PANIC, NULL);
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001981}
Robert Olsson19baf832005-06-21 12:43:18 -07001982
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001983
David S. Miller5348ba82011-02-01 15:30:56 -08001984struct fib_table *fib_trie_table(u32 id)
Robert Olsson19baf832005-06-21 12:43:18 -07001985{
1986 struct fib_table *tb;
1987 struct trie *t;
1988
Robert Olsson19baf832005-06-21 12:43:18 -07001989 tb = kmalloc(sizeof(struct fib_table) + sizeof(struct trie),
1990 GFP_KERNEL);
1991 if (tb == NULL)
1992 return NULL;
1993
1994 tb->tb_id = id;
Denis V. Lunev971b8932007-12-08 00:32:23 -08001995 tb->tb_default = -1;
David S. Miller21d8c492011-04-14 14:49:37 -07001996 tb->tb_num_default = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001997
1998 t = (struct trie *) tb->tb_data;
Stephen Hemmingerc28a1cf2008-01-12 20:49:13 -08001999 memset(t, 0, sizeof(*t));
Robert Olsson19baf832005-06-21 12:43:18 -07002000
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07002001 if (id == RT_TABLE_LOCAL)
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002002 pr_info("IPv4 FIB: Using LC-trie version %s\n", VERSION);
Robert Olsson19baf832005-06-21 12:43:18 -07002003
2004 return tb;
2005}
2006
Robert Olsson19baf832005-06-21 12:43:18 -07002007#ifdef CONFIG_PROC_FS
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002008/* Depth first Trie walk iterator */
2009struct fib_trie_iter {
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002010 struct seq_net_private p;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002011 struct fib_table *tb;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002012 struct tnode *tnode;
Eric Dumazeta034ee32010-09-09 23:32:28 +00002013 unsigned int index;
2014 unsigned int depth;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002015};
Robert Olsson19baf832005-06-21 12:43:18 -07002016
David S. Millerb299e4f2011-02-02 20:48:10 -08002017static struct rt_trie_node *fib_trie_get_next(struct fib_trie_iter *iter)
Robert Olsson19baf832005-06-21 12:43:18 -07002018{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002019 struct tnode *tn = iter->tnode;
Eric Dumazeta034ee32010-09-09 23:32:28 +00002020 unsigned int cindex = iter->index;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002021 struct tnode *p;
2022
Eric W. Biederman6640e692007-01-24 14:42:04 -08002023 /* A single entry routing table */
2024 if (!tn)
2025 return NULL;
2026
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002027 pr_debug("get_next iter={node=%p index=%d depth=%d}\n",
2028 iter->tnode, iter->index, iter->depth);
2029rescan:
2030 while (cindex < (1<<tn->bits)) {
David S. Millerb299e4f2011-02-02 20:48:10 -08002031 struct rt_trie_node *n = tnode_get_child_rcu(tn, cindex);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002032
2033 if (n) {
2034 if (IS_LEAF(n)) {
2035 iter->tnode = tn;
2036 iter->index = cindex + 1;
2037 } else {
2038 /* push down one level */
2039 iter->tnode = (struct tnode *) n;
2040 iter->index = 0;
2041 ++iter->depth;
2042 }
2043 return n;
2044 }
2045
2046 ++cindex;
2047 }
2048
2049 /* Current node exhausted, pop back up */
David S. Millerb299e4f2011-02-02 20:48:10 -08002050 p = node_parent_rcu((struct rt_trie_node *)tn);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002051 if (p) {
2052 cindex = tkey_extract_bits(tn->key, p->pos, p->bits)+1;
2053 tn = p;
2054 --iter->depth;
2055 goto rescan;
2056 }
2057
2058 /* got root? */
Robert Olsson19baf832005-06-21 12:43:18 -07002059 return NULL;
2060}
2061
David S. Millerb299e4f2011-02-02 20:48:10 -08002062static struct rt_trie_node *fib_trie_get_first(struct fib_trie_iter *iter,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002063 struct trie *t)
Robert Olsson19baf832005-06-21 12:43:18 -07002064{
David S. Millerb299e4f2011-02-02 20:48:10 -08002065 struct rt_trie_node *n;
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08002066
Stephen Hemminger132adf52007-03-08 20:44:43 -08002067 if (!t)
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08002068 return NULL;
2069
2070 n = rcu_dereference(t->trie);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002071 if (!n)
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08002072 return NULL;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002073
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002074 if (IS_TNODE(n)) {
2075 iter->tnode = (struct tnode *) n;
2076 iter->index = 0;
2077 iter->depth = 1;
2078 } else {
2079 iter->tnode = NULL;
2080 iter->index = 0;
2081 iter->depth = 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002082 }
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002083
2084 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07002085}
2086
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002087static void trie_collect_stats(struct trie *t, struct trie_stat *s)
Robert Olsson19baf832005-06-21 12:43:18 -07002088{
David S. Millerb299e4f2011-02-02 20:48:10 -08002089 struct rt_trie_node *n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002090 struct fib_trie_iter iter;
Robert Olsson19baf832005-06-21 12:43:18 -07002091
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002092 memset(s, 0, sizeof(*s));
Robert Olsson19baf832005-06-21 12:43:18 -07002093
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002094 rcu_read_lock();
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002095 for (n = fib_trie_get_first(&iter, t); n; n = fib_trie_get_next(&iter)) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002096 if (IS_LEAF(n)) {
Stephen Hemminger93672292008-01-22 21:54:05 -08002097 struct leaf *l = (struct leaf *)n;
2098 struct leaf_info *li;
2099 struct hlist_node *tmp;
2100
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002101 s->leaves++;
2102 s->totdepth += iter.depth;
2103 if (iter.depth > s->maxdepth)
2104 s->maxdepth = iter.depth;
Stephen Hemminger93672292008-01-22 21:54:05 -08002105
2106 hlist_for_each_entry_rcu(li, tmp, &l->list, hlist)
2107 ++s->prefixes;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002108 } else {
2109 const struct tnode *tn = (const struct tnode *) n;
2110 int i;
Robert Olsson19baf832005-06-21 12:43:18 -07002111
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002112 s->tnodes++;
Stephen Hemminger132adf52007-03-08 20:44:43 -08002113 if (tn->bits < MAX_STAT_DEPTH)
Robert Olsson06ef9212006-03-20 21:35:01 -08002114 s->nodesizes[tn->bits]++;
2115
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002116 for (i = 0; i < (1<<tn->bits); i++)
2117 if (!tn->child[i])
2118 s->nullpointers++;
2119 }
2120 }
2121 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07002122}
2123
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07002124/*
Robert Olsson19baf832005-06-21 12:43:18 -07002125 * This outputs /proc/net/fib_triestats
Robert Olsson19baf832005-06-21 12:43:18 -07002126 */
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002127static void trie_show_stats(struct seq_file *seq, struct trie_stat *stat)
Robert Olsson19baf832005-06-21 12:43:18 -07002128{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002129 unsigned int i, max, pointers, bytes, avdepth;
Robert Olsson19baf832005-06-21 12:43:18 -07002130
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002131 if (stat->leaves)
2132 avdepth = stat->totdepth*100 / stat->leaves;
2133 else
2134 avdepth = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07002135
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002136 seq_printf(seq, "\tAver depth: %u.%02d\n",
2137 avdepth / 100, avdepth % 100);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002138 seq_printf(seq, "\tMax depth: %u\n", stat->maxdepth);
Robert Olsson19baf832005-06-21 12:43:18 -07002139
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002140 seq_printf(seq, "\tLeaves: %u\n", stat->leaves);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002141 bytes = sizeof(struct leaf) * stat->leaves;
Stephen Hemminger93672292008-01-22 21:54:05 -08002142
2143 seq_printf(seq, "\tPrefixes: %u\n", stat->prefixes);
2144 bytes += sizeof(struct leaf_info) * stat->prefixes;
2145
Stephen Hemminger187b5182008-01-12 20:55:55 -08002146 seq_printf(seq, "\tInternal nodes: %u\n\t", stat->tnodes);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002147 bytes += sizeof(struct tnode) * stat->tnodes;
Robert Olsson19baf832005-06-21 12:43:18 -07002148
Robert Olsson06ef9212006-03-20 21:35:01 -08002149 max = MAX_STAT_DEPTH;
2150 while (max > 0 && stat->nodesizes[max-1] == 0)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002151 max--;
Robert Olsson19baf832005-06-21 12:43:18 -07002152
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002153 pointers = 0;
2154 for (i = 1; i <= max; i++)
2155 if (stat->nodesizes[i] != 0) {
Stephen Hemminger187b5182008-01-12 20:55:55 -08002156 seq_printf(seq, " %u: %u", i, stat->nodesizes[i]);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002157 pointers += (1<<i) * stat->nodesizes[i];
2158 }
2159 seq_putc(seq, '\n');
Stephen Hemminger187b5182008-01-12 20:55:55 -08002160 seq_printf(seq, "\tPointers: %u\n", pointers);
Robert Olsson19baf832005-06-21 12:43:18 -07002161
David S. Millerb299e4f2011-02-02 20:48:10 -08002162 bytes += sizeof(struct rt_trie_node *) * pointers;
Stephen Hemminger187b5182008-01-12 20:55:55 -08002163 seq_printf(seq, "Null ptrs: %u\n", stat->nullpointers);
2164 seq_printf(seq, "Total size: %u kB\n", (bytes + 1023) / 1024);
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002165}
Robert Olsson19baf832005-06-21 12:43:18 -07002166
2167#ifdef CONFIG_IP_FIB_TRIE_STATS
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002168static void trie_show_usage(struct seq_file *seq,
2169 const struct trie_use_stats *stats)
2170{
2171 seq_printf(seq, "\nCounters:\n---------\n");
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002172 seq_printf(seq, "gets = %u\n", stats->gets);
2173 seq_printf(seq, "backtracks = %u\n", stats->backtrack);
2174 seq_printf(seq, "semantic match passed = %u\n",
2175 stats->semantic_match_passed);
2176 seq_printf(seq, "semantic match miss = %u\n",
2177 stats->semantic_match_miss);
2178 seq_printf(seq, "null node hit= %u\n", stats->null_node_hit);
2179 seq_printf(seq, "skipped node resize = %u\n\n",
2180 stats->resize_node_skipped);
Robert Olsson19baf832005-06-21 12:43:18 -07002181}
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002182#endif /* CONFIG_IP_FIB_TRIE_STATS */
2183
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002184static void fib_table_print(struct seq_file *seq, struct fib_table *tb)
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002185{
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002186 if (tb->tb_id == RT_TABLE_LOCAL)
2187 seq_puts(seq, "Local:\n");
2188 else if (tb->tb_id == RT_TABLE_MAIN)
2189 seq_puts(seq, "Main:\n");
2190 else
2191 seq_printf(seq, "Id %d:\n", tb->tb_id);
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002192}
Robert Olsson19baf832005-06-21 12:43:18 -07002193
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002194
Robert Olsson19baf832005-06-21 12:43:18 -07002195static int fib_triestat_seq_show(struct seq_file *seq, void *v)
2196{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002197 struct net *net = (struct net *)seq->private;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002198 unsigned int h;
Eric W. Biederman877a9bf2007-12-07 00:47:47 -08002199
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002200 seq_printf(seq,
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002201 "Basic info: size of leaf:"
2202 " %Zd bytes, size of tnode: %Zd bytes.\n",
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002203 sizeof(struct leaf), sizeof(struct tnode));
Olof Johansson91b9a272005-08-09 20:24:39 -07002204
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002205 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2206 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
2207 struct hlist_node *node;
2208 struct fib_table *tb;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002209
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002210 hlist_for_each_entry_rcu(tb, node, head, tb_hlist) {
2211 struct trie *t = (struct trie *) tb->tb_data;
2212 struct trie_stat stat;
2213
2214 if (!t)
2215 continue;
2216
2217 fib_table_print(seq, tb);
2218
2219 trie_collect_stats(t, &stat);
2220 trie_show_stats(seq, &stat);
2221#ifdef CONFIG_IP_FIB_TRIE_STATS
2222 trie_show_usage(seq, &t->stats);
2223#endif
2224 }
2225 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002226
Robert Olsson19baf832005-06-21 12:43:18 -07002227 return 0;
2228}
2229
Robert Olsson19baf832005-06-21 12:43:18 -07002230static int fib_triestat_seq_open(struct inode *inode, struct file *file)
2231{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002232 return single_open_net(inode, file, fib_triestat_seq_show);
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002233}
2234
Arjan van de Ven9a321442007-02-12 00:55:35 -08002235static const struct file_operations fib_triestat_fops = {
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07002236 .owner = THIS_MODULE,
2237 .open = fib_triestat_seq_open,
2238 .read = seq_read,
2239 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002240 .release = single_release_net,
Robert Olsson19baf832005-06-21 12:43:18 -07002241};
2242
David S. Millerb299e4f2011-02-02 20:48:10 -08002243static struct rt_trie_node *fib_trie_get_idx(struct seq_file *seq, loff_t pos)
Robert Olsson19baf832005-06-21 12:43:18 -07002244{
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002245 struct fib_trie_iter *iter = seq->private;
2246 struct net *net = seq_file_net(seq);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002247 loff_t idx = 0;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002248 unsigned int h;
Robert Olsson19baf832005-06-21 12:43:18 -07002249
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002250 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2251 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
2252 struct hlist_node *node;
2253 struct fib_table *tb;
2254
2255 hlist_for_each_entry_rcu(tb, node, head, tb_hlist) {
David S. Millerb299e4f2011-02-02 20:48:10 -08002256 struct rt_trie_node *n;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002257
2258 for (n = fib_trie_get_first(iter,
2259 (struct trie *) tb->tb_data);
2260 n; n = fib_trie_get_next(iter))
2261 if (pos == idx++) {
2262 iter->tb = tb;
2263 return n;
2264 }
2265 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002266 }
Robert Olsson19baf832005-06-21 12:43:18 -07002267
Robert Olsson19baf832005-06-21 12:43:18 -07002268 return NULL;
2269}
2270
2271static void *fib_trie_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08002272 __acquires(RCU)
Robert Olsson19baf832005-06-21 12:43:18 -07002273{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002274 rcu_read_lock();
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002275 return fib_trie_get_idx(seq, *pos);
Robert Olsson19baf832005-06-21 12:43:18 -07002276}
2277
2278static void *fib_trie_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2279{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002280 struct fib_trie_iter *iter = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002281 struct net *net = seq_file_net(seq);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002282 struct fib_table *tb = iter->tb;
2283 struct hlist_node *tb_node;
2284 unsigned int h;
David S. Millerb299e4f2011-02-02 20:48:10 -08002285 struct rt_trie_node *n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002286
Robert Olsson19baf832005-06-21 12:43:18 -07002287 ++*pos;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002288 /* next node in same table */
2289 n = fib_trie_get_next(iter);
2290 if (n)
2291 return n;
Olof Johansson91b9a272005-08-09 20:24:39 -07002292
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002293 /* walk rest of this hash chain */
2294 h = tb->tb_id & (FIB_TABLE_HASHSZ - 1);
Eric Dumazet0a5c0472011-03-31 01:51:35 -07002295 while ((tb_node = rcu_dereference(hlist_next_rcu(&tb->tb_hlist)))) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002296 tb = hlist_entry(tb_node, struct fib_table, tb_hlist);
2297 n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
2298 if (n)
2299 goto found;
2300 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002301
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002302 /* new hash chain */
2303 while (++h < FIB_TABLE_HASHSZ) {
2304 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
2305 hlist_for_each_entry_rcu(tb, tb_node, head, tb_hlist) {
2306 n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
2307 if (n)
2308 goto found;
2309 }
2310 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002311 return NULL;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002312
2313found:
2314 iter->tb = tb;
2315 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07002316}
2317
2318static void fib_trie_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08002319 __releases(RCU)
Robert Olsson19baf832005-06-21 12:43:18 -07002320{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002321 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07002322}
2323
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002324static void seq_indent(struct seq_file *seq, int n)
2325{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002326 while (n-- > 0)
2327 seq_puts(seq, " ");
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002328}
Robert Olsson19baf832005-06-21 12:43:18 -07002329
Eric Dumazet28d36e32008-01-14 23:09:56 -08002330static inline const char *rtn_scope(char *buf, size_t len, enum rt_scope_t s)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002331{
Stephen Hemminger132adf52007-03-08 20:44:43 -08002332 switch (s) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002333 case RT_SCOPE_UNIVERSE: return "universe";
2334 case RT_SCOPE_SITE: return "site";
2335 case RT_SCOPE_LINK: return "link";
2336 case RT_SCOPE_HOST: return "host";
2337 case RT_SCOPE_NOWHERE: return "nowhere";
2338 default:
Eric Dumazet28d36e32008-01-14 23:09:56 -08002339 snprintf(buf, len, "scope=%d", s);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002340 return buf;
2341 }
2342}
2343
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -07002344static const char *const rtn_type_names[__RTN_MAX] = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002345 [RTN_UNSPEC] = "UNSPEC",
2346 [RTN_UNICAST] = "UNICAST",
2347 [RTN_LOCAL] = "LOCAL",
2348 [RTN_BROADCAST] = "BROADCAST",
2349 [RTN_ANYCAST] = "ANYCAST",
2350 [RTN_MULTICAST] = "MULTICAST",
2351 [RTN_BLACKHOLE] = "BLACKHOLE",
2352 [RTN_UNREACHABLE] = "UNREACHABLE",
2353 [RTN_PROHIBIT] = "PROHIBIT",
2354 [RTN_THROW] = "THROW",
2355 [RTN_NAT] = "NAT",
2356 [RTN_XRESOLVE] = "XRESOLVE",
2357};
2358
Eric Dumazeta034ee32010-09-09 23:32:28 +00002359static inline const char *rtn_type(char *buf, size_t len, unsigned int t)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002360{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002361 if (t < __RTN_MAX && rtn_type_names[t])
2362 return rtn_type_names[t];
Eric Dumazet28d36e32008-01-14 23:09:56 -08002363 snprintf(buf, len, "type %u", t);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002364 return buf;
2365}
2366
2367/* Pretty print the trie */
Robert Olsson19baf832005-06-21 12:43:18 -07002368static int fib_trie_seq_show(struct seq_file *seq, void *v)
2369{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002370 const struct fib_trie_iter *iter = seq->private;
David S. Millerb299e4f2011-02-02 20:48:10 -08002371 struct rt_trie_node *n = v;
Robert Olsson19baf832005-06-21 12:43:18 -07002372
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002373 if (!node_parent_rcu(n))
2374 fib_table_print(seq, iter->tb);
Robert Olsson095b8502007-01-26 19:06:01 -08002375
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002376 if (IS_TNODE(n)) {
2377 struct tnode *tn = (struct tnode *) n;
Stephen Hemmingerab66b4a2007-08-10 15:22:58 -07002378 __be32 prf = htonl(mask_pfx(tn->key, tn->pos));
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002379
Robert Olsson1d25cd62005-09-19 15:29:52 -07002380 seq_indent(seq, iter->depth-1);
Harvey Harrison673d57e2008-10-31 00:53:57 -07002381 seq_printf(seq, " +-- %pI4/%d %d %d %d\n",
2382 &prf, tn->pos, tn->bits, tn->full_children,
Robert Olsson1d25cd62005-09-19 15:29:52 -07002383 tn->empty_children);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002384
Olof Johansson91b9a272005-08-09 20:24:39 -07002385 } else {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002386 struct leaf *l = (struct leaf *) n;
Stephen Hemminger13280422008-01-22 21:54:37 -08002387 struct leaf_info *li;
2388 struct hlist_node *node;
Al Viro32ab5f82006-09-26 22:21:45 -07002389 __be32 val = htonl(l->key);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002390
2391 seq_indent(seq, iter->depth);
Harvey Harrison673d57e2008-10-31 00:53:57 -07002392 seq_printf(seq, " |-- %pI4\n", &val);
Eric Dumazet28d36e32008-01-14 23:09:56 -08002393
Stephen Hemminger13280422008-01-22 21:54:37 -08002394 hlist_for_each_entry_rcu(li, node, &l->list, hlist) {
2395 struct fib_alias *fa;
Eric Dumazet28d36e32008-01-14 23:09:56 -08002396
Stephen Hemminger13280422008-01-22 21:54:37 -08002397 list_for_each_entry_rcu(fa, &li->falh, fa_list) {
2398 char buf1[32], buf2[32];
Eric Dumazet28d36e32008-01-14 23:09:56 -08002399
Stephen Hemminger13280422008-01-22 21:54:37 -08002400 seq_indent(seq, iter->depth+1);
2401 seq_printf(seq, " /%d %s %s", li->plen,
2402 rtn_scope(buf1, sizeof(buf1),
David S. Miller37e826c2011-03-24 18:06:47 -07002403 fa->fa_info->fib_scope),
Stephen Hemminger13280422008-01-22 21:54:37 -08002404 rtn_type(buf2, sizeof(buf2),
2405 fa->fa_type));
2406 if (fa->fa_tos)
Denis V. Lunevb9c4d822008-02-05 02:58:45 -08002407 seq_printf(seq, " tos=%d", fa->fa_tos);
Stephen Hemminger13280422008-01-22 21:54:37 -08002408 seq_putc(seq, '\n');
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002409 }
2410 }
Robert Olsson19baf832005-06-21 12:43:18 -07002411 }
2412
2413 return 0;
2414}
2415
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002416static const struct seq_operations fib_trie_seq_ops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002417 .start = fib_trie_seq_start,
2418 .next = fib_trie_seq_next,
2419 .stop = fib_trie_seq_stop,
2420 .show = fib_trie_seq_show,
Robert Olsson19baf832005-06-21 12:43:18 -07002421};
2422
2423static int fib_trie_seq_open(struct inode *inode, struct file *file)
2424{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002425 return seq_open_net(inode, file, &fib_trie_seq_ops,
2426 sizeof(struct fib_trie_iter));
Robert Olsson19baf832005-06-21 12:43:18 -07002427}
2428
Arjan van de Ven9a321442007-02-12 00:55:35 -08002429static const struct file_operations fib_trie_fops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002430 .owner = THIS_MODULE,
2431 .open = fib_trie_seq_open,
2432 .read = seq_read,
2433 .llseek = seq_lseek,
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002434 .release = seq_release_net,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002435};
2436
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002437struct fib_route_iter {
2438 struct seq_net_private p;
2439 struct trie *main_trie;
2440 loff_t pos;
2441 t_key key;
2442};
2443
2444static struct leaf *fib_route_get_idx(struct fib_route_iter *iter, loff_t pos)
2445{
2446 struct leaf *l = NULL;
2447 struct trie *t = iter->main_trie;
2448
2449 /* use cache location of last found key */
2450 if (iter->pos > 0 && pos >= iter->pos && (l = fib_find_node(t, iter->key)))
2451 pos -= iter->pos;
2452 else {
2453 iter->pos = 0;
2454 l = trie_firstleaf(t);
2455 }
2456
2457 while (l && pos-- > 0) {
2458 iter->pos++;
2459 l = trie_nextleaf(l);
2460 }
2461
2462 if (l)
2463 iter->key = pos; /* remember it */
2464 else
2465 iter->pos = 0; /* forget it */
2466
2467 return l;
2468}
2469
2470static void *fib_route_seq_start(struct seq_file *seq, loff_t *pos)
2471 __acquires(RCU)
2472{
2473 struct fib_route_iter *iter = seq->private;
2474 struct fib_table *tb;
2475
2476 rcu_read_lock();
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002477 tb = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN);
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002478 if (!tb)
2479 return NULL;
2480
2481 iter->main_trie = (struct trie *) tb->tb_data;
2482 if (*pos == 0)
2483 return SEQ_START_TOKEN;
2484 else
2485 return fib_route_get_idx(iter, *pos - 1);
2486}
2487
2488static void *fib_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2489{
2490 struct fib_route_iter *iter = seq->private;
2491 struct leaf *l = v;
2492
2493 ++*pos;
2494 if (v == SEQ_START_TOKEN) {
2495 iter->pos = 0;
2496 l = trie_firstleaf(iter->main_trie);
2497 } else {
2498 iter->pos++;
2499 l = trie_nextleaf(l);
2500 }
2501
2502 if (l)
2503 iter->key = l->key;
2504 else
2505 iter->pos = 0;
2506 return l;
2507}
2508
2509static void fib_route_seq_stop(struct seq_file *seq, void *v)
2510 __releases(RCU)
2511{
2512 rcu_read_unlock();
2513}
2514
Eric Dumazeta034ee32010-09-09 23:32:28 +00002515static unsigned int fib_flag_trans(int type, __be32 mask, const struct fib_info *fi)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002516{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002517 unsigned int flags = 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002518
Eric Dumazeta034ee32010-09-09 23:32:28 +00002519 if (type == RTN_UNREACHABLE || type == RTN_PROHIBIT)
2520 flags = RTF_REJECT;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002521 if (fi && fi->fib_nh->nh_gw)
2522 flags |= RTF_GATEWAY;
Al Viro32ab5f82006-09-26 22:21:45 -07002523 if (mask == htonl(0xFFFFFFFF))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002524 flags |= RTF_HOST;
2525 flags |= RTF_UP;
2526 return flags;
2527}
2528
2529/*
2530 * This outputs /proc/net/route.
2531 * The format of the file is not supposed to be changed
Eric Dumazeta034ee32010-09-09 23:32:28 +00002532 * and needs to be same as fib_hash output to avoid breaking
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002533 * legacy utilities
2534 */
2535static int fib_route_seq_show(struct seq_file *seq, void *v)
2536{
2537 struct leaf *l = v;
Stephen Hemminger13280422008-01-22 21:54:37 -08002538 struct leaf_info *li;
2539 struct hlist_node *node;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002540
2541 if (v == SEQ_START_TOKEN) {
2542 seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
2543 "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
2544 "\tWindow\tIRTT");
2545 return 0;
2546 }
2547
Stephen Hemminger13280422008-01-22 21:54:37 -08002548 hlist_for_each_entry_rcu(li, node, &l->list, hlist) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002549 struct fib_alias *fa;
Al Viro32ab5f82006-09-26 22:21:45 -07002550 __be32 mask, prefix;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002551
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002552 mask = inet_make_mask(li->plen);
2553 prefix = htonl(l->key);
2554
2555 list_for_each_entry_rcu(fa, &li->falh, fa_list) {
Herbert Xu1371e372005-10-15 09:42:39 +10002556 const struct fib_info *fi = fa->fa_info;
Eric Dumazeta034ee32010-09-09 23:32:28 +00002557 unsigned int flags = fib_flag_trans(fa->fa_type, mask, fi);
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002558 int len;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002559
2560 if (fa->fa_type == RTN_BROADCAST
2561 || fa->fa_type == RTN_MULTICAST)
2562 continue;
2563
2564 if (fi)
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002565 seq_printf(seq,
2566 "%s\t%08X\t%08X\t%04X\t%d\t%u\t"
2567 "%d\t%08X\t%d\t%u\t%u%n",
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002568 fi->fib_dev ? fi->fib_dev->name : "*",
2569 prefix,
2570 fi->fib_nh->nh_gw, flags, 0, 0,
2571 fi->fib_priority,
2572 mask,
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002573 (fi->fib_advmss ?
2574 fi->fib_advmss + 40 : 0),
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002575 fi->fib_window,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002576 fi->fib_rtt >> 3, &len);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002577 else
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002578 seq_printf(seq,
2579 "*\t%08X\t%08X\t%04X\t%d\t%u\t"
2580 "%d\t%08X\t%d\t%u\t%u%n",
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002581 prefix, 0, flags, 0, 0, 0,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002582 mask, 0, 0, 0, &len);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002583
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002584 seq_printf(seq, "%*s\n", 127 - len, "");
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002585 }
2586 }
2587
2588 return 0;
2589}
2590
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002591static const struct seq_operations fib_route_seq_ops = {
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002592 .start = fib_route_seq_start,
2593 .next = fib_route_seq_next,
2594 .stop = fib_route_seq_stop,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002595 .show = fib_route_seq_show,
2596};
2597
2598static int fib_route_seq_open(struct inode *inode, struct file *file)
2599{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002600 return seq_open_net(inode, file, &fib_route_seq_ops,
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002601 sizeof(struct fib_route_iter));
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002602}
2603
Arjan van de Ven9a321442007-02-12 00:55:35 -08002604static const struct file_operations fib_route_fops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002605 .owner = THIS_MODULE,
2606 .open = fib_route_seq_open,
2607 .read = seq_read,
2608 .llseek = seq_lseek,
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002609 .release = seq_release_net,
Robert Olsson19baf832005-06-21 12:43:18 -07002610};
2611
Denis V. Lunev61a02652008-01-10 03:21:09 -08002612int __net_init fib_proc_init(struct net *net)
Robert Olsson19baf832005-06-21 12:43:18 -07002613{
Denis V. Lunev61a02652008-01-10 03:21:09 -08002614 if (!proc_net_fops_create(net, "fib_trie", S_IRUGO, &fib_trie_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002615 goto out1;
2616
Denis V. Lunev61a02652008-01-10 03:21:09 -08002617 if (!proc_net_fops_create(net, "fib_triestat", S_IRUGO,
2618 &fib_triestat_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002619 goto out2;
2620
Denis V. Lunev61a02652008-01-10 03:21:09 -08002621 if (!proc_net_fops_create(net, "route", S_IRUGO, &fib_route_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002622 goto out3;
2623
Robert Olsson19baf832005-06-21 12:43:18 -07002624 return 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002625
2626out3:
Denis V. Lunev61a02652008-01-10 03:21:09 -08002627 proc_net_remove(net, "fib_triestat");
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002628out2:
Denis V. Lunev61a02652008-01-10 03:21:09 -08002629 proc_net_remove(net, "fib_trie");
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002630out1:
2631 return -ENOMEM;
Robert Olsson19baf832005-06-21 12:43:18 -07002632}
2633
Denis V. Lunev61a02652008-01-10 03:21:09 -08002634void __net_exit fib_proc_exit(struct net *net)
Robert Olsson19baf832005-06-21 12:43:18 -07002635{
Denis V. Lunev61a02652008-01-10 03:21:09 -08002636 proc_net_remove(net, "fib_trie");
2637 proc_net_remove(net, "fib_triestat");
2638 proc_net_remove(net, "route");
Robert Olsson19baf832005-06-21 12:43:18 -07002639}
2640
2641#endif /* CONFIG_PROC_FS */