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