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