blob: 08676c56efc333b1c70d990f5cc4ac9e65cb4bd3 [file] [log] [blame]
Robert Olsson19baf832005-06-21 12:43:18 -07001/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version
5 * 2 of the License, or (at your option) any later version.
6 *
7 * Robert Olsson <robert.olsson@its.uu.se> Uppsala Universitet
8 * & Swedish University of Agricultural Sciences.
9 *
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090010 * Jens Laas <jens.laas@data.slu.se> Swedish University of
Robert Olsson19baf832005-06-21 12:43:18 -070011 * Agricultural Sciences.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090012 *
Robert Olsson19baf832005-06-21 12:43:18 -070013 * Hans Liss <hans.liss@its.uu.se> Uppsala Universitet
14 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015 * This work is based on the LPC-trie which is originally described in:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090016 *
Robert Olsson19baf832005-06-21 12:43:18 -070017 * An experimental study of compression methods for dynamic tries
18 * Stefan Nilsson and Matti Tikkanen. Algorithmica, 33(1):19-33, 2002.
Justin P. Mattock631dd1a2010-10-18 11:03:14 +020019 * http://www.csc.kth.se/~snilsson/software/dyntrie2/
Robert Olsson19baf832005-06-21 12:43:18 -070020 *
21 *
22 * IP-address lookup using LC-tries. Stefan Nilsson and Gunnar Karlsson
23 * IEEE Journal on Selected Areas in Communications, 17(6):1083-1092, June 1999
24 *
Robert Olsson19baf832005-06-21 12:43:18 -070025 *
26 * Code from fib_hash has been reused which includes the following header:
27 *
28 *
29 * INET An implementation of the TCP/IP protocol suite for the LINUX
30 * operating system. INET is implemented using the BSD Socket
31 * interface as the means of communication with the user level.
32 *
33 * IPv4 FIB: lookup engine and maintenance routines.
34 *
35 *
36 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
37 *
38 * This program is free software; you can redistribute it and/or
39 * modify it under the terms of the GNU General Public License
40 * as published by the Free Software Foundation; either version
41 * 2 of the License, or (at your option) any later version.
Robert Olssonfd966252005-12-22 11:25:10 -080042 *
43 * Substantial contributions to this work comes from:
44 *
45 * David S. Miller, <davem@davemloft.net>
46 * Stephen Hemminger <shemminger@osdl.org>
47 * Paul E. McKenney <paulmck@us.ibm.com>
48 * Patrick McHardy <kaber@trash.net>
Robert Olsson19baf832005-06-21 12:43:18 -070049 */
50
Jens Låås80b71b82009-08-28 23:57:15 -070051#define VERSION "0.409"
Robert Olsson19baf832005-06-21 12:43:18 -070052
Robert Olsson19baf832005-06-21 12:43:18 -070053#include <asm/uaccess.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070054#include <linux/bitops.h>
Robert Olsson19baf832005-06-21 12:43:18 -070055#include <linux/types.h>
56#include <linux/kernel.h>
Robert Olsson19baf832005-06-21 12:43:18 -070057#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>
Stephen Hemmingercd8787a2006-01-03 14:38:34 -080064#include <linux/inetdevice.h>
Robert Olsson19baf832005-06-21 12:43:18 -070065#include <linux/netdevice.h>
66#include <linux/if_arp.h>
67#include <linux/proc_fs.h>
Robert Olsson2373ce12005-08-25 13:01:29 -070068#include <linux/rcupdate.h>
Robert Olsson19baf832005-06-21 12:43:18 -070069#include <linux/skbuff.h>
70#include <linux/netlink.h>
71#include <linux/init.h>
72#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090073#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040074#include <linux/export.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020075#include <net/net_namespace.h>
Robert Olsson19baf832005-06-21 12:43:18 -070076#include <net/ip.h>
77#include <net/protocol.h>
78#include <net/route.h>
79#include <net/tcp.h>
80#include <net/sock.h>
81#include <net/ip_fib.h>
82#include "fib_lookup.h"
83
Robert Olsson06ef9212006-03-20 21:35:01 -080084#define MAX_STAT_DEPTH 32
Robert Olsson19baf832005-06-21 12:43:18 -070085
Alexander Duyck95f60ea2015-01-22 15:51:26 -080086#define KEYLENGTH (8*sizeof(t_key))
87#define KEY_MAX ((t_key)~0)
Robert Olsson19baf832005-06-21 12:43:18 -070088
Robert Olsson19baf832005-06-21 12:43:18 -070089typedef unsigned int t_key;
90
Alexander Duyck64c9b6f2014-12-31 10:55:35 -080091#define IS_TNODE(n) ((n)->bits)
92#define IS_LEAF(n) (!(n)->bits)
Robert Olsson2373ce12005-08-25 13:01:29 -070093
Alexander Duycke9b44012014-12-31 10:56:12 -080094#define get_index(_key, _kv) (((_key) ^ (_kv)->key) >> (_kv)->pos)
Alexander Duyck9f9e6362014-12-31 10:55:54 -080095
Alexander Duyck64c9b6f2014-12-31 10:55:35 -080096struct tnode {
Alexander Duyck37fd30f2014-12-31 10:55:41 -080097 struct rcu_head rcu;
Alexander Duyck41b489f2015-03-04 15:02:33 -080098
99 t_key empty_children; /* KEYLENGTH bits needed */
100 t_key full_children; /* KEYLENGTH bits needed */
101 struct tnode __rcu *parent;
102
103 t_key key;
104 unsigned char pos; /* 2log(KEYLENGTH) bits needed */
105 unsigned char bits; /* 2log(KEYLENGTH) bits needed */
106 unsigned char slen;
Alexander Duyckadaf9812014-12-31 10:55:47 -0800107 union {
Alexander Duyck41b489f2015-03-04 15:02:33 -0800108 /* This list pointer if valid if (pos | bits) == 0 (LEAF) */
Alexander Duyck79e5ad22015-02-25 15:31:51 -0800109 struct hlist_head leaf;
Alexander Duyck41b489f2015-03-04 15:02:33 -0800110 /* This array is valid if (pos | bits) > 0 (TNODE) */
111 struct tnode __rcu *tnode[0];
Alexander Duyckadaf9812014-12-31 10:55:47 -0800112 };
Robert Olsson19baf832005-06-21 12:43:18 -0700113};
114
Alexander Duyck41b489f2015-03-04 15:02:33 -0800115#define TNODE_SIZE(n) offsetof(struct tnode, tnode[n])
116#define LEAF_SIZE TNODE_SIZE(1)
117
Robert Olsson19baf832005-06-21 12:43:18 -0700118#ifdef CONFIG_IP_FIB_TRIE_STATS
119struct trie_use_stats {
120 unsigned int gets;
121 unsigned int backtrack;
122 unsigned int semantic_match_passed;
123 unsigned int semantic_match_miss;
124 unsigned int null_node_hit;
Robert Olsson2f368952005-07-05 15:02:40 -0700125 unsigned int resize_node_skipped;
Robert Olsson19baf832005-06-21 12:43:18 -0700126};
127#endif
128
129struct trie_stat {
130 unsigned int totdepth;
131 unsigned int maxdepth;
132 unsigned int tnodes;
133 unsigned int leaves;
134 unsigned int nullpointers;
Stephen Hemminger93672292008-01-22 21:54:05 -0800135 unsigned int prefixes;
Robert Olsson06ef9212006-03-20 21:35:01 -0800136 unsigned int nodesizes[MAX_STAT_DEPTH];
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700137};
Robert Olsson19baf832005-06-21 12:43:18 -0700138
139struct trie {
Alexander Duyckadaf9812014-12-31 10:55:47 -0800140 struct tnode __rcu *trie;
Robert Olsson19baf832005-06-21 12:43:18 -0700141#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -0800142 struct trie_use_stats __percpu *stats;
Robert Olsson19baf832005-06-21 12:43:18 -0700143#endif
Robert Olsson19baf832005-06-21 12:43:18 -0700144};
145
Alexander Duyckff181ed2014-12-31 10:56:43 -0800146static void resize(struct trie *t, struct tnode *tn);
Jarek Poplawskic3059472009-07-14 08:33:08 +0000147static size_t tnode_free_size;
148
149/*
150 * synchronize_rcu after call_rcu for that many pages; it should be especially
151 * useful before resizing the root node with PREEMPT_NONE configs; the value was
152 * obtained experimentally, aiming to avoid visible slowdown.
153 */
154static const int sync_pages = 128;
Robert Olsson19baf832005-06-21 12:43:18 -0700155
Christoph Lametere18b8902006-12-06 20:33:20 -0800156static struct kmem_cache *fn_alias_kmem __read_mostly;
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -0800157static struct kmem_cache *trie_leaf_kmem __read_mostly;
Robert Olsson19baf832005-06-21 12:43:18 -0700158
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800159/* caller must hold RTNL */
160#define node_parent(n) rtnl_dereference((n)->parent)
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700161
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800162/* caller must hold RCU read lock or RTNL */
163#define node_parent_rcu(n) rcu_dereference_rtnl((n)->parent)
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700164
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800165/* wrapper for rcu_assign_pointer */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800166static inline void node_set_parent(struct tnode *n, struct tnode *tp)
Stephen Hemminger06801912007-08-10 15:22:13 -0700167{
Alexander Duyckadaf9812014-12-31 10:55:47 -0800168 if (n)
169 rcu_assign_pointer(n->parent, tp);
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800170}
171
172#define NODE_INIT_PARENT(n, p) RCU_INIT_POINTER((n)->parent, p)
173
174/* This provides us with the number of children in this node, in the case of a
175 * leaf this will return 0 meaning none of the children are accessible.
176 */
Alexander Duyck98293e82014-12-31 10:56:18 -0800177static inline unsigned long tnode_child_length(const struct tnode *tn)
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800178{
179 return (1ul << tn->bits) & ~(1ul);
Stephen Hemminger06801912007-08-10 15:22:13 -0700180}
Robert Olsson2373ce12005-08-25 13:01:29 -0700181
Alexander Duyck98293e82014-12-31 10:56:18 -0800182/* caller must hold RTNL */
183static inline struct tnode *tnode_get_child(const struct tnode *tn,
184 unsigned long i)
Robert Olsson19baf832005-06-21 12:43:18 -0700185{
Alexander Duyck41b489f2015-03-04 15:02:33 -0800186 return rtnl_dereference(tn->tnode[i]);
Eric Dumazetb59cfbf2008-01-18 03:31:36 -0800187}
188
Alexander Duyck98293e82014-12-31 10:56:18 -0800189/* caller must hold RCU read lock or RTNL */
190static inline struct tnode *tnode_get_child_rcu(const struct tnode *tn,
191 unsigned long i)
Eric Dumazetb59cfbf2008-01-18 03:31:36 -0800192{
Alexander Duyck41b489f2015-03-04 15:02:33 -0800193 return rcu_dereference_rtnl(tn->tnode[i]);
Robert Olsson19baf832005-06-21 12:43:18 -0700194}
195
Alexander Duycka7e53532015-03-04 15:02:44 -0800196static inline struct fib_table *trie_get_table(struct trie *t)
197{
198 unsigned long *tb_data = (unsigned long *)t;
199
200 return container_of(tb_data, struct fib_table, tb_data[0]);
201}
202
Alexander Duycke9b44012014-12-31 10:56:12 -0800203/* 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 */
Robert Olsson19baf832005-06-21 12:43:18 -0700261
Denis V. Lunevf5026fa2007-12-13 09:47:57 -0800262static const int halve_threshold = 25;
263static const int inflate_threshold = 50;
Jarek Poplawski345aa032009-07-07 19:39:16 -0700264static const int halve_threshold_root = 15;
Jens Låås80b71b82009-08-28 23:57:15 -0700265static const int inflate_threshold_root = 30;
Robert Olsson2373ce12005-08-25 13:01:29 -0700266
267static void __alias_free_mem(struct rcu_head *head)
268{
269 struct fib_alias *fa = container_of(head, struct fib_alias, rcu);
270 kmem_cache_free(fn_alias_kmem, fa);
271}
272
273static inline void alias_free_mem_rcu(struct fib_alias *fa)
274{
275 call_rcu(&fa->rcu, __alias_free_mem);
276}
277
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800278#define TNODE_KMALLOC_MAX \
Alexander Duyck41b489f2015-03-04 15:02:33 -0800279 ilog2((PAGE_SIZE - TNODE_SIZE(0)) / sizeof(struct tnode *))
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800280
281static void __node_free_rcu(struct rcu_head *head)
Robert Olsson2373ce12005-08-25 13:01:29 -0700282{
Alexander Duyckadaf9812014-12-31 10:55:47 -0800283 struct tnode *n = container_of(head, struct tnode, rcu);
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800284
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);
Robert Olsson2373ce12005-08-25 13:01:29 -0700291}
292
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800293#define node_free(n) call_rcu(&n->rcu, __node_free_rcu)
Stephen Hemminger387a5482008-04-10 03:47:34 -0700294
Eric Dumazet8d965442008-01-13 22:31:44 -0800295static struct tnode *tnode_alloc(size_t size)
Robert Olsson2373ce12005-08-25 13:01:29 -0700296{
Robert Olsson2373ce12005-08-25 13:01:29 -0700297 if (size <= PAGE_SIZE)
Eric Dumazet8d965442008-01-13 22:31:44 -0800298 return kzalloc(size, GFP_KERNEL);
Stephen Hemminger15be75c2008-04-10 02:56:38 -0700299 else
Eric Dumazet7a1c8e52010-11-20 07:46:35 +0000300 return vzalloc(size);
Stephen Hemminger15be75c2008-04-10 02:56:38 -0700301}
Robert Olsson2373ce12005-08-25 13:01:29 -0700302
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800303static inline void empty_child_inc(struct tnode *n)
304{
305 ++n->empty_children ? : ++n->full_children;
306}
307
308static inline void empty_child_dec(struct tnode *n)
309{
310 n->empty_children-- ? : n->full_children--;
311}
312
Alexander Duyckd5d64872015-03-04 15:02:18 -0800313static struct tnode *leaf_new(t_key key, struct fib_alias *fa)
Robert Olsson19baf832005-06-21 12:43:18 -0700314{
Alexander Duyckadaf9812014-12-31 10:55:47 -0800315 struct tnode *l = kmem_cache_alloc(trie_leaf_kmem, GFP_KERNEL);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700316 if (l) {
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800317 l->parent = NULL;
318 /* set key and pos to reflect full key value
319 * any trailing zeros in the key should be ignored
320 * as the nodes are searched
321 */
322 l->key = key;
Alexander Duyckd5d64872015-03-04 15:02:18 -0800323 l->slen = fa->fa_slen;
Alexander Duycke9b44012014-12-31 10:56:12 -0800324 l->pos = 0;
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800325 /* set bits to 0 indicating we are not a tnode */
326 l->bits = 0;
327
Alexander Duyckd5d64872015-03-04 15:02:18 -0800328 /* link leaf to fib alias */
Alexander Duyck79e5ad22015-02-25 15:31:51 -0800329 INIT_HLIST_HEAD(&l->leaf);
Alexander Duyckd5d64872015-03-04 15:02:18 -0800330 hlist_add_head(&fa->fa_list, &l->leaf);
Robert Olsson19baf832005-06-21 12:43:18 -0700331 }
332 return l;
333}
334
Stephen Hemmingera07f5f52008-01-22 21:53:36 -0800335static struct tnode *tnode_new(t_key key, int pos, int bits)
Robert Olsson19baf832005-06-21 12:43:18 -0700336{
Alexander Duyck41b489f2015-03-04 15:02:33 -0800337 size_t sz = TNODE_SIZE(1ul << bits);
Patrick McHardyf0e36f82005-07-05 14:44:55 -0700338 struct tnode *tn = tnode_alloc(sz);
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800339 unsigned int shift = pos + bits;
340
341 /* verify bits and pos their msb bits clear and values are valid */
342 BUG_ON(!bits || (shift > KEYLENGTH));
Robert Olsson19baf832005-06-21 12:43:18 -0700343
Olof Johansson91b9a272005-08-09 20:24:39 -0700344 if (tn) {
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800345 tn->parent = NULL;
Alexander Duyck5405afd2014-12-31 10:57:08 -0800346 tn->slen = pos;
Robert Olsson19baf832005-06-21 12:43:18 -0700347 tn->pos = pos;
348 tn->bits = bits;
Alexander Duycke9b44012014-12-31 10:56:12 -0800349 tn->key = (shift < KEYLENGTH) ? (key >> shift) << shift : 0;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800350 if (bits == KEYLENGTH)
351 tn->full_children = 1;
352 else
353 tn->empty_children = 1ul << bits;
Robert Olsson19baf832005-06-21 12:43:18 -0700354 }
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700355
Alexander Duyck41b489f2015-03-04 15:02:33 -0800356 pr_debug("AT %p s=%zu %zu\n", tn, TNODE_SIZE(0),
Alexander Duyckadaf9812014-12-31 10:55:47 -0800357 sizeof(struct tnode *) << bits);
Robert Olsson19baf832005-06-21 12:43:18 -0700358 return tn;
359}
360
Alexander Duycke9b44012014-12-31 10:56:12 -0800361/* Check whether a tnode 'n' is "full", i.e. it is an internal node
Robert Olsson19baf832005-06-21 12:43:18 -0700362 * and no bits are skipped. See discussion in dyntree paper p. 6
363 */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800364static inline int tnode_full(const struct tnode *tn, const struct tnode *n)
Robert Olsson19baf832005-06-21 12:43:18 -0700365{
Alexander Duycke9b44012014-12-31 10:56:12 -0800366 return n && ((n->pos + n->bits) == tn->pos) && IS_TNODE(n);
Robert Olsson19baf832005-06-21 12:43:18 -0700367}
368
Alexander Duyckff181ed2014-12-31 10:56:43 -0800369/* 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)
Robert Olsson19baf832005-06-21 12:43:18 -0700373{
Alexander Duyck21d1f112014-12-31 10:57:02 -0800374 struct tnode *chi = tnode_get_child(tn, i);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800375 int isfull, wasfull;
Robert Olsson19baf832005-06-21 12:43:18 -0700376
Alexander Duyck98293e82014-12-31 10:56:18 -0800377 BUG_ON(i >= tnode_child_length(tn));
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700378
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800379 /* update emptyChildren, overflow into fullChildren */
Robert Olsson19baf832005-06-21 12:43:18 -0700380 if (n == NULL && chi != NULL)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800381 empty_child_inc(tn);
382 if (n != NULL && chi == NULL)
383 empty_child_dec(tn);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700384
Robert Olsson19baf832005-06-21 12:43:18 -0700385 /* update fullChildren */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800386 wasfull = tnode_full(tn, chi);
Robert Olsson19baf832005-06-21 12:43:18 -0700387 isfull = tnode_full(tn, n);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800388
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700389 if (wasfull && !isfull)
Robert Olsson19baf832005-06-21 12:43:18 -0700390 tn->full_children--;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700391 else if (!wasfull && isfull)
Robert Olsson19baf832005-06-21 12:43:18 -0700392 tn->full_children++;
Olof Johansson91b9a272005-08-09 20:24:39 -0700393
Alexander Duyck5405afd2014-12-31 10:57:08 -0800394 if (n && (tn->slen < n->slen))
395 tn->slen = n->slen;
396
Alexander Duyck41b489f2015-03-04 15:02:33 -0800397 rcu_assign_pointer(tn->tnode[i], n);
Robert Olsson19baf832005-06-21 12:43:18 -0700398}
399
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800400static void update_children(struct tnode *tn)
401{
402 unsigned long i;
403
404 /* update all of the child parent pointers */
405 for (i = tnode_child_length(tn); i;) {
406 struct tnode *inode = tnode_get_child(tn, --i);
407
408 if (!inode)
409 continue;
410
411 /* Either update the children of a tnode that
412 * already belongs to us or update the child
413 * to point to ourselves.
414 */
415 if (node_parent(inode) == tn)
416 update_children(inode);
417 else
418 node_set_parent(inode, tn);
419 }
420}
421
422static inline void put_child_root(struct tnode *tp, struct trie *t,
423 t_key key, struct tnode *n)
Alexander Duyck836a0122014-12-31 10:56:06 -0800424{
425 if (tp)
426 put_child(tp, get_index(key, tp), n);
427 else
428 rcu_assign_pointer(t->trie, n);
429}
430
Alexander Duyckfc86a932014-12-31 10:56:49 -0800431static inline void tnode_free_init(struct tnode *tn)
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700432{
Alexander Duyckfc86a932014-12-31 10:56:49 -0800433 tn->rcu.next = NULL;
434}
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700435
Alexander Duyckfc86a932014-12-31 10:56:49 -0800436static inline void tnode_free_append(struct tnode *tn, struct tnode *n)
437{
438 n->rcu.next = tn->rcu.next;
439 tn->rcu.next = &n->rcu;
440}
441
442static void tnode_free(struct tnode *tn)
443{
444 struct callback_head *head = &tn->rcu;
445
446 while (head) {
447 head = head->next;
Alexander Duyck41b489f2015-03-04 15:02:33 -0800448 tnode_free_size += TNODE_SIZE(1ul << tn->bits);
Alexander Duyckfc86a932014-12-31 10:56:49 -0800449 node_free(tn);
450
451 tn = container_of(head, struct tnode, rcu);
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700452 }
Alexander Duyckfc86a932014-12-31 10:56:49 -0800453
454 if (tnode_free_size >= PAGE_SIZE * sync_pages) {
455 tnode_free_size = 0;
456 synchronize_rcu();
457 }
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700458}
459
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800460static void replace(struct trie *t, struct tnode *oldtnode, struct tnode *tn)
461{
462 struct tnode *tp = node_parent(oldtnode);
463 unsigned long i;
464
465 /* setup the parent pointer out of and back into this node */
466 NODE_INIT_PARENT(tn, tp);
467 put_child_root(tp, t, tn->key, tn);
468
469 /* update all of the child parent pointers */
470 update_children(tn);
471
472 /* all pointers should be clean so we are done */
473 tnode_free(oldtnode);
474
475 /* resize children now that oldtnode is freed */
476 for (i = tnode_child_length(tn); i;) {
477 struct tnode *inode = tnode_get_child(tn, --i);
478
479 /* resize child node */
480 if (tnode_full(tn, inode))
481 resize(t, inode);
482 }
483}
484
Alexander Duyckff181ed2014-12-31 10:56:43 -0800485static int inflate(struct trie *t, struct tnode *oldtnode)
Robert Olsson19baf832005-06-21 12:43:18 -0700486{
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800487 struct tnode *tn;
488 unsigned long i;
Alexander Duycke9b44012014-12-31 10:56:12 -0800489 t_key m;
Robert Olsson19baf832005-06-21 12:43:18 -0700490
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700491 pr_debug("In inflate\n");
Robert Olsson19baf832005-06-21 12:43:18 -0700492
Alexander Duycke9b44012014-12-31 10:56:12 -0800493 tn = tnode_new(oldtnode->key, oldtnode->pos - 1, oldtnode->bits + 1);
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700494 if (!tn)
Alexander Duyckff181ed2014-12-31 10:56:43 -0800495 return -ENOMEM;
Robert Olsson2f368952005-07-05 15:02:40 -0700496
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800497 /* prepare oldtnode to be freed */
498 tnode_free_init(oldtnode);
499
Alexander Duyck12c081a2014-12-31 10:56:55 -0800500 /* Assemble all of the pointers in our cluster, in this case that
501 * represents all of the pointers out of our allocated nodes that
502 * point to existing tnodes and the links between our allocated
503 * nodes.
Robert Olsson2f368952005-07-05 15:02:40 -0700504 */
Alexander Duyck12c081a2014-12-31 10:56:55 -0800505 for (i = tnode_child_length(oldtnode), m = 1u << tn->pos; i;) {
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800506 struct tnode *inode = tnode_get_child(oldtnode, --i);
507 struct tnode *node0, *node1;
508 unsigned long j, k;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700509
Robert Olsson19baf832005-06-21 12:43:18 -0700510 /* An empty child */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800511 if (inode == NULL)
Robert Olsson19baf832005-06-21 12:43:18 -0700512 continue;
513
514 /* A leaf or an internal node with skipped bits */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800515 if (!tnode_full(oldtnode, inode)) {
Alexander Duycke9b44012014-12-31 10:56:12 -0800516 put_child(tn, get_index(inode->key, tn), inode);
Robert Olsson19baf832005-06-21 12:43:18 -0700517 continue;
518 }
519
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800520 /* drop the node in the old tnode free list */
521 tnode_free_append(oldtnode, inode);
522
Robert Olsson19baf832005-06-21 12:43:18 -0700523 /* An internal node with two children */
Robert Olsson19baf832005-06-21 12:43:18 -0700524 if (inode->bits == 1) {
Alexander Duyck12c081a2014-12-31 10:56:55 -0800525 put_child(tn, 2 * i + 1, tnode_get_child(inode, 1));
526 put_child(tn, 2 * i, tnode_get_child(inode, 0));
Olof Johansson91b9a272005-08-09 20:24:39 -0700527 continue;
Robert Olsson19baf832005-06-21 12:43:18 -0700528 }
529
Olof Johansson91b9a272005-08-09 20:24:39 -0700530 /* We will replace this node 'inode' with two new
Alexander Duyck12c081a2014-12-31 10:56:55 -0800531 * ones, 'node0' and 'node1', each with half of the
Olof Johansson91b9a272005-08-09 20:24:39 -0700532 * original children. The two new nodes will have
533 * a position one bit further down the key and this
534 * means that the "significant" part of their keys
535 * (see the discussion near the top of this file)
536 * will differ by one bit, which will be "0" in
Alexander Duyck12c081a2014-12-31 10:56:55 -0800537 * node0's key and "1" in node1's key. Since we are
Olof Johansson91b9a272005-08-09 20:24:39 -0700538 * moving the key position by one step, the bit that
539 * we are moving away from - the bit at position
Alexander Duyck12c081a2014-12-31 10:56:55 -0800540 * (tn->pos) - is the one that will differ between
541 * node0 and node1. So... we synthesize that bit in the
542 * two new keys.
Olof Johansson91b9a272005-08-09 20:24:39 -0700543 */
Alexander Duyck12c081a2014-12-31 10:56:55 -0800544 node1 = tnode_new(inode->key | m, inode->pos, inode->bits - 1);
545 if (!node1)
546 goto nomem;
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800547 node0 = tnode_new(inode->key, inode->pos, inode->bits - 1);
Robert Olsson19baf832005-06-21 12:43:18 -0700548
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800549 tnode_free_append(tn, node1);
Alexander Duyck12c081a2014-12-31 10:56:55 -0800550 if (!node0)
551 goto nomem;
552 tnode_free_append(tn, node0);
Robert Olsson19baf832005-06-21 12:43:18 -0700553
Alexander Duyck12c081a2014-12-31 10:56:55 -0800554 /* populate child pointers in new nodes */
555 for (k = tnode_child_length(inode), j = k / 2; j;) {
556 put_child(node1, --j, tnode_get_child(inode, --k));
557 put_child(node0, j, tnode_get_child(inode, j));
558 put_child(node1, --j, tnode_get_child(inode, --k));
559 put_child(node0, j, tnode_get_child(inode, j));
Robert Olsson19baf832005-06-21 12:43:18 -0700560 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800561
Alexander Duyck12c081a2014-12-31 10:56:55 -0800562 /* link new nodes to parent */
563 NODE_INIT_PARENT(node1, tn);
564 NODE_INIT_PARENT(node0, tn);
Olof Johansson91b9a272005-08-09 20:24:39 -0700565
Alexander Duyck12c081a2014-12-31 10:56:55 -0800566 /* link parent to nodes */
567 put_child(tn, 2 * i + 1, node1);
568 put_child(tn, 2 * i, node0);
Robert Olsson19baf832005-06-21 12:43:18 -0700569 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800570
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800571 /* setup the parent pointers into and out of this node */
572 replace(t, oldtnode, tn);
Alexander Duyckfc86a932014-12-31 10:56:49 -0800573
Alexander Duyckff181ed2014-12-31 10:56:43 -0800574 return 0;
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700575nomem:
Alexander Duyckfc86a932014-12-31 10:56:49 -0800576 /* all pointers should be clean so we are done */
577 tnode_free(tn);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800578 return -ENOMEM;
Robert Olsson19baf832005-06-21 12:43:18 -0700579}
580
Alexander Duyckff181ed2014-12-31 10:56:43 -0800581static int halve(struct trie *t, struct tnode *oldtnode)
Robert Olsson19baf832005-06-21 12:43:18 -0700582{
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800583 struct tnode *tn;
Alexander Duyck12c081a2014-12-31 10:56:55 -0800584 unsigned long i;
Robert Olsson19baf832005-06-21 12:43:18 -0700585
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700586 pr_debug("In halve\n");
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700587
Alexander Duycke9b44012014-12-31 10:56:12 -0800588 tn = tnode_new(oldtnode->key, oldtnode->pos + 1, oldtnode->bits - 1);
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700589 if (!tn)
Alexander Duyckff181ed2014-12-31 10:56:43 -0800590 return -ENOMEM;
Robert Olsson2f368952005-07-05 15:02:40 -0700591
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800592 /* prepare oldtnode to be freed */
593 tnode_free_init(oldtnode);
594
Alexander Duyck12c081a2014-12-31 10:56:55 -0800595 /* Assemble all of the pointers in our cluster, in this case that
596 * represents all of the pointers out of our allocated nodes that
597 * point to existing tnodes and the links between our allocated
598 * nodes.
Robert Olsson2f368952005-07-05 15:02:40 -0700599 */
Alexander Duyck12c081a2014-12-31 10:56:55 -0800600 for (i = tnode_child_length(oldtnode); i;) {
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800601 struct tnode *node1 = tnode_get_child(oldtnode, --i);
602 struct tnode *node0 = tnode_get_child(oldtnode, --i);
603 struct tnode *inode;
Robert Olsson2f368952005-07-05 15:02:40 -0700604
Alexander Duyck12c081a2014-12-31 10:56:55 -0800605 /* At least one of the children is empty */
606 if (!node1 || !node0) {
607 put_child(tn, i / 2, node1 ? : node0);
608 continue;
Robert Olsson2f368952005-07-05 15:02:40 -0700609 }
Robert Olsson2f368952005-07-05 15:02:40 -0700610
Alexander Duyck12c081a2014-12-31 10:56:55 -0800611 /* Two nonempty children */
612 inode = tnode_new(node0->key, oldtnode->pos, 1);
613 if (!inode) {
614 tnode_free(tn);
615 return -ENOMEM;
616 }
617 tnode_free_append(tn, inode);
618
619 /* initialize pointers out of node */
620 put_child(inode, 1, node1);
621 put_child(inode, 0, node0);
622 NODE_INIT_PARENT(inode, tn);
623
624 /* link parent to node */
625 put_child(tn, i / 2, inode);
Robert Olsson2f368952005-07-05 15:02:40 -0700626 }
Robert Olsson19baf832005-06-21 12:43:18 -0700627
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800628 /* setup the parent pointers into and out of this node */
629 replace(t, oldtnode, tn);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800630
631 return 0;
Robert Olsson19baf832005-06-21 12:43:18 -0700632}
633
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800634static void collapse(struct trie *t, struct tnode *oldtnode)
635{
636 struct tnode *n, *tp;
637 unsigned long i;
638
639 /* scan the tnode looking for that one child that might still exist */
640 for (n = NULL, i = tnode_child_length(oldtnode); !n && i;)
641 n = tnode_get_child(oldtnode, --i);
642
643 /* compress one level */
644 tp = node_parent(oldtnode);
645 put_child_root(tp, t, oldtnode->key, n);
646 node_set_parent(n, tp);
647
648 /* drop dead node */
649 node_free(oldtnode);
650}
651
Alexander Duyck5405afd2014-12-31 10:57:08 -0800652static unsigned char update_suffix(struct tnode *tn)
653{
654 unsigned char slen = tn->pos;
655 unsigned long stride, i;
656
657 /* search though the list of children looking for nodes that might
658 * have a suffix greater than the one we currently have. This is
659 * why we start with a stride of 2 since a stride of 1 would
660 * represent the nodes with suffix length equal to tn->pos
661 */
662 for (i = 0, stride = 0x2ul ; i < tnode_child_length(tn); i += stride) {
663 struct tnode *n = tnode_get_child(tn, i);
664
665 if (!n || (n->slen <= slen))
666 continue;
667
668 /* update stride and slen based on new value */
669 stride <<= (n->slen - slen);
670 slen = n->slen;
671 i &= ~(stride - 1);
672
673 /* if slen covers all but the last bit we can stop here
674 * there will be nothing longer than that since only node
675 * 0 and 1 << (bits - 1) could have that as their suffix
676 * length.
677 */
678 if ((slen + 1) >= (tn->pos + tn->bits))
679 break;
680 }
681
682 tn->slen = slen;
683
684 return slen;
685}
686
Alexander Duyckf05a4812014-12-31 10:56:37 -0800687/* From "Implementing a dynamic compressed trie" by Stefan Nilsson of
688 * the Helsinki University of Technology and Matti Tikkanen of Nokia
689 * Telecommunications, page 6:
690 * "A node is doubled if the ratio of non-empty children to all
691 * children in the *doubled* node is at least 'high'."
692 *
693 * 'high' in this instance is the variable 'inflate_threshold'. It
694 * is expressed as a percentage, so we multiply it with
695 * tnode_child_length() and instead of multiplying by 2 (since the
696 * child array will be doubled by inflate()) and multiplying
697 * the left-hand side by 100 (to handle the percentage thing) we
698 * multiply the left-hand side by 50.
699 *
700 * The left-hand side may look a bit weird: tnode_child_length(tn)
701 * - tn->empty_children is of course the number of non-null children
702 * in the current node. tn->full_children is the number of "full"
703 * children, that is non-null tnodes with a skip value of 0.
704 * All of those will be doubled in the resulting inflated tnode, so
705 * we just count them one extra time here.
706 *
707 * A clearer way to write this would be:
708 *
709 * to_be_doubled = tn->full_children;
710 * not_to_be_doubled = tnode_child_length(tn) - tn->empty_children -
711 * tn->full_children;
712 *
713 * new_child_length = tnode_child_length(tn) * 2;
714 *
715 * new_fill_factor = 100 * (not_to_be_doubled + 2*to_be_doubled) /
716 * new_child_length;
717 * if (new_fill_factor >= inflate_threshold)
718 *
719 * ...and so on, tho it would mess up the while () loop.
720 *
721 * anyway,
722 * 100 * (not_to_be_doubled + 2*to_be_doubled) / new_child_length >=
723 * inflate_threshold
724 *
725 * avoid a division:
726 * 100 * (not_to_be_doubled + 2*to_be_doubled) >=
727 * inflate_threshold * new_child_length
728 *
729 * expand not_to_be_doubled and to_be_doubled, and shorten:
730 * 100 * (tnode_child_length(tn) - tn->empty_children +
731 * tn->full_children) >= inflate_threshold * new_child_length
732 *
733 * expand new_child_length:
734 * 100 * (tnode_child_length(tn) - tn->empty_children +
735 * tn->full_children) >=
736 * inflate_threshold * tnode_child_length(tn) * 2
737 *
738 * shorten again:
739 * 50 * (tn->full_children + tnode_child_length(tn) -
740 * tn->empty_children) >= inflate_threshold *
741 * tnode_child_length(tn)
742 *
743 */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800744static bool should_inflate(const struct tnode *tp, const struct tnode *tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800745{
746 unsigned long used = tnode_child_length(tn);
747 unsigned long threshold = used;
748
749 /* Keep root node larger */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800750 threshold *= tp ? inflate_threshold : inflate_threshold_root;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800751 used -= tn->empty_children;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800752 used += tn->full_children;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800753
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800754 /* if bits == KEYLENGTH then pos = 0, and will fail below */
755
756 return (used > 1) && tn->pos && ((50 * used) >= threshold);
Alexander Duyckf05a4812014-12-31 10:56:37 -0800757}
758
Alexander Duyckff181ed2014-12-31 10:56:43 -0800759static bool should_halve(const struct tnode *tp, const struct tnode *tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800760{
761 unsigned long used = tnode_child_length(tn);
762 unsigned long threshold = used;
763
764 /* Keep root node larger */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800765 threshold *= tp ? halve_threshold : halve_threshold_root;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800766 used -= tn->empty_children;
767
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800768 /* if bits == KEYLENGTH then used = 100% on wrap, and will fail below */
769
770 return (used > 1) && (tn->bits > 1) && ((100 * used) < threshold);
771}
772
773static bool should_collapse(const struct tnode *tn)
774{
775 unsigned long used = tnode_child_length(tn);
776
777 used -= tn->empty_children;
778
779 /* account for bits == KEYLENGTH case */
780 if ((tn->bits == KEYLENGTH) && tn->full_children)
781 used -= KEY_MAX;
782
783 /* One child or none, time to drop us from the trie */
784 return used < 2;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800785}
786
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800787#define MAX_WORK 10
Alexander Duyckff181ed2014-12-31 10:56:43 -0800788static void resize(struct trie *t, struct tnode *tn)
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800789{
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800790 struct tnode *tp = node_parent(tn);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800791 struct tnode __rcu **cptr;
Alexander Duycka80e89d2015-01-22 15:51:20 -0800792 int max_work = MAX_WORK;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800793
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800794 pr_debug("In tnode_resize %p inflate_threshold=%d threshold=%d\n",
795 tn, inflate_threshold, halve_threshold);
796
Alexander Duyckff181ed2014-12-31 10:56:43 -0800797 /* track the tnode via the pointer from the parent instead of
798 * doing it ourselves. This way we can let RCU fully do its
799 * thing without us interfering
800 */
Alexander Duyck41b489f2015-03-04 15:02:33 -0800801 cptr = tp ? &tp->tnode[get_index(tn->key, tp)] : &t->trie;
Alexander Duyckff181ed2014-12-31 10:56:43 -0800802 BUG_ON(tn != rtnl_dereference(*cptr));
803
Alexander Duyckf05a4812014-12-31 10:56:37 -0800804 /* Double as long as the resulting node has a number of
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800805 * nonempty nodes that are above the threshold.
806 */
Alexander Duycka80e89d2015-01-22 15:51:20 -0800807 while (should_inflate(tp, tn) && max_work) {
Alexander Duyckff181ed2014-12-31 10:56:43 -0800808 if (inflate(t, tn)) {
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800809#ifdef CONFIG_IP_FIB_TRIE_STATS
810 this_cpu_inc(t->stats->resize_node_skipped);
811#endif
812 break;
813 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800814
Alexander Duycka80e89d2015-01-22 15:51:20 -0800815 max_work--;
Alexander Duyckff181ed2014-12-31 10:56:43 -0800816 tn = rtnl_dereference(*cptr);
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800817 }
818
819 /* Return if at least one inflate is run */
820 if (max_work != MAX_WORK)
Alexander Duyckff181ed2014-12-31 10:56:43 -0800821 return;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800822
Alexander Duyckf05a4812014-12-31 10:56:37 -0800823 /* Halve as long as the number of empty children in this
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800824 * node is above threshold.
825 */
Alexander Duycka80e89d2015-01-22 15:51:20 -0800826 while (should_halve(tp, tn) && max_work) {
Alexander Duyckff181ed2014-12-31 10:56:43 -0800827 if (halve(t, tn)) {
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800828#ifdef CONFIG_IP_FIB_TRIE_STATS
829 this_cpu_inc(t->stats->resize_node_skipped);
830#endif
831 break;
832 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800833
Alexander Duycka80e89d2015-01-22 15:51:20 -0800834 max_work--;
Alexander Duyckff181ed2014-12-31 10:56:43 -0800835 tn = rtnl_dereference(*cptr);
836 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800837
838 /* Only one child remains */
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800839 if (should_collapse(tn)) {
840 collapse(t, tn);
Alexander Duyck5405afd2014-12-31 10:57:08 -0800841 return;
842 }
843
844 /* Return if at least one deflate was run */
845 if (max_work != MAX_WORK)
846 return;
847
848 /* push the suffix length to the parent node */
849 if (tn->slen > tn->pos) {
850 unsigned char slen = update_suffix(tn);
851
852 if (tp && (slen > tp->slen))
853 tp->slen = slen;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800854 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800855}
856
Alexander Duyckd5d64872015-03-04 15:02:18 -0800857static void leaf_pull_suffix(struct tnode *tp, struct tnode *l)
Robert Olsson19baf832005-06-21 12:43:18 -0700858{
Alexander Duyck5405afd2014-12-31 10:57:08 -0800859 while (tp && (tp->slen > tp->pos) && (tp->slen > l->slen)) {
860 if (update_suffix(tp) > l->slen)
861 break;
862 tp = node_parent(tp);
863 }
864}
865
Alexander Duyckd5d64872015-03-04 15:02:18 -0800866static void leaf_push_suffix(struct tnode *tn, struct tnode *l)
Alexander Duyck5405afd2014-12-31 10:57:08 -0800867{
Alexander Duyck5405afd2014-12-31 10:57:08 -0800868 /* if this is a new leaf then tn will be NULL and we can sort
869 * out parent suffix lengths as a part of trie_rebalance
870 */
871 while (tn && (tn->slen < l->slen)) {
872 tn->slen = l->slen;
873 tn = node_parent(tn);
874 }
875}
876
Robert Olsson2373ce12005-08-25 13:01:29 -0700877/* rcu_read_lock needs to be hold by caller from readside */
Alexander Duyckd4a975e2015-03-04 15:01:59 -0800878static struct tnode *fib_find_node(struct trie *t, struct tnode **tn, u32 key)
Robert Olsson19baf832005-06-21 12:43:18 -0700879{
Alexander Duyckd4a975e2015-03-04 15:01:59 -0800880 struct tnode *pn = NULL, *n = rcu_dereference_rtnl(t->trie);
Robert Olsson19baf832005-06-21 12:43:18 -0700881
Alexander Duyck939afb02014-12-31 10:56:00 -0800882 while (n) {
883 unsigned long index = get_index(key, n);
884
885 /* This bit of code is a bit tricky but it combines multiple
886 * checks into a single check. The prefix consists of the
887 * prefix plus zeros for the bits in the cindex. The index
888 * is the difference between the key and this value. From
889 * this we can actually derive several pieces of data.
Alexander Duyckd4a975e2015-03-04 15:01:59 -0800890 * if (index >= (1ul << bits))
Alexander Duyck939afb02014-12-31 10:56:00 -0800891 * we have a mismatch in skip bits and failed
Alexander Duyckb3832112015-01-22 15:51:08 -0800892 * else
893 * we know the value is cindex
Alexander Duyckd4a975e2015-03-04 15:01:59 -0800894 *
895 * This check is safe even if bits == KEYLENGTH due to the
896 * fact that we can only allocate a node with 32 bits if a
897 * long is greater than 32 bits.
Alexander Duyck939afb02014-12-31 10:56:00 -0800898 */
Alexander Duyckd4a975e2015-03-04 15:01:59 -0800899 if (index >= (1ul << n->bits)) {
900 n = NULL;
901 break;
902 }
Alexander Duyck939afb02014-12-31 10:56:00 -0800903
904 /* we have found a leaf. Prefixes have already been compared */
905 if (IS_LEAF(n))
Robert Olsson19baf832005-06-21 12:43:18 -0700906 break;
Alexander Duyck939afb02014-12-31 10:56:00 -0800907
Alexander Duyckd4a975e2015-03-04 15:01:59 -0800908 pn = n;
Alexander Duyck21d1f112014-12-31 10:57:02 -0800909 n = tnode_get_child_rcu(n, index);
Robert Olsson19baf832005-06-21 12:43:18 -0700910 }
Robert Olsson19baf832005-06-21 12:43:18 -0700911
Alexander Duyckd4a975e2015-03-04 15:01:59 -0800912 *tn = pn;
913
Alexander Duyck939afb02014-12-31 10:56:00 -0800914 return n;
Robert Olsson19baf832005-06-21 12:43:18 -0700915}
916
Alexander Duyck02525362015-01-22 15:51:39 -0800917/* Return the first fib alias matching TOS with
918 * priority less than or equal to PRIO.
919 */
Alexander Duyck79e5ad22015-02-25 15:31:51 -0800920static struct fib_alias *fib_find_alias(struct hlist_head *fah, u8 slen,
921 u8 tos, u32 prio)
Alexander Duyck02525362015-01-22 15:51:39 -0800922{
923 struct fib_alias *fa;
924
925 if (!fah)
926 return NULL;
927
Alexander Duyck56315f92015-02-25 15:31:31 -0800928 hlist_for_each_entry(fa, fah, fa_list) {
Alexander Duyck79e5ad22015-02-25 15:31:51 -0800929 if (fa->fa_slen < slen)
930 continue;
931 if (fa->fa_slen != slen)
932 break;
Alexander Duyck02525362015-01-22 15:51:39 -0800933 if (fa->fa_tos > tos)
934 continue;
935 if (fa->fa_info->fib_priority >= prio || fa->fa_tos < tos)
936 return fa;
937 }
938
939 return NULL;
940}
941
Jarek Poplawski7b855762009-06-18 00:28:51 -0700942static void trie_rebalance(struct trie *t, struct tnode *tn)
Robert Olsson19baf832005-06-21 12:43:18 -0700943{
Stephen Hemminger06801912007-08-10 15:22:13 -0700944 struct tnode *tp;
Robert Olsson19baf832005-06-21 12:43:18 -0700945
Alexander Duyckd5d64872015-03-04 15:02:18 -0800946 while (tn) {
947 tp = node_parent(tn);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800948 resize(t, tn);
Stephen Hemminger06801912007-08-10 15:22:13 -0700949 tn = tp;
Robert Olsson19baf832005-06-21 12:43:18 -0700950 }
Robert Olsson19baf832005-06-21 12:43:18 -0700951}
952
Robert Olsson2373ce12005-08-25 13:01:29 -0700953/* only used from updater-side */
Alexander Duyckd5d64872015-03-04 15:02:18 -0800954static int fib_insert_node(struct trie *t, struct tnode *tp,
955 struct fib_alias *new, t_key key)
Robert Olsson19baf832005-06-21 12:43:18 -0700956{
Alexander Duyckd5d64872015-03-04 15:02:18 -0800957 struct tnode *n, *l;
Alexander Duyck836a0122014-12-31 10:56:06 -0800958
Alexander Duyckd5d64872015-03-04 15:02:18 -0800959 l = leaf_new(key, new);
Alexander Duyck79e5ad22015-02-25 15:31:51 -0800960 if (!l)
Alexander Duyckd5d64872015-03-04 15:02:18 -0800961 return -ENOMEM;
962
963 /* retrieve child from parent node */
964 if (tp)
965 n = tnode_get_child(tp, get_index(key, tp));
966 else
967 n = rcu_dereference_rtnl(t->trie);
Alexander Duyck836a0122014-12-31 10:56:06 -0800968
969 /* Case 2: n is a LEAF or a TNODE and the key doesn't match.
970 *
971 * Add a new tnode here
972 * first tnode need some special handling
973 * leaves us in position for handling as case 3
974 */
975 if (n) {
976 struct tnode *tn;
Alexander Duyck836a0122014-12-31 10:56:06 -0800977
Alexander Duycke9b44012014-12-31 10:56:12 -0800978 tn = tnode_new(key, __fls(key ^ n->key), 1);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700979 if (!tn) {
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800980 node_free(l);
Alexander Duyckd5d64872015-03-04 15:02:18 -0800981 return -ENOMEM;
Olof Johansson91b9a272005-08-09 20:24:39 -0700982 }
983
Alexander Duyck836a0122014-12-31 10:56:06 -0800984 /* initialize routes out of node */
985 NODE_INIT_PARENT(tn, tp);
986 put_child(tn, get_index(key, tn) ^ 1, n);
Robert Olsson19baf832005-06-21 12:43:18 -0700987
Alexander Duyck836a0122014-12-31 10:56:06 -0800988 /* start adding routes into the node */
989 put_child_root(tp, t, key, tn);
990 node_set_parent(n, tn);
Robert Olsson19baf832005-06-21 12:43:18 -0700991
Alexander Duyck836a0122014-12-31 10:56:06 -0800992 /* parent now has a NULL spot where the leaf can go */
Alexander Duycke962f302014-12-10 21:49:22 -0800993 tp = tn;
Robert Olsson19baf832005-06-21 12:43:18 -0700994 }
Olof Johansson91b9a272005-08-09 20:24:39 -0700995
Alexander Duyck836a0122014-12-31 10:56:06 -0800996 /* Case 3: n is NULL, and will just insert a new leaf */
Alexander Duyckd5d64872015-03-04 15:02:18 -0800997 NODE_INIT_PARENT(l, tp);
998 put_child_root(tp, t, key, l);
999 trie_rebalance(t, tp);
Olof Johansson91b9a272005-08-09 20:24:39 -07001000
Alexander Duyckd5d64872015-03-04 15:02:18 -08001001 return 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001002}
1003
Alexander Duyckd5d64872015-03-04 15:02:18 -08001004static int fib_insert_alias(struct trie *t, struct tnode *tp,
1005 struct tnode *l, struct fib_alias *new,
1006 struct fib_alias *fa, t_key key)
1007{
1008 if (!l)
1009 return fib_insert_node(t, tp, new, key);
1010
1011 if (fa) {
1012 hlist_add_before_rcu(&new->fa_list, &fa->fa_list);
1013 } else {
1014 struct fib_alias *last;
1015
1016 hlist_for_each_entry(last, &l->leaf, fa_list) {
1017 if (new->fa_slen < last->fa_slen)
1018 break;
1019 fa = last;
1020 }
1021
1022 if (fa)
1023 hlist_add_behind_rcu(&new->fa_list, &fa->fa_list);
1024 else
1025 hlist_add_head_rcu(&new->fa_list, &l->leaf);
1026 }
1027
1028 /* if we added to the tail node then we need to update slen */
1029 if (l->slen < new->fa_slen) {
1030 l->slen = new->fa_slen;
1031 leaf_push_suffix(tp, l);
1032 }
1033
1034 return 0;
1035}
1036
1037/* Caller must hold RTNL. */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001038int fib_table_insert(struct fib_table *tb, struct fib_config *cfg)
Robert Olsson19baf832005-06-21 12:43:18 -07001039{
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001040 struct trie *t = (struct trie *)tb->tb_data;
Robert Olsson19baf832005-06-21 12:43:18 -07001041 struct fib_alias *fa, *new_fa;
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001042 struct tnode *l, *tp;
Robert Olsson19baf832005-06-21 12:43:18 -07001043 struct fib_info *fi;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001044 u8 plen = cfg->fc_dst_len;
1045 u8 slen = KEYLENGTH - plen;
Thomas Graf4e902c52006-08-17 18:14:52 -07001046 u8 tos = cfg->fc_tos;
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001047 u32 key;
Robert Olsson19baf832005-06-21 12:43:18 -07001048 int err;
Robert Olsson19baf832005-06-21 12:43:18 -07001049
Alexander Duyck5786ec62015-02-25 15:31:37 -08001050 if (plen > KEYLENGTH)
Robert Olsson19baf832005-06-21 12:43:18 -07001051 return -EINVAL;
1052
Thomas Graf4e902c52006-08-17 18:14:52 -07001053 key = ntohl(cfg->fc_dst);
Robert Olsson19baf832005-06-21 12:43:18 -07001054
Patrick McHardy2dfe55b2006-08-10 23:08:33 -07001055 pr_debug("Insert table=%u %08x/%d\n", tb->tb_id, key, plen);
Robert Olsson19baf832005-06-21 12:43:18 -07001056
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001057 if ((plen < KEYLENGTH) && (key << plen))
Robert Olsson19baf832005-06-21 12:43:18 -07001058 return -EINVAL;
1059
Thomas Graf4e902c52006-08-17 18:14:52 -07001060 fi = fib_create_info(cfg);
1061 if (IS_ERR(fi)) {
1062 err = PTR_ERR(fi);
Robert Olsson19baf832005-06-21 12:43:18 -07001063 goto err;
Thomas Graf4e902c52006-08-17 18:14:52 -07001064 }
Robert Olsson19baf832005-06-21 12:43:18 -07001065
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001066 l = fib_find_node(t, &tp, key);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001067 fa = l ? fib_find_alias(&l->leaf, slen, tos, fi->fib_priority) : NULL;
Robert Olsson19baf832005-06-21 12:43:18 -07001068
1069 /* Now fa, if non-NULL, points to the first fib alias
1070 * with the same keys [prefix,tos,priority], if such key already
1071 * exists or to the node before which we will insert new one.
1072 *
1073 * If fa is NULL, we will need to allocate a new one and
Alexander Duyck56315f92015-02-25 15:31:31 -08001074 * insert to the tail of the section matching the suffix length
1075 * of the new alias.
Robert Olsson19baf832005-06-21 12:43:18 -07001076 */
1077
Julian Anastasov936f6f82008-01-28 21:18:06 -08001078 if (fa && fa->fa_tos == tos &&
1079 fa->fa_info->fib_priority == fi->fib_priority) {
1080 struct fib_alias *fa_first, *fa_match;
Robert Olsson19baf832005-06-21 12:43:18 -07001081
1082 err = -EEXIST;
Thomas Graf4e902c52006-08-17 18:14:52 -07001083 if (cfg->fc_nlflags & NLM_F_EXCL)
Robert Olsson19baf832005-06-21 12:43:18 -07001084 goto out;
1085
Julian Anastasov936f6f82008-01-28 21:18:06 -08001086 /* We have 2 goals:
1087 * 1. Find exact match for type, scope, fib_info to avoid
1088 * duplicate routes
1089 * 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it
1090 */
1091 fa_match = NULL;
1092 fa_first = fa;
Alexander Duyck56315f92015-02-25 15:31:31 -08001093 hlist_for_each_entry_from(fa, fa_list) {
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001094 if ((fa->fa_slen != slen) || (fa->fa_tos != tos))
Julian Anastasov936f6f82008-01-28 21:18:06 -08001095 break;
1096 if (fa->fa_info->fib_priority != fi->fib_priority)
1097 break;
1098 if (fa->fa_type == cfg->fc_type &&
Julian Anastasov936f6f82008-01-28 21:18:06 -08001099 fa->fa_info == fi) {
1100 fa_match = fa;
1101 break;
1102 }
1103 }
1104
Thomas Graf4e902c52006-08-17 18:14:52 -07001105 if (cfg->fc_nlflags & NLM_F_REPLACE) {
Robert Olsson19baf832005-06-21 12:43:18 -07001106 struct fib_info *fi_drop;
1107 u8 state;
1108
Julian Anastasov936f6f82008-01-28 21:18:06 -08001109 fa = fa_first;
1110 if (fa_match) {
1111 if (fa == fa_match)
1112 err = 0;
Joonwoo Park67250332008-01-18 03:45:18 -08001113 goto out;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001114 }
Robert Olsson2373ce12005-08-25 13:01:29 -07001115 err = -ENOBUFS;
Christoph Lametere94b1762006-12-06 20:33:17 -08001116 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
Robert Olsson2373ce12005-08-25 13:01:29 -07001117 if (new_fa == NULL)
1118 goto out;
Robert Olsson19baf832005-06-21 12:43:18 -07001119
1120 fi_drop = fa->fa_info;
Robert Olsson2373ce12005-08-25 13:01:29 -07001121 new_fa->fa_tos = fa->fa_tos;
1122 new_fa->fa_info = fi;
Thomas Graf4e902c52006-08-17 18:14:52 -07001123 new_fa->fa_type = cfg->fc_type;
Robert Olsson19baf832005-06-21 12:43:18 -07001124 state = fa->fa_state;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001125 new_fa->fa_state = state & ~FA_S_ACCESSED;
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08001126 new_fa->fa_slen = fa->fa_slen;
Robert Olsson19baf832005-06-21 12:43:18 -07001127
Alexander Duyck56315f92015-02-25 15:31:31 -08001128 hlist_replace_rcu(&fa->fa_list, &new_fa->fa_list);
Robert Olsson2373ce12005-08-25 13:01:29 -07001129 alias_free_mem_rcu(fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001130
1131 fib_release_info(fi_drop);
1132 if (state & FA_S_ACCESSED)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001133 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Milan Kocianb8f55832007-05-23 14:55:06 -07001134 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen,
1135 tb->tb_id, &cfg->fc_nlinfo, NLM_F_REPLACE);
Robert Olsson19baf832005-06-21 12:43:18 -07001136
Olof Johansson91b9a272005-08-09 20:24:39 -07001137 goto succeeded;
Robert Olsson19baf832005-06-21 12:43:18 -07001138 }
1139 /* Error if we find a perfect match which
1140 * uses the same scope, type, and nexthop
1141 * information.
1142 */
Julian Anastasov936f6f82008-01-28 21:18:06 -08001143 if (fa_match)
1144 goto out;
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001145
Thomas Graf4e902c52006-08-17 18:14:52 -07001146 if (!(cfg->fc_nlflags & NLM_F_APPEND))
Julian Anastasov936f6f82008-01-28 21:18:06 -08001147 fa = fa_first;
Robert Olsson19baf832005-06-21 12:43:18 -07001148 }
1149 err = -ENOENT;
Thomas Graf4e902c52006-08-17 18:14:52 -07001150 if (!(cfg->fc_nlflags & NLM_F_CREATE))
Robert Olsson19baf832005-06-21 12:43:18 -07001151 goto out;
1152
1153 err = -ENOBUFS;
Christoph Lametere94b1762006-12-06 20:33:17 -08001154 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
Robert Olsson19baf832005-06-21 12:43:18 -07001155 if (new_fa == NULL)
1156 goto out;
1157
1158 new_fa->fa_info = fi;
1159 new_fa->fa_tos = tos;
Thomas Graf4e902c52006-08-17 18:14:52 -07001160 new_fa->fa_type = cfg->fc_type;
Robert Olsson19baf832005-06-21 12:43:18 -07001161 new_fa->fa_state = 0;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001162 new_fa->fa_slen = slen;
Robert Olsson19baf832005-06-21 12:43:18 -07001163
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08001164 /* Insert new entry to the list. */
Alexander Duyckd5d64872015-03-04 15:02:18 -08001165 err = fib_insert_alias(t, tp, l, new_fa, fa, key);
1166 if (err)
1167 goto out_free_new_fa;
Robert Olsson19baf832005-06-21 12:43:18 -07001168
David S. Miller21d8c492011-04-14 14:49:37 -07001169 if (!plen)
1170 tb->tb_num_default++;
1171
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001172 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Thomas Graf4e902c52006-08-17 18:14:52 -07001173 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen, tb->tb_id,
Milan Kocianb8f55832007-05-23 14:55:06 -07001174 &cfg->fc_nlinfo, 0);
Robert Olsson19baf832005-06-21 12:43:18 -07001175succeeded:
1176 return 0;
Robert Olssonf835e472005-06-28 15:00:39 -07001177
1178out_free_new_fa:
1179 kmem_cache_free(fn_alias_kmem, new_fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001180out:
1181 fib_release_info(fi);
Olof Johansson91b9a272005-08-09 20:24:39 -07001182err:
Robert Olsson19baf832005-06-21 12:43:18 -07001183 return err;
1184}
1185
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001186static inline t_key prefix_mismatch(t_key key, struct tnode *n)
1187{
1188 t_key prefix = n->key;
1189
1190 return (key ^ prefix) & (prefix | -prefix);
1191}
1192
Alexander Duyck345e9b52014-12-31 10:56:24 -08001193/* should be called with rcu_read_lock */
David S. Miller22bd5b92011-03-11 19:54:08 -05001194int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00001195 struct fib_result *res, int fib_flags)
Robert Olsson19baf832005-06-21 12:43:18 -07001196{
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001197 struct trie *t = (struct trie *)tb->tb_data;
Alexander Duyck8274a972014-12-31 10:55:29 -08001198#ifdef CONFIG_IP_FIB_TRIE_STATS
1199 struct trie_use_stats __percpu *stats = t->stats;
1200#endif
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001201 const t_key key = ntohl(flp->daddr);
1202 struct tnode *n, *pn;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001203 struct fib_alias *fa;
Alexander Duyck71e8b672015-03-04 15:04:03 -08001204 unsigned long index;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001205 t_key cindex;
Robert Olsson19baf832005-06-21 12:43:18 -07001206
Robert Olsson2373ce12005-08-25 13:01:29 -07001207 n = rcu_dereference(t->trie);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001208 if (!n)
Alexander Duyck345e9b52014-12-31 10:56:24 -08001209 return -EAGAIN;
Robert Olsson19baf832005-06-21 12:43:18 -07001210
1211#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -08001212 this_cpu_inc(stats->gets);
Robert Olsson19baf832005-06-21 12:43:18 -07001213#endif
1214
Alexander Duyckadaf9812014-12-31 10:55:47 -08001215 pn = n;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001216 cindex = 0;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001217
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001218 /* Step 1: Travel to the longest prefix match in the trie */
1219 for (;;) {
Alexander Duyck71e8b672015-03-04 15:04:03 -08001220 index = get_index(key, n);
Robert Olsson19baf832005-06-21 12:43:18 -07001221
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001222 /* This bit of code is a bit tricky but it combines multiple
1223 * checks into a single check. The prefix consists of the
1224 * prefix plus zeros for the "bits" in the prefix. The index
1225 * is the difference between the key and this value. From
1226 * this we can actually derive several pieces of data.
Alexander Duyck71e8b672015-03-04 15:04:03 -08001227 * if (index >= (1ul << bits))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001228 * we have a mismatch in skip bits and failed
Alexander Duyckb3832112015-01-22 15:51:08 -08001229 * else
1230 * we know the value is cindex
Alexander Duyck71e8b672015-03-04 15:04:03 -08001231 *
1232 * This check is safe even if bits == KEYLENGTH due to the
1233 * fact that we can only allocate a node with 32 bits if a
1234 * long is greater than 32 bits.
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001235 */
Alexander Duyck71e8b672015-03-04 15:04:03 -08001236 if (index >= (1ul << n->bits))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001237 break;
Robert Olsson19baf832005-06-21 12:43:18 -07001238
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001239 /* we have found a leaf. Prefixes have already been compared */
1240 if (IS_LEAF(n))
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001241 goto found;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001242
1243 /* only record pn and cindex if we are going to be chopping
1244 * bits later. Otherwise we are just wasting cycles.
1245 */
Alexander Duyck5405afd2014-12-31 10:57:08 -08001246 if (n->slen > n->pos) {
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001247 pn = n;
1248 cindex = index;
Olof Johansson91b9a272005-08-09 20:24:39 -07001249 }
1250
Alexander Duyck21d1f112014-12-31 10:57:02 -08001251 n = tnode_get_child_rcu(n, index);
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001252 if (unlikely(!n))
Robert Olsson19baf832005-06-21 12:43:18 -07001253 goto backtrace;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001254 }
1255
1256 /* Step 2: Sort out leaves and begin backtracing for longest prefix */
1257 for (;;) {
1258 /* record the pointer where our next node pointer is stored */
Alexander Duyck41b489f2015-03-04 15:02:33 -08001259 struct tnode __rcu **cptr = n->tnode;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001260
1261 /* This test verifies that none of the bits that differ
1262 * between the key and the prefix exist in the region of
1263 * the lsb and higher in the prefix.
1264 */
Alexander Duyck5405afd2014-12-31 10:57:08 -08001265 if (unlikely(prefix_mismatch(key, n)) || (n->slen == n->pos))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001266 goto backtrace;
1267
1268 /* exit out and process leaf */
1269 if (unlikely(IS_LEAF(n)))
1270 break;
1271
1272 /* Don't bother recording parent info. Since we are in
1273 * prefix match mode we will have to come back to wherever
1274 * we started this traversal anyway
1275 */
1276
1277 while ((n = rcu_dereference(*cptr)) == NULL) {
1278backtrace:
1279#ifdef CONFIG_IP_FIB_TRIE_STATS
1280 if (!n)
1281 this_cpu_inc(stats->null_node_hit);
1282#endif
1283 /* If we are at cindex 0 there are no more bits for
1284 * us to strip at this level so we must ascend back
1285 * up one level to see if there are any more bits to
1286 * be stripped there.
1287 */
1288 while (!cindex) {
1289 t_key pkey = pn->key;
1290
1291 pn = node_parent_rcu(pn);
1292 if (unlikely(!pn))
Alexander Duyck345e9b52014-12-31 10:56:24 -08001293 return -EAGAIN;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001294#ifdef CONFIG_IP_FIB_TRIE_STATS
1295 this_cpu_inc(stats->backtrack);
1296#endif
1297 /* Get Child's index */
1298 cindex = get_index(pkey, pn);
1299 }
1300
1301 /* strip the least significant bit from the cindex */
1302 cindex &= cindex - 1;
1303
1304 /* grab pointer for next child node */
Alexander Duyck41b489f2015-03-04 15:02:33 -08001305 cptr = &pn->tnode[cindex];
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001306 }
Robert Olsson19baf832005-06-21 12:43:18 -07001307 }
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001308
Robert Olsson19baf832005-06-21 12:43:18 -07001309found:
Alexander Duyck71e8b672015-03-04 15:04:03 -08001310 /* this line carries forward the xor from earlier in the function */
1311 index = key ^ n->key;
1312
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001313 /* Step 3: Process the leaf, if that fails fall back to backtracing */
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001314 hlist_for_each_entry_rcu(fa, &n->leaf, fa_list) {
1315 struct fib_info *fi = fa->fa_info;
1316 int nhsel, err;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001317
Alexander Duyck71e8b672015-03-04 15:04:03 -08001318 if ((index >= (1ul << fa->fa_slen)) &&
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001319 ((BITS_PER_LONG > KEYLENGTH) || (fa->fa_slen != KEYLENGTH)))
Alexander Duyck71e8b672015-03-04 15:04:03 -08001320 continue;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001321 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
1322 continue;
1323 if (fi->fib_dead)
1324 continue;
1325 if (fa->fa_info->fib_scope < flp->flowi4_scope)
1326 continue;
1327 fib_alias_accessed(fa);
1328 err = fib_props[fa->fa_type].error;
1329 if (unlikely(err < 0)) {
Alexander Duyck345e9b52014-12-31 10:56:24 -08001330#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001331 this_cpu_inc(stats->semantic_match_passed);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001332#endif
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001333 return err;
1334 }
1335 if (fi->fib_flags & RTNH_F_DEAD)
1336 continue;
1337 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
1338 const struct fib_nh *nh = &fi->fib_nh[nhsel];
1339
1340 if (nh->nh_flags & RTNH_F_DEAD)
Alexander Duyck345e9b52014-12-31 10:56:24 -08001341 continue;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001342 if (flp->flowi4_oif && flp->flowi4_oif != nh->nh_oif)
1343 continue;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001344
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001345 if (!(fib_flags & FIB_LOOKUP_NOREF))
1346 atomic_inc(&fi->fib_clntref);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001347
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001348 res->prefixlen = KEYLENGTH - fa->fa_slen;
1349 res->nh_sel = nhsel;
1350 res->type = fa->fa_type;
1351 res->scope = fi->fib_scope;
1352 res->fi = fi;
1353 res->table = tb;
1354 res->fa_head = &n->leaf;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001355#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001356 this_cpu_inc(stats->semantic_match_passed);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001357#endif
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001358 return err;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001359 }
Alexander Duyck345e9b52014-12-31 10:56:24 -08001360 }
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08001361#ifdef CONFIG_IP_FIB_TRIE_STATS
1362 this_cpu_inc(stats->semantic_match_miss);
1363#endif
Alexander Duyck345e9b52014-12-31 10:56:24 -08001364 goto backtrace;
Robert Olsson19baf832005-06-21 12:43:18 -07001365}
Florian Westphal6fc01432011-08-25 13:46:12 +02001366EXPORT_SYMBOL_GPL(fib_table_lookup);
Robert Olsson19baf832005-06-21 12:43:18 -07001367
Alexander Duyckd5d64872015-03-04 15:02:18 -08001368static void fib_remove_alias(struct trie *t, struct tnode *tp,
1369 struct tnode *l, struct fib_alias *old)
1370{
1371 /* record the location of the previous list_info entry */
1372 struct hlist_node **pprev = old->fa_list.pprev;
1373 struct fib_alias *fa = hlist_entry(pprev, typeof(*fa), fa_list.next);
1374
1375 /* remove the fib_alias from the list */
1376 hlist_del_rcu(&old->fa_list);
1377
1378 /* if we emptied the list this leaf will be freed and we can sort
1379 * out parent suffix lengths as a part of trie_rebalance
1380 */
1381 if (hlist_empty(&l->leaf)) {
1382 put_child_root(tp, t, l->key, NULL);
1383 node_free(l);
1384 trie_rebalance(t, tp);
1385 return;
1386 }
1387
1388 /* only access fa if it is pointing at the last valid hlist_node */
1389 if (*pprev)
1390 return;
1391
1392 /* update the trie with the latest suffix length */
1393 l->slen = fa->fa_slen;
1394 leaf_pull_suffix(tp, l);
1395}
1396
1397/* Caller must hold RTNL. */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001398int fib_table_delete(struct fib_table *tb, struct fib_config *cfg)
Robert Olsson19baf832005-06-21 12:43:18 -07001399{
1400 struct trie *t = (struct trie *) tb->tb_data;
Robert Olsson19baf832005-06-21 12:43:18 -07001401 struct fib_alias *fa, *fa_to_delete;
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001402 struct tnode *l, *tp;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001403 u8 plen = cfg->fc_dst_len;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001404 u8 slen = KEYLENGTH - plen;
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001405 u8 tos = cfg->fc_tos;
1406 u32 key;
Olof Johansson91b9a272005-08-09 20:24:39 -07001407
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001408 if (plen > KEYLENGTH)
Robert Olsson19baf832005-06-21 12:43:18 -07001409 return -EINVAL;
1410
Thomas Graf4e902c52006-08-17 18:14:52 -07001411 key = ntohl(cfg->fc_dst);
Robert Olsson19baf832005-06-21 12:43:18 -07001412
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001413 if ((plen < KEYLENGTH) && (key << plen))
Robert Olsson19baf832005-06-21 12:43:18 -07001414 return -EINVAL;
1415
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001416 l = fib_find_node(t, &tp, key);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001417 if (!l)
Robert Olsson19baf832005-06-21 12:43:18 -07001418 return -ESRCH;
1419
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001420 fa = fib_find_alias(&l->leaf, slen, tos, 0);
Robert Olsson19baf832005-06-21 12:43:18 -07001421 if (!fa)
1422 return -ESRCH;
1423
Stephen Hemminger0c7770c2005-08-23 21:59:41 -07001424 pr_debug("Deleting %08x/%d tos=%d t=%p\n", key, plen, tos, t);
Robert Olsson19baf832005-06-21 12:43:18 -07001425
1426 fa_to_delete = NULL;
Alexander Duyck56315f92015-02-25 15:31:31 -08001427 hlist_for_each_entry_from(fa, fa_list) {
Robert Olsson19baf832005-06-21 12:43:18 -07001428 struct fib_info *fi = fa->fa_info;
1429
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001430 if ((fa->fa_slen != slen) || (fa->fa_tos != tos))
Robert Olsson19baf832005-06-21 12:43:18 -07001431 break;
1432
Thomas Graf4e902c52006-08-17 18:14:52 -07001433 if ((!cfg->fc_type || fa->fa_type == cfg->fc_type) &&
1434 (cfg->fc_scope == RT_SCOPE_NOWHERE ||
David S. Miller37e826c2011-03-24 18:06:47 -07001435 fa->fa_info->fib_scope == cfg->fc_scope) &&
Julian Anastasov74cb3c12011-03-19 12:13:46 +00001436 (!cfg->fc_prefsrc ||
1437 fi->fib_prefsrc == cfg->fc_prefsrc) &&
Thomas Graf4e902c52006-08-17 18:14:52 -07001438 (!cfg->fc_protocol ||
1439 fi->fib_protocol == cfg->fc_protocol) &&
1440 fib_nh_match(cfg, fi) == 0) {
Robert Olsson19baf832005-06-21 12:43:18 -07001441 fa_to_delete = fa;
1442 break;
1443 }
1444 }
1445
Olof Johansson91b9a272005-08-09 20:24:39 -07001446 if (!fa_to_delete)
1447 return -ESRCH;
Robert Olsson19baf832005-06-21 12:43:18 -07001448
Alexander Duyckd5d64872015-03-04 15:02:18 -08001449 rtmsg_fib(RTM_DELROUTE, htonl(key), fa_to_delete, plen, tb->tb_id,
Milan Kocianb8f55832007-05-23 14:55:06 -07001450 &cfg->fc_nlinfo, 0);
Robert Olsson19baf832005-06-21 12:43:18 -07001451
David S. Miller21d8c492011-04-14 14:49:37 -07001452 if (!plen)
1453 tb->tb_num_default--;
1454
Alexander Duyckd5d64872015-03-04 15:02:18 -08001455 fib_remove_alias(t, tp, l, fa_to_delete);
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001456
Alexander Duyckd5d64872015-03-04 15:02:18 -08001457 if (fa_to_delete->fa_state & FA_S_ACCESSED)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001458 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Olof Johansson91b9a272005-08-09 20:24:39 -07001459
Alexander Duyckd5d64872015-03-04 15:02:18 -08001460 fib_release_info(fa_to_delete->fa_info);
1461 alias_free_mem_rcu(fa_to_delete);
Olof Johansson91b9a272005-08-09 20:24:39 -07001462 return 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001463}
1464
Alexander Duyck8be33e92015-03-04 14:59:19 -08001465/* Scan for the next leaf starting at the provided key value */
1466static struct tnode *leaf_walk_rcu(struct tnode **tn, t_key key)
Robert Olsson19baf832005-06-21 12:43:18 -07001467{
Alexander Duyck8be33e92015-03-04 14:59:19 -08001468 struct tnode *pn, *n = *tn;
1469 unsigned long cindex;
Robert Olsson19baf832005-06-21 12:43:18 -07001470
Alexander Duyck8be33e92015-03-04 14:59:19 -08001471 /* record parent node for backtracing */
1472 pn = n;
1473 cindex = n ? get_index(key, n) : 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001474
Alexander Duyck8be33e92015-03-04 14:59:19 -08001475 /* this loop is meant to try and find the key in the trie */
1476 while (n) {
1477 unsigned long idx = get_index(key, n);
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001478
Alexander Duyck8be33e92015-03-04 14:59:19 -08001479 /* guarantee forward progress on the keys */
1480 if (IS_LEAF(n) && (n->key >= key))
1481 goto found;
1482 if (idx >= (1ul << n->bits))
1483 break;
1484
1485 /* record parent and next child index */
1486 pn = n;
1487 cindex = idx;
1488
1489 /* descend into the next child */
1490 n = tnode_get_child_rcu(pn, cindex++);
1491 }
1492
1493 /* this loop will search for the next leaf with a greater key */
1494 while (pn) {
1495 /* if we exhausted the parent node we will need to climb */
1496 if (cindex >= (1ul << pn->bits)) {
1497 t_key pkey = pn->key;
1498
1499 pn = node_parent_rcu(pn);
1500 if (!pn)
1501 break;
1502
1503 cindex = get_index(pkey, pn) + 1;
1504 continue;
Robert Olsson19baf832005-06-21 12:43:18 -07001505 }
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001506
Alexander Duyck8be33e92015-03-04 14:59:19 -08001507 /* grab the next available node */
1508 n = tnode_get_child_rcu(pn, cindex++);
1509 if (!n)
1510 continue;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001511
Alexander Duyck8be33e92015-03-04 14:59:19 -08001512 /* no need to compare keys since we bumped the index */
1513 if (IS_LEAF(n))
1514 goto found;
1515
1516 /* Rescan start scanning in new node */
1517 pn = n;
1518 cindex = 0;
1519 }
1520
1521 *tn = pn;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001522 return NULL; /* Root of trie */
Alexander Duyck8be33e92015-03-04 14:59:19 -08001523found:
1524 /* if we are at the limit for keys just return NULL for the tnode */
1525 *tn = (n->key == KEY_MAX) ? NULL : pn;
1526 return n;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001527}
1528
Alexander Duyck8be33e92015-03-04 14:59:19 -08001529/* Caller must hold RTNL. */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001530int fib_table_flush(struct fib_table *tb)
Robert Olsson19baf832005-06-21 12:43:18 -07001531{
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001532 struct trie *t = (struct trie *)tb->tb_data;
1533 struct hlist_node *tmp;
1534 struct fib_alias *fa;
1535 struct tnode *n, *pn;
1536 unsigned long cindex;
1537 unsigned char slen;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001538 int found = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001539
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001540 n = rcu_dereference(t->trie);
1541 if (!n)
1542 goto flush_complete;
Robert Olsson19baf832005-06-21 12:43:18 -07001543
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001544 pn = NULL;
1545 cindex = 0;
1546
1547 while (IS_TNODE(n)) {
1548 /* record pn and cindex for leaf walking */
1549 pn = n;
1550 cindex = 1ul << n->bits;
1551backtrace:
1552 /* walk trie in reverse order */
1553 do {
1554 while (!(cindex--)) {
1555 t_key pkey = pn->key;
1556
1557 n = pn;
1558 pn = node_parent(n);
1559
1560 /* resize completed node */
1561 resize(t, n);
1562
1563 /* if we got the root we are done */
1564 if (!pn)
1565 goto flush_complete;
1566
1567 cindex = get_index(pkey, pn);
1568 }
1569
1570 /* grab the next available node */
1571 n = tnode_get_child(pn, cindex);
1572 } while (!n);
1573 }
1574
1575 /* track slen in case any prefixes survive */
1576 slen = 0;
1577
1578 hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
1579 struct fib_info *fi = fa->fa_info;
1580
1581 if (fi && (fi->fib_flags & RTNH_F_DEAD)) {
1582 hlist_del_rcu(&fa->fa_list);
1583 fib_release_info(fa->fa_info);
1584 alias_free_mem_rcu(fa);
1585 found++;
1586
1587 continue;
Alexander Duyck64c62722015-01-22 15:51:45 -08001588 }
1589
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001590 slen = fa->fa_slen;
Robert Olsson19baf832005-06-21 12:43:18 -07001591 }
1592
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001593 /* update leaf slen */
1594 n->slen = slen;
1595
1596 if (hlist_empty(&n->leaf)) {
1597 put_child_root(pn, t, n->key, NULL);
1598 node_free(n);
1599 } else {
Alexander Duyckd5d64872015-03-04 15:02:18 -08001600 leaf_pull_suffix(pn, n);
Alexander Duyck64c62722015-01-22 15:51:45 -08001601 }
Robert Olsson19baf832005-06-21 12:43:18 -07001602
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001603 /* if trie is leaf only loop is completed */
1604 if (pn)
1605 goto backtrace;
1606flush_complete:
Stephen Hemminger0c7770c2005-08-23 21:59:41 -07001607 pr_debug("trie_flush found=%d\n", found);
Robert Olsson19baf832005-06-21 12:43:18 -07001608 return found;
1609}
1610
Alexander Duycka7e53532015-03-04 15:02:44 -08001611static void __trie_free_rcu(struct rcu_head *head)
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00001612{
Alexander Duycka7e53532015-03-04 15:02:44 -08001613 struct fib_table *tb = container_of(head, struct fib_table, rcu);
Alexander Duyck8274a972014-12-31 10:55:29 -08001614#ifdef CONFIG_IP_FIB_TRIE_STATS
1615 struct trie *t = (struct trie *)tb->tb_data;
1616
1617 free_percpu(t->stats);
1618#endif /* CONFIG_IP_FIB_TRIE_STATS */
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00001619 kfree(tb);
1620}
1621
Alexander Duycka7e53532015-03-04 15:02:44 -08001622void fib_free_table(struct fib_table *tb)
1623{
1624 call_rcu(&tb->rcu, __trie_free_rcu);
1625}
1626
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001627static int fn_trie_dump_leaf(struct tnode *l, struct fib_table *tb,
1628 struct sk_buff *skb, struct netlink_callback *cb)
Robert Olsson19baf832005-06-21 12:43:18 -07001629{
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001630 __be32 xkey = htonl(l->key);
Robert Olsson19baf832005-06-21 12:43:18 -07001631 struct fib_alias *fa;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001632 int i, s_i;
Robert Olsson19baf832005-06-21 12:43:18 -07001633
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001634 s_i = cb->args[4];
Robert Olsson19baf832005-06-21 12:43:18 -07001635 i = 0;
1636
Robert Olsson2373ce12005-08-25 13:01:29 -07001637 /* rcu_read_lock is hold by caller */
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001638 hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
Robert Olsson19baf832005-06-21 12:43:18 -07001639 if (i < s_i) {
1640 i++;
1641 continue;
1642 }
Robert Olsson19baf832005-06-21 12:43:18 -07001643
Eric W. Biederman15e47302012-09-07 20:12:54 +00001644 if (fib_dump_info(skb, NETLINK_CB(cb->skb).portid,
Robert Olsson19baf832005-06-21 12:43:18 -07001645 cb->nlh->nlmsg_seq,
1646 RTM_NEWROUTE,
1647 tb->tb_id,
1648 fa->fa_type,
Thomas Grafbe403ea2006-08-17 18:15:17 -07001649 xkey,
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08001650 KEYLENGTH - fa->fa_slen,
Robert Olsson19baf832005-06-21 12:43:18 -07001651 fa->fa_tos,
Stephen Hemminger64347f72008-01-22 21:55:01 -08001652 fa->fa_info, NLM_F_MULTI) < 0) {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001653 cb->args[4] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001654 return -1;
1655 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001656 i++;
Robert Olsson19baf832005-06-21 12:43:18 -07001657 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001658
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001659 cb->args[4] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001660 return skb->len;
1661}
1662
Alexander Duycka7e53532015-03-04 15:02:44 -08001663/* rcu_read_lock needs to be hold by caller from readside */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001664int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
1665 struct netlink_callback *cb)
Robert Olsson19baf832005-06-21 12:43:18 -07001666{
Alexander Duyck8be33e92015-03-04 14:59:19 -08001667 struct trie *t = (struct trie *)tb->tb_data;
1668 struct tnode *l, *tp;
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001669 /* Dump starting at last key.
1670 * Note: 0.0.0.0/0 (ie default) is first key.
1671 */
Alexander Duyck8be33e92015-03-04 14:59:19 -08001672 int count = cb->args[2];
1673 t_key key = cb->args[3];
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001674
Alexander Duyck8be33e92015-03-04 14:59:19 -08001675 tp = rcu_dereference_rtnl(t->trie);
1676
1677 while ((l = leaf_walk_rcu(&tp, key)) != NULL) {
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001678 if (fn_trie_dump_leaf(l, tb, skb, cb) < 0) {
Alexander Duyck8be33e92015-03-04 14:59:19 -08001679 cb->args[3] = key;
1680 cb->args[2] = count;
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001681 return -1;
Robert Olsson19baf832005-06-21 12:43:18 -07001682 }
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001683
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001684 ++count;
Alexander Duyck8be33e92015-03-04 14:59:19 -08001685 key = l->key + 1;
1686
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001687 memset(&cb->args[4], 0,
1688 sizeof(cb->args) - 4*sizeof(cb->args[0]));
Alexander Duyck8be33e92015-03-04 14:59:19 -08001689
1690 /* stop loop if key wrapped back to 0 */
1691 if (key < l->key)
1692 break;
Robert Olsson19baf832005-06-21 12:43:18 -07001693 }
Alexander Duyck8be33e92015-03-04 14:59:19 -08001694
Alexander Duyck8be33e92015-03-04 14:59:19 -08001695 cb->args[3] = key;
1696 cb->args[2] = count;
1697
Robert Olsson19baf832005-06-21 12:43:18 -07001698 return skb->len;
Robert Olsson19baf832005-06-21 12:43:18 -07001699}
1700
David S. Miller5348ba82011-02-01 15:30:56 -08001701void __init fib_trie_init(void)
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001702{
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001703 fn_alias_kmem = kmem_cache_create("ip_fib_alias",
1704 sizeof(struct fib_alias),
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -08001705 0, SLAB_PANIC, NULL);
1706
1707 trie_leaf_kmem = kmem_cache_create("ip_fib_trie",
Alexander Duyck41b489f2015-03-04 15:02:33 -08001708 LEAF_SIZE,
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -08001709 0, SLAB_PANIC, NULL);
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001710}
Robert Olsson19baf832005-06-21 12:43:18 -07001711
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001712
David S. Miller5348ba82011-02-01 15:30:56 -08001713struct fib_table *fib_trie_table(u32 id)
Robert Olsson19baf832005-06-21 12:43:18 -07001714{
1715 struct fib_table *tb;
1716 struct trie *t;
1717
Robert Olsson19baf832005-06-21 12:43:18 -07001718 tb = kmalloc(sizeof(struct fib_table) + sizeof(struct trie),
1719 GFP_KERNEL);
1720 if (tb == NULL)
1721 return NULL;
1722
1723 tb->tb_id = id;
Denis V. Lunev971b8932007-12-08 00:32:23 -08001724 tb->tb_default = -1;
David S. Miller21d8c492011-04-14 14:49:37 -07001725 tb->tb_num_default = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001726
1727 t = (struct trie *) tb->tb_data;
Alexander Duyck8274a972014-12-31 10:55:29 -08001728 RCU_INIT_POINTER(t->trie, NULL);
1729#ifdef CONFIG_IP_FIB_TRIE_STATS
1730 t->stats = alloc_percpu(struct trie_use_stats);
1731 if (!t->stats) {
1732 kfree(tb);
1733 tb = NULL;
1734 }
1735#endif
Robert Olsson19baf832005-06-21 12:43:18 -07001736
Robert Olsson19baf832005-06-21 12:43:18 -07001737 return tb;
1738}
1739
Robert Olsson19baf832005-06-21 12:43:18 -07001740#ifdef CONFIG_PROC_FS
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001741/* Depth first Trie walk iterator */
1742struct fib_trie_iter {
Denis V. Lunev1c340b22008-01-10 03:27:17 -08001743 struct seq_net_private p;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001744 struct fib_table *tb;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001745 struct tnode *tnode;
Eric Dumazeta034ee32010-09-09 23:32:28 +00001746 unsigned int index;
1747 unsigned int depth;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001748};
Robert Olsson19baf832005-06-21 12:43:18 -07001749
Alexander Duyckadaf9812014-12-31 10:55:47 -08001750static struct tnode *fib_trie_get_next(struct fib_trie_iter *iter)
Robert Olsson19baf832005-06-21 12:43:18 -07001751{
Alexander Duyck98293e82014-12-31 10:56:18 -08001752 unsigned long cindex = iter->index;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001753 struct tnode *tn = iter->tnode;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001754 struct tnode *p;
1755
Eric W. Biederman6640e692007-01-24 14:42:04 -08001756 /* A single entry routing table */
1757 if (!tn)
1758 return NULL;
1759
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001760 pr_debug("get_next iter={node=%p index=%d depth=%d}\n",
1761 iter->tnode, iter->index, iter->depth);
1762rescan:
Alexander Duyck98293e82014-12-31 10:56:18 -08001763 while (cindex < tnode_child_length(tn)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08001764 struct tnode *n = tnode_get_child_rcu(tn, cindex);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001765
1766 if (n) {
1767 if (IS_LEAF(n)) {
1768 iter->tnode = tn;
1769 iter->index = cindex + 1;
1770 } else {
1771 /* push down one level */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001772 iter->tnode = n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001773 iter->index = 0;
1774 ++iter->depth;
1775 }
1776 return n;
1777 }
1778
1779 ++cindex;
1780 }
1781
1782 /* Current node exhausted, pop back up */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001783 p = node_parent_rcu(tn);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001784 if (p) {
Alexander Duycke9b44012014-12-31 10:56:12 -08001785 cindex = get_index(tn->key, p) + 1;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001786 tn = p;
1787 --iter->depth;
1788 goto rescan;
1789 }
1790
1791 /* got root? */
Robert Olsson19baf832005-06-21 12:43:18 -07001792 return NULL;
1793}
1794
Alexander Duyckadaf9812014-12-31 10:55:47 -08001795static struct tnode *fib_trie_get_first(struct fib_trie_iter *iter,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001796 struct trie *t)
Robert Olsson19baf832005-06-21 12:43:18 -07001797{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001798 struct tnode *n;
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08001799
Stephen Hemminger132adf52007-03-08 20:44:43 -08001800 if (!t)
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08001801 return NULL;
1802
1803 n = rcu_dereference(t->trie);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001804 if (!n)
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08001805 return NULL;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001806
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001807 if (IS_TNODE(n)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08001808 iter->tnode = n;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001809 iter->index = 0;
1810 iter->depth = 1;
1811 } else {
1812 iter->tnode = NULL;
1813 iter->index = 0;
1814 iter->depth = 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001815 }
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001816
1817 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07001818}
1819
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001820static void trie_collect_stats(struct trie *t, struct trie_stat *s)
Robert Olsson19baf832005-06-21 12:43:18 -07001821{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001822 struct tnode *n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001823 struct fib_trie_iter iter;
Robert Olsson19baf832005-06-21 12:43:18 -07001824
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001825 memset(s, 0, sizeof(*s));
Robert Olsson19baf832005-06-21 12:43:18 -07001826
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001827 rcu_read_lock();
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001828 for (n = fib_trie_get_first(&iter, t); n; n = fib_trie_get_next(&iter)) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001829 if (IS_LEAF(n)) {
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001830 struct fib_alias *fa;
Stephen Hemminger93672292008-01-22 21:54:05 -08001831
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001832 s->leaves++;
1833 s->totdepth += iter.depth;
1834 if (iter.depth > s->maxdepth)
1835 s->maxdepth = iter.depth;
Stephen Hemminger93672292008-01-22 21:54:05 -08001836
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001837 hlist_for_each_entry_rcu(fa, &n->leaf, fa_list)
Stephen Hemminger93672292008-01-22 21:54:05 -08001838 ++s->prefixes;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001839 } else {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001840 s->tnodes++;
Alexander Duyckadaf9812014-12-31 10:55:47 -08001841 if (n->bits < MAX_STAT_DEPTH)
1842 s->nodesizes[n->bits]++;
Alexander Duyck30cfe7c2015-01-22 15:51:33 -08001843 s->nullpointers += n->empty_children;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001844 }
1845 }
1846 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07001847}
1848
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001849/*
Robert Olsson19baf832005-06-21 12:43:18 -07001850 * This outputs /proc/net/fib_triestats
Robert Olsson19baf832005-06-21 12:43:18 -07001851 */
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001852static void trie_show_stats(struct seq_file *seq, struct trie_stat *stat)
Robert Olsson19baf832005-06-21 12:43:18 -07001853{
Eric Dumazeta034ee32010-09-09 23:32:28 +00001854 unsigned int i, max, pointers, bytes, avdepth;
Robert Olsson19baf832005-06-21 12:43:18 -07001855
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001856 if (stat->leaves)
1857 avdepth = stat->totdepth*100 / stat->leaves;
1858 else
1859 avdepth = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001860
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001861 seq_printf(seq, "\tAver depth: %u.%02d\n",
1862 avdepth / 100, avdepth % 100);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001863 seq_printf(seq, "\tMax depth: %u\n", stat->maxdepth);
Robert Olsson19baf832005-06-21 12:43:18 -07001864
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001865 seq_printf(seq, "\tLeaves: %u\n", stat->leaves);
Alexander Duyck41b489f2015-03-04 15:02:33 -08001866 bytes = LEAF_SIZE * stat->leaves;
Stephen Hemminger93672292008-01-22 21:54:05 -08001867
1868 seq_printf(seq, "\tPrefixes: %u\n", stat->prefixes);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001869 bytes += sizeof(struct fib_alias) * stat->prefixes;
Stephen Hemminger93672292008-01-22 21:54:05 -08001870
Stephen Hemminger187b5182008-01-12 20:55:55 -08001871 seq_printf(seq, "\tInternal nodes: %u\n\t", stat->tnodes);
Alexander Duyck41b489f2015-03-04 15:02:33 -08001872 bytes += TNODE_SIZE(0) * stat->tnodes;
Robert Olsson19baf832005-06-21 12:43:18 -07001873
Robert Olsson06ef9212006-03-20 21:35:01 -08001874 max = MAX_STAT_DEPTH;
1875 while (max > 0 && stat->nodesizes[max-1] == 0)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001876 max--;
Robert Olsson19baf832005-06-21 12:43:18 -07001877
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001878 pointers = 0;
Jerry Snitselaarf585a992013-07-22 12:01:58 -07001879 for (i = 1; i < max; i++)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001880 if (stat->nodesizes[i] != 0) {
Stephen Hemminger187b5182008-01-12 20:55:55 -08001881 seq_printf(seq, " %u: %u", i, stat->nodesizes[i]);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001882 pointers += (1<<i) * stat->nodesizes[i];
1883 }
1884 seq_putc(seq, '\n');
Stephen Hemminger187b5182008-01-12 20:55:55 -08001885 seq_printf(seq, "\tPointers: %u\n", pointers);
Robert Olsson19baf832005-06-21 12:43:18 -07001886
Alexander Duyckadaf9812014-12-31 10:55:47 -08001887 bytes += sizeof(struct tnode *) * pointers;
Stephen Hemminger187b5182008-01-12 20:55:55 -08001888 seq_printf(seq, "Null ptrs: %u\n", stat->nullpointers);
1889 seq_printf(seq, "Total size: %u kB\n", (bytes + 1023) / 1024);
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08001890}
Robert Olsson19baf832005-06-21 12:43:18 -07001891
1892#ifdef CONFIG_IP_FIB_TRIE_STATS
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08001893static void trie_show_usage(struct seq_file *seq,
Alexander Duyck8274a972014-12-31 10:55:29 -08001894 const struct trie_use_stats __percpu *stats)
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08001895{
Alexander Duyck8274a972014-12-31 10:55:29 -08001896 struct trie_use_stats s = { 0 };
1897 int cpu;
1898
1899 /* loop through all of the CPUs and gather up the stats */
1900 for_each_possible_cpu(cpu) {
1901 const struct trie_use_stats *pcpu = per_cpu_ptr(stats, cpu);
1902
1903 s.gets += pcpu->gets;
1904 s.backtrack += pcpu->backtrack;
1905 s.semantic_match_passed += pcpu->semantic_match_passed;
1906 s.semantic_match_miss += pcpu->semantic_match_miss;
1907 s.null_node_hit += pcpu->null_node_hit;
1908 s.resize_node_skipped += pcpu->resize_node_skipped;
1909 }
1910
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08001911 seq_printf(seq, "\nCounters:\n---------\n");
Alexander Duyck8274a972014-12-31 10:55:29 -08001912 seq_printf(seq, "gets = %u\n", s.gets);
1913 seq_printf(seq, "backtracks = %u\n", s.backtrack);
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001914 seq_printf(seq, "semantic match passed = %u\n",
Alexander Duyck8274a972014-12-31 10:55:29 -08001915 s.semantic_match_passed);
1916 seq_printf(seq, "semantic match miss = %u\n", s.semantic_match_miss);
1917 seq_printf(seq, "null node hit= %u\n", s.null_node_hit);
1918 seq_printf(seq, "skipped node resize = %u\n\n", s.resize_node_skipped);
Robert Olsson19baf832005-06-21 12:43:18 -07001919}
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08001920#endif /* CONFIG_IP_FIB_TRIE_STATS */
1921
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001922static void fib_table_print(struct seq_file *seq, struct fib_table *tb)
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08001923{
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001924 if (tb->tb_id == RT_TABLE_LOCAL)
1925 seq_puts(seq, "Local:\n");
1926 else if (tb->tb_id == RT_TABLE_MAIN)
1927 seq_puts(seq, "Main:\n");
1928 else
1929 seq_printf(seq, "Id %d:\n", tb->tb_id);
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08001930}
Robert Olsson19baf832005-06-21 12:43:18 -07001931
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001932
Robert Olsson19baf832005-06-21 12:43:18 -07001933static int fib_triestat_seq_show(struct seq_file *seq, void *v)
1934{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08001935 struct net *net = (struct net *)seq->private;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001936 unsigned int h;
Eric W. Biederman877a9bf2007-12-07 00:47:47 -08001937
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08001938 seq_printf(seq,
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001939 "Basic info: size of leaf:"
1940 " %Zd bytes, size of tnode: %Zd bytes.\n",
Alexander Duyck41b489f2015-03-04 15:02:33 -08001941 LEAF_SIZE, TNODE_SIZE(0));
Olof Johansson91b9a272005-08-09 20:24:39 -07001942
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001943 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
1944 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001945 struct fib_table *tb;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001946
Sasha Levinb67bfe02013-02-27 17:06:00 -08001947 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001948 struct trie *t = (struct trie *) tb->tb_data;
1949 struct trie_stat stat;
1950
1951 if (!t)
1952 continue;
1953
1954 fib_table_print(seq, tb);
1955
1956 trie_collect_stats(t, &stat);
1957 trie_show_stats(seq, &stat);
1958#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -08001959 trie_show_usage(seq, t->stats);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001960#endif
1961 }
1962 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001963
Robert Olsson19baf832005-06-21 12:43:18 -07001964 return 0;
1965}
1966
Robert Olsson19baf832005-06-21 12:43:18 -07001967static int fib_triestat_seq_open(struct inode *inode, struct file *file)
1968{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07001969 return single_open_net(inode, file, fib_triestat_seq_show);
Denis V. Lunev1c340b22008-01-10 03:27:17 -08001970}
1971
Arjan van de Ven9a321442007-02-12 00:55:35 -08001972static const struct file_operations fib_triestat_fops = {
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001973 .owner = THIS_MODULE,
1974 .open = fib_triestat_seq_open,
1975 .read = seq_read,
1976 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07001977 .release = single_release_net,
Robert Olsson19baf832005-06-21 12:43:18 -07001978};
1979
Alexander Duyckadaf9812014-12-31 10:55:47 -08001980static struct tnode *fib_trie_get_idx(struct seq_file *seq, loff_t pos)
Robert Olsson19baf832005-06-21 12:43:18 -07001981{
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001982 struct fib_trie_iter *iter = seq->private;
1983 struct net *net = seq_file_net(seq);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001984 loff_t idx = 0;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001985 unsigned int h;
Robert Olsson19baf832005-06-21 12:43:18 -07001986
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001987 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
1988 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001989 struct fib_table *tb;
1990
Sasha Levinb67bfe02013-02-27 17:06:00 -08001991 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08001992 struct tnode *n;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001993
1994 for (n = fib_trie_get_first(iter,
1995 (struct trie *) tb->tb_data);
1996 n; n = fib_trie_get_next(iter))
1997 if (pos == idx++) {
1998 iter->tb = tb;
1999 return n;
2000 }
2001 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002002 }
Robert Olsson19baf832005-06-21 12:43:18 -07002003
Robert Olsson19baf832005-06-21 12:43:18 -07002004 return NULL;
2005}
2006
2007static void *fib_trie_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08002008 __acquires(RCU)
Robert Olsson19baf832005-06-21 12:43:18 -07002009{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002010 rcu_read_lock();
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002011 return fib_trie_get_idx(seq, *pos);
Robert Olsson19baf832005-06-21 12:43:18 -07002012}
2013
2014static void *fib_trie_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2015{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002016 struct fib_trie_iter *iter = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002017 struct net *net = seq_file_net(seq);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002018 struct fib_table *tb = iter->tb;
2019 struct hlist_node *tb_node;
2020 unsigned int h;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002021 struct tnode *n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002022
Robert Olsson19baf832005-06-21 12:43:18 -07002023 ++*pos;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002024 /* next node in same table */
2025 n = fib_trie_get_next(iter);
2026 if (n)
2027 return n;
Olof Johansson91b9a272005-08-09 20:24:39 -07002028
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002029 /* walk rest of this hash chain */
2030 h = tb->tb_id & (FIB_TABLE_HASHSZ - 1);
Eric Dumazet0a5c0472011-03-31 01:51:35 -07002031 while ((tb_node = rcu_dereference(hlist_next_rcu(&tb->tb_hlist)))) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002032 tb = hlist_entry(tb_node, struct fib_table, tb_hlist);
2033 n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
2034 if (n)
2035 goto found;
2036 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002037
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002038 /* new hash chain */
2039 while (++h < FIB_TABLE_HASHSZ) {
2040 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002041 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002042 n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
2043 if (n)
2044 goto found;
2045 }
2046 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002047 return NULL;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002048
2049found:
2050 iter->tb = tb;
2051 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07002052}
2053
2054static void fib_trie_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08002055 __releases(RCU)
Robert Olsson19baf832005-06-21 12:43:18 -07002056{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002057 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07002058}
2059
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002060static void seq_indent(struct seq_file *seq, int n)
2061{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002062 while (n-- > 0)
2063 seq_puts(seq, " ");
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002064}
Robert Olsson19baf832005-06-21 12:43:18 -07002065
Eric Dumazet28d36e32008-01-14 23:09:56 -08002066static inline const char *rtn_scope(char *buf, size_t len, enum rt_scope_t s)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002067{
Stephen Hemminger132adf52007-03-08 20:44:43 -08002068 switch (s) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002069 case RT_SCOPE_UNIVERSE: return "universe";
2070 case RT_SCOPE_SITE: return "site";
2071 case RT_SCOPE_LINK: return "link";
2072 case RT_SCOPE_HOST: return "host";
2073 case RT_SCOPE_NOWHERE: return "nowhere";
2074 default:
Eric Dumazet28d36e32008-01-14 23:09:56 -08002075 snprintf(buf, len, "scope=%d", s);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002076 return buf;
2077 }
2078}
2079
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -07002080static const char *const rtn_type_names[__RTN_MAX] = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002081 [RTN_UNSPEC] = "UNSPEC",
2082 [RTN_UNICAST] = "UNICAST",
2083 [RTN_LOCAL] = "LOCAL",
2084 [RTN_BROADCAST] = "BROADCAST",
2085 [RTN_ANYCAST] = "ANYCAST",
2086 [RTN_MULTICAST] = "MULTICAST",
2087 [RTN_BLACKHOLE] = "BLACKHOLE",
2088 [RTN_UNREACHABLE] = "UNREACHABLE",
2089 [RTN_PROHIBIT] = "PROHIBIT",
2090 [RTN_THROW] = "THROW",
2091 [RTN_NAT] = "NAT",
2092 [RTN_XRESOLVE] = "XRESOLVE",
2093};
2094
Eric Dumazeta034ee32010-09-09 23:32:28 +00002095static inline const char *rtn_type(char *buf, size_t len, unsigned int t)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002096{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002097 if (t < __RTN_MAX && rtn_type_names[t])
2098 return rtn_type_names[t];
Eric Dumazet28d36e32008-01-14 23:09:56 -08002099 snprintf(buf, len, "type %u", t);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002100 return buf;
2101}
2102
2103/* Pretty print the trie */
Robert Olsson19baf832005-06-21 12:43:18 -07002104static int fib_trie_seq_show(struct seq_file *seq, void *v)
2105{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002106 const struct fib_trie_iter *iter = seq->private;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002107 struct tnode *n = v;
Robert Olsson19baf832005-06-21 12:43:18 -07002108
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002109 if (!node_parent_rcu(n))
2110 fib_table_print(seq, iter->tb);
Robert Olsson095b8502007-01-26 19:06:01 -08002111
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002112 if (IS_TNODE(n)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002113 __be32 prf = htonl(n->key);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002114
Alexander Duycke9b44012014-12-31 10:56:12 -08002115 seq_indent(seq, iter->depth-1);
2116 seq_printf(seq, " +-- %pI4/%zu %u %u %u\n",
2117 &prf, KEYLENGTH - n->pos - n->bits, n->bits,
2118 n->full_children, n->empty_children);
Olof Johansson91b9a272005-08-09 20:24:39 -07002119 } else {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002120 __be32 val = htonl(n->key);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002121 struct fib_alias *fa;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002122
2123 seq_indent(seq, iter->depth);
Harvey Harrison673d57e2008-10-31 00:53:57 -07002124 seq_printf(seq, " |-- %pI4\n", &val);
Eric Dumazet28d36e32008-01-14 23:09:56 -08002125
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002126 hlist_for_each_entry_rcu(fa, &n->leaf, fa_list) {
2127 char buf1[32], buf2[32];
Eric Dumazet28d36e32008-01-14 23:09:56 -08002128
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002129 seq_indent(seq, iter->depth + 1);
2130 seq_printf(seq, " /%zu %s %s",
2131 KEYLENGTH - fa->fa_slen,
2132 rtn_scope(buf1, sizeof(buf1),
2133 fa->fa_info->fib_scope),
2134 rtn_type(buf2, sizeof(buf2),
2135 fa->fa_type));
2136 if (fa->fa_tos)
2137 seq_printf(seq, " tos=%d", fa->fa_tos);
2138 seq_putc(seq, '\n');
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002139 }
Robert Olsson19baf832005-06-21 12:43:18 -07002140 }
2141
2142 return 0;
2143}
2144
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002145static const struct seq_operations fib_trie_seq_ops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002146 .start = fib_trie_seq_start,
2147 .next = fib_trie_seq_next,
2148 .stop = fib_trie_seq_stop,
2149 .show = fib_trie_seq_show,
Robert Olsson19baf832005-06-21 12:43:18 -07002150};
2151
2152static int fib_trie_seq_open(struct inode *inode, struct file *file)
2153{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002154 return seq_open_net(inode, file, &fib_trie_seq_ops,
2155 sizeof(struct fib_trie_iter));
Robert Olsson19baf832005-06-21 12:43:18 -07002156}
2157
Arjan van de Ven9a321442007-02-12 00:55:35 -08002158static const struct file_operations fib_trie_fops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002159 .owner = THIS_MODULE,
2160 .open = fib_trie_seq_open,
2161 .read = seq_read,
2162 .llseek = seq_lseek,
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002163 .release = seq_release_net,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002164};
2165
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002166struct fib_route_iter {
2167 struct seq_net_private p;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002168 struct fib_table *main_tb;
2169 struct tnode *tnode;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002170 loff_t pos;
2171 t_key key;
2172};
2173
Alexander Duyckadaf9812014-12-31 10:55:47 -08002174static struct tnode *fib_route_get_idx(struct fib_route_iter *iter, loff_t pos)
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002175{
Alexander Duyck8be33e92015-03-04 14:59:19 -08002176 struct fib_table *tb = iter->main_tb;
2177 struct tnode *l, **tp = &iter->tnode;
2178 struct trie *t;
2179 t_key key;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002180
Alexander Duyck8be33e92015-03-04 14:59:19 -08002181 /* use cache location of next-to-find key */
2182 if (iter->pos > 0 && pos >= iter->pos) {
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002183 pos -= iter->pos;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002184 key = iter->key;
2185 } else {
2186 t = (struct trie *)tb->tb_data;
2187 iter->tnode = rcu_dereference_rtnl(t->trie);
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002188 iter->pos = 0;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002189 key = 0;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002190 }
2191
Alexander Duyck8be33e92015-03-04 14:59:19 -08002192 while ((l = leaf_walk_rcu(tp, key)) != NULL) {
2193 key = l->key + 1;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002194 iter->pos++;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002195
2196 if (pos-- <= 0)
2197 break;
2198
2199 l = NULL;
2200
2201 /* handle unlikely case of a key wrap */
2202 if (!key)
2203 break;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002204 }
2205
2206 if (l)
Alexander Duyck8be33e92015-03-04 14:59:19 -08002207 iter->key = key; /* remember it */
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002208 else
2209 iter->pos = 0; /* forget it */
2210
2211 return l;
2212}
2213
2214static void *fib_route_seq_start(struct seq_file *seq, loff_t *pos)
2215 __acquires(RCU)
2216{
2217 struct fib_route_iter *iter = seq->private;
2218 struct fib_table *tb;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002219 struct trie *t;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002220
2221 rcu_read_lock();
Alexander Duyck8be33e92015-03-04 14:59:19 -08002222
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002223 tb = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN);
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002224 if (!tb)
2225 return NULL;
2226
Alexander Duyck8be33e92015-03-04 14:59:19 -08002227 iter->main_tb = tb;
2228
2229 if (*pos != 0)
2230 return fib_route_get_idx(iter, *pos);
2231
2232 t = (struct trie *)tb->tb_data;
2233 iter->tnode = rcu_dereference_rtnl(t->trie);
2234 iter->pos = 0;
2235 iter->key = 0;
2236
2237 return SEQ_START_TOKEN;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002238}
2239
2240static void *fib_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2241{
2242 struct fib_route_iter *iter = seq->private;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002243 struct tnode *l = NULL;
2244 t_key key = iter->key;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002245
2246 ++*pos;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002247
2248 /* only allow key of 0 for start of sequence */
2249 if ((v == SEQ_START_TOKEN) || key)
2250 l = leaf_walk_rcu(&iter->tnode, key);
2251
2252 if (l) {
2253 iter->key = l->key + 1;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002254 iter->pos++;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002255 } else {
2256 iter->pos = 0;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002257 }
2258
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002259 return l;
2260}
2261
2262static void fib_route_seq_stop(struct seq_file *seq, void *v)
2263 __releases(RCU)
2264{
2265 rcu_read_unlock();
2266}
2267
Eric Dumazeta034ee32010-09-09 23:32:28 +00002268static unsigned int fib_flag_trans(int type, __be32 mask, const struct fib_info *fi)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002269{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002270 unsigned int flags = 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002271
Eric Dumazeta034ee32010-09-09 23:32:28 +00002272 if (type == RTN_UNREACHABLE || type == RTN_PROHIBIT)
2273 flags = RTF_REJECT;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002274 if (fi && fi->fib_nh->nh_gw)
2275 flags |= RTF_GATEWAY;
Al Viro32ab5f82006-09-26 22:21:45 -07002276 if (mask == htonl(0xFFFFFFFF))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002277 flags |= RTF_HOST;
2278 flags |= RTF_UP;
2279 return flags;
2280}
2281
2282/*
2283 * This outputs /proc/net/route.
2284 * The format of the file is not supposed to be changed
Eric Dumazeta034ee32010-09-09 23:32:28 +00002285 * and needs to be same as fib_hash output to avoid breaking
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002286 * legacy utilities
2287 */
2288static int fib_route_seq_show(struct seq_file *seq, void *v)
2289{
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002290 struct fib_alias *fa;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002291 struct tnode *l = v;
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08002292 __be32 prefix;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002293
2294 if (v == SEQ_START_TOKEN) {
2295 seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
2296 "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
2297 "\tWindow\tIRTT");
2298 return 0;
2299 }
2300
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08002301 prefix = htonl(l->key);
2302
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002303 hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
2304 const struct fib_info *fi = fa->fa_info;
2305 __be32 mask = inet_make_mask(KEYLENGTH - fa->fa_slen);
2306 unsigned int flags = fib_flag_trans(fa->fa_type, mask, fi);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002307
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002308 if ((fa->fa_type == RTN_BROADCAST) ||
2309 (fa->fa_type == RTN_MULTICAST))
2310 continue;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002311
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002312 seq_setwidth(seq, 127);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002313
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002314 if (fi)
2315 seq_printf(seq,
2316 "%s\t%08X\t%08X\t%04X\t%d\t%u\t"
2317 "%d\t%08X\t%d\t%u\t%u",
2318 fi->fib_dev ? fi->fib_dev->name : "*",
2319 prefix,
2320 fi->fib_nh->nh_gw, flags, 0, 0,
2321 fi->fib_priority,
2322 mask,
2323 (fi->fib_advmss ?
2324 fi->fib_advmss + 40 : 0),
2325 fi->fib_window,
2326 fi->fib_rtt >> 3);
2327 else
2328 seq_printf(seq,
2329 "*\t%08X\t%08X\t%04X\t%d\t%u\t"
2330 "%d\t%08X\t%d\t%u\t%u",
2331 prefix, 0, flags, 0, 0, 0,
2332 mask, 0, 0, 0);
Tetsuo Handa652586d2013-11-14 14:31:57 -08002333
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002334 seq_pad(seq, '\n');
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002335 }
2336
2337 return 0;
2338}
2339
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002340static const struct seq_operations fib_route_seq_ops = {
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002341 .start = fib_route_seq_start,
2342 .next = fib_route_seq_next,
2343 .stop = fib_route_seq_stop,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002344 .show = fib_route_seq_show,
2345};
2346
2347static int fib_route_seq_open(struct inode *inode, struct file *file)
2348{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002349 return seq_open_net(inode, file, &fib_route_seq_ops,
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002350 sizeof(struct fib_route_iter));
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002351}
2352
Arjan van de Ven9a321442007-02-12 00:55:35 -08002353static const struct file_operations fib_route_fops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002354 .owner = THIS_MODULE,
2355 .open = fib_route_seq_open,
2356 .read = seq_read,
2357 .llseek = seq_lseek,
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002358 .release = seq_release_net,
Robert Olsson19baf832005-06-21 12:43:18 -07002359};
2360
Denis V. Lunev61a02652008-01-10 03:21:09 -08002361int __net_init fib_proc_init(struct net *net)
Robert Olsson19baf832005-06-21 12:43:18 -07002362{
Gao fengd4beaa62013-02-18 01:34:54 +00002363 if (!proc_create("fib_trie", S_IRUGO, net->proc_net, &fib_trie_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002364 goto out1;
2365
Gao fengd4beaa62013-02-18 01:34:54 +00002366 if (!proc_create("fib_triestat", S_IRUGO, net->proc_net,
2367 &fib_triestat_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002368 goto out2;
2369
Gao fengd4beaa62013-02-18 01:34:54 +00002370 if (!proc_create("route", S_IRUGO, net->proc_net, &fib_route_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002371 goto out3;
2372
Robert Olsson19baf832005-06-21 12:43:18 -07002373 return 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002374
2375out3:
Gao fengece31ff2013-02-18 01:34:56 +00002376 remove_proc_entry("fib_triestat", net->proc_net);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002377out2:
Gao fengece31ff2013-02-18 01:34:56 +00002378 remove_proc_entry("fib_trie", net->proc_net);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002379out1:
2380 return -ENOMEM;
Robert Olsson19baf832005-06-21 12:43:18 -07002381}
2382
Denis V. Lunev61a02652008-01-10 03:21:09 -08002383void __net_exit fib_proc_exit(struct net *net)
Robert Olsson19baf832005-06-21 12:43:18 -07002384{
Gao fengece31ff2013-02-18 01:34:56 +00002385 remove_proc_entry("fib_trie", net->proc_net);
2386 remove_proc_entry("fib_triestat", net->proc_net);
2387 remove_proc_entry("route", net->proc_net);
Robert Olsson19baf832005-06-21 12:43:18 -07002388}
2389
2390#endif /* CONFIG_PROC_FS */