blob: d28362db51a0e8bd3efb23ec13d64b687b29cbf5 [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 {
97 t_key key;
98 unsigned char bits; /* 2log(KEYLENGTH) bits needed */
99 unsigned char pos; /* 2log(KEYLENGTH) bits needed */
Alexander Duyck5405afd2014-12-31 10:57:08 -0800100 unsigned char slen;
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800101 struct tnode __rcu *parent;
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800102 struct rcu_head rcu;
Alexander Duyckadaf9812014-12-31 10:55:47 -0800103 union {
104 /* The fields in this struct are valid if bits > 0 (TNODE) */
105 struct {
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800106 t_key empty_children; /* KEYLENGTH bits needed */
107 t_key full_children; /* KEYLENGTH bits needed */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800108 struct tnode __rcu *child[0];
109 };
110 /* This list pointer if valid if bits == 0 (LEAF) */
111 struct hlist_head list;
112 };
Robert Olsson19baf832005-06-21 12:43:18 -0700113};
114
115struct leaf_info {
116 struct hlist_node hlist;
Alexander Duyck5786ec62015-02-25 15:31:37 -0800117 unsigned char slen;
Alexander Duyck56315f92015-02-25 15:31:31 -0800118 struct hlist_head falh;
Eric Dumazet5c745012011-07-18 03:16:33 +0000119 struct rcu_head rcu;
Robert Olsson19baf832005-06-21 12:43:18 -0700120};
121
Robert Olsson19baf832005-06-21 12:43:18 -0700122#ifdef CONFIG_IP_FIB_TRIE_STATS
123struct trie_use_stats {
124 unsigned int gets;
125 unsigned int backtrack;
126 unsigned int semantic_match_passed;
127 unsigned int semantic_match_miss;
128 unsigned int null_node_hit;
Robert Olsson2f368952005-07-05 15:02:40 -0700129 unsigned int resize_node_skipped;
Robert Olsson19baf832005-06-21 12:43:18 -0700130};
131#endif
132
133struct trie_stat {
134 unsigned int totdepth;
135 unsigned int maxdepth;
136 unsigned int tnodes;
137 unsigned int leaves;
138 unsigned int nullpointers;
Stephen Hemminger93672292008-01-22 21:54:05 -0800139 unsigned int prefixes;
Robert Olsson06ef9212006-03-20 21:35:01 -0800140 unsigned int nodesizes[MAX_STAT_DEPTH];
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700141};
Robert Olsson19baf832005-06-21 12:43:18 -0700142
143struct trie {
Alexander Duyckadaf9812014-12-31 10:55:47 -0800144 struct tnode __rcu *trie;
Robert Olsson19baf832005-06-21 12:43:18 -0700145#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -0800146 struct trie_use_stats __percpu *stats;
Robert Olsson19baf832005-06-21 12:43:18 -0700147#endif
Robert Olsson19baf832005-06-21 12:43:18 -0700148};
149
Alexander Duyckff181ed2014-12-31 10:56:43 -0800150static void resize(struct trie *t, struct tnode *tn);
Jarek Poplawskic3059472009-07-14 08:33:08 +0000151static size_t tnode_free_size;
152
153/*
154 * synchronize_rcu after call_rcu for that many pages; it should be especially
155 * useful before resizing the root node with PREEMPT_NONE configs; the value was
156 * obtained experimentally, aiming to avoid visible slowdown.
157 */
158static const int sync_pages = 128;
Robert Olsson19baf832005-06-21 12:43:18 -0700159
Christoph Lametere18b8902006-12-06 20:33:20 -0800160static struct kmem_cache *fn_alias_kmem __read_mostly;
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -0800161static struct kmem_cache *trie_leaf_kmem __read_mostly;
Robert Olsson19baf832005-06-21 12:43:18 -0700162
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800163/* caller must hold RTNL */
164#define node_parent(n) rtnl_dereference((n)->parent)
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700165
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800166/* caller must hold RCU read lock or RTNL */
167#define node_parent_rcu(n) rcu_dereference_rtnl((n)->parent)
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700168
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800169/* wrapper for rcu_assign_pointer */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800170static inline void node_set_parent(struct tnode *n, struct tnode *tp)
Stephen Hemminger06801912007-08-10 15:22:13 -0700171{
Alexander Duyckadaf9812014-12-31 10:55:47 -0800172 if (n)
173 rcu_assign_pointer(n->parent, tp);
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800174}
175
176#define NODE_INIT_PARENT(n, p) RCU_INIT_POINTER((n)->parent, p)
177
178/* This provides us with the number of children in this node, in the case of a
179 * leaf this will return 0 meaning none of the children are accessible.
180 */
Alexander Duyck98293e82014-12-31 10:56:18 -0800181static inline unsigned long tnode_child_length(const struct tnode *tn)
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800182{
183 return (1ul << tn->bits) & ~(1ul);
Stephen Hemminger06801912007-08-10 15:22:13 -0700184}
Robert Olsson2373ce12005-08-25 13:01:29 -0700185
Alexander Duyck98293e82014-12-31 10:56:18 -0800186/* caller must hold RTNL */
187static inline struct tnode *tnode_get_child(const struct tnode *tn,
188 unsigned long i)
Robert Olsson19baf832005-06-21 12:43:18 -0700189{
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700190 return rtnl_dereference(tn->child[i]);
Eric Dumazetb59cfbf2008-01-18 03:31:36 -0800191}
192
Alexander Duyck98293e82014-12-31 10:56:18 -0800193/* caller must hold RCU read lock or RTNL */
194static inline struct tnode *tnode_get_child_rcu(const struct tnode *tn,
195 unsigned long i)
Eric Dumazetb59cfbf2008-01-18 03:31:36 -0800196{
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700197 return rcu_dereference_rtnl(tn->child[i]);
Robert Olsson19baf832005-06-21 12:43:18 -0700198}
199
Alexander Duycke9b44012014-12-31 10:56:12 -0800200/* To understand this stuff, an understanding of keys and all their bits is
201 * necessary. Every node in the trie has a key associated with it, but not
202 * all of the bits in that key are significant.
203 *
204 * Consider a node 'n' and its parent 'tp'.
205 *
206 * If n is a leaf, every bit in its key is significant. Its presence is
207 * necessitated by path compression, since during a tree traversal (when
208 * searching for a leaf - unless we are doing an insertion) we will completely
209 * ignore all skipped bits we encounter. Thus we need to verify, at the end of
210 * a potentially successful search, that we have indeed been walking the
211 * correct key path.
212 *
213 * Note that we can never "miss" the correct key in the tree if present by
214 * following the wrong path. Path compression ensures that segments of the key
215 * that are the same for all keys with a given prefix are skipped, but the
216 * skipped part *is* identical for each node in the subtrie below the skipped
217 * bit! trie_insert() in this implementation takes care of that.
218 *
219 * if n is an internal node - a 'tnode' here, the various parts of its key
220 * have many different meanings.
221 *
222 * Example:
223 * _________________________________________________________________
224 * | i | i | i | i | i | i | i | N | N | N | S | S | S | S | S | C |
225 * -----------------------------------------------------------------
226 * 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
227 *
228 * _________________________________________________________________
229 * | C | C | C | u | u | u | u | u | u | u | u | u | u | u | u | u |
230 * -----------------------------------------------------------------
231 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
232 *
233 * tp->pos = 22
234 * tp->bits = 3
235 * n->pos = 13
236 * n->bits = 4
237 *
238 * First, let's just ignore the bits that come before the parent tp, that is
239 * the bits from (tp->pos + tp->bits) to 31. They are *known* but at this
240 * point we do not use them for anything.
241 *
242 * The bits from (tp->pos) to (tp->pos + tp->bits - 1) - "N", above - are the
243 * index into the parent's child array. That is, they will be used to find
244 * 'n' among tp's children.
245 *
246 * The bits from (n->pos + n->bits) to (tn->pos - 1) - "S" - are skipped bits
247 * for the node n.
248 *
249 * All the bits we have seen so far are significant to the node n. The rest
250 * of the bits are really not needed or indeed known in n->key.
251 *
252 * The bits from (n->pos) to (n->pos + n->bits - 1) - "C" - are the index into
253 * n's child array, and will of course be different for each child.
254 *
255 * The rest of the bits, from 0 to (n->pos + n->bits), are completely unknown
256 * at this point.
257 */
Robert Olsson19baf832005-06-21 12:43:18 -0700258
Denis V. Lunevf5026fa2007-12-13 09:47:57 -0800259static const int halve_threshold = 25;
260static const int inflate_threshold = 50;
Jarek Poplawski345aa032009-07-07 19:39:16 -0700261static const int halve_threshold_root = 15;
Jens Låås80b71b82009-08-28 23:57:15 -0700262static const int inflate_threshold_root = 30;
Robert Olsson2373ce12005-08-25 13:01:29 -0700263
264static void __alias_free_mem(struct rcu_head *head)
265{
266 struct fib_alias *fa = container_of(head, struct fib_alias, rcu);
267 kmem_cache_free(fn_alias_kmem, fa);
268}
269
270static inline void alias_free_mem_rcu(struct fib_alias *fa)
271{
272 call_rcu(&fa->rcu, __alias_free_mem);
273}
274
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800275#define TNODE_KMALLOC_MAX \
Alexander Duyckadaf9812014-12-31 10:55:47 -0800276 ilog2((PAGE_SIZE - sizeof(struct tnode)) / sizeof(struct tnode *))
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800277
278static void __node_free_rcu(struct rcu_head *head)
Robert Olsson2373ce12005-08-25 13:01:29 -0700279{
Alexander Duyckadaf9812014-12-31 10:55:47 -0800280 struct tnode *n = container_of(head, struct tnode, rcu);
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800281
282 if (IS_LEAF(n))
283 kmem_cache_free(trie_leaf_kmem, n);
284 else if (n->bits <= TNODE_KMALLOC_MAX)
285 kfree(n);
286 else
287 vfree(n);
Robert Olsson2373ce12005-08-25 13:01:29 -0700288}
289
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800290#define node_free(n) call_rcu(&n->rcu, __node_free_rcu)
Stephen Hemminger387a5482008-04-10 03:47:34 -0700291
Robert Olsson2373ce12005-08-25 13:01:29 -0700292static inline void free_leaf_info(struct leaf_info *leaf)
293{
Lai Jiangshanbceb0f42011-03-18 11:42:34 +0800294 kfree_rcu(leaf, rcu);
Robert Olsson2373ce12005-08-25 13:01:29 -0700295}
296
Eric Dumazet8d9654442008-01-13 22:31:44 -0800297static struct tnode *tnode_alloc(size_t size)
Robert Olsson2373ce12005-08-25 13:01:29 -0700298{
Robert Olsson2373ce12005-08-25 13:01:29 -0700299 if (size <= PAGE_SIZE)
Eric Dumazet8d9654442008-01-13 22:31:44 -0800300 return kzalloc(size, GFP_KERNEL);
Stephen Hemminger15be75c2008-04-10 02:56:38 -0700301 else
Eric Dumazet7a1c8e52010-11-20 07:46:35 +0000302 return vzalloc(size);
Stephen Hemminger15be75c2008-04-10 02:56:38 -0700303}
Robert Olsson2373ce12005-08-25 13:01:29 -0700304
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800305static inline void empty_child_inc(struct tnode *n)
306{
307 ++n->empty_children ? : ++n->full_children;
308}
309
310static inline void empty_child_dec(struct tnode *n)
311{
312 n->empty_children-- ? : n->full_children--;
313}
314
Alexander Duyckadaf9812014-12-31 10:55:47 -0800315static struct tnode *leaf_new(t_key key)
Robert Olsson19baf832005-06-21 12:43:18 -0700316{
Alexander Duyckadaf9812014-12-31 10:55:47 -0800317 struct tnode *l = kmem_cache_alloc(trie_leaf_kmem, GFP_KERNEL);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700318 if (l) {
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800319 l->parent = NULL;
320 /* set key and pos to reflect full key value
321 * any trailing zeros in the key should be ignored
322 * as the nodes are searched
323 */
324 l->key = key;
Alexander Duyck5405afd2014-12-31 10:57:08 -0800325 l->slen = 0;
Alexander Duycke9b44012014-12-31 10:56:12 -0800326 l->pos = 0;
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800327 /* set bits to 0 indicating we are not a tnode */
328 l->bits = 0;
329
Robert Olsson19baf832005-06-21 12:43:18 -0700330 INIT_HLIST_HEAD(&l->list);
331 }
332 return l;
333}
334
335static struct leaf_info *leaf_info_new(int plen)
336{
337 struct leaf_info *li = kmalloc(sizeof(struct leaf_info), GFP_KERNEL);
Robert Olsson2373ce12005-08-25 13:01:29 -0700338 if (li) {
Alexander Duyck5786ec62015-02-25 15:31:37 -0800339 li->slen = KEYLENGTH - plen;
Alexander Duyck56315f92015-02-25 15:31:31 -0800340 INIT_HLIST_HEAD(&li->falh);
Patrick McHardyf0e36f82005-07-05 14:44:55 -0700341 }
Robert Olsson2373ce12005-08-25 13:01:29 -0700342 return li;
Patrick McHardyf0e36f82005-07-05 14:44:55 -0700343}
344
Stephen Hemmingera07f5f52008-01-22 21:53:36 -0800345static struct tnode *tnode_new(t_key key, int pos, int bits)
Robert Olsson19baf832005-06-21 12:43:18 -0700346{
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800347 size_t sz = offsetof(struct tnode, child[1ul << bits]);
Patrick McHardyf0e36f82005-07-05 14:44:55 -0700348 struct tnode *tn = tnode_alloc(sz);
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800349 unsigned int shift = pos + bits;
350
351 /* verify bits and pos their msb bits clear and values are valid */
352 BUG_ON(!bits || (shift > KEYLENGTH));
Robert Olsson19baf832005-06-21 12:43:18 -0700353
Olof Johansson91b9a272005-08-09 20:24:39 -0700354 if (tn) {
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800355 tn->parent = NULL;
Alexander Duyck5405afd2014-12-31 10:57:08 -0800356 tn->slen = pos;
Robert Olsson19baf832005-06-21 12:43:18 -0700357 tn->pos = pos;
358 tn->bits = bits;
Alexander Duycke9b44012014-12-31 10:56:12 -0800359 tn->key = (shift < KEYLENGTH) ? (key >> shift) << shift : 0;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800360 if (bits == KEYLENGTH)
361 tn->full_children = 1;
362 else
363 tn->empty_children = 1ul << bits;
Robert Olsson19baf832005-06-21 12:43:18 -0700364 }
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700365
Eric Dumazeta034ee32010-09-09 23:32:28 +0000366 pr_debug("AT %p s=%zu %zu\n", tn, sizeof(struct tnode),
Alexander Duyckadaf9812014-12-31 10:55:47 -0800367 sizeof(struct tnode *) << bits);
Robert Olsson19baf832005-06-21 12:43:18 -0700368 return tn;
369}
370
Alexander Duycke9b44012014-12-31 10:56:12 -0800371/* Check whether a tnode 'n' is "full", i.e. it is an internal node
Robert Olsson19baf832005-06-21 12:43:18 -0700372 * and no bits are skipped. See discussion in dyntree paper p. 6
373 */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800374static inline int tnode_full(const struct tnode *tn, const struct tnode *n)
Robert Olsson19baf832005-06-21 12:43:18 -0700375{
Alexander Duycke9b44012014-12-31 10:56:12 -0800376 return n && ((n->pos + n->bits) == tn->pos) && IS_TNODE(n);
Robert Olsson19baf832005-06-21 12:43:18 -0700377}
378
Alexander Duyckff181ed2014-12-31 10:56:43 -0800379/* Add a child at position i overwriting the old value.
380 * Update the value of full_children and empty_children.
381 */
382static void put_child(struct tnode *tn, unsigned long i, struct tnode *n)
Robert Olsson19baf832005-06-21 12:43:18 -0700383{
Alexander Duyck21d1f112014-12-31 10:57:02 -0800384 struct tnode *chi = tnode_get_child(tn, i);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800385 int isfull, wasfull;
Robert Olsson19baf832005-06-21 12:43:18 -0700386
Alexander Duyck98293e82014-12-31 10:56:18 -0800387 BUG_ON(i >= tnode_child_length(tn));
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700388
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800389 /* update emptyChildren, overflow into fullChildren */
Robert Olsson19baf832005-06-21 12:43:18 -0700390 if (n == NULL && chi != NULL)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800391 empty_child_inc(tn);
392 if (n != NULL && chi == NULL)
393 empty_child_dec(tn);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700394
Robert Olsson19baf832005-06-21 12:43:18 -0700395 /* update fullChildren */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800396 wasfull = tnode_full(tn, chi);
Robert Olsson19baf832005-06-21 12:43:18 -0700397 isfull = tnode_full(tn, n);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800398
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700399 if (wasfull && !isfull)
Robert Olsson19baf832005-06-21 12:43:18 -0700400 tn->full_children--;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700401 else if (!wasfull && isfull)
Robert Olsson19baf832005-06-21 12:43:18 -0700402 tn->full_children++;
Olof Johansson91b9a272005-08-09 20:24:39 -0700403
Alexander Duyck5405afd2014-12-31 10:57:08 -0800404 if (n && (tn->slen < n->slen))
405 tn->slen = n->slen;
406
Eric Dumazetcf778b02012-01-12 04:41:32 +0000407 rcu_assign_pointer(tn->child[i], n);
Robert Olsson19baf832005-06-21 12:43:18 -0700408}
409
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800410static void update_children(struct tnode *tn)
411{
412 unsigned long i;
413
414 /* update all of the child parent pointers */
415 for (i = tnode_child_length(tn); i;) {
416 struct tnode *inode = tnode_get_child(tn, --i);
417
418 if (!inode)
419 continue;
420
421 /* Either update the children of a tnode that
422 * already belongs to us or update the child
423 * to point to ourselves.
424 */
425 if (node_parent(inode) == tn)
426 update_children(inode);
427 else
428 node_set_parent(inode, tn);
429 }
430}
431
432static inline void put_child_root(struct tnode *tp, struct trie *t,
433 t_key key, struct tnode *n)
Alexander Duyck836a0122014-12-31 10:56:06 -0800434{
435 if (tp)
436 put_child(tp, get_index(key, tp), n);
437 else
438 rcu_assign_pointer(t->trie, n);
439}
440
Alexander Duyckfc86a932014-12-31 10:56:49 -0800441static inline void tnode_free_init(struct tnode *tn)
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700442{
Alexander Duyckfc86a932014-12-31 10:56:49 -0800443 tn->rcu.next = NULL;
444}
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700445
Alexander Duyckfc86a932014-12-31 10:56:49 -0800446static inline void tnode_free_append(struct tnode *tn, struct tnode *n)
447{
448 n->rcu.next = tn->rcu.next;
449 tn->rcu.next = &n->rcu;
450}
451
452static void tnode_free(struct tnode *tn)
453{
454 struct callback_head *head = &tn->rcu;
455
456 while (head) {
457 head = head->next;
458 tnode_free_size += offsetof(struct tnode, child[1 << tn->bits]);
459 node_free(tn);
460
461 tn = container_of(head, struct tnode, rcu);
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700462 }
Alexander Duyckfc86a932014-12-31 10:56:49 -0800463
464 if (tnode_free_size >= PAGE_SIZE * sync_pages) {
465 tnode_free_size = 0;
466 synchronize_rcu();
467 }
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700468}
469
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800470static void replace(struct trie *t, struct tnode *oldtnode, struct tnode *tn)
471{
472 struct tnode *tp = node_parent(oldtnode);
473 unsigned long i;
474
475 /* setup the parent pointer out of and back into this node */
476 NODE_INIT_PARENT(tn, tp);
477 put_child_root(tp, t, tn->key, tn);
478
479 /* update all of the child parent pointers */
480 update_children(tn);
481
482 /* all pointers should be clean so we are done */
483 tnode_free(oldtnode);
484
485 /* resize children now that oldtnode is freed */
486 for (i = tnode_child_length(tn); i;) {
487 struct tnode *inode = tnode_get_child(tn, --i);
488
489 /* resize child node */
490 if (tnode_full(tn, inode))
491 resize(t, inode);
492 }
493}
494
Alexander Duyckff181ed2014-12-31 10:56:43 -0800495static int inflate(struct trie *t, struct tnode *oldtnode)
Robert Olsson19baf832005-06-21 12:43:18 -0700496{
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800497 struct tnode *tn;
498 unsigned long i;
Alexander Duycke9b44012014-12-31 10:56:12 -0800499 t_key m;
Robert Olsson19baf832005-06-21 12:43:18 -0700500
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700501 pr_debug("In inflate\n");
Robert Olsson19baf832005-06-21 12:43:18 -0700502
Alexander Duycke9b44012014-12-31 10:56:12 -0800503 tn = tnode_new(oldtnode->key, oldtnode->pos - 1, oldtnode->bits + 1);
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700504 if (!tn)
Alexander Duyckff181ed2014-12-31 10:56:43 -0800505 return -ENOMEM;
Robert Olsson2f368952005-07-05 15:02:40 -0700506
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800507 /* prepare oldtnode to be freed */
508 tnode_free_init(oldtnode);
509
Alexander Duyck12c081a2014-12-31 10:56:55 -0800510 /* Assemble all of the pointers in our cluster, in this case that
511 * represents all of the pointers out of our allocated nodes that
512 * point to existing tnodes and the links between our allocated
513 * nodes.
Robert Olsson2f368952005-07-05 15:02:40 -0700514 */
Alexander Duyck12c081a2014-12-31 10:56:55 -0800515 for (i = tnode_child_length(oldtnode), m = 1u << tn->pos; i;) {
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800516 struct tnode *inode = tnode_get_child(oldtnode, --i);
517 struct tnode *node0, *node1;
518 unsigned long j, k;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700519
Robert Olsson19baf832005-06-21 12:43:18 -0700520 /* An empty child */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800521 if (inode == NULL)
Robert Olsson19baf832005-06-21 12:43:18 -0700522 continue;
523
524 /* A leaf or an internal node with skipped bits */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800525 if (!tnode_full(oldtnode, inode)) {
Alexander Duycke9b44012014-12-31 10:56:12 -0800526 put_child(tn, get_index(inode->key, tn), inode);
Robert Olsson19baf832005-06-21 12:43:18 -0700527 continue;
528 }
529
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800530 /* drop the node in the old tnode free list */
531 tnode_free_append(oldtnode, inode);
532
Robert Olsson19baf832005-06-21 12:43:18 -0700533 /* An internal node with two children */
Robert Olsson19baf832005-06-21 12:43:18 -0700534 if (inode->bits == 1) {
Alexander Duyck12c081a2014-12-31 10:56:55 -0800535 put_child(tn, 2 * i + 1, tnode_get_child(inode, 1));
536 put_child(tn, 2 * i, tnode_get_child(inode, 0));
Olof Johansson91b9a272005-08-09 20:24:39 -0700537 continue;
Robert Olsson19baf832005-06-21 12:43:18 -0700538 }
539
Olof Johansson91b9a272005-08-09 20:24:39 -0700540 /* We will replace this node 'inode' with two new
Alexander Duyck12c081a2014-12-31 10:56:55 -0800541 * ones, 'node0' and 'node1', each with half of the
Olof Johansson91b9a272005-08-09 20:24:39 -0700542 * original children. The two new nodes will have
543 * a position one bit further down the key and this
544 * means that the "significant" part of their keys
545 * (see the discussion near the top of this file)
546 * will differ by one bit, which will be "0" in
Alexander Duyck12c081a2014-12-31 10:56:55 -0800547 * node0's key and "1" in node1's key. Since we are
Olof Johansson91b9a272005-08-09 20:24:39 -0700548 * moving the key position by one step, the bit that
549 * we are moving away from - the bit at position
Alexander Duyck12c081a2014-12-31 10:56:55 -0800550 * (tn->pos) - is the one that will differ between
551 * node0 and node1. So... we synthesize that bit in the
552 * two new keys.
Olof Johansson91b9a272005-08-09 20:24:39 -0700553 */
Alexander Duyck12c081a2014-12-31 10:56:55 -0800554 node1 = tnode_new(inode->key | m, inode->pos, inode->bits - 1);
555 if (!node1)
556 goto nomem;
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800557 node0 = tnode_new(inode->key, inode->pos, inode->bits - 1);
Robert Olsson19baf832005-06-21 12:43:18 -0700558
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800559 tnode_free_append(tn, node1);
Alexander Duyck12c081a2014-12-31 10:56:55 -0800560 if (!node0)
561 goto nomem;
562 tnode_free_append(tn, node0);
Robert Olsson19baf832005-06-21 12:43:18 -0700563
Alexander Duyck12c081a2014-12-31 10:56:55 -0800564 /* populate child pointers in new nodes */
565 for (k = tnode_child_length(inode), j = k / 2; j;) {
566 put_child(node1, --j, tnode_get_child(inode, --k));
567 put_child(node0, j, tnode_get_child(inode, j));
568 put_child(node1, --j, tnode_get_child(inode, --k));
569 put_child(node0, j, tnode_get_child(inode, j));
Robert Olsson19baf832005-06-21 12:43:18 -0700570 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800571
Alexander Duyck12c081a2014-12-31 10:56:55 -0800572 /* link new nodes to parent */
573 NODE_INIT_PARENT(node1, tn);
574 NODE_INIT_PARENT(node0, tn);
Olof Johansson91b9a272005-08-09 20:24:39 -0700575
Alexander Duyck12c081a2014-12-31 10:56:55 -0800576 /* link parent to nodes */
577 put_child(tn, 2 * i + 1, node1);
578 put_child(tn, 2 * i, node0);
Robert Olsson19baf832005-06-21 12:43:18 -0700579 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800580
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800581 /* setup the parent pointers into and out of this node */
582 replace(t, oldtnode, tn);
Alexander Duyckfc86a932014-12-31 10:56:49 -0800583
Alexander Duyckff181ed2014-12-31 10:56:43 -0800584 return 0;
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700585nomem:
Alexander Duyckfc86a932014-12-31 10:56:49 -0800586 /* all pointers should be clean so we are done */
587 tnode_free(tn);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800588 return -ENOMEM;
Robert Olsson19baf832005-06-21 12:43:18 -0700589}
590
Alexander Duyckff181ed2014-12-31 10:56:43 -0800591static int halve(struct trie *t, struct tnode *oldtnode)
Robert Olsson19baf832005-06-21 12:43:18 -0700592{
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800593 struct tnode *tn;
Alexander Duyck12c081a2014-12-31 10:56:55 -0800594 unsigned long i;
Robert Olsson19baf832005-06-21 12:43:18 -0700595
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700596 pr_debug("In halve\n");
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700597
Alexander Duycke9b44012014-12-31 10:56:12 -0800598 tn = tnode_new(oldtnode->key, oldtnode->pos + 1, oldtnode->bits - 1);
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700599 if (!tn)
Alexander Duyckff181ed2014-12-31 10:56:43 -0800600 return -ENOMEM;
Robert Olsson2f368952005-07-05 15:02:40 -0700601
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800602 /* prepare oldtnode to be freed */
603 tnode_free_init(oldtnode);
604
Alexander Duyck12c081a2014-12-31 10:56:55 -0800605 /* Assemble all of the pointers in our cluster, in this case that
606 * represents all of the pointers out of our allocated nodes that
607 * point to existing tnodes and the links between our allocated
608 * nodes.
Robert Olsson2f368952005-07-05 15:02:40 -0700609 */
Alexander Duyck12c081a2014-12-31 10:56:55 -0800610 for (i = tnode_child_length(oldtnode); i;) {
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800611 struct tnode *node1 = tnode_get_child(oldtnode, --i);
612 struct tnode *node0 = tnode_get_child(oldtnode, --i);
613 struct tnode *inode;
Robert Olsson2f368952005-07-05 15:02:40 -0700614
Alexander Duyck12c081a2014-12-31 10:56:55 -0800615 /* At least one of the children is empty */
616 if (!node1 || !node0) {
617 put_child(tn, i / 2, node1 ? : node0);
618 continue;
Robert Olsson2f368952005-07-05 15:02:40 -0700619 }
Robert Olsson2f368952005-07-05 15:02:40 -0700620
Alexander Duyck12c081a2014-12-31 10:56:55 -0800621 /* Two nonempty children */
622 inode = tnode_new(node0->key, oldtnode->pos, 1);
623 if (!inode) {
624 tnode_free(tn);
625 return -ENOMEM;
626 }
627 tnode_free_append(tn, inode);
628
629 /* initialize pointers out of node */
630 put_child(inode, 1, node1);
631 put_child(inode, 0, node0);
632 NODE_INIT_PARENT(inode, tn);
633
634 /* link parent to node */
635 put_child(tn, i / 2, inode);
Robert Olsson2f368952005-07-05 15:02:40 -0700636 }
Robert Olsson19baf832005-06-21 12:43:18 -0700637
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800638 /* setup the parent pointers into and out of this node */
639 replace(t, oldtnode, tn);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800640
641 return 0;
Robert Olsson19baf832005-06-21 12:43:18 -0700642}
643
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800644static void collapse(struct trie *t, struct tnode *oldtnode)
645{
646 struct tnode *n, *tp;
647 unsigned long i;
648
649 /* scan the tnode looking for that one child that might still exist */
650 for (n = NULL, i = tnode_child_length(oldtnode); !n && i;)
651 n = tnode_get_child(oldtnode, --i);
652
653 /* compress one level */
654 tp = node_parent(oldtnode);
655 put_child_root(tp, t, oldtnode->key, n);
656 node_set_parent(n, tp);
657
658 /* drop dead node */
659 node_free(oldtnode);
660}
661
Alexander Duyck5405afd2014-12-31 10:57:08 -0800662static unsigned char update_suffix(struct tnode *tn)
663{
664 unsigned char slen = tn->pos;
665 unsigned long stride, i;
666
667 /* search though the list of children looking for nodes that might
668 * have a suffix greater than the one we currently have. This is
669 * why we start with a stride of 2 since a stride of 1 would
670 * represent the nodes with suffix length equal to tn->pos
671 */
672 for (i = 0, stride = 0x2ul ; i < tnode_child_length(tn); i += stride) {
673 struct tnode *n = tnode_get_child(tn, i);
674
675 if (!n || (n->slen <= slen))
676 continue;
677
678 /* update stride and slen based on new value */
679 stride <<= (n->slen - slen);
680 slen = n->slen;
681 i &= ~(stride - 1);
682
683 /* if slen covers all but the last bit we can stop here
684 * there will be nothing longer than that since only node
685 * 0 and 1 << (bits - 1) could have that as their suffix
686 * length.
687 */
688 if ((slen + 1) >= (tn->pos + tn->bits))
689 break;
690 }
691
692 tn->slen = slen;
693
694 return slen;
695}
696
Alexander Duyckf05a4812014-12-31 10:56:37 -0800697/* From "Implementing a dynamic compressed trie" by Stefan Nilsson of
698 * the Helsinki University of Technology and Matti Tikkanen of Nokia
699 * Telecommunications, page 6:
700 * "A node is doubled if the ratio of non-empty children to all
701 * children in the *doubled* node is at least 'high'."
702 *
703 * 'high' in this instance is the variable 'inflate_threshold'. It
704 * is expressed as a percentage, so we multiply it with
705 * tnode_child_length() and instead of multiplying by 2 (since the
706 * child array will be doubled by inflate()) and multiplying
707 * the left-hand side by 100 (to handle the percentage thing) we
708 * multiply the left-hand side by 50.
709 *
710 * The left-hand side may look a bit weird: tnode_child_length(tn)
711 * - tn->empty_children is of course the number of non-null children
712 * in the current node. tn->full_children is the number of "full"
713 * children, that is non-null tnodes with a skip value of 0.
714 * All of those will be doubled in the resulting inflated tnode, so
715 * we just count them one extra time here.
716 *
717 * A clearer way to write this would be:
718 *
719 * to_be_doubled = tn->full_children;
720 * not_to_be_doubled = tnode_child_length(tn) - tn->empty_children -
721 * tn->full_children;
722 *
723 * new_child_length = tnode_child_length(tn) * 2;
724 *
725 * new_fill_factor = 100 * (not_to_be_doubled + 2*to_be_doubled) /
726 * new_child_length;
727 * if (new_fill_factor >= inflate_threshold)
728 *
729 * ...and so on, tho it would mess up the while () loop.
730 *
731 * anyway,
732 * 100 * (not_to_be_doubled + 2*to_be_doubled) / new_child_length >=
733 * inflate_threshold
734 *
735 * avoid a division:
736 * 100 * (not_to_be_doubled + 2*to_be_doubled) >=
737 * inflate_threshold * new_child_length
738 *
739 * expand not_to_be_doubled and to_be_doubled, and shorten:
740 * 100 * (tnode_child_length(tn) - tn->empty_children +
741 * tn->full_children) >= inflate_threshold * new_child_length
742 *
743 * expand new_child_length:
744 * 100 * (tnode_child_length(tn) - tn->empty_children +
745 * tn->full_children) >=
746 * inflate_threshold * tnode_child_length(tn) * 2
747 *
748 * shorten again:
749 * 50 * (tn->full_children + tnode_child_length(tn) -
750 * tn->empty_children) >= inflate_threshold *
751 * tnode_child_length(tn)
752 *
753 */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800754static bool should_inflate(const struct tnode *tp, const struct tnode *tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800755{
756 unsigned long used = tnode_child_length(tn);
757 unsigned long threshold = used;
758
759 /* Keep root node larger */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800760 threshold *= tp ? inflate_threshold : inflate_threshold_root;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800761 used -= tn->empty_children;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800762 used += tn->full_children;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800763
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800764 /* if bits == KEYLENGTH then pos = 0, and will fail below */
765
766 return (used > 1) && tn->pos && ((50 * used) >= threshold);
Alexander Duyckf05a4812014-12-31 10:56:37 -0800767}
768
Alexander Duyckff181ed2014-12-31 10:56:43 -0800769static bool should_halve(const struct tnode *tp, const struct tnode *tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800770{
771 unsigned long used = tnode_child_length(tn);
772 unsigned long threshold = used;
773
774 /* Keep root node larger */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800775 threshold *= tp ? halve_threshold : halve_threshold_root;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800776 used -= tn->empty_children;
777
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800778 /* if bits == KEYLENGTH then used = 100% on wrap, and will fail below */
779
780 return (used > 1) && (tn->bits > 1) && ((100 * used) < threshold);
781}
782
783static bool should_collapse(const struct tnode *tn)
784{
785 unsigned long used = tnode_child_length(tn);
786
787 used -= tn->empty_children;
788
789 /* account for bits == KEYLENGTH case */
790 if ((tn->bits == KEYLENGTH) && tn->full_children)
791 used -= KEY_MAX;
792
793 /* One child or none, time to drop us from the trie */
794 return used < 2;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800795}
796
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800797#define MAX_WORK 10
Alexander Duyckff181ed2014-12-31 10:56:43 -0800798static void resize(struct trie *t, struct tnode *tn)
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800799{
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800800 struct tnode *tp = node_parent(tn);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800801 struct tnode __rcu **cptr;
Alexander Duycka80e89d2015-01-22 15:51:20 -0800802 int max_work = MAX_WORK;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800803
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800804 pr_debug("In tnode_resize %p inflate_threshold=%d threshold=%d\n",
805 tn, inflate_threshold, halve_threshold);
806
Alexander Duyckff181ed2014-12-31 10:56:43 -0800807 /* track the tnode via the pointer from the parent instead of
808 * doing it ourselves. This way we can let RCU fully do its
809 * thing without us interfering
810 */
811 cptr = tp ? &tp->child[get_index(tn->key, tp)] : &t->trie;
812 BUG_ON(tn != rtnl_dereference(*cptr));
813
Alexander Duyckf05a4812014-12-31 10:56:37 -0800814 /* Double as long as the resulting node has a number of
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800815 * nonempty nodes that are above the threshold.
816 */
Alexander Duycka80e89d2015-01-22 15:51:20 -0800817 while (should_inflate(tp, tn) && max_work) {
Alexander Duyckff181ed2014-12-31 10:56:43 -0800818 if (inflate(t, tn)) {
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800819#ifdef CONFIG_IP_FIB_TRIE_STATS
820 this_cpu_inc(t->stats->resize_node_skipped);
821#endif
822 break;
823 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800824
Alexander Duycka80e89d2015-01-22 15:51:20 -0800825 max_work--;
Alexander Duyckff181ed2014-12-31 10:56:43 -0800826 tn = rtnl_dereference(*cptr);
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800827 }
828
829 /* Return if at least one inflate is run */
830 if (max_work != MAX_WORK)
Alexander Duyckff181ed2014-12-31 10:56:43 -0800831 return;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800832
Alexander Duyckf05a4812014-12-31 10:56:37 -0800833 /* Halve as long as the number of empty children in this
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800834 * node is above threshold.
835 */
Alexander Duycka80e89d2015-01-22 15:51:20 -0800836 while (should_halve(tp, tn) && max_work) {
Alexander Duyckff181ed2014-12-31 10:56:43 -0800837 if (halve(t, tn)) {
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800838#ifdef CONFIG_IP_FIB_TRIE_STATS
839 this_cpu_inc(t->stats->resize_node_skipped);
840#endif
841 break;
842 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800843
Alexander Duycka80e89d2015-01-22 15:51:20 -0800844 max_work--;
Alexander Duyckff181ed2014-12-31 10:56:43 -0800845 tn = rtnl_dereference(*cptr);
846 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800847
848 /* Only one child remains */
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800849 if (should_collapse(tn)) {
850 collapse(t, tn);
Alexander Duyck5405afd2014-12-31 10:57:08 -0800851 return;
852 }
853
854 /* Return if at least one deflate was run */
855 if (max_work != MAX_WORK)
856 return;
857
858 /* push the suffix length to the parent node */
859 if (tn->slen > tn->pos) {
860 unsigned char slen = update_suffix(tn);
861
862 if (tp && (slen > tp->slen))
863 tp->slen = slen;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800864 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800865}
866
Robert Olsson772cb712005-09-19 15:31:18 -0700867/* readside must use rcu_read_lock currently dump routines
Robert Olsson2373ce12005-08-25 13:01:29 -0700868 via get_fa_head and dump */
869
Alexander Duyckadaf9812014-12-31 10:55:47 -0800870static struct leaf_info *find_leaf_info(struct tnode *l, int plen)
Robert Olsson19baf832005-06-21 12:43:18 -0700871{
Robert Olsson772cb712005-09-19 15:31:18 -0700872 struct hlist_head *head = &l->list;
Robert Olsson19baf832005-06-21 12:43:18 -0700873 struct leaf_info *li;
Alexander Duyck5786ec62015-02-25 15:31:37 -0800874 int slen = KEYLENGTH - plen;
Robert Olsson19baf832005-06-21 12:43:18 -0700875
Sasha Levinb67bfe02013-02-27 17:06:00 -0800876 hlist_for_each_entry_rcu(li, head, hlist)
Alexander Duyck5786ec62015-02-25 15:31:37 -0800877 if (li->slen == slen)
Robert Olsson19baf832005-06-21 12:43:18 -0700878 return li;
Olof Johansson91b9a272005-08-09 20:24:39 -0700879
Robert Olsson19baf832005-06-21 12:43:18 -0700880 return NULL;
881}
882
Alexander Duyck56315f92015-02-25 15:31:31 -0800883static inline struct hlist_head *get_fa_head(struct tnode *l, int plen)
Robert Olsson19baf832005-06-21 12:43:18 -0700884{
Robert Olsson772cb712005-09-19 15:31:18 -0700885 struct leaf_info *li = find_leaf_info(l, plen);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700886
Olof Johansson91b9a272005-08-09 20:24:39 -0700887 if (!li)
888 return NULL;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700889
Olof Johansson91b9a272005-08-09 20:24:39 -0700890 return &li->falh;
Robert Olsson19baf832005-06-21 12:43:18 -0700891}
892
Alexander Duyck5405afd2014-12-31 10:57:08 -0800893static void leaf_pull_suffix(struct tnode *l)
Robert Olsson19baf832005-06-21 12:43:18 -0700894{
Alexander Duyck5405afd2014-12-31 10:57:08 -0800895 struct tnode *tp = node_parent(l);
896
897 while (tp && (tp->slen > tp->pos) && (tp->slen > l->slen)) {
898 if (update_suffix(tp) > l->slen)
899 break;
900 tp = node_parent(tp);
901 }
902}
903
904static void leaf_push_suffix(struct tnode *l)
905{
906 struct tnode *tn = node_parent(l);
907
908 /* if this is a new leaf then tn will be NULL and we can sort
909 * out parent suffix lengths as a part of trie_rebalance
910 */
911 while (tn && (tn->slen < l->slen)) {
912 tn->slen = l->slen;
913 tn = node_parent(tn);
914 }
915}
916
917static void remove_leaf_info(struct tnode *l, struct leaf_info *old)
918{
Alexander Duyck64c62722015-01-22 15:51:45 -0800919 /* record the location of the previous list_info entry */
920 struct hlist_node **pprev = old->hlist.pprev;
921 struct leaf_info *li = hlist_entry(pprev, typeof(*li), hlist.next);
Alexander Duyck5405afd2014-12-31 10:57:08 -0800922
923 /* remove the leaf info from the list */
924 hlist_del_rcu(&old->hlist);
925
Alexander Duyck64c62722015-01-22 15:51:45 -0800926 /* only access li if it is pointing at the last valid hlist_node */
927 if (hlist_empty(&l->list) || (*pprev))
Alexander Duyck5405afd2014-12-31 10:57:08 -0800928 return;
929
Alexander Duyck64c62722015-01-22 15:51:45 -0800930 /* update the trie with the latest suffix length */
Alexander Duyck5786ec62015-02-25 15:31:37 -0800931 l->slen = li->slen;
Alexander Duyck64c62722015-01-22 15:51:45 -0800932 leaf_pull_suffix(l);
Alexander Duyck5405afd2014-12-31 10:57:08 -0800933}
934
935static void insert_leaf_info(struct tnode *l, struct leaf_info *new)
936{
937 struct hlist_head *head = &l->list;
Alexander Duyck5786ec62015-02-25 15:31:37 -0800938 struct leaf_info *li, *last = NULL;
Robert Olsson19baf832005-06-21 12:43:18 -0700939
Alexander Duyck5786ec62015-02-25 15:31:37 -0800940 hlist_for_each_entry(li, head, hlist) {
941 if (new->slen < li->slen)
942 break;
943 last = li;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900944 }
Alexander Duyck5405afd2014-12-31 10:57:08 -0800945
Alexander Duyck5786ec62015-02-25 15:31:37 -0800946 if (last)
947 hlist_add_behind_rcu(&new->hlist, &last->hlist);
948 else
949 hlist_add_head_rcu(&new->hlist, head);
950
Alexander Duyck5405afd2014-12-31 10:57:08 -0800951 /* if we added to the tail node then we need to update slen */
Alexander Duyck5786ec62015-02-25 15:31:37 -0800952 if (l->slen < new->slen) {
953 l->slen = new->slen;
Alexander Duyck5405afd2014-12-31 10:57:08 -0800954 leaf_push_suffix(l);
955 }
Robert Olsson19baf832005-06-21 12:43:18 -0700956}
957
Robert Olsson2373ce12005-08-25 13:01:29 -0700958/* rcu_read_lock needs to be hold by caller from readside */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800959static struct tnode *fib_find_node(struct trie *t, u32 key)
Robert Olsson19baf832005-06-21 12:43:18 -0700960{
Alexander Duyckadaf9812014-12-31 10:55:47 -0800961 struct tnode *n = rcu_dereference_rtnl(t->trie);
Robert Olsson19baf832005-06-21 12:43:18 -0700962
Alexander Duyck939afb02014-12-31 10:56:00 -0800963 while (n) {
964 unsigned long index = get_index(key, n);
965
966 /* This bit of code is a bit tricky but it combines multiple
967 * checks into a single check. The prefix consists of the
968 * prefix plus zeros for the bits in the cindex. The index
969 * is the difference between the key and this value. From
970 * this we can actually derive several pieces of data.
Alexander Duyckb3832112015-01-22 15:51:08 -0800971 * if (index & (~0ul << bits))
Alexander Duyck939afb02014-12-31 10:56:00 -0800972 * we have a mismatch in skip bits and failed
Alexander Duyckb3832112015-01-22 15:51:08 -0800973 * else
974 * we know the value is cindex
Alexander Duyck939afb02014-12-31 10:56:00 -0800975 */
Alexander Duyckb3832112015-01-22 15:51:08 -0800976 if (index & (~0ul << n->bits))
Alexander Duyck939afb02014-12-31 10:56:00 -0800977 return NULL;
978
979 /* we have found a leaf. Prefixes have already been compared */
980 if (IS_LEAF(n))
Robert Olsson19baf832005-06-21 12:43:18 -0700981 break;
Alexander Duyck939afb02014-12-31 10:56:00 -0800982
Alexander Duyck21d1f112014-12-31 10:57:02 -0800983 n = tnode_get_child_rcu(n, index);
Robert Olsson19baf832005-06-21 12:43:18 -0700984 }
Robert Olsson19baf832005-06-21 12:43:18 -0700985
Alexander Duyck939afb02014-12-31 10:56:00 -0800986 return n;
Robert Olsson19baf832005-06-21 12:43:18 -0700987}
988
Alexander Duyck02525362015-01-22 15:51:39 -0800989/* Return the first fib alias matching TOS with
990 * priority less than or equal to PRIO.
991 */
Alexander Duyck56315f92015-02-25 15:31:31 -0800992static struct fib_alias *fib_find_alias(struct hlist_head *fah, u8 tos,
993 u32 prio)
Alexander Duyck02525362015-01-22 15:51:39 -0800994{
995 struct fib_alias *fa;
996
997 if (!fah)
998 return NULL;
999
Alexander Duyck56315f92015-02-25 15:31:31 -08001000 hlist_for_each_entry(fa, fah, fa_list) {
Alexander Duyck02525362015-01-22 15:51:39 -08001001 if (fa->fa_tos > tos)
1002 continue;
1003 if (fa->fa_info->fib_priority >= prio || fa->fa_tos < tos)
1004 return fa;
1005 }
1006
1007 return NULL;
1008}
1009
Jarek Poplawski7b855762009-06-18 00:28:51 -07001010static void trie_rebalance(struct trie *t, struct tnode *tn)
Robert Olsson19baf832005-06-21 12:43:18 -07001011{
Stephen Hemminger06801912007-08-10 15:22:13 -07001012 struct tnode *tp;
Robert Olsson19baf832005-06-21 12:43:18 -07001013
Alexander Duyckff181ed2014-12-31 10:56:43 -08001014 while ((tp = node_parent(tn)) != NULL) {
1015 resize(t, tn);
Stephen Hemminger06801912007-08-10 15:22:13 -07001016 tn = tp;
Robert Olsson19baf832005-06-21 12:43:18 -07001017 }
Stephen Hemminger06801912007-08-10 15:22:13 -07001018
Robert Olsson19baf832005-06-21 12:43:18 -07001019 /* Handle last (top) tnode */
Jarek Poplawski7b855762009-06-18 00:28:51 -07001020 if (IS_TNODE(tn))
Alexander Duyckff181ed2014-12-31 10:56:43 -08001021 resize(t, tn);
Robert Olsson19baf832005-06-21 12:43:18 -07001022}
1023
Robert Olsson2373ce12005-08-25 13:01:29 -07001024/* only used from updater-side */
1025
Alexander Duyck56315f92015-02-25 15:31:31 -08001026static struct hlist_head *fib_insert_node(struct trie *t, u32 key, int plen)
Robert Olsson19baf832005-06-21 12:43:18 -07001027{
Alexander Duyck56315f92015-02-25 15:31:31 -08001028 struct hlist_head *fa_head = NULL;
Alexander Duyck836a0122014-12-31 10:56:06 -08001029 struct tnode *l, *n, *tp = NULL;
Robert Olsson19baf832005-06-21 12:43:18 -07001030 struct leaf_info *li;
Robert Olsson19baf832005-06-21 12:43:18 -07001031
Alexander Duyck836a0122014-12-31 10:56:06 -08001032 li = leaf_info_new(plen);
1033 if (!li)
1034 return NULL;
1035 fa_head = &li->falh;
1036
Eric Dumazet0a5c0472011-03-31 01:51:35 -07001037 n = rtnl_dereference(t->trie);
Robert Olsson19baf832005-06-21 12:43:18 -07001038
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001039 /* If we point to NULL, stop. Either the tree is empty and we should
1040 * just put a new leaf in if, or we have reached an empty child slot,
Robert Olsson19baf832005-06-21 12:43:18 -07001041 * and we should just put our new leaf in that.
Robert Olsson19baf832005-06-21 12:43:18 -07001042 *
Alexander Duyck836a0122014-12-31 10:56:06 -08001043 * If we hit a node with a key that does't match then we should stop
1044 * and create a new tnode to replace that node and insert ourselves
1045 * and the other node into the new tnode.
Robert Olsson19baf832005-06-21 12:43:18 -07001046 */
Alexander Duyck836a0122014-12-31 10:56:06 -08001047 while (n) {
1048 unsigned long index = get_index(key, n);
Robert Olsson19baf832005-06-21 12:43:18 -07001049
Alexander Duyck836a0122014-12-31 10:56:06 -08001050 /* This bit of code is a bit tricky but it combines multiple
1051 * checks into a single check. The prefix consists of the
1052 * prefix plus zeros for the "bits" in the prefix. The index
1053 * is the difference between the key and this value. From
1054 * this we can actually derive several pieces of data.
1055 * if !(index >> bits)
1056 * we know the value is child index
1057 * else
1058 * we have a mismatch in skip bits and failed
Robert Olsson19baf832005-06-21 12:43:18 -07001059 */
Alexander Duyck836a0122014-12-31 10:56:06 -08001060 if (index >> n->bits)
1061 break;
Robert Olsson19baf832005-06-21 12:43:18 -07001062
Alexander Duyck836a0122014-12-31 10:56:06 -08001063 /* we have found a leaf. Prefixes have already been compared */
1064 if (IS_LEAF(n)) {
1065 /* Case 1: n is a leaf, and prefixes match*/
Alexander Duyck5405afd2014-12-31 10:57:08 -08001066 insert_leaf_info(n, li);
Alexander Duyck836a0122014-12-31 10:56:06 -08001067 return fa_head;
Robert Olsson19baf832005-06-21 12:43:18 -07001068 }
Robert Olsson19baf832005-06-21 12:43:18 -07001069
Alexander Duyck836a0122014-12-31 10:56:06 -08001070 tp = n;
Alexander Duyck21d1f112014-12-31 10:57:02 -08001071 n = tnode_get_child_rcu(n, index);
Alexander Duyck836a0122014-12-31 10:56:06 -08001072 }
1073
1074 l = leaf_new(key);
1075 if (!l) {
1076 free_leaf_info(li);
1077 return NULL;
1078 }
1079
Alexander Duyck5405afd2014-12-31 10:57:08 -08001080 insert_leaf_info(l, li);
Alexander Duyck836a0122014-12-31 10:56:06 -08001081
1082 /* Case 2: n is a LEAF or a TNODE and the key doesn't match.
1083 *
1084 * Add a new tnode here
1085 * first tnode need some special handling
1086 * leaves us in position for handling as case 3
1087 */
1088 if (n) {
1089 struct tnode *tn;
Alexander Duyck836a0122014-12-31 10:56:06 -08001090
Alexander Duycke9b44012014-12-31 10:56:12 -08001091 tn = tnode_new(key, __fls(key ^ n->key), 1);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001092 if (!tn) {
Robert Olssonf835e472005-06-28 15:00:39 -07001093 free_leaf_info(li);
Alexander Duyck37fd30f2014-12-31 10:55:41 -08001094 node_free(l);
Stephen Hemmingerfea86ad2008-01-12 20:57:07 -08001095 return NULL;
Olof Johansson91b9a272005-08-09 20:24:39 -07001096 }
1097
Alexander Duyck836a0122014-12-31 10:56:06 -08001098 /* initialize routes out of node */
1099 NODE_INIT_PARENT(tn, tp);
1100 put_child(tn, get_index(key, tn) ^ 1, n);
Robert Olsson19baf832005-06-21 12:43:18 -07001101
Alexander Duyck836a0122014-12-31 10:56:06 -08001102 /* start adding routes into the node */
1103 put_child_root(tp, t, key, tn);
1104 node_set_parent(n, tn);
Robert Olsson19baf832005-06-21 12:43:18 -07001105
Alexander Duyck836a0122014-12-31 10:56:06 -08001106 /* parent now has a NULL spot where the leaf can go */
Alexander Duycke962f302014-12-10 21:49:22 -08001107 tp = tn;
Robert Olsson19baf832005-06-21 12:43:18 -07001108 }
Olof Johansson91b9a272005-08-09 20:24:39 -07001109
Alexander Duyck836a0122014-12-31 10:56:06 -08001110 /* Case 3: n is NULL, and will just insert a new leaf */
1111 if (tp) {
1112 NODE_INIT_PARENT(l, tp);
1113 put_child(tp, get_index(key, tp), l);
1114 trie_rebalance(t, tp);
1115 } else {
1116 rcu_assign_pointer(t->trie, l);
1117 }
Olof Johansson91b9a272005-08-09 20:24:39 -07001118
Robert Olsson19baf832005-06-21 12:43:18 -07001119 return fa_head;
1120}
1121
Robert Olssond562f1f2007-03-26 14:22:22 -07001122/*
1123 * Caller must hold RTNL.
1124 */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001125int fib_table_insert(struct fib_table *tb, struct fib_config *cfg)
Robert Olsson19baf832005-06-21 12:43:18 -07001126{
1127 struct trie *t = (struct trie *) tb->tb_data;
1128 struct fib_alias *fa, *new_fa;
Alexander Duyck56315f92015-02-25 15:31:31 -08001129 struct hlist_head *fa_head = NULL;
Robert Olsson19baf832005-06-21 12:43:18 -07001130 struct fib_info *fi;
Thomas Graf4e902c52006-08-17 18:14:52 -07001131 int plen = cfg->fc_dst_len;
1132 u8 tos = cfg->fc_tos;
Robert Olsson19baf832005-06-21 12:43:18 -07001133 u32 key, mask;
1134 int err;
Alexander Duyckadaf9812014-12-31 10:55:47 -08001135 struct tnode *l;
Robert Olsson19baf832005-06-21 12:43:18 -07001136
Alexander Duyck5786ec62015-02-25 15:31:37 -08001137 if (plen > KEYLENGTH)
Robert Olsson19baf832005-06-21 12:43:18 -07001138 return -EINVAL;
1139
Thomas Graf4e902c52006-08-17 18:14:52 -07001140 key = ntohl(cfg->fc_dst);
Robert Olsson19baf832005-06-21 12:43:18 -07001141
Patrick McHardy2dfe55b2006-08-10 23:08:33 -07001142 pr_debug("Insert table=%u %08x/%d\n", tb->tb_id, key, plen);
Robert Olsson19baf832005-06-21 12:43:18 -07001143
Olof Johansson91b9a272005-08-09 20:24:39 -07001144 mask = ntohl(inet_make_mask(plen));
Robert Olsson19baf832005-06-21 12:43:18 -07001145
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001146 if (key & ~mask)
Robert Olsson19baf832005-06-21 12:43:18 -07001147 return -EINVAL;
1148
1149 key = key & mask;
1150
Thomas Graf4e902c52006-08-17 18:14:52 -07001151 fi = fib_create_info(cfg);
1152 if (IS_ERR(fi)) {
1153 err = PTR_ERR(fi);
Robert Olsson19baf832005-06-21 12:43:18 -07001154 goto err;
Thomas Graf4e902c52006-08-17 18:14:52 -07001155 }
Robert Olsson19baf832005-06-21 12:43:18 -07001156
1157 l = fib_find_node(t, key);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001158 fa = NULL;
Robert Olsson19baf832005-06-21 12:43:18 -07001159
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001160 if (l) {
Robert Olsson19baf832005-06-21 12:43:18 -07001161 fa_head = get_fa_head(l, plen);
1162 fa = fib_find_alias(fa_head, tos, fi->fib_priority);
1163 }
1164
1165 /* Now fa, if non-NULL, points to the first fib alias
1166 * with the same keys [prefix,tos,priority], if such key already
1167 * exists or to the node before which we will insert new one.
1168 *
1169 * If fa is NULL, we will need to allocate a new one and
Alexander Duyck56315f92015-02-25 15:31:31 -08001170 * insert to the tail of the section matching the suffix length
1171 * of the new alias.
Robert Olsson19baf832005-06-21 12:43:18 -07001172 */
1173
Julian Anastasov936f6f82008-01-28 21:18:06 -08001174 if (fa && fa->fa_tos == tos &&
1175 fa->fa_info->fib_priority == fi->fib_priority) {
1176 struct fib_alias *fa_first, *fa_match;
Robert Olsson19baf832005-06-21 12:43:18 -07001177
1178 err = -EEXIST;
Thomas Graf4e902c52006-08-17 18:14:52 -07001179 if (cfg->fc_nlflags & NLM_F_EXCL)
Robert Olsson19baf832005-06-21 12:43:18 -07001180 goto out;
1181
Julian Anastasov936f6f82008-01-28 21:18:06 -08001182 /* We have 2 goals:
1183 * 1. Find exact match for type, scope, fib_info to avoid
1184 * duplicate routes
1185 * 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it
1186 */
1187 fa_match = NULL;
1188 fa_first = fa;
Alexander Duyck56315f92015-02-25 15:31:31 -08001189 hlist_for_each_entry_from(fa, fa_list) {
Julian Anastasov936f6f82008-01-28 21:18:06 -08001190 if (fa->fa_tos != tos)
1191 break;
1192 if (fa->fa_info->fib_priority != fi->fib_priority)
1193 break;
1194 if (fa->fa_type == cfg->fc_type &&
Julian Anastasov936f6f82008-01-28 21:18:06 -08001195 fa->fa_info == fi) {
1196 fa_match = fa;
1197 break;
1198 }
1199 }
1200
Thomas Graf4e902c52006-08-17 18:14:52 -07001201 if (cfg->fc_nlflags & NLM_F_REPLACE) {
Robert Olsson19baf832005-06-21 12:43:18 -07001202 struct fib_info *fi_drop;
1203 u8 state;
1204
Julian Anastasov936f6f82008-01-28 21:18:06 -08001205 fa = fa_first;
1206 if (fa_match) {
1207 if (fa == fa_match)
1208 err = 0;
Joonwoo Park67250332008-01-18 03:45:18 -08001209 goto out;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001210 }
Robert Olsson2373ce12005-08-25 13:01:29 -07001211 err = -ENOBUFS;
Christoph Lametere94b1762006-12-06 20:33:17 -08001212 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
Robert Olsson2373ce12005-08-25 13:01:29 -07001213 if (new_fa == NULL)
1214 goto out;
Robert Olsson19baf832005-06-21 12:43:18 -07001215
1216 fi_drop = fa->fa_info;
Robert Olsson2373ce12005-08-25 13:01:29 -07001217 new_fa->fa_tos = fa->fa_tos;
1218 new_fa->fa_info = fi;
Thomas Graf4e902c52006-08-17 18:14:52 -07001219 new_fa->fa_type = cfg->fc_type;
Robert Olsson19baf832005-06-21 12:43:18 -07001220 state = fa->fa_state;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001221 new_fa->fa_state = state & ~FA_S_ACCESSED;
Robert Olsson19baf832005-06-21 12:43:18 -07001222
Alexander Duyck56315f92015-02-25 15:31:31 -08001223 hlist_replace_rcu(&fa->fa_list, &new_fa->fa_list);
Robert Olsson2373ce12005-08-25 13:01:29 -07001224 alias_free_mem_rcu(fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001225
1226 fib_release_info(fi_drop);
1227 if (state & FA_S_ACCESSED)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001228 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Milan Kocianb8f55832007-05-23 14:55:06 -07001229 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen,
1230 tb->tb_id, &cfg->fc_nlinfo, NLM_F_REPLACE);
Robert Olsson19baf832005-06-21 12:43:18 -07001231
Olof Johansson91b9a272005-08-09 20:24:39 -07001232 goto succeeded;
Robert Olsson19baf832005-06-21 12:43:18 -07001233 }
1234 /* Error if we find a perfect match which
1235 * uses the same scope, type, and nexthop
1236 * information.
1237 */
Julian Anastasov936f6f82008-01-28 21:18:06 -08001238 if (fa_match)
1239 goto out;
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001240
Thomas Graf4e902c52006-08-17 18:14:52 -07001241 if (!(cfg->fc_nlflags & NLM_F_APPEND))
Julian Anastasov936f6f82008-01-28 21:18:06 -08001242 fa = fa_first;
Robert Olsson19baf832005-06-21 12:43:18 -07001243 }
1244 err = -ENOENT;
Thomas Graf4e902c52006-08-17 18:14:52 -07001245 if (!(cfg->fc_nlflags & NLM_F_CREATE))
Robert Olsson19baf832005-06-21 12:43:18 -07001246 goto out;
1247
1248 err = -ENOBUFS;
Christoph Lametere94b1762006-12-06 20:33:17 -08001249 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
Robert Olsson19baf832005-06-21 12:43:18 -07001250 if (new_fa == NULL)
1251 goto out;
1252
1253 new_fa->fa_info = fi;
1254 new_fa->fa_tos = tos;
Thomas Graf4e902c52006-08-17 18:14:52 -07001255 new_fa->fa_type = cfg->fc_type;
Robert Olsson19baf832005-06-21 12:43:18 -07001256 new_fa->fa_state = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001257 /*
1258 * Insert new entry to the list.
1259 */
1260
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001261 if (!fa_head) {
Stephen Hemmingerfea86ad2008-01-12 20:57:07 -08001262 fa_head = fib_insert_node(t, key, plen);
1263 if (unlikely(!fa_head)) {
1264 err = -ENOMEM;
Robert Olssonf835e472005-06-28 15:00:39 -07001265 goto out_free_new_fa;
Stephen Hemmingerfea86ad2008-01-12 20:57:07 -08001266 }
Robert Olssonf835e472005-06-28 15:00:39 -07001267 }
Robert Olsson19baf832005-06-21 12:43:18 -07001268
David S. Miller21d8c492011-04-14 14:49:37 -07001269 if (!plen)
1270 tb->tb_num_default++;
1271
Alexander Duyck56315f92015-02-25 15:31:31 -08001272 if (fa) {
1273 hlist_add_before_rcu(&new_fa->fa_list, &fa->fa_list);
1274 } else {
1275 struct fib_alias *last;
1276
1277 hlist_for_each_entry(last, fa_head, fa_list)
1278 fa = last;
1279
1280 if (fa)
1281 hlist_add_behind_rcu(&new_fa->fa_list, &fa->fa_list);
1282 else
1283 hlist_add_head_rcu(&new_fa->fa_list, fa_head);
1284 }
Robert Olsson19baf832005-06-21 12:43:18 -07001285
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001286 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Thomas Graf4e902c52006-08-17 18:14:52 -07001287 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen, tb->tb_id,
Milan Kocianb8f55832007-05-23 14:55:06 -07001288 &cfg->fc_nlinfo, 0);
Robert Olsson19baf832005-06-21 12:43:18 -07001289succeeded:
1290 return 0;
Robert Olssonf835e472005-06-28 15:00:39 -07001291
1292out_free_new_fa:
1293 kmem_cache_free(fn_alias_kmem, new_fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001294out:
1295 fib_release_info(fi);
Olof Johansson91b9a272005-08-09 20:24:39 -07001296err:
Robert Olsson19baf832005-06-21 12:43:18 -07001297 return err;
1298}
1299
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001300static inline t_key prefix_mismatch(t_key key, struct tnode *n)
1301{
1302 t_key prefix = n->key;
1303
1304 return (key ^ prefix) & (prefix | -prefix);
1305}
1306
Alexander Duyck345e9b52014-12-31 10:56:24 -08001307/* should be called with rcu_read_lock */
David S. Miller22bd5b92011-03-11 19:54:08 -05001308int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00001309 struct fib_result *res, int fib_flags)
Robert Olsson19baf832005-06-21 12:43:18 -07001310{
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001311 struct trie *t = (struct trie *)tb->tb_data;
Alexander Duyck8274a972014-12-31 10:55:29 -08001312#ifdef CONFIG_IP_FIB_TRIE_STATS
1313 struct trie_use_stats __percpu *stats = t->stats;
1314#endif
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001315 const t_key key = ntohl(flp->daddr);
1316 struct tnode *n, *pn;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001317 struct leaf_info *li;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001318 t_key cindex;
Robert Olsson19baf832005-06-21 12:43:18 -07001319
Robert Olsson2373ce12005-08-25 13:01:29 -07001320 n = rcu_dereference(t->trie);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001321 if (!n)
Alexander Duyck345e9b52014-12-31 10:56:24 -08001322 return -EAGAIN;
Robert Olsson19baf832005-06-21 12:43:18 -07001323
1324#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -08001325 this_cpu_inc(stats->gets);
Robert Olsson19baf832005-06-21 12:43:18 -07001326#endif
1327
Alexander Duyckadaf9812014-12-31 10:55:47 -08001328 pn = n;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001329 cindex = 0;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001330
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001331 /* Step 1: Travel to the longest prefix match in the trie */
1332 for (;;) {
1333 unsigned long index = get_index(key, n);
Robert Olsson19baf832005-06-21 12:43:18 -07001334
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001335 /* This bit of code is a bit tricky but it combines multiple
1336 * checks into a single check. The prefix consists of the
1337 * prefix plus zeros for the "bits" in the prefix. The index
1338 * is the difference between the key and this value. From
1339 * this we can actually derive several pieces of data.
Alexander Duyckb3832112015-01-22 15:51:08 -08001340 * if (index & (~0ul << bits))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001341 * we have a mismatch in skip bits and failed
Alexander Duyckb3832112015-01-22 15:51:08 -08001342 * else
1343 * we know the value is cindex
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001344 */
Alexander Duyckb3832112015-01-22 15:51:08 -08001345 if (index & (~0ul << n->bits))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001346 break;
Robert Olsson19baf832005-06-21 12:43:18 -07001347
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001348 /* we have found a leaf. Prefixes have already been compared */
1349 if (IS_LEAF(n))
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001350 goto found;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001351
1352 /* only record pn and cindex if we are going to be chopping
1353 * bits later. Otherwise we are just wasting cycles.
1354 */
Alexander Duyck5405afd2014-12-31 10:57:08 -08001355 if (n->slen > n->pos) {
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001356 pn = n;
1357 cindex = index;
Olof Johansson91b9a272005-08-09 20:24:39 -07001358 }
1359
Alexander Duyck21d1f112014-12-31 10:57:02 -08001360 n = tnode_get_child_rcu(n, index);
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001361 if (unlikely(!n))
Robert Olsson19baf832005-06-21 12:43:18 -07001362 goto backtrace;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001363 }
1364
1365 /* Step 2: Sort out leaves and begin backtracing for longest prefix */
1366 for (;;) {
1367 /* record the pointer where our next node pointer is stored */
1368 struct tnode __rcu **cptr = n->child;
1369
1370 /* This test verifies that none of the bits that differ
1371 * between the key and the prefix exist in the region of
1372 * the lsb and higher in the prefix.
1373 */
Alexander Duyck5405afd2014-12-31 10:57:08 -08001374 if (unlikely(prefix_mismatch(key, n)) || (n->slen == n->pos))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001375 goto backtrace;
1376
1377 /* exit out and process leaf */
1378 if (unlikely(IS_LEAF(n)))
1379 break;
1380
1381 /* Don't bother recording parent info. Since we are in
1382 * prefix match mode we will have to come back to wherever
1383 * we started this traversal anyway
1384 */
1385
1386 while ((n = rcu_dereference(*cptr)) == NULL) {
1387backtrace:
1388#ifdef CONFIG_IP_FIB_TRIE_STATS
1389 if (!n)
1390 this_cpu_inc(stats->null_node_hit);
1391#endif
1392 /* If we are at cindex 0 there are no more bits for
1393 * us to strip at this level so we must ascend back
1394 * up one level to see if there are any more bits to
1395 * be stripped there.
1396 */
1397 while (!cindex) {
1398 t_key pkey = pn->key;
1399
1400 pn = node_parent_rcu(pn);
1401 if (unlikely(!pn))
Alexander Duyck345e9b52014-12-31 10:56:24 -08001402 return -EAGAIN;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001403#ifdef CONFIG_IP_FIB_TRIE_STATS
1404 this_cpu_inc(stats->backtrack);
1405#endif
1406 /* Get Child's index */
1407 cindex = get_index(pkey, pn);
1408 }
1409
1410 /* strip the least significant bit from the cindex */
1411 cindex &= cindex - 1;
1412
1413 /* grab pointer for next child node */
1414 cptr = &pn->child[cindex];
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001415 }
Robert Olsson19baf832005-06-21 12:43:18 -07001416 }
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001417
Robert Olsson19baf832005-06-21 12:43:18 -07001418found:
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001419 /* Step 3: Process the leaf, if that fails fall back to backtracing */
Alexander Duyck345e9b52014-12-31 10:56:24 -08001420 hlist_for_each_entry_rcu(li, &n->list, hlist) {
1421 struct fib_alias *fa;
1422
Alexander Duyck5786ec62015-02-25 15:31:37 -08001423 if (((key ^ n->key) >= (1ul << li->slen)) &&
1424 ((BITS_PER_LONG > KEYLENGTH) || (li->slen != KEYLENGTH)))
Alexander Duyck345e9b52014-12-31 10:56:24 -08001425 continue;
1426
Alexander Duyck56315f92015-02-25 15:31:31 -08001427 hlist_for_each_entry_rcu(fa, &li->falh, fa_list) {
Alexander Duyck345e9b52014-12-31 10:56:24 -08001428 struct fib_info *fi = fa->fa_info;
1429 int nhsel, err;
1430
1431 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
1432 continue;
1433 if (fi->fib_dead)
1434 continue;
1435 if (fa->fa_info->fib_scope < flp->flowi4_scope)
1436 continue;
1437 fib_alias_accessed(fa);
1438 err = fib_props[fa->fa_type].error;
1439 if (unlikely(err < 0)) {
1440#ifdef CONFIG_IP_FIB_TRIE_STATS
1441 this_cpu_inc(stats->semantic_match_passed);
1442#endif
1443 return err;
1444 }
1445 if (fi->fib_flags & RTNH_F_DEAD)
1446 continue;
1447 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
1448 const struct fib_nh *nh = &fi->fib_nh[nhsel];
1449
1450 if (nh->nh_flags & RTNH_F_DEAD)
1451 continue;
1452 if (flp->flowi4_oif && flp->flowi4_oif != nh->nh_oif)
1453 continue;
1454
1455 if (!(fib_flags & FIB_LOOKUP_NOREF))
1456 atomic_inc(&fi->fib_clntref);
1457
Alexander Duyck5786ec62015-02-25 15:31:37 -08001458 res->prefixlen = KEYLENGTH - li->slen;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001459 res->nh_sel = nhsel;
1460 res->type = fa->fa_type;
1461 res->scope = fi->fib_scope;
1462 res->fi = fi;
1463 res->table = tb;
1464 res->fa_head = &li->falh;
1465#ifdef CONFIG_IP_FIB_TRIE_STATS
1466 this_cpu_inc(stats->semantic_match_passed);
1467#endif
1468 return err;
1469 }
1470 }
1471
1472#ifdef CONFIG_IP_FIB_TRIE_STATS
1473 this_cpu_inc(stats->semantic_match_miss);
1474#endif
1475 }
1476 goto backtrace;
Robert Olsson19baf832005-06-21 12:43:18 -07001477}
Florian Westphal6fc01432011-08-25 13:46:12 +02001478EXPORT_SYMBOL_GPL(fib_table_lookup);
Robert Olsson19baf832005-06-21 12:43:18 -07001479
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001480/*
1481 * Remove the leaf and return parent.
1482 */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001483static void trie_leaf_remove(struct trie *t, struct tnode *l)
Robert Olsson19baf832005-06-21 12:43:18 -07001484{
Alexander Duyck64c9b6f2014-12-31 10:55:35 -08001485 struct tnode *tp = node_parent(l);
Robert Olsson19baf832005-06-21 12:43:18 -07001486
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001487 pr_debug("entering trie_leaf_remove(%p)\n", l);
Robert Olsson19baf832005-06-21 12:43:18 -07001488
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001489 if (tp) {
Alexander Duyck836a0122014-12-31 10:56:06 -08001490 put_child(tp, get_index(l->key, tp), NULL);
Jarek Poplawski7b855762009-06-18 00:28:51 -07001491 trie_rebalance(t, tp);
Alexander Duyck836a0122014-12-31 10:56:06 -08001492 } else {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001493 RCU_INIT_POINTER(t->trie, NULL);
Alexander Duyck836a0122014-12-31 10:56:06 -08001494 }
Robert Olsson19baf832005-06-21 12:43:18 -07001495
Alexander Duyck37fd30f2014-12-31 10:55:41 -08001496 node_free(l);
Robert Olsson19baf832005-06-21 12:43:18 -07001497}
1498
Robert Olssond562f1f2007-03-26 14:22:22 -07001499/*
1500 * Caller must hold RTNL.
1501 */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001502int fib_table_delete(struct fib_table *tb, struct fib_config *cfg)
Robert Olsson19baf832005-06-21 12:43:18 -07001503{
1504 struct trie *t = (struct trie *) tb->tb_data;
1505 u32 key, mask;
Thomas Graf4e902c52006-08-17 18:14:52 -07001506 int plen = cfg->fc_dst_len;
1507 u8 tos = cfg->fc_tos;
Robert Olsson19baf832005-06-21 12:43:18 -07001508 struct fib_alias *fa, *fa_to_delete;
Alexander Duyck56315f92015-02-25 15:31:31 -08001509 struct hlist_head *fa_head;
Alexander Duyckadaf9812014-12-31 10:55:47 -08001510 struct tnode *l;
Olof Johansson91b9a272005-08-09 20:24:39 -07001511 struct leaf_info *li;
1512
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001513 if (plen > 32)
Robert Olsson19baf832005-06-21 12:43:18 -07001514 return -EINVAL;
1515
Thomas Graf4e902c52006-08-17 18:14:52 -07001516 key = ntohl(cfg->fc_dst);
Olof Johansson91b9a272005-08-09 20:24:39 -07001517 mask = ntohl(inet_make_mask(plen));
Robert Olsson19baf832005-06-21 12:43:18 -07001518
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001519 if (key & ~mask)
Robert Olsson19baf832005-06-21 12:43:18 -07001520 return -EINVAL;
1521
1522 key = key & mask;
1523 l = fib_find_node(t, key);
1524
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001525 if (!l)
Robert Olsson19baf832005-06-21 12:43:18 -07001526 return -ESRCH;
1527
Igor Maravicad5b3102012-08-13 10:26:08 +02001528 li = find_leaf_info(l, plen);
1529
1530 if (!li)
1531 return -ESRCH;
1532
1533 fa_head = &li->falh;
Robert Olsson19baf832005-06-21 12:43:18 -07001534 fa = fib_find_alias(fa_head, tos, 0);
1535
1536 if (!fa)
1537 return -ESRCH;
1538
Stephen Hemminger0c7770c2005-08-23 21:59:41 -07001539 pr_debug("Deleting %08x/%d tos=%d t=%p\n", key, plen, tos, t);
Robert Olsson19baf832005-06-21 12:43:18 -07001540
1541 fa_to_delete = NULL;
Alexander Duyck56315f92015-02-25 15:31:31 -08001542 hlist_for_each_entry_from(fa, fa_list) {
Robert Olsson19baf832005-06-21 12:43:18 -07001543 struct fib_info *fi = fa->fa_info;
1544
1545 if (fa->fa_tos != tos)
1546 break;
1547
Thomas Graf4e902c52006-08-17 18:14:52 -07001548 if ((!cfg->fc_type || fa->fa_type == cfg->fc_type) &&
1549 (cfg->fc_scope == RT_SCOPE_NOWHERE ||
David S. Miller37e826c2011-03-24 18:06:47 -07001550 fa->fa_info->fib_scope == cfg->fc_scope) &&
Julian Anastasov74cb3c12011-03-19 12:13:46 +00001551 (!cfg->fc_prefsrc ||
1552 fi->fib_prefsrc == cfg->fc_prefsrc) &&
Thomas Graf4e902c52006-08-17 18:14:52 -07001553 (!cfg->fc_protocol ||
1554 fi->fib_protocol == cfg->fc_protocol) &&
1555 fib_nh_match(cfg, fi) == 0) {
Robert Olsson19baf832005-06-21 12:43:18 -07001556 fa_to_delete = fa;
1557 break;
1558 }
1559 }
1560
Olof Johansson91b9a272005-08-09 20:24:39 -07001561 if (!fa_to_delete)
1562 return -ESRCH;
Robert Olsson19baf832005-06-21 12:43:18 -07001563
Olof Johansson91b9a272005-08-09 20:24:39 -07001564 fa = fa_to_delete;
Thomas Graf4e902c52006-08-17 18:14:52 -07001565 rtmsg_fib(RTM_DELROUTE, htonl(key), fa, plen, tb->tb_id,
Milan Kocianb8f55832007-05-23 14:55:06 -07001566 &cfg->fc_nlinfo, 0);
Robert Olsson19baf832005-06-21 12:43:18 -07001567
Alexander Duyck56315f92015-02-25 15:31:31 -08001568 hlist_del_rcu(&fa->fa_list);
Robert Olsson19baf832005-06-21 12:43:18 -07001569
David S. Miller21d8c492011-04-14 14:49:37 -07001570 if (!plen)
1571 tb->tb_num_default--;
1572
Alexander Duyck56315f92015-02-25 15:31:31 -08001573 if (hlist_empty(fa_head)) {
Alexander Duyck5405afd2014-12-31 10:57:08 -08001574 remove_leaf_info(l, li);
Olof Johansson91b9a272005-08-09 20:24:39 -07001575 free_leaf_info(li);
Robert Olsson2373ce12005-08-25 13:01:29 -07001576 }
Olof Johansson91b9a272005-08-09 20:24:39 -07001577
1578 if (hlist_empty(&l->list))
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001579 trie_leaf_remove(t, l);
Olof Johansson91b9a272005-08-09 20:24:39 -07001580
1581 if (fa->fa_state & FA_S_ACCESSED)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001582 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Olof Johansson91b9a272005-08-09 20:24:39 -07001583
Robert Olsson2373ce12005-08-25 13:01:29 -07001584 fib_release_info(fa->fa_info);
1585 alias_free_mem_rcu(fa);
Olof Johansson91b9a272005-08-09 20:24:39 -07001586 return 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001587}
1588
Alexander Duyck56315f92015-02-25 15:31:31 -08001589static int trie_flush_list(struct hlist_head *head)
Robert Olsson19baf832005-06-21 12:43:18 -07001590{
Alexander Duyck56315f92015-02-25 15:31:31 -08001591 struct hlist_node *tmp;
1592 struct fib_alias *fa;
Robert Olsson19baf832005-06-21 12:43:18 -07001593 int found = 0;
1594
Alexander Duyck56315f92015-02-25 15:31:31 -08001595 hlist_for_each_entry_safe(fa, tmp, head, fa_list) {
Robert Olsson19baf832005-06-21 12:43:18 -07001596 struct fib_info *fi = fa->fa_info;
Robert Olsson19baf832005-06-21 12:43:18 -07001597
Robert Olsson2373ce12005-08-25 13:01:29 -07001598 if (fi && (fi->fib_flags & RTNH_F_DEAD)) {
Alexander Duyck56315f92015-02-25 15:31:31 -08001599 hlist_del_rcu(&fa->fa_list);
Robert Olsson2373ce12005-08-25 13:01:29 -07001600 fib_release_info(fa->fa_info);
1601 alias_free_mem_rcu(fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001602 found++;
1603 }
1604 }
1605 return found;
1606}
1607
Alexander Duyckadaf9812014-12-31 10:55:47 -08001608static int trie_flush_leaf(struct tnode *l)
Robert Olsson19baf832005-06-21 12:43:18 -07001609{
1610 int found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001611 struct hlist_node *tmp;
Alexander Duyck56315f92015-02-25 15:31:31 -08001612 struct leaf_info *li;
Alexander Duyck5786ec62015-02-25 15:31:37 -08001613 unsigned char slen = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001614
Alexander Duyck56315f92015-02-25 15:31:31 -08001615 hlist_for_each_entry_safe(li, tmp, &l->list, hlist) {
Stephen Hemmingeref3660c2008-04-10 03:46:12 -07001616 found += trie_flush_list(&li->falh);
Robert Olsson19baf832005-06-21 12:43:18 -07001617
Alexander Duyck56315f92015-02-25 15:31:31 -08001618 if (hlist_empty(&li->falh)) {
Robert Olsson2373ce12005-08-25 13:01:29 -07001619 hlist_del_rcu(&li->hlist);
Robert Olsson19baf832005-06-21 12:43:18 -07001620 free_leaf_info(li);
Alexander Duyck64c62722015-01-22 15:51:45 -08001621 continue;
Robert Olsson19baf832005-06-21 12:43:18 -07001622 }
Alexander Duyck64c62722015-01-22 15:51:45 -08001623
Alexander Duyck5786ec62015-02-25 15:31:37 -08001624 slen = li->slen;
Robert Olsson19baf832005-06-21 12:43:18 -07001625 }
Alexander Duyck64c62722015-01-22 15:51:45 -08001626
Alexander Duyck5786ec62015-02-25 15:31:37 -08001627 l->slen = slen;
Alexander Duyck64c62722015-01-22 15:51:45 -08001628
Robert Olsson19baf832005-06-21 12:43:18 -07001629 return found;
1630}
1631
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001632/*
1633 * Scan for the next right leaf starting at node p->child[idx]
1634 * Since we have back pointer, no recursion necessary.
1635 */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001636static struct tnode *leaf_walk_rcu(struct tnode *p, struct tnode *c)
Robert Olsson19baf832005-06-21 12:43:18 -07001637{
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001638 do {
Alexander Duyck98293e82014-12-31 10:56:18 -08001639 unsigned long idx = c ? idx = get_index(c->key, p) + 1 : 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001640
Alexander Duyck98293e82014-12-31 10:56:18 -08001641 while (idx < tnode_child_length(p)) {
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001642 c = tnode_get_child_rcu(p, idx++);
Robert Olsson2373ce12005-08-25 13:01:29 -07001643 if (!c)
Olof Johansson91b9a272005-08-09 20:24:39 -07001644 continue;
Robert Olsson19baf832005-06-21 12:43:18 -07001645
Eric Dumazetaab515d2013-08-05 11:18:49 -07001646 if (IS_LEAF(c))
Alexander Duyckadaf9812014-12-31 10:55:47 -08001647 return c;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001648
1649 /* Rescan start scanning in new node */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001650 p = c;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001651 idx = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001652 }
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001653
1654 /* Node empty, walk back up to parent */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001655 c = p;
Eric Dumazeta034ee32010-09-09 23:32:28 +00001656 } while ((p = node_parent_rcu(c)) != NULL);
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001657
1658 return NULL; /* Root of trie */
1659}
1660
Alexander Duyckadaf9812014-12-31 10:55:47 -08001661static struct tnode *trie_firstleaf(struct trie *t)
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001662{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001663 struct tnode *n = rcu_dereference_rtnl(t->trie);
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001664
1665 if (!n)
1666 return NULL;
1667
1668 if (IS_LEAF(n)) /* trie is just a leaf */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001669 return n;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001670
1671 return leaf_walk_rcu(n, NULL);
1672}
1673
Alexander Duyckadaf9812014-12-31 10:55:47 -08001674static struct tnode *trie_nextleaf(struct tnode *l)
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001675{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001676 struct tnode *p = node_parent_rcu(l);
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001677
1678 if (!p)
1679 return NULL; /* trie with just one leaf */
1680
Alexander Duyckadaf9812014-12-31 10:55:47 -08001681 return leaf_walk_rcu(p, l);
Robert Olsson19baf832005-06-21 12:43:18 -07001682}
1683
Alexander Duyckadaf9812014-12-31 10:55:47 -08001684static struct tnode *trie_leafindex(struct trie *t, int index)
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001685{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001686 struct tnode *l = trie_firstleaf(t);
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001687
Stephen Hemmingerec28cf72008-02-11 21:12:49 -08001688 while (l && index-- > 0)
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001689 l = trie_nextleaf(l);
Stephen Hemmingerec28cf72008-02-11 21:12:49 -08001690
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001691 return l;
1692}
1693
1694
Robert Olssond562f1f2007-03-26 14:22:22 -07001695/*
1696 * Caller must hold RTNL.
1697 */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001698int fib_table_flush(struct fib_table *tb)
Robert Olsson19baf832005-06-21 12:43:18 -07001699{
1700 struct trie *t = (struct trie *) tb->tb_data;
Alexander Duyckadaf9812014-12-31 10:55:47 -08001701 struct tnode *l, *ll = NULL;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001702 int found = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001703
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001704 for (l = trie_firstleaf(t); l; l = trie_nextleaf(l)) {
Stephen Hemmingeref3660c2008-04-10 03:46:12 -07001705 found += trie_flush_leaf(l);
Robert Olsson19baf832005-06-21 12:43:18 -07001706
Alexander Duyck64c62722015-01-22 15:51:45 -08001707 if (ll) {
1708 if (hlist_empty(&ll->list))
1709 trie_leaf_remove(t, ll);
1710 else
1711 leaf_pull_suffix(ll);
1712 }
1713
Robert Olsson19baf832005-06-21 12:43:18 -07001714 ll = l;
1715 }
1716
Alexander Duyck64c62722015-01-22 15:51:45 -08001717 if (ll) {
1718 if (hlist_empty(&ll->list))
1719 trie_leaf_remove(t, ll);
1720 else
1721 leaf_pull_suffix(ll);
1722 }
Robert Olsson19baf832005-06-21 12:43:18 -07001723
Stephen Hemminger0c7770c2005-08-23 21:59:41 -07001724 pr_debug("trie_flush found=%d\n", found);
Robert Olsson19baf832005-06-21 12:43:18 -07001725 return found;
1726}
1727
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00001728void fib_free_table(struct fib_table *tb)
1729{
Alexander Duyck8274a972014-12-31 10:55:29 -08001730#ifdef CONFIG_IP_FIB_TRIE_STATS
1731 struct trie *t = (struct trie *)tb->tb_data;
1732
1733 free_percpu(t->stats);
1734#endif /* CONFIG_IP_FIB_TRIE_STATS */
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00001735 kfree(tb);
1736}
1737
Alexander Duyck5786ec62015-02-25 15:31:37 -08001738static int fn_trie_dump_fa(t_key key, int slen, struct hlist_head *fah,
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001739 struct fib_table *tb,
Robert Olsson19baf832005-06-21 12:43:18 -07001740 struct sk_buff *skb, struct netlink_callback *cb)
1741{
1742 int i, s_i;
1743 struct fib_alias *fa;
Al Viro32ab5f82006-09-26 22:21:45 -07001744 __be32 xkey = htonl(key);
Robert Olsson19baf832005-06-21 12:43:18 -07001745
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001746 s_i = cb->args[5];
Robert Olsson19baf832005-06-21 12:43:18 -07001747 i = 0;
1748
Robert Olsson2373ce12005-08-25 13:01:29 -07001749 /* rcu_read_lock is hold by caller */
1750
Alexander Duyck56315f92015-02-25 15:31:31 -08001751 hlist_for_each_entry_rcu(fa, fah, fa_list) {
Robert Olsson19baf832005-06-21 12:43:18 -07001752 if (i < s_i) {
1753 i++;
1754 continue;
1755 }
Robert Olsson19baf832005-06-21 12:43:18 -07001756
Eric W. Biederman15e47302012-09-07 20:12:54 +00001757 if (fib_dump_info(skb, NETLINK_CB(cb->skb).portid,
Robert Olsson19baf832005-06-21 12:43:18 -07001758 cb->nlh->nlmsg_seq,
1759 RTM_NEWROUTE,
1760 tb->tb_id,
1761 fa->fa_type,
Thomas Grafbe403ea2006-08-17 18:15:17 -07001762 xkey,
Alexander Duyck5786ec62015-02-25 15:31:37 -08001763 KEYLENGTH - slen,
Robert Olsson19baf832005-06-21 12:43:18 -07001764 fa->fa_tos,
Stephen Hemminger64347f72008-01-22 21:55:01 -08001765 fa->fa_info, NLM_F_MULTI) < 0) {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001766 cb->args[5] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001767 return -1;
Olof Johansson91b9a272005-08-09 20:24:39 -07001768 }
Robert Olsson19baf832005-06-21 12:43:18 -07001769 i++;
1770 }
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001771 cb->args[5] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001772 return skb->len;
1773}
1774
Alexander Duyckadaf9812014-12-31 10:55:47 -08001775static int fn_trie_dump_leaf(struct tnode *l, struct fib_table *tb,
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001776 struct sk_buff *skb, struct netlink_callback *cb)
Robert Olsson19baf832005-06-21 12:43:18 -07001777{
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001778 struct leaf_info *li;
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001779 int i, s_i;
Robert Olsson19baf832005-06-21 12:43:18 -07001780
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001781 s_i = cb->args[4];
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001782 i = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001783
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001784 /* rcu_read_lock is hold by caller */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001785 hlist_for_each_entry_rcu(li, &l->list, hlist) {
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001786 if (i < s_i) {
1787 i++;
Robert Olsson19baf832005-06-21 12:43:18 -07001788 continue;
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001789 }
Robert Olsson19baf832005-06-21 12:43:18 -07001790
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001791 if (i > s_i)
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001792 cb->args[5] = 0;
Olof Johansson91b9a272005-08-09 20:24:39 -07001793
Alexander Duyck56315f92015-02-25 15:31:31 -08001794 if (hlist_empty(&li->falh))
Robert Olsson19baf832005-06-21 12:43:18 -07001795 continue;
1796
Alexander Duyck5786ec62015-02-25 15:31:37 -08001797 if (fn_trie_dump_fa(l->key, li->slen, &li->falh, tb, skb, cb) < 0) {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001798 cb->args[4] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001799 return -1;
1800 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001801 i++;
Robert Olsson19baf832005-06-21 12:43:18 -07001802 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001803
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001804 cb->args[4] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001805 return skb->len;
1806}
1807
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001808int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
1809 struct netlink_callback *cb)
Robert Olsson19baf832005-06-21 12:43:18 -07001810{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001811 struct tnode *l;
Robert Olsson19baf832005-06-21 12:43:18 -07001812 struct trie *t = (struct trie *) tb->tb_data;
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001813 t_key key = cb->args[2];
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001814 int count = cb->args[3];
Robert Olsson19baf832005-06-21 12:43:18 -07001815
Robert Olsson2373ce12005-08-25 13:01:29 -07001816 rcu_read_lock();
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001817 /* Dump starting at last key.
1818 * Note: 0.0.0.0/0 (ie default) is first key.
1819 */
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001820 if (count == 0)
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001821 l = trie_firstleaf(t);
1822 else {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001823 /* Normally, continue from last key, but if that is missing
1824 * fallback to using slow rescan
1825 */
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001826 l = fib_find_node(t, key);
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001827 if (!l)
1828 l = trie_leafindex(t, count);
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001829 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001830
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001831 while (l) {
1832 cb->args[2] = l->key;
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001833 if (fn_trie_dump_leaf(l, tb, skb, cb) < 0) {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001834 cb->args[3] = count;
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001835 rcu_read_unlock();
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001836 return -1;
Robert Olsson19baf832005-06-21 12:43:18 -07001837 }
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001838
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001839 ++count;
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001840 l = trie_nextleaf(l);
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001841 memset(&cb->args[4], 0,
1842 sizeof(cb->args) - 4*sizeof(cb->args[0]));
Robert Olsson19baf832005-06-21 12:43:18 -07001843 }
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001844 cb->args[3] = count;
Robert Olsson2373ce12005-08-25 13:01:29 -07001845 rcu_read_unlock();
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001846
Robert Olsson19baf832005-06-21 12:43:18 -07001847 return skb->len;
Robert Olsson19baf832005-06-21 12:43:18 -07001848}
1849
David S. Miller5348ba82011-02-01 15:30:56 -08001850void __init fib_trie_init(void)
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001851{
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001852 fn_alias_kmem = kmem_cache_create("ip_fib_alias",
1853 sizeof(struct fib_alias),
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -08001854 0, SLAB_PANIC, NULL);
1855
1856 trie_leaf_kmem = kmem_cache_create("ip_fib_trie",
Alexander Duyckadaf9812014-12-31 10:55:47 -08001857 max(sizeof(struct tnode),
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -08001858 sizeof(struct leaf_info)),
1859 0, SLAB_PANIC, NULL);
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001860}
Robert Olsson19baf832005-06-21 12:43:18 -07001861
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001862
David S. Miller5348ba82011-02-01 15:30:56 -08001863struct fib_table *fib_trie_table(u32 id)
Robert Olsson19baf832005-06-21 12:43:18 -07001864{
1865 struct fib_table *tb;
1866 struct trie *t;
1867
Robert Olsson19baf832005-06-21 12:43:18 -07001868 tb = kmalloc(sizeof(struct fib_table) + sizeof(struct trie),
1869 GFP_KERNEL);
1870 if (tb == NULL)
1871 return NULL;
1872
1873 tb->tb_id = id;
Denis V. Lunev971b8932007-12-08 00:32:23 -08001874 tb->tb_default = -1;
David S. Miller21d8c492011-04-14 14:49:37 -07001875 tb->tb_num_default = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001876
1877 t = (struct trie *) tb->tb_data;
Alexander Duyck8274a972014-12-31 10:55:29 -08001878 RCU_INIT_POINTER(t->trie, NULL);
1879#ifdef CONFIG_IP_FIB_TRIE_STATS
1880 t->stats = alloc_percpu(struct trie_use_stats);
1881 if (!t->stats) {
1882 kfree(tb);
1883 tb = NULL;
1884 }
1885#endif
Robert Olsson19baf832005-06-21 12:43:18 -07001886
Robert Olsson19baf832005-06-21 12:43:18 -07001887 return tb;
1888}
1889
Robert Olsson19baf832005-06-21 12:43:18 -07001890#ifdef CONFIG_PROC_FS
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001891/* Depth first Trie walk iterator */
1892struct fib_trie_iter {
Denis V. Lunev1c340b22008-01-10 03:27:17 -08001893 struct seq_net_private p;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001894 struct fib_table *tb;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001895 struct tnode *tnode;
Eric Dumazeta034ee32010-09-09 23:32:28 +00001896 unsigned int index;
1897 unsigned int depth;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001898};
Robert Olsson19baf832005-06-21 12:43:18 -07001899
Alexander Duyckadaf9812014-12-31 10:55:47 -08001900static struct tnode *fib_trie_get_next(struct fib_trie_iter *iter)
Robert Olsson19baf832005-06-21 12:43:18 -07001901{
Alexander Duyck98293e82014-12-31 10:56:18 -08001902 unsigned long cindex = iter->index;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001903 struct tnode *tn = iter->tnode;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001904 struct tnode *p;
1905
Eric W. Biederman6640e692007-01-24 14:42:04 -08001906 /* A single entry routing table */
1907 if (!tn)
1908 return NULL;
1909
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001910 pr_debug("get_next iter={node=%p index=%d depth=%d}\n",
1911 iter->tnode, iter->index, iter->depth);
1912rescan:
Alexander Duyck98293e82014-12-31 10:56:18 -08001913 while (cindex < tnode_child_length(tn)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08001914 struct tnode *n = tnode_get_child_rcu(tn, cindex);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001915
1916 if (n) {
1917 if (IS_LEAF(n)) {
1918 iter->tnode = tn;
1919 iter->index = cindex + 1;
1920 } else {
1921 /* push down one level */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001922 iter->tnode = n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001923 iter->index = 0;
1924 ++iter->depth;
1925 }
1926 return n;
1927 }
1928
1929 ++cindex;
1930 }
1931
1932 /* Current node exhausted, pop back up */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001933 p = node_parent_rcu(tn);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001934 if (p) {
Alexander Duycke9b44012014-12-31 10:56:12 -08001935 cindex = get_index(tn->key, p) + 1;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001936 tn = p;
1937 --iter->depth;
1938 goto rescan;
1939 }
1940
1941 /* got root? */
Robert Olsson19baf832005-06-21 12:43:18 -07001942 return NULL;
1943}
1944
Alexander Duyckadaf9812014-12-31 10:55:47 -08001945static struct tnode *fib_trie_get_first(struct fib_trie_iter *iter,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001946 struct trie *t)
Robert Olsson19baf832005-06-21 12:43:18 -07001947{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001948 struct tnode *n;
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08001949
Stephen Hemminger132adf52007-03-08 20:44:43 -08001950 if (!t)
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08001951 return NULL;
1952
1953 n = rcu_dereference(t->trie);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001954 if (!n)
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08001955 return NULL;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001956
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001957 if (IS_TNODE(n)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08001958 iter->tnode = n;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001959 iter->index = 0;
1960 iter->depth = 1;
1961 } else {
1962 iter->tnode = NULL;
1963 iter->index = 0;
1964 iter->depth = 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001965 }
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001966
1967 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07001968}
1969
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001970static void trie_collect_stats(struct trie *t, struct trie_stat *s)
Robert Olsson19baf832005-06-21 12:43:18 -07001971{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001972 struct tnode *n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001973 struct fib_trie_iter iter;
Robert Olsson19baf832005-06-21 12:43:18 -07001974
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001975 memset(s, 0, sizeof(*s));
Robert Olsson19baf832005-06-21 12:43:18 -07001976
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001977 rcu_read_lock();
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001978 for (n = fib_trie_get_first(&iter, t); n; n = fib_trie_get_next(&iter)) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001979 if (IS_LEAF(n)) {
Stephen Hemminger93672292008-01-22 21:54:05 -08001980 struct leaf_info *li;
Stephen Hemminger93672292008-01-22 21:54:05 -08001981
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001982 s->leaves++;
1983 s->totdepth += iter.depth;
1984 if (iter.depth > s->maxdepth)
1985 s->maxdepth = iter.depth;
Stephen Hemminger93672292008-01-22 21:54:05 -08001986
Alexander Duyckadaf9812014-12-31 10:55:47 -08001987 hlist_for_each_entry_rcu(li, &n->list, hlist)
Stephen Hemminger93672292008-01-22 21:54:05 -08001988 ++s->prefixes;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001989 } else {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001990 s->tnodes++;
Alexander Duyckadaf9812014-12-31 10:55:47 -08001991 if (n->bits < MAX_STAT_DEPTH)
1992 s->nodesizes[n->bits]++;
Alexander Duyck30cfe7c2015-01-22 15:51:33 -08001993 s->nullpointers += n->empty_children;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001994 }
1995 }
1996 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07001997}
1998
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001999/*
Robert Olsson19baf832005-06-21 12:43:18 -07002000 * This outputs /proc/net/fib_triestats
Robert Olsson19baf832005-06-21 12:43:18 -07002001 */
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002002static void trie_show_stats(struct seq_file *seq, struct trie_stat *stat)
Robert Olsson19baf832005-06-21 12:43:18 -07002003{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002004 unsigned int i, max, pointers, bytes, avdepth;
Robert Olsson19baf832005-06-21 12:43:18 -07002005
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002006 if (stat->leaves)
2007 avdepth = stat->totdepth*100 / stat->leaves;
2008 else
2009 avdepth = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07002010
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002011 seq_printf(seq, "\tAver depth: %u.%02d\n",
2012 avdepth / 100, avdepth % 100);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002013 seq_printf(seq, "\tMax depth: %u\n", stat->maxdepth);
Robert Olsson19baf832005-06-21 12:43:18 -07002014
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002015 seq_printf(seq, "\tLeaves: %u\n", stat->leaves);
Alexander Duyckadaf9812014-12-31 10:55:47 -08002016 bytes = sizeof(struct tnode) * stat->leaves;
Stephen Hemminger93672292008-01-22 21:54:05 -08002017
2018 seq_printf(seq, "\tPrefixes: %u\n", stat->prefixes);
2019 bytes += sizeof(struct leaf_info) * stat->prefixes;
2020
Stephen Hemminger187b5182008-01-12 20:55:55 -08002021 seq_printf(seq, "\tInternal nodes: %u\n\t", stat->tnodes);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002022 bytes += sizeof(struct tnode) * stat->tnodes;
Robert Olsson19baf832005-06-21 12:43:18 -07002023
Robert Olsson06ef9212006-03-20 21:35:01 -08002024 max = MAX_STAT_DEPTH;
2025 while (max > 0 && stat->nodesizes[max-1] == 0)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002026 max--;
Robert Olsson19baf832005-06-21 12:43:18 -07002027
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002028 pointers = 0;
Jerry Snitselaarf585a992013-07-22 12:01:58 -07002029 for (i = 1; i < max; i++)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002030 if (stat->nodesizes[i] != 0) {
Stephen Hemminger187b5182008-01-12 20:55:55 -08002031 seq_printf(seq, " %u: %u", i, stat->nodesizes[i]);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002032 pointers += (1<<i) * stat->nodesizes[i];
2033 }
2034 seq_putc(seq, '\n');
Stephen Hemminger187b5182008-01-12 20:55:55 -08002035 seq_printf(seq, "\tPointers: %u\n", pointers);
Robert Olsson19baf832005-06-21 12:43:18 -07002036
Alexander Duyckadaf9812014-12-31 10:55:47 -08002037 bytes += sizeof(struct tnode *) * pointers;
Stephen Hemminger187b5182008-01-12 20:55:55 -08002038 seq_printf(seq, "Null ptrs: %u\n", stat->nullpointers);
2039 seq_printf(seq, "Total size: %u kB\n", (bytes + 1023) / 1024);
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002040}
Robert Olsson19baf832005-06-21 12:43:18 -07002041
2042#ifdef CONFIG_IP_FIB_TRIE_STATS
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002043static void trie_show_usage(struct seq_file *seq,
Alexander Duyck8274a972014-12-31 10:55:29 -08002044 const struct trie_use_stats __percpu *stats)
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002045{
Alexander Duyck8274a972014-12-31 10:55:29 -08002046 struct trie_use_stats s = { 0 };
2047 int cpu;
2048
2049 /* loop through all of the CPUs and gather up the stats */
2050 for_each_possible_cpu(cpu) {
2051 const struct trie_use_stats *pcpu = per_cpu_ptr(stats, cpu);
2052
2053 s.gets += pcpu->gets;
2054 s.backtrack += pcpu->backtrack;
2055 s.semantic_match_passed += pcpu->semantic_match_passed;
2056 s.semantic_match_miss += pcpu->semantic_match_miss;
2057 s.null_node_hit += pcpu->null_node_hit;
2058 s.resize_node_skipped += pcpu->resize_node_skipped;
2059 }
2060
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002061 seq_printf(seq, "\nCounters:\n---------\n");
Alexander Duyck8274a972014-12-31 10:55:29 -08002062 seq_printf(seq, "gets = %u\n", s.gets);
2063 seq_printf(seq, "backtracks = %u\n", s.backtrack);
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002064 seq_printf(seq, "semantic match passed = %u\n",
Alexander Duyck8274a972014-12-31 10:55:29 -08002065 s.semantic_match_passed);
2066 seq_printf(seq, "semantic match miss = %u\n", s.semantic_match_miss);
2067 seq_printf(seq, "null node hit= %u\n", s.null_node_hit);
2068 seq_printf(seq, "skipped node resize = %u\n\n", s.resize_node_skipped);
Robert Olsson19baf832005-06-21 12:43:18 -07002069}
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002070#endif /* CONFIG_IP_FIB_TRIE_STATS */
2071
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002072static void fib_table_print(struct seq_file *seq, struct fib_table *tb)
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002073{
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002074 if (tb->tb_id == RT_TABLE_LOCAL)
2075 seq_puts(seq, "Local:\n");
2076 else if (tb->tb_id == RT_TABLE_MAIN)
2077 seq_puts(seq, "Main:\n");
2078 else
2079 seq_printf(seq, "Id %d:\n", tb->tb_id);
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002080}
Robert Olsson19baf832005-06-21 12:43:18 -07002081
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002082
Robert Olsson19baf832005-06-21 12:43:18 -07002083static int fib_triestat_seq_show(struct seq_file *seq, void *v)
2084{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002085 struct net *net = (struct net *)seq->private;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002086 unsigned int h;
Eric W. Biederman877a9bf2007-12-07 00:47:47 -08002087
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002088 seq_printf(seq,
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002089 "Basic info: size of leaf:"
2090 " %Zd bytes, size of tnode: %Zd bytes.\n",
Alexander Duyckadaf9812014-12-31 10:55:47 -08002091 sizeof(struct tnode), sizeof(struct tnode));
Olof Johansson91b9a272005-08-09 20:24:39 -07002092
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002093 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2094 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002095 struct fib_table *tb;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002096
Sasha Levinb67bfe02013-02-27 17:06:00 -08002097 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002098 struct trie *t = (struct trie *) tb->tb_data;
2099 struct trie_stat stat;
2100
2101 if (!t)
2102 continue;
2103
2104 fib_table_print(seq, tb);
2105
2106 trie_collect_stats(t, &stat);
2107 trie_show_stats(seq, &stat);
2108#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -08002109 trie_show_usage(seq, t->stats);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002110#endif
2111 }
2112 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002113
Robert Olsson19baf832005-06-21 12:43:18 -07002114 return 0;
2115}
2116
Robert Olsson19baf832005-06-21 12:43:18 -07002117static int fib_triestat_seq_open(struct inode *inode, struct file *file)
2118{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002119 return single_open_net(inode, file, fib_triestat_seq_show);
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002120}
2121
Arjan van de Ven9a321442007-02-12 00:55:35 -08002122static const struct file_operations fib_triestat_fops = {
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07002123 .owner = THIS_MODULE,
2124 .open = fib_triestat_seq_open,
2125 .read = seq_read,
2126 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002127 .release = single_release_net,
Robert Olsson19baf832005-06-21 12:43:18 -07002128};
2129
Alexander Duyckadaf9812014-12-31 10:55:47 -08002130static struct tnode *fib_trie_get_idx(struct seq_file *seq, loff_t pos)
Robert Olsson19baf832005-06-21 12:43:18 -07002131{
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002132 struct fib_trie_iter *iter = seq->private;
2133 struct net *net = seq_file_net(seq);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002134 loff_t idx = 0;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002135 unsigned int h;
Robert Olsson19baf832005-06-21 12:43:18 -07002136
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002137 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2138 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002139 struct fib_table *tb;
2140
Sasha Levinb67bfe02013-02-27 17:06:00 -08002141 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002142 struct tnode *n;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002143
2144 for (n = fib_trie_get_first(iter,
2145 (struct trie *) tb->tb_data);
2146 n; n = fib_trie_get_next(iter))
2147 if (pos == idx++) {
2148 iter->tb = tb;
2149 return n;
2150 }
2151 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002152 }
Robert Olsson19baf832005-06-21 12:43:18 -07002153
Robert Olsson19baf832005-06-21 12:43:18 -07002154 return NULL;
2155}
2156
2157static void *fib_trie_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08002158 __acquires(RCU)
Robert Olsson19baf832005-06-21 12:43:18 -07002159{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002160 rcu_read_lock();
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002161 return fib_trie_get_idx(seq, *pos);
Robert Olsson19baf832005-06-21 12:43:18 -07002162}
2163
2164static void *fib_trie_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2165{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002166 struct fib_trie_iter *iter = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002167 struct net *net = seq_file_net(seq);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002168 struct fib_table *tb = iter->tb;
2169 struct hlist_node *tb_node;
2170 unsigned int h;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002171 struct tnode *n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002172
Robert Olsson19baf832005-06-21 12:43:18 -07002173 ++*pos;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002174 /* next node in same table */
2175 n = fib_trie_get_next(iter);
2176 if (n)
2177 return n;
Olof Johansson91b9a272005-08-09 20:24:39 -07002178
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002179 /* walk rest of this hash chain */
2180 h = tb->tb_id & (FIB_TABLE_HASHSZ - 1);
Eric Dumazet0a5c0472011-03-31 01:51:35 -07002181 while ((tb_node = rcu_dereference(hlist_next_rcu(&tb->tb_hlist)))) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002182 tb = hlist_entry(tb_node, struct fib_table, tb_hlist);
2183 n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
2184 if (n)
2185 goto found;
2186 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002187
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002188 /* new hash chain */
2189 while (++h < FIB_TABLE_HASHSZ) {
2190 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002191 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002192 n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
2193 if (n)
2194 goto found;
2195 }
2196 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002197 return NULL;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002198
2199found:
2200 iter->tb = tb;
2201 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07002202}
2203
2204static void fib_trie_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08002205 __releases(RCU)
Robert Olsson19baf832005-06-21 12:43:18 -07002206{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002207 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07002208}
2209
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002210static void seq_indent(struct seq_file *seq, int n)
2211{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002212 while (n-- > 0)
2213 seq_puts(seq, " ");
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002214}
Robert Olsson19baf832005-06-21 12:43:18 -07002215
Eric Dumazet28d36e32008-01-14 23:09:56 -08002216static inline const char *rtn_scope(char *buf, size_t len, enum rt_scope_t s)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002217{
Stephen Hemminger132adf52007-03-08 20:44:43 -08002218 switch (s) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002219 case RT_SCOPE_UNIVERSE: return "universe";
2220 case RT_SCOPE_SITE: return "site";
2221 case RT_SCOPE_LINK: return "link";
2222 case RT_SCOPE_HOST: return "host";
2223 case RT_SCOPE_NOWHERE: return "nowhere";
2224 default:
Eric Dumazet28d36e32008-01-14 23:09:56 -08002225 snprintf(buf, len, "scope=%d", s);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002226 return buf;
2227 }
2228}
2229
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -07002230static const char *const rtn_type_names[__RTN_MAX] = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002231 [RTN_UNSPEC] = "UNSPEC",
2232 [RTN_UNICAST] = "UNICAST",
2233 [RTN_LOCAL] = "LOCAL",
2234 [RTN_BROADCAST] = "BROADCAST",
2235 [RTN_ANYCAST] = "ANYCAST",
2236 [RTN_MULTICAST] = "MULTICAST",
2237 [RTN_BLACKHOLE] = "BLACKHOLE",
2238 [RTN_UNREACHABLE] = "UNREACHABLE",
2239 [RTN_PROHIBIT] = "PROHIBIT",
2240 [RTN_THROW] = "THROW",
2241 [RTN_NAT] = "NAT",
2242 [RTN_XRESOLVE] = "XRESOLVE",
2243};
2244
Eric Dumazeta034ee32010-09-09 23:32:28 +00002245static inline const char *rtn_type(char *buf, size_t len, unsigned int t)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002246{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002247 if (t < __RTN_MAX && rtn_type_names[t])
2248 return rtn_type_names[t];
Eric Dumazet28d36e32008-01-14 23:09:56 -08002249 snprintf(buf, len, "type %u", t);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002250 return buf;
2251}
2252
2253/* Pretty print the trie */
Robert Olsson19baf832005-06-21 12:43:18 -07002254static int fib_trie_seq_show(struct seq_file *seq, void *v)
2255{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002256 const struct fib_trie_iter *iter = seq->private;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002257 struct tnode *n = v;
Robert Olsson19baf832005-06-21 12:43:18 -07002258
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002259 if (!node_parent_rcu(n))
2260 fib_table_print(seq, iter->tb);
Robert Olsson095b8502007-01-26 19:06:01 -08002261
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002262 if (IS_TNODE(n)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002263 __be32 prf = htonl(n->key);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002264
Alexander Duycke9b44012014-12-31 10:56:12 -08002265 seq_indent(seq, iter->depth-1);
2266 seq_printf(seq, " +-- %pI4/%zu %u %u %u\n",
2267 &prf, KEYLENGTH - n->pos - n->bits, n->bits,
2268 n->full_children, n->empty_children);
Olof Johansson91b9a272005-08-09 20:24:39 -07002269 } else {
Stephen Hemminger13280422008-01-22 21:54:37 -08002270 struct leaf_info *li;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002271 __be32 val = htonl(n->key);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002272
2273 seq_indent(seq, iter->depth);
Harvey Harrison673d57e2008-10-31 00:53:57 -07002274 seq_printf(seq, " |-- %pI4\n", &val);
Eric Dumazet28d36e32008-01-14 23:09:56 -08002275
Alexander Duyckadaf9812014-12-31 10:55:47 -08002276 hlist_for_each_entry_rcu(li, &n->list, hlist) {
Stephen Hemminger13280422008-01-22 21:54:37 -08002277 struct fib_alias *fa;
Eric Dumazet28d36e32008-01-14 23:09:56 -08002278
Alexander Duyck56315f92015-02-25 15:31:31 -08002279 hlist_for_each_entry_rcu(fa, &li->falh, fa_list) {
Stephen Hemminger13280422008-01-22 21:54:37 -08002280 char buf1[32], buf2[32];
Eric Dumazet28d36e32008-01-14 23:09:56 -08002281
Stephen Hemminger13280422008-01-22 21:54:37 -08002282 seq_indent(seq, iter->depth+1);
Alexander Duyck5786ec62015-02-25 15:31:37 -08002283 seq_printf(seq, " /%zu %s %s",
2284 KEYLENGTH - li->slen,
Stephen Hemminger13280422008-01-22 21:54:37 -08002285 rtn_scope(buf1, sizeof(buf1),
David S. Miller37e826c2011-03-24 18:06:47 -07002286 fa->fa_info->fib_scope),
Stephen Hemminger13280422008-01-22 21:54:37 -08002287 rtn_type(buf2, sizeof(buf2),
2288 fa->fa_type));
2289 if (fa->fa_tos)
Denis V. Lunevb9c4d822008-02-05 02:58:45 -08002290 seq_printf(seq, " tos=%d", fa->fa_tos);
Stephen Hemminger13280422008-01-22 21:54:37 -08002291 seq_putc(seq, '\n');
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002292 }
2293 }
Robert Olsson19baf832005-06-21 12:43:18 -07002294 }
2295
2296 return 0;
2297}
2298
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002299static const struct seq_operations fib_trie_seq_ops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002300 .start = fib_trie_seq_start,
2301 .next = fib_trie_seq_next,
2302 .stop = fib_trie_seq_stop,
2303 .show = fib_trie_seq_show,
Robert Olsson19baf832005-06-21 12:43:18 -07002304};
2305
2306static int fib_trie_seq_open(struct inode *inode, struct file *file)
2307{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002308 return seq_open_net(inode, file, &fib_trie_seq_ops,
2309 sizeof(struct fib_trie_iter));
Robert Olsson19baf832005-06-21 12:43:18 -07002310}
2311
Arjan van de Ven9a321442007-02-12 00:55:35 -08002312static const struct file_operations fib_trie_fops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002313 .owner = THIS_MODULE,
2314 .open = fib_trie_seq_open,
2315 .read = seq_read,
2316 .llseek = seq_lseek,
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002317 .release = seq_release_net,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002318};
2319
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002320struct fib_route_iter {
2321 struct seq_net_private p;
2322 struct trie *main_trie;
2323 loff_t pos;
2324 t_key key;
2325};
2326
Alexander Duyckadaf9812014-12-31 10:55:47 -08002327static struct tnode *fib_route_get_idx(struct fib_route_iter *iter, loff_t pos)
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002328{
Alexander Duyckadaf9812014-12-31 10:55:47 -08002329 struct tnode *l = NULL;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002330 struct trie *t = iter->main_trie;
2331
2332 /* use cache location of last found key */
2333 if (iter->pos > 0 && pos >= iter->pos && (l = fib_find_node(t, iter->key)))
2334 pos -= iter->pos;
2335 else {
2336 iter->pos = 0;
2337 l = trie_firstleaf(t);
2338 }
2339
2340 while (l && pos-- > 0) {
2341 iter->pos++;
2342 l = trie_nextleaf(l);
2343 }
2344
2345 if (l)
2346 iter->key = pos; /* remember it */
2347 else
2348 iter->pos = 0; /* forget it */
2349
2350 return l;
2351}
2352
2353static void *fib_route_seq_start(struct seq_file *seq, loff_t *pos)
2354 __acquires(RCU)
2355{
2356 struct fib_route_iter *iter = seq->private;
2357 struct fib_table *tb;
2358
2359 rcu_read_lock();
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002360 tb = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN);
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002361 if (!tb)
2362 return NULL;
2363
2364 iter->main_trie = (struct trie *) tb->tb_data;
2365 if (*pos == 0)
2366 return SEQ_START_TOKEN;
2367 else
2368 return fib_route_get_idx(iter, *pos - 1);
2369}
2370
2371static void *fib_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2372{
2373 struct fib_route_iter *iter = seq->private;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002374 struct tnode *l = v;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002375
2376 ++*pos;
2377 if (v == SEQ_START_TOKEN) {
2378 iter->pos = 0;
2379 l = trie_firstleaf(iter->main_trie);
2380 } else {
2381 iter->pos++;
2382 l = trie_nextleaf(l);
2383 }
2384
2385 if (l)
2386 iter->key = l->key;
2387 else
2388 iter->pos = 0;
2389 return l;
2390}
2391
2392static void fib_route_seq_stop(struct seq_file *seq, void *v)
2393 __releases(RCU)
2394{
2395 rcu_read_unlock();
2396}
2397
Eric Dumazeta034ee32010-09-09 23:32:28 +00002398static unsigned int fib_flag_trans(int type, __be32 mask, const struct fib_info *fi)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002399{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002400 unsigned int flags = 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002401
Eric Dumazeta034ee32010-09-09 23:32:28 +00002402 if (type == RTN_UNREACHABLE || type == RTN_PROHIBIT)
2403 flags = RTF_REJECT;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002404 if (fi && fi->fib_nh->nh_gw)
2405 flags |= RTF_GATEWAY;
Al Viro32ab5f82006-09-26 22:21:45 -07002406 if (mask == htonl(0xFFFFFFFF))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002407 flags |= RTF_HOST;
2408 flags |= RTF_UP;
2409 return flags;
2410}
2411
2412/*
2413 * This outputs /proc/net/route.
2414 * The format of the file is not supposed to be changed
Eric Dumazeta034ee32010-09-09 23:32:28 +00002415 * and needs to be same as fib_hash output to avoid breaking
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002416 * legacy utilities
2417 */
2418static int fib_route_seq_show(struct seq_file *seq, void *v)
2419{
Alexander Duyckadaf9812014-12-31 10:55:47 -08002420 struct tnode *l = v;
Stephen Hemminger13280422008-01-22 21:54:37 -08002421 struct leaf_info *li;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002422
2423 if (v == SEQ_START_TOKEN) {
2424 seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
2425 "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
2426 "\tWindow\tIRTT");
2427 return 0;
2428 }
2429
Sasha Levinb67bfe02013-02-27 17:06:00 -08002430 hlist_for_each_entry_rcu(li, &l->list, hlist) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002431 struct fib_alias *fa;
Al Viro32ab5f82006-09-26 22:21:45 -07002432 __be32 mask, prefix;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002433
Alexander Duyck5786ec62015-02-25 15:31:37 -08002434 mask = inet_make_mask(KEYLENGTH - li->slen);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002435 prefix = htonl(l->key);
2436
Alexander Duyck56315f92015-02-25 15:31:31 -08002437 hlist_for_each_entry_rcu(fa, &li->falh, fa_list) {
Herbert Xu1371e372005-10-15 09:42:39 +10002438 const struct fib_info *fi = fa->fa_info;
Eric Dumazeta034ee32010-09-09 23:32:28 +00002439 unsigned int flags = fib_flag_trans(fa->fa_type, mask, fi);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002440
2441 if (fa->fa_type == RTN_BROADCAST
2442 || fa->fa_type == RTN_MULTICAST)
2443 continue;
2444
Tetsuo Handa652586d2013-11-14 14:31:57 -08002445 seq_setwidth(seq, 127);
2446
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002447 if (fi)
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002448 seq_printf(seq,
2449 "%s\t%08X\t%08X\t%04X\t%d\t%u\t"
Tetsuo Handa652586d2013-11-14 14:31:57 -08002450 "%d\t%08X\t%d\t%u\t%u",
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002451 fi->fib_dev ? fi->fib_dev->name : "*",
2452 prefix,
2453 fi->fib_nh->nh_gw, flags, 0, 0,
2454 fi->fib_priority,
2455 mask,
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002456 (fi->fib_advmss ?
2457 fi->fib_advmss + 40 : 0),
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002458 fi->fib_window,
Tetsuo Handa652586d2013-11-14 14:31:57 -08002459 fi->fib_rtt >> 3);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002460 else
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002461 seq_printf(seq,
2462 "*\t%08X\t%08X\t%04X\t%d\t%u\t"
Tetsuo Handa652586d2013-11-14 14:31:57 -08002463 "%d\t%08X\t%d\t%u\t%u",
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002464 prefix, 0, flags, 0, 0, 0,
Tetsuo Handa652586d2013-11-14 14:31:57 -08002465 mask, 0, 0, 0);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002466
Tetsuo Handa652586d2013-11-14 14:31:57 -08002467 seq_pad(seq, '\n');
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002468 }
2469 }
2470
2471 return 0;
2472}
2473
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002474static const struct seq_operations fib_route_seq_ops = {
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002475 .start = fib_route_seq_start,
2476 .next = fib_route_seq_next,
2477 .stop = fib_route_seq_stop,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002478 .show = fib_route_seq_show,
2479};
2480
2481static int fib_route_seq_open(struct inode *inode, struct file *file)
2482{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002483 return seq_open_net(inode, file, &fib_route_seq_ops,
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002484 sizeof(struct fib_route_iter));
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002485}
2486
Arjan van de Ven9a321442007-02-12 00:55:35 -08002487static const struct file_operations fib_route_fops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002488 .owner = THIS_MODULE,
2489 .open = fib_route_seq_open,
2490 .read = seq_read,
2491 .llseek = seq_lseek,
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002492 .release = seq_release_net,
Robert Olsson19baf832005-06-21 12:43:18 -07002493};
2494
Denis V. Lunev61a02652008-01-10 03:21:09 -08002495int __net_init fib_proc_init(struct net *net)
Robert Olsson19baf832005-06-21 12:43:18 -07002496{
Gao fengd4beaa62013-02-18 01:34:54 +00002497 if (!proc_create("fib_trie", S_IRUGO, net->proc_net, &fib_trie_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002498 goto out1;
2499
Gao fengd4beaa62013-02-18 01:34:54 +00002500 if (!proc_create("fib_triestat", S_IRUGO, net->proc_net,
2501 &fib_triestat_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002502 goto out2;
2503
Gao fengd4beaa62013-02-18 01:34:54 +00002504 if (!proc_create("route", S_IRUGO, net->proc_net, &fib_route_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002505 goto out3;
2506
Robert Olsson19baf832005-06-21 12:43:18 -07002507 return 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002508
2509out3:
Gao fengece31ff2013-02-18 01:34:56 +00002510 remove_proc_entry("fib_triestat", net->proc_net);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002511out2:
Gao fengece31ff2013-02-18 01:34:56 +00002512 remove_proc_entry("fib_trie", net->proc_net);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002513out1:
2514 return -ENOMEM;
Robert Olsson19baf832005-06-21 12:43:18 -07002515}
2516
Denis V. Lunev61a02652008-01-10 03:21:09 -08002517void __net_exit fib_proc_exit(struct net *net)
Robert Olsson19baf832005-06-21 12:43:18 -07002518{
Gao fengece31ff2013-02-18 01:34:56 +00002519 remove_proc_entry("fib_trie", net->proc_net);
2520 remove_proc_entry("fib_triestat", net->proc_net);
2521 remove_proc_entry("route", net->proc_net);
Robert Olsson19baf832005-06-21 12:43:18 -07002522}
2523
2524#endif /* CONFIG_PROC_FS */