blob: 80892f5650301e3c7f21ae381663fefe6a47c4a9 [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
Robert Olsson19baf832005-06-21 12:43:18 -070086#define KEYLENGTH (8*sizeof(t_key))
Robert Olsson19baf832005-06-21 12:43:18 -070087
Robert Olsson19baf832005-06-21 12:43:18 -070088typedef unsigned int t_key;
89
Alexander Duyck64c9b6f2014-12-31 10:55:35 -080090#define IS_TNODE(n) ((n)->bits)
91#define IS_LEAF(n) (!(n)->bits)
Robert Olsson2373ce12005-08-25 13:01:29 -070092
Alexander Duycke9b44012014-12-31 10:56:12 -080093#define get_index(_key, _kv) (((_key) ^ (_kv)->key) >> (_kv)->pos)
Alexander Duyck9f9e6362014-12-31 10:55:54 -080094
Alexander Duyck64c9b6f2014-12-31 10:55:35 -080095struct tnode {
96 t_key key;
97 unsigned char bits; /* 2log(KEYLENGTH) bits needed */
98 unsigned char pos; /* 2log(KEYLENGTH) bits needed */
Alexander Duyck5405afd2014-12-31 10:57:08 -080099 unsigned char slen;
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800100 struct tnode __rcu *parent;
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800101 struct rcu_head rcu;
Alexander Duyckadaf9812014-12-31 10:55:47 -0800102 union {
103 /* The fields in this struct are valid if bits > 0 (TNODE) */
104 struct {
105 unsigned int full_children; /* KEYLENGTH bits needed */
106 unsigned int empty_children; /* KEYLENGTH bits needed */
107 struct tnode __rcu *child[0];
108 };
109 /* This list pointer if valid if bits == 0 (LEAF) */
110 struct hlist_head list;
111 };
Robert Olsson19baf832005-06-21 12:43:18 -0700112};
113
114struct leaf_info {
115 struct hlist_node hlist;
116 int plen;
Eric Dumazet5c745012011-07-18 03:16:33 +0000117 u32 mask_plen; /* ntohl(inet_make_mask(plen)) */
Robert Olsson19baf832005-06-21 12:43:18 -0700118 struct list_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 Dumazet8d965442008-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 Dumazet8d965442008-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 Duyckadaf9812014-12-31 10:55:47 -0800305static struct tnode *leaf_new(t_key key)
Robert Olsson19baf832005-06-21 12:43:18 -0700306{
Alexander Duyckadaf9812014-12-31 10:55:47 -0800307 struct tnode *l = kmem_cache_alloc(trie_leaf_kmem, GFP_KERNEL);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700308 if (l) {
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800309 l->parent = NULL;
310 /* set key and pos to reflect full key value
311 * any trailing zeros in the key should be ignored
312 * as the nodes are searched
313 */
314 l->key = key;
Alexander Duyck5405afd2014-12-31 10:57:08 -0800315 l->slen = 0;
Alexander Duycke9b44012014-12-31 10:56:12 -0800316 l->pos = 0;
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800317 /* set bits to 0 indicating we are not a tnode */
318 l->bits = 0;
319
Robert Olsson19baf832005-06-21 12:43:18 -0700320 INIT_HLIST_HEAD(&l->list);
321 }
322 return l;
323}
324
325static struct leaf_info *leaf_info_new(int plen)
326{
327 struct leaf_info *li = kmalloc(sizeof(struct leaf_info), GFP_KERNEL);
Robert Olsson2373ce12005-08-25 13:01:29 -0700328 if (li) {
329 li->plen = plen;
Eric Dumazet5c745012011-07-18 03:16:33 +0000330 li->mask_plen = ntohl(inet_make_mask(plen));
Robert Olsson2373ce12005-08-25 13:01:29 -0700331 INIT_LIST_HEAD(&li->falh);
Patrick McHardyf0e36f82005-07-05 14:44:55 -0700332 }
Robert Olsson2373ce12005-08-25 13:01:29 -0700333 return li;
Patrick McHardyf0e36f82005-07-05 14:44:55 -0700334}
335
Stephen Hemmingera07f5f52008-01-22 21:53:36 -0800336static struct tnode *tnode_new(t_key key, int pos, int bits)
Robert Olsson19baf832005-06-21 12:43:18 -0700337{
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800338 size_t sz = offsetof(struct tnode, child[1 << bits]);
Patrick McHardyf0e36f82005-07-05 14:44:55 -0700339 struct tnode *tn = tnode_alloc(sz);
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800340 unsigned int shift = pos + bits;
341
342 /* verify bits and pos their msb bits clear and values are valid */
343 BUG_ON(!bits || (shift > KEYLENGTH));
Robert Olsson19baf832005-06-21 12:43:18 -0700344
Olof Johansson91b9a272005-08-09 20:24:39 -0700345 if (tn) {
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800346 tn->parent = NULL;
Alexander Duyck5405afd2014-12-31 10:57:08 -0800347 tn->slen = pos;
Robert Olsson19baf832005-06-21 12:43:18 -0700348 tn->pos = pos;
349 tn->bits = bits;
Alexander Duycke9b44012014-12-31 10:56:12 -0800350 tn->key = (shift < KEYLENGTH) ? (key >> shift) << shift : 0;
Robert Olsson19baf832005-06-21 12:43:18 -0700351 tn->full_children = 0;
352 tn->empty_children = 1<<bits;
353 }
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700354
Eric Dumazeta034ee32010-09-09 23:32:28 +0000355 pr_debug("AT %p s=%zu %zu\n", tn, sizeof(struct tnode),
Alexander Duyckadaf9812014-12-31 10:55:47 -0800356 sizeof(struct tnode *) << bits);
Robert Olsson19baf832005-06-21 12:43:18 -0700357 return tn;
358}
359
Alexander Duycke9b44012014-12-31 10:56:12 -0800360/* Check whether a tnode 'n' is "full", i.e. it is an internal node
Robert Olsson19baf832005-06-21 12:43:18 -0700361 * and no bits are skipped. See discussion in dyntree paper p. 6
362 */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800363static inline int tnode_full(const struct tnode *tn, const struct tnode *n)
Robert Olsson19baf832005-06-21 12:43:18 -0700364{
Alexander Duycke9b44012014-12-31 10:56:12 -0800365 return n && ((n->pos + n->bits) == tn->pos) && IS_TNODE(n);
Robert Olsson19baf832005-06-21 12:43:18 -0700366}
367
Alexander Duyckff181ed2014-12-31 10:56:43 -0800368/* Add a child at position i overwriting the old value.
369 * Update the value of full_children and empty_children.
370 */
371static void put_child(struct tnode *tn, unsigned long i, struct tnode *n)
Robert Olsson19baf832005-06-21 12:43:18 -0700372{
Alexander Duyck21d1f112014-12-31 10:57:02 -0800373 struct tnode *chi = tnode_get_child(tn, i);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800374 int isfull, wasfull;
Robert Olsson19baf832005-06-21 12:43:18 -0700375
Alexander Duyck98293e82014-12-31 10:56:18 -0800376 BUG_ON(i >= tnode_child_length(tn));
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700377
Robert Olsson19baf832005-06-21 12:43:18 -0700378 /* update emptyChildren */
379 if (n == NULL && chi != NULL)
380 tn->empty_children++;
381 else if (n != NULL && chi == NULL)
382 tn->empty_children--;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700383
Robert Olsson19baf832005-06-21 12:43:18 -0700384 /* update fullChildren */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800385 wasfull = tnode_full(tn, chi);
Robert Olsson19baf832005-06-21 12:43:18 -0700386 isfull = tnode_full(tn, n);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800387
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700388 if (wasfull && !isfull)
Robert Olsson19baf832005-06-21 12:43:18 -0700389 tn->full_children--;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700390 else if (!wasfull && isfull)
Robert Olsson19baf832005-06-21 12:43:18 -0700391 tn->full_children++;
Olof Johansson91b9a272005-08-09 20:24:39 -0700392
Alexander Duyck5405afd2014-12-31 10:57:08 -0800393 if (n && (tn->slen < n->slen))
394 tn->slen = n->slen;
395
Eric Dumazetcf778b02012-01-12 04:41:32 +0000396 rcu_assign_pointer(tn->child[i], n);
Robert Olsson19baf832005-06-21 12:43:18 -0700397}
398
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800399static void update_children(struct tnode *tn)
400{
401 unsigned long i;
402
403 /* update all of the child parent pointers */
404 for (i = tnode_child_length(tn); i;) {
405 struct tnode *inode = tnode_get_child(tn, --i);
406
407 if (!inode)
408 continue;
409
410 /* Either update the children of a tnode that
411 * already belongs to us or update the child
412 * to point to ourselves.
413 */
414 if (node_parent(inode) == tn)
415 update_children(inode);
416 else
417 node_set_parent(inode, tn);
418 }
419}
420
421static inline void put_child_root(struct tnode *tp, struct trie *t,
422 t_key key, struct tnode *n)
Alexander Duyck836a0122014-12-31 10:56:06 -0800423{
424 if (tp)
425 put_child(tp, get_index(key, tp), n);
426 else
427 rcu_assign_pointer(t->trie, n);
428}
429
Alexander Duyckfc86a932014-12-31 10:56:49 -0800430static inline void tnode_free_init(struct tnode *tn)
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700431{
Alexander Duyckfc86a932014-12-31 10:56:49 -0800432 tn->rcu.next = NULL;
433}
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700434
Alexander Duyckfc86a932014-12-31 10:56:49 -0800435static inline void tnode_free_append(struct tnode *tn, struct tnode *n)
436{
437 n->rcu.next = tn->rcu.next;
438 tn->rcu.next = &n->rcu;
439}
440
441static void tnode_free(struct tnode *tn)
442{
443 struct callback_head *head = &tn->rcu;
444
445 while (head) {
446 head = head->next;
447 tnode_free_size += offsetof(struct tnode, child[1 << tn->bits]);
448 node_free(tn);
449
450 tn = container_of(head, struct tnode, rcu);
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700451 }
Alexander Duyckfc86a932014-12-31 10:56:49 -0800452
453 if (tnode_free_size >= PAGE_SIZE * sync_pages) {
454 tnode_free_size = 0;
455 synchronize_rcu();
456 }
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700457}
458
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800459static void replace(struct trie *t, struct tnode *oldtnode, struct tnode *tn)
460{
461 struct tnode *tp = node_parent(oldtnode);
462 unsigned long i;
463
464 /* setup the parent pointer out of and back into this node */
465 NODE_INIT_PARENT(tn, tp);
466 put_child_root(tp, t, tn->key, tn);
467
468 /* update all of the child parent pointers */
469 update_children(tn);
470
471 /* all pointers should be clean so we are done */
472 tnode_free(oldtnode);
473
474 /* resize children now that oldtnode is freed */
475 for (i = tnode_child_length(tn); i;) {
476 struct tnode *inode = tnode_get_child(tn, --i);
477
478 /* resize child node */
479 if (tnode_full(tn, inode))
480 resize(t, inode);
481 }
482}
483
Alexander Duyckff181ed2014-12-31 10:56:43 -0800484static int inflate(struct trie *t, struct tnode *oldtnode)
Robert Olsson19baf832005-06-21 12:43:18 -0700485{
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800486 struct tnode *tn;
487 unsigned long i;
Alexander Duycke9b44012014-12-31 10:56:12 -0800488 t_key m;
Robert Olsson19baf832005-06-21 12:43:18 -0700489
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700490 pr_debug("In inflate\n");
Robert Olsson19baf832005-06-21 12:43:18 -0700491
Alexander Duycke9b44012014-12-31 10:56:12 -0800492 tn = tnode_new(oldtnode->key, oldtnode->pos - 1, oldtnode->bits + 1);
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700493 if (!tn)
Alexander Duyckff181ed2014-12-31 10:56:43 -0800494 return -ENOMEM;
Robert Olsson2f368952005-07-05 15:02:40 -0700495
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800496 /* prepare oldtnode to be freed */
497 tnode_free_init(oldtnode);
498
Alexander Duyck12c081a2014-12-31 10:56:55 -0800499 /* Assemble all of the pointers in our cluster, in this case that
500 * represents all of the pointers out of our allocated nodes that
501 * point to existing tnodes and the links between our allocated
502 * nodes.
Robert Olsson2f368952005-07-05 15:02:40 -0700503 */
Alexander Duyck12c081a2014-12-31 10:56:55 -0800504 for (i = tnode_child_length(oldtnode), m = 1u << tn->pos; i;) {
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800505 struct tnode *inode = tnode_get_child(oldtnode, --i);
506 struct tnode *node0, *node1;
507 unsigned long j, k;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700508
Robert Olsson19baf832005-06-21 12:43:18 -0700509 /* An empty child */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800510 if (inode == NULL)
Robert Olsson19baf832005-06-21 12:43:18 -0700511 continue;
512
513 /* A leaf or an internal node with skipped bits */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800514 if (!tnode_full(oldtnode, inode)) {
Alexander Duycke9b44012014-12-31 10:56:12 -0800515 put_child(tn, get_index(inode->key, tn), inode);
Robert Olsson19baf832005-06-21 12:43:18 -0700516 continue;
517 }
518
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800519 /* drop the node in the old tnode free list */
520 tnode_free_append(oldtnode, inode);
521
Robert Olsson19baf832005-06-21 12:43:18 -0700522 /* An internal node with two children */
Robert Olsson19baf832005-06-21 12:43:18 -0700523 if (inode->bits == 1) {
Alexander Duyck12c081a2014-12-31 10:56:55 -0800524 put_child(tn, 2 * i + 1, tnode_get_child(inode, 1));
525 put_child(tn, 2 * i, tnode_get_child(inode, 0));
Olof Johansson91b9a272005-08-09 20:24:39 -0700526 continue;
Robert Olsson19baf832005-06-21 12:43:18 -0700527 }
528
Olof Johansson91b9a272005-08-09 20:24:39 -0700529 /* We will replace this node 'inode' with two new
Alexander Duyck12c081a2014-12-31 10:56:55 -0800530 * ones, 'node0' and 'node1', each with half of the
Olof Johansson91b9a272005-08-09 20:24:39 -0700531 * original children. The two new nodes will have
532 * a position one bit further down the key and this
533 * means that the "significant" part of their keys
534 * (see the discussion near the top of this file)
535 * will differ by one bit, which will be "0" in
Alexander Duyck12c081a2014-12-31 10:56:55 -0800536 * node0's key and "1" in node1's key. Since we are
Olof Johansson91b9a272005-08-09 20:24:39 -0700537 * moving the key position by one step, the bit that
538 * we are moving away from - the bit at position
Alexander Duyck12c081a2014-12-31 10:56:55 -0800539 * (tn->pos) - is the one that will differ between
540 * node0 and node1. So... we synthesize that bit in the
541 * two new keys.
Olof Johansson91b9a272005-08-09 20:24:39 -0700542 */
Alexander Duyck12c081a2014-12-31 10:56:55 -0800543 node1 = tnode_new(inode->key | m, inode->pos, inode->bits - 1);
544 if (!node1)
545 goto nomem;
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800546 node0 = tnode_new(inode->key, inode->pos, inode->bits - 1);
Robert Olsson19baf832005-06-21 12:43:18 -0700547
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800548 tnode_free_append(tn, node1);
Alexander Duyck12c081a2014-12-31 10:56:55 -0800549 if (!node0)
550 goto nomem;
551 tnode_free_append(tn, node0);
Robert Olsson19baf832005-06-21 12:43:18 -0700552
Alexander Duyck12c081a2014-12-31 10:56:55 -0800553 /* populate child pointers in new nodes */
554 for (k = tnode_child_length(inode), j = k / 2; j;) {
555 put_child(node1, --j, tnode_get_child(inode, --k));
556 put_child(node0, j, tnode_get_child(inode, j));
557 put_child(node1, --j, tnode_get_child(inode, --k));
558 put_child(node0, j, tnode_get_child(inode, j));
Robert Olsson19baf832005-06-21 12:43:18 -0700559 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800560
Alexander Duyck12c081a2014-12-31 10:56:55 -0800561 /* link new nodes to parent */
562 NODE_INIT_PARENT(node1, tn);
563 NODE_INIT_PARENT(node0, tn);
Olof Johansson91b9a272005-08-09 20:24:39 -0700564
Alexander Duyck12c081a2014-12-31 10:56:55 -0800565 /* link parent to nodes */
566 put_child(tn, 2 * i + 1, node1);
567 put_child(tn, 2 * i, node0);
Robert Olsson19baf832005-06-21 12:43:18 -0700568 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800569
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800570 /* setup the parent pointers into and out of this node */
571 replace(t, oldtnode, tn);
Alexander Duyckfc86a932014-12-31 10:56:49 -0800572
Alexander Duyckff181ed2014-12-31 10:56:43 -0800573 return 0;
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700574nomem:
Alexander Duyckfc86a932014-12-31 10:56:49 -0800575 /* all pointers should be clean so we are done */
576 tnode_free(tn);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800577 return -ENOMEM;
Robert Olsson19baf832005-06-21 12:43:18 -0700578}
579
Alexander Duyckff181ed2014-12-31 10:56:43 -0800580static int halve(struct trie *t, struct tnode *oldtnode)
Robert Olsson19baf832005-06-21 12:43:18 -0700581{
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800582 struct tnode *tn;
Alexander Duyck12c081a2014-12-31 10:56:55 -0800583 unsigned long i;
Robert Olsson19baf832005-06-21 12:43:18 -0700584
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700585 pr_debug("In halve\n");
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700586
Alexander Duycke9b44012014-12-31 10:56:12 -0800587 tn = tnode_new(oldtnode->key, oldtnode->pos + 1, oldtnode->bits - 1);
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700588 if (!tn)
Alexander Duyckff181ed2014-12-31 10:56:43 -0800589 return -ENOMEM;
Robert Olsson2f368952005-07-05 15:02:40 -0700590
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800591 /* prepare oldtnode to be freed */
592 tnode_free_init(oldtnode);
593
Alexander Duyck12c081a2014-12-31 10:56:55 -0800594 /* Assemble all of the pointers in our cluster, in this case that
595 * represents all of the pointers out of our allocated nodes that
596 * point to existing tnodes and the links between our allocated
597 * nodes.
Robert Olsson2f368952005-07-05 15:02:40 -0700598 */
Alexander Duyck12c081a2014-12-31 10:56:55 -0800599 for (i = tnode_child_length(oldtnode); i;) {
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800600 struct tnode *node1 = tnode_get_child(oldtnode, --i);
601 struct tnode *node0 = tnode_get_child(oldtnode, --i);
602 struct tnode *inode;
Robert Olsson2f368952005-07-05 15:02:40 -0700603
Alexander Duyck12c081a2014-12-31 10:56:55 -0800604 /* At least one of the children is empty */
605 if (!node1 || !node0) {
606 put_child(tn, i / 2, node1 ? : node0);
607 continue;
Robert Olsson2f368952005-07-05 15:02:40 -0700608 }
Robert Olsson2f368952005-07-05 15:02:40 -0700609
Alexander Duyck12c081a2014-12-31 10:56:55 -0800610 /* Two nonempty children */
611 inode = tnode_new(node0->key, oldtnode->pos, 1);
612 if (!inode) {
613 tnode_free(tn);
614 return -ENOMEM;
615 }
616 tnode_free_append(tn, inode);
617
618 /* initialize pointers out of node */
619 put_child(inode, 1, node1);
620 put_child(inode, 0, node0);
621 NODE_INIT_PARENT(inode, tn);
622
623 /* link parent to node */
624 put_child(tn, i / 2, inode);
Robert Olsson2f368952005-07-05 15:02:40 -0700625 }
Robert Olsson19baf832005-06-21 12:43:18 -0700626
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800627 /* setup the parent pointers into and out of this node */
628 replace(t, oldtnode, tn);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800629
630 return 0;
Robert Olsson19baf832005-06-21 12:43:18 -0700631}
632
Alexander Duyck5405afd2014-12-31 10:57:08 -0800633static unsigned char update_suffix(struct tnode *tn)
634{
635 unsigned char slen = tn->pos;
636 unsigned long stride, i;
637
638 /* search though the list of children looking for nodes that might
639 * have a suffix greater than the one we currently have. This is
640 * why we start with a stride of 2 since a stride of 1 would
641 * represent the nodes with suffix length equal to tn->pos
642 */
643 for (i = 0, stride = 0x2ul ; i < tnode_child_length(tn); i += stride) {
644 struct tnode *n = tnode_get_child(tn, i);
645
646 if (!n || (n->slen <= slen))
647 continue;
648
649 /* update stride and slen based on new value */
650 stride <<= (n->slen - slen);
651 slen = n->slen;
652 i &= ~(stride - 1);
653
654 /* if slen covers all but the last bit we can stop here
655 * there will be nothing longer than that since only node
656 * 0 and 1 << (bits - 1) could have that as their suffix
657 * length.
658 */
659 if ((slen + 1) >= (tn->pos + tn->bits))
660 break;
661 }
662
663 tn->slen = slen;
664
665 return slen;
666}
667
Alexander Duyckf05a4812014-12-31 10:56:37 -0800668/* From "Implementing a dynamic compressed trie" by Stefan Nilsson of
669 * the Helsinki University of Technology and Matti Tikkanen of Nokia
670 * Telecommunications, page 6:
671 * "A node is doubled if the ratio of non-empty children to all
672 * children in the *doubled* node is at least 'high'."
673 *
674 * 'high' in this instance is the variable 'inflate_threshold'. It
675 * is expressed as a percentage, so we multiply it with
676 * tnode_child_length() and instead of multiplying by 2 (since the
677 * child array will be doubled by inflate()) and multiplying
678 * the left-hand side by 100 (to handle the percentage thing) we
679 * multiply the left-hand side by 50.
680 *
681 * The left-hand side may look a bit weird: tnode_child_length(tn)
682 * - tn->empty_children is of course the number of non-null children
683 * in the current node. tn->full_children is the number of "full"
684 * children, that is non-null tnodes with a skip value of 0.
685 * All of those will be doubled in the resulting inflated tnode, so
686 * we just count them one extra time here.
687 *
688 * A clearer way to write this would be:
689 *
690 * to_be_doubled = tn->full_children;
691 * not_to_be_doubled = tnode_child_length(tn) - tn->empty_children -
692 * tn->full_children;
693 *
694 * new_child_length = tnode_child_length(tn) * 2;
695 *
696 * new_fill_factor = 100 * (not_to_be_doubled + 2*to_be_doubled) /
697 * new_child_length;
698 * if (new_fill_factor >= inflate_threshold)
699 *
700 * ...and so on, tho it would mess up the while () loop.
701 *
702 * anyway,
703 * 100 * (not_to_be_doubled + 2*to_be_doubled) / new_child_length >=
704 * inflate_threshold
705 *
706 * avoid a division:
707 * 100 * (not_to_be_doubled + 2*to_be_doubled) >=
708 * inflate_threshold * new_child_length
709 *
710 * expand not_to_be_doubled and to_be_doubled, and shorten:
711 * 100 * (tnode_child_length(tn) - tn->empty_children +
712 * tn->full_children) >= inflate_threshold * new_child_length
713 *
714 * expand new_child_length:
715 * 100 * (tnode_child_length(tn) - tn->empty_children +
716 * tn->full_children) >=
717 * inflate_threshold * tnode_child_length(tn) * 2
718 *
719 * shorten again:
720 * 50 * (tn->full_children + tnode_child_length(tn) -
721 * tn->empty_children) >= inflate_threshold *
722 * tnode_child_length(tn)
723 *
724 */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800725static bool should_inflate(const struct tnode *tp, const struct tnode *tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800726{
727 unsigned long used = tnode_child_length(tn);
728 unsigned long threshold = used;
729
730 /* Keep root node larger */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800731 threshold *= tp ? inflate_threshold : inflate_threshold_root;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800732 used += tn->full_children;
733 used -= tn->empty_children;
734
735 return tn->pos && ((50 * used) >= threshold);
736}
737
Alexander Duyckff181ed2014-12-31 10:56:43 -0800738static bool should_halve(const struct tnode *tp, const struct tnode *tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800739{
740 unsigned long used = tnode_child_length(tn);
741 unsigned long threshold = used;
742
743 /* Keep root node larger */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800744 threshold *= tp ? halve_threshold : halve_threshold_root;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800745 used -= tn->empty_children;
746
747 return (tn->bits > 1) && ((100 * used) < threshold);
748}
749
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800750#define MAX_WORK 10
Alexander Duyckff181ed2014-12-31 10:56:43 -0800751static void resize(struct trie *t, struct tnode *tn)
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800752{
Alexander Duyckff181ed2014-12-31 10:56:43 -0800753 struct tnode *tp = node_parent(tn), *n = NULL;
754 struct tnode __rcu **cptr;
Alexander Duycka80e89d2015-01-22 15:51:20 -0800755 int max_work = MAX_WORK;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800756
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800757 pr_debug("In tnode_resize %p inflate_threshold=%d threshold=%d\n",
758 tn, inflate_threshold, halve_threshold);
759
Alexander Duyckff181ed2014-12-31 10:56:43 -0800760 /* track the tnode via the pointer from the parent instead of
761 * doing it ourselves. This way we can let RCU fully do its
762 * thing without us interfering
763 */
764 cptr = tp ? &tp->child[get_index(tn->key, tp)] : &t->trie;
765 BUG_ON(tn != rtnl_dereference(*cptr));
766
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800767 /* No children */
768 if (tn->empty_children > (tnode_child_length(tn) - 1))
769 goto no_children;
770
771 /* One child */
772 if (tn->empty_children == (tnode_child_length(tn) - 1))
773 goto one_child;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800774
775 /* Double as long as the resulting node has a number of
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800776 * nonempty nodes that are above the threshold.
777 */
Alexander Duycka80e89d2015-01-22 15:51:20 -0800778 while (should_inflate(tp, tn) && max_work) {
Alexander Duyckff181ed2014-12-31 10:56:43 -0800779 if (inflate(t, tn)) {
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800780#ifdef CONFIG_IP_FIB_TRIE_STATS
781 this_cpu_inc(t->stats->resize_node_skipped);
782#endif
783 break;
784 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800785
Alexander Duycka80e89d2015-01-22 15:51:20 -0800786 max_work--;
Alexander Duyckff181ed2014-12-31 10:56:43 -0800787 tn = rtnl_dereference(*cptr);
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800788 }
789
790 /* Return if at least one inflate is run */
791 if (max_work != MAX_WORK)
Alexander Duyckff181ed2014-12-31 10:56:43 -0800792 return;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800793
Alexander Duyckf05a4812014-12-31 10:56:37 -0800794 /* Halve as long as the number of empty children in this
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800795 * node is above threshold.
796 */
Alexander Duycka80e89d2015-01-22 15:51:20 -0800797 while (should_halve(tp, tn) && max_work) {
Alexander Duyckff181ed2014-12-31 10:56:43 -0800798 if (halve(t, tn)) {
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800799#ifdef CONFIG_IP_FIB_TRIE_STATS
800 this_cpu_inc(t->stats->resize_node_skipped);
801#endif
802 break;
803 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800804
Alexander Duycka80e89d2015-01-22 15:51:20 -0800805 max_work--;
Alexander Duyckff181ed2014-12-31 10:56:43 -0800806 tn = rtnl_dereference(*cptr);
807 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800808
809 /* Only one child remains */
810 if (tn->empty_children == (tnode_child_length(tn) - 1)) {
811 unsigned long i;
812one_child:
813 for (i = tnode_child_length(tn); !n && i;)
814 n = tnode_get_child(tn, --i);
815no_children:
816 /* compress one level */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800817 put_child_root(tp, t, tn->key, n);
818 node_set_parent(n, tp);
819
820 /* drop dead node */
Alexander Duyckfc86a932014-12-31 10:56:49 -0800821 tnode_free_init(tn);
822 tnode_free(tn);
Alexander Duyck5405afd2014-12-31 10:57:08 -0800823 return;
824 }
825
826 /* Return if at least one deflate was run */
827 if (max_work != MAX_WORK)
828 return;
829
830 /* push the suffix length to the parent node */
831 if (tn->slen > tn->pos) {
832 unsigned char slen = update_suffix(tn);
833
834 if (tp && (slen > tp->slen))
835 tp->slen = slen;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800836 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800837}
838
Robert Olsson772cb712005-09-19 15:31:18 -0700839/* readside must use rcu_read_lock currently dump routines
Robert Olsson2373ce12005-08-25 13:01:29 -0700840 via get_fa_head and dump */
841
Alexander Duyckadaf9812014-12-31 10:55:47 -0800842static struct leaf_info *find_leaf_info(struct tnode *l, int plen)
Robert Olsson19baf832005-06-21 12:43:18 -0700843{
Robert Olsson772cb712005-09-19 15:31:18 -0700844 struct hlist_head *head = &l->list;
Robert Olsson19baf832005-06-21 12:43:18 -0700845 struct leaf_info *li;
846
Sasha Levinb67bfe02013-02-27 17:06:00 -0800847 hlist_for_each_entry_rcu(li, head, hlist)
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700848 if (li->plen == plen)
Robert Olsson19baf832005-06-21 12:43:18 -0700849 return li;
Olof Johansson91b9a272005-08-09 20:24:39 -0700850
Robert Olsson19baf832005-06-21 12:43:18 -0700851 return NULL;
852}
853
Alexander Duyckadaf9812014-12-31 10:55:47 -0800854static inline struct list_head *get_fa_head(struct tnode *l, int plen)
Robert Olsson19baf832005-06-21 12:43:18 -0700855{
Robert Olsson772cb712005-09-19 15:31:18 -0700856 struct leaf_info *li = find_leaf_info(l, plen);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700857
Olof Johansson91b9a272005-08-09 20:24:39 -0700858 if (!li)
859 return NULL;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700860
Olof Johansson91b9a272005-08-09 20:24:39 -0700861 return &li->falh;
Robert Olsson19baf832005-06-21 12:43:18 -0700862}
863
Alexander Duyck5405afd2014-12-31 10:57:08 -0800864static void leaf_pull_suffix(struct tnode *l)
Robert Olsson19baf832005-06-21 12:43:18 -0700865{
Alexander Duyck5405afd2014-12-31 10:57:08 -0800866 struct tnode *tp = node_parent(l);
867
868 while (tp && (tp->slen > tp->pos) && (tp->slen > l->slen)) {
869 if (update_suffix(tp) > l->slen)
870 break;
871 tp = node_parent(tp);
872 }
873}
874
875static void leaf_push_suffix(struct tnode *l)
876{
877 struct tnode *tn = node_parent(l);
878
879 /* if this is a new leaf then tn will be NULL and we can sort
880 * out parent suffix lengths as a part of trie_rebalance
881 */
882 while (tn && (tn->slen < l->slen)) {
883 tn->slen = l->slen;
884 tn = node_parent(tn);
885 }
886}
887
888static void remove_leaf_info(struct tnode *l, struct leaf_info *old)
889{
890 struct hlist_node *prev;
891
892 /* record the location of the pointer to this object */
893 prev = rtnl_dereference(hlist_pprev_rcu(&old->hlist));
894
895 /* remove the leaf info from the list */
896 hlist_del_rcu(&old->hlist);
897
898 /* if we emptied the list this leaf will be freed and we can sort
899 * out parent suffix lengths as a part of trie_rebalance
900 */
901 if (hlist_empty(&l->list))
902 return;
903
904 /* if we removed the tail then we need to update slen */
905 if (!rcu_access_pointer(hlist_next_rcu(prev))) {
906 struct leaf_info *li = hlist_entry(prev, typeof(*li), hlist);
907
908 l->slen = KEYLENGTH - li->plen;
909 leaf_pull_suffix(l);
910 }
911}
912
913static void insert_leaf_info(struct tnode *l, struct leaf_info *new)
914{
915 struct hlist_head *head = &l->list;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900916 struct leaf_info *li = NULL, *last = NULL;
Robert Olsson19baf832005-06-21 12:43:18 -0700917
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900918 if (hlist_empty(head)) {
919 hlist_add_head_rcu(&new->hlist, head);
920 } else {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800921 hlist_for_each_entry(li, head, hlist) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900922 if (new->plen > li->plen)
923 break;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700924
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900925 last = li;
926 }
927 if (last)
Ken Helias1d023282014-08-06 16:09:16 -0700928 hlist_add_behind_rcu(&new->hlist, &last->hlist);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900929 else
930 hlist_add_before_rcu(&new->hlist, &li->hlist);
931 }
Alexander Duyck5405afd2014-12-31 10:57:08 -0800932
933 /* if we added to the tail node then we need to update slen */
934 if (!rcu_access_pointer(hlist_next_rcu(&new->hlist))) {
935 l->slen = KEYLENGTH - new->plen;
936 leaf_push_suffix(l);
937 }
Robert Olsson19baf832005-06-21 12:43:18 -0700938}
939
Robert Olsson2373ce12005-08-25 13:01:29 -0700940/* rcu_read_lock needs to be hold by caller from readside */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800941static struct tnode *fib_find_node(struct trie *t, u32 key)
Robert Olsson19baf832005-06-21 12:43:18 -0700942{
Alexander Duyckadaf9812014-12-31 10:55:47 -0800943 struct tnode *n = rcu_dereference_rtnl(t->trie);
Robert Olsson19baf832005-06-21 12:43:18 -0700944
Alexander Duyck939afb02014-12-31 10:56:00 -0800945 while (n) {
946 unsigned long index = get_index(key, n);
947
948 /* This bit of code is a bit tricky but it combines multiple
949 * checks into a single check. The prefix consists of the
950 * prefix plus zeros for the bits in the cindex. The index
951 * is the difference between the key and this value. From
952 * this we can actually derive several pieces of data.
Alexander Duyckb3832112015-01-22 15:51:08 -0800953 * if (index & (~0ul << bits))
Alexander Duyck939afb02014-12-31 10:56:00 -0800954 * we have a mismatch in skip bits and failed
Alexander Duyckb3832112015-01-22 15:51:08 -0800955 * else
956 * we know the value is cindex
Alexander Duyck939afb02014-12-31 10:56:00 -0800957 */
Alexander Duyckb3832112015-01-22 15:51:08 -0800958 if (index & (~0ul << n->bits))
Alexander Duyck939afb02014-12-31 10:56:00 -0800959 return NULL;
960
961 /* we have found a leaf. Prefixes have already been compared */
962 if (IS_LEAF(n))
Robert Olsson19baf832005-06-21 12:43:18 -0700963 break;
Alexander Duyck939afb02014-12-31 10:56:00 -0800964
Alexander Duyck21d1f112014-12-31 10:57:02 -0800965 n = tnode_get_child_rcu(n, index);
Robert Olsson19baf832005-06-21 12:43:18 -0700966 }
Robert Olsson19baf832005-06-21 12:43:18 -0700967
Alexander Duyck939afb02014-12-31 10:56:00 -0800968 return n;
Robert Olsson19baf832005-06-21 12:43:18 -0700969}
970
Jarek Poplawski7b855762009-06-18 00:28:51 -0700971static void trie_rebalance(struct trie *t, struct tnode *tn)
Robert Olsson19baf832005-06-21 12:43:18 -0700972{
Stephen Hemminger06801912007-08-10 15:22:13 -0700973 struct tnode *tp;
Robert Olsson19baf832005-06-21 12:43:18 -0700974
Alexander Duyckff181ed2014-12-31 10:56:43 -0800975 while ((tp = node_parent(tn)) != NULL) {
976 resize(t, tn);
Stephen Hemminger06801912007-08-10 15:22:13 -0700977 tn = tp;
Robert Olsson19baf832005-06-21 12:43:18 -0700978 }
Stephen Hemminger06801912007-08-10 15:22:13 -0700979
Robert Olsson19baf832005-06-21 12:43:18 -0700980 /* Handle last (top) tnode */
Jarek Poplawski7b855762009-06-18 00:28:51 -0700981 if (IS_TNODE(tn))
Alexander Duyckff181ed2014-12-31 10:56:43 -0800982 resize(t, tn);
Robert Olsson19baf832005-06-21 12:43:18 -0700983}
984
Robert Olsson2373ce12005-08-25 13:01:29 -0700985/* only used from updater-side */
986
Stephen Hemmingerfea86ad2008-01-12 20:57:07 -0800987static struct list_head *fib_insert_node(struct trie *t, u32 key, int plen)
Robert Olsson19baf832005-06-21 12:43:18 -0700988{
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700989 struct list_head *fa_head = NULL;
Alexander Duyck836a0122014-12-31 10:56:06 -0800990 struct tnode *l, *n, *tp = NULL;
Robert Olsson19baf832005-06-21 12:43:18 -0700991 struct leaf_info *li;
Robert Olsson19baf832005-06-21 12:43:18 -0700992
Alexander Duyck836a0122014-12-31 10:56:06 -0800993 li = leaf_info_new(plen);
994 if (!li)
995 return NULL;
996 fa_head = &li->falh;
997
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700998 n = rtnl_dereference(t->trie);
Robert Olsson19baf832005-06-21 12:43:18 -0700999
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001000 /* If we point to NULL, stop. Either the tree is empty and we should
1001 * just put a new leaf in if, or we have reached an empty child slot,
Robert Olsson19baf832005-06-21 12:43:18 -07001002 * and we should just put our new leaf in that.
Robert Olsson19baf832005-06-21 12:43:18 -07001003 *
Alexander Duyck836a0122014-12-31 10:56:06 -08001004 * If we hit a node with a key that does't match then we should stop
1005 * and create a new tnode to replace that node and insert ourselves
1006 * and the other node into the new tnode.
Robert Olsson19baf832005-06-21 12:43:18 -07001007 */
Alexander Duyck836a0122014-12-31 10:56:06 -08001008 while (n) {
1009 unsigned long index = get_index(key, n);
Robert Olsson19baf832005-06-21 12:43:18 -07001010
Alexander Duyck836a0122014-12-31 10:56:06 -08001011 /* This bit of code is a bit tricky but it combines multiple
1012 * checks into a single check. The prefix consists of the
1013 * prefix plus zeros for the "bits" in the prefix. The index
1014 * is the difference between the key and this value. From
1015 * this we can actually derive several pieces of data.
1016 * if !(index >> bits)
1017 * we know the value is child index
1018 * else
1019 * we have a mismatch in skip bits and failed
Robert Olsson19baf832005-06-21 12:43:18 -07001020 */
Alexander Duyck836a0122014-12-31 10:56:06 -08001021 if (index >> n->bits)
1022 break;
Robert Olsson19baf832005-06-21 12:43:18 -07001023
Alexander Duyck836a0122014-12-31 10:56:06 -08001024 /* we have found a leaf. Prefixes have already been compared */
1025 if (IS_LEAF(n)) {
1026 /* Case 1: n is a leaf, and prefixes match*/
Alexander Duyck5405afd2014-12-31 10:57:08 -08001027 insert_leaf_info(n, li);
Alexander Duyck836a0122014-12-31 10:56:06 -08001028 return fa_head;
Robert Olsson19baf832005-06-21 12:43:18 -07001029 }
Robert Olsson19baf832005-06-21 12:43:18 -07001030
Alexander Duyck836a0122014-12-31 10:56:06 -08001031 tp = n;
Alexander Duyck21d1f112014-12-31 10:57:02 -08001032 n = tnode_get_child_rcu(n, index);
Alexander Duyck836a0122014-12-31 10:56:06 -08001033 }
1034
1035 l = leaf_new(key);
1036 if (!l) {
1037 free_leaf_info(li);
1038 return NULL;
1039 }
1040
Alexander Duyck5405afd2014-12-31 10:57:08 -08001041 insert_leaf_info(l, li);
Alexander Duyck836a0122014-12-31 10:56:06 -08001042
1043 /* Case 2: n is a LEAF or a TNODE and the key doesn't match.
1044 *
1045 * Add a new tnode here
1046 * first tnode need some special handling
1047 * leaves us in position for handling as case 3
1048 */
1049 if (n) {
1050 struct tnode *tn;
Alexander Duyck836a0122014-12-31 10:56:06 -08001051
Alexander Duycke9b44012014-12-31 10:56:12 -08001052 tn = tnode_new(key, __fls(key ^ n->key), 1);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001053 if (!tn) {
Robert Olssonf835e472005-06-28 15:00:39 -07001054 free_leaf_info(li);
Alexander Duyck37fd30f2014-12-31 10:55:41 -08001055 node_free(l);
Stephen Hemmingerfea86ad2008-01-12 20:57:07 -08001056 return NULL;
Olof Johansson91b9a272005-08-09 20:24:39 -07001057 }
1058
Alexander Duyck836a0122014-12-31 10:56:06 -08001059 /* initialize routes out of node */
1060 NODE_INIT_PARENT(tn, tp);
1061 put_child(tn, get_index(key, tn) ^ 1, n);
Robert Olsson19baf832005-06-21 12:43:18 -07001062
Alexander Duyck836a0122014-12-31 10:56:06 -08001063 /* start adding routes into the node */
1064 put_child_root(tp, t, key, tn);
1065 node_set_parent(n, tn);
Robert Olsson19baf832005-06-21 12:43:18 -07001066
Alexander Duyck836a0122014-12-31 10:56:06 -08001067 /* parent now has a NULL spot where the leaf can go */
Alexander Duycke962f302014-12-10 21:49:22 -08001068 tp = tn;
Robert Olsson19baf832005-06-21 12:43:18 -07001069 }
Olof Johansson91b9a272005-08-09 20:24:39 -07001070
Alexander Duyck836a0122014-12-31 10:56:06 -08001071 /* Case 3: n is NULL, and will just insert a new leaf */
1072 if (tp) {
1073 NODE_INIT_PARENT(l, tp);
1074 put_child(tp, get_index(key, tp), l);
1075 trie_rebalance(t, tp);
1076 } else {
1077 rcu_assign_pointer(t->trie, l);
1078 }
Olof Johansson91b9a272005-08-09 20:24:39 -07001079
Robert Olsson19baf832005-06-21 12:43:18 -07001080 return fa_head;
1081}
1082
Robert Olssond562f1f2007-03-26 14:22:22 -07001083/*
1084 * Caller must hold RTNL.
1085 */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001086int fib_table_insert(struct fib_table *tb, struct fib_config *cfg)
Robert Olsson19baf832005-06-21 12:43:18 -07001087{
1088 struct trie *t = (struct trie *) tb->tb_data;
1089 struct fib_alias *fa, *new_fa;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001090 struct list_head *fa_head = NULL;
Robert Olsson19baf832005-06-21 12:43:18 -07001091 struct fib_info *fi;
Thomas Graf4e902c52006-08-17 18:14:52 -07001092 int plen = cfg->fc_dst_len;
1093 u8 tos = cfg->fc_tos;
Robert Olsson19baf832005-06-21 12:43:18 -07001094 u32 key, mask;
1095 int err;
Alexander Duyckadaf9812014-12-31 10:55:47 -08001096 struct tnode *l;
Robert Olsson19baf832005-06-21 12:43:18 -07001097
1098 if (plen > 32)
1099 return -EINVAL;
1100
Thomas Graf4e902c52006-08-17 18:14:52 -07001101 key = ntohl(cfg->fc_dst);
Robert Olsson19baf832005-06-21 12:43:18 -07001102
Patrick McHardy2dfe55b2006-08-10 23:08:33 -07001103 pr_debug("Insert table=%u %08x/%d\n", tb->tb_id, key, plen);
Robert Olsson19baf832005-06-21 12:43:18 -07001104
Olof Johansson91b9a272005-08-09 20:24:39 -07001105 mask = ntohl(inet_make_mask(plen));
Robert Olsson19baf832005-06-21 12:43:18 -07001106
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001107 if (key & ~mask)
Robert Olsson19baf832005-06-21 12:43:18 -07001108 return -EINVAL;
1109
1110 key = key & mask;
1111
Thomas Graf4e902c52006-08-17 18:14:52 -07001112 fi = fib_create_info(cfg);
1113 if (IS_ERR(fi)) {
1114 err = PTR_ERR(fi);
Robert Olsson19baf832005-06-21 12:43:18 -07001115 goto err;
Thomas Graf4e902c52006-08-17 18:14:52 -07001116 }
Robert Olsson19baf832005-06-21 12:43:18 -07001117
1118 l = fib_find_node(t, key);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001119 fa = NULL;
Robert Olsson19baf832005-06-21 12:43:18 -07001120
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001121 if (l) {
Robert Olsson19baf832005-06-21 12:43:18 -07001122 fa_head = get_fa_head(l, plen);
1123 fa = fib_find_alias(fa_head, tos, fi->fib_priority);
1124 }
1125
1126 /* Now fa, if non-NULL, points to the first fib alias
1127 * with the same keys [prefix,tos,priority], if such key already
1128 * exists or to the node before which we will insert new one.
1129 *
1130 * If fa is NULL, we will need to allocate a new one and
1131 * insert to the head of f.
1132 *
1133 * If f is NULL, no fib node matched the destination key
1134 * and we need to allocate a new one of those as well.
1135 */
1136
Julian Anastasov936f6f82008-01-28 21:18:06 -08001137 if (fa && fa->fa_tos == tos &&
1138 fa->fa_info->fib_priority == fi->fib_priority) {
1139 struct fib_alias *fa_first, *fa_match;
Robert Olsson19baf832005-06-21 12:43:18 -07001140
1141 err = -EEXIST;
Thomas Graf4e902c52006-08-17 18:14:52 -07001142 if (cfg->fc_nlflags & NLM_F_EXCL)
Robert Olsson19baf832005-06-21 12:43:18 -07001143 goto out;
1144
Julian Anastasov936f6f82008-01-28 21:18:06 -08001145 /* We have 2 goals:
1146 * 1. Find exact match for type, scope, fib_info to avoid
1147 * duplicate routes
1148 * 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it
1149 */
1150 fa_match = NULL;
1151 fa_first = fa;
1152 fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
1153 list_for_each_entry_continue(fa, fa_head, fa_list) {
1154 if (fa->fa_tos != tos)
1155 break;
1156 if (fa->fa_info->fib_priority != fi->fib_priority)
1157 break;
1158 if (fa->fa_type == cfg->fc_type &&
Julian Anastasov936f6f82008-01-28 21:18:06 -08001159 fa->fa_info == fi) {
1160 fa_match = fa;
1161 break;
1162 }
1163 }
1164
Thomas Graf4e902c52006-08-17 18:14:52 -07001165 if (cfg->fc_nlflags & NLM_F_REPLACE) {
Robert Olsson19baf832005-06-21 12:43:18 -07001166 struct fib_info *fi_drop;
1167 u8 state;
1168
Julian Anastasov936f6f82008-01-28 21:18:06 -08001169 fa = fa_first;
1170 if (fa_match) {
1171 if (fa == fa_match)
1172 err = 0;
Joonwoo Park67250332008-01-18 03:45:18 -08001173 goto out;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001174 }
Robert Olsson2373ce12005-08-25 13:01:29 -07001175 err = -ENOBUFS;
Christoph Lametere94b1762006-12-06 20:33:17 -08001176 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
Robert Olsson2373ce12005-08-25 13:01:29 -07001177 if (new_fa == NULL)
1178 goto out;
Robert Olsson19baf832005-06-21 12:43:18 -07001179
1180 fi_drop = fa->fa_info;
Robert Olsson2373ce12005-08-25 13:01:29 -07001181 new_fa->fa_tos = fa->fa_tos;
1182 new_fa->fa_info = fi;
Thomas Graf4e902c52006-08-17 18:14:52 -07001183 new_fa->fa_type = cfg->fc_type;
Robert Olsson19baf832005-06-21 12:43:18 -07001184 state = fa->fa_state;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001185 new_fa->fa_state = state & ~FA_S_ACCESSED;
Robert Olsson19baf832005-06-21 12:43:18 -07001186
Robert Olsson2373ce12005-08-25 13:01:29 -07001187 list_replace_rcu(&fa->fa_list, &new_fa->fa_list);
1188 alias_free_mem_rcu(fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001189
1190 fib_release_info(fi_drop);
1191 if (state & FA_S_ACCESSED)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001192 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Milan Kocianb8f55832007-05-23 14:55:06 -07001193 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen,
1194 tb->tb_id, &cfg->fc_nlinfo, NLM_F_REPLACE);
Robert Olsson19baf832005-06-21 12:43:18 -07001195
Olof Johansson91b9a272005-08-09 20:24:39 -07001196 goto succeeded;
Robert Olsson19baf832005-06-21 12:43:18 -07001197 }
1198 /* Error if we find a perfect match which
1199 * uses the same scope, type, and nexthop
1200 * information.
1201 */
Julian Anastasov936f6f82008-01-28 21:18:06 -08001202 if (fa_match)
1203 goto out;
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001204
Thomas Graf4e902c52006-08-17 18:14:52 -07001205 if (!(cfg->fc_nlflags & NLM_F_APPEND))
Julian Anastasov936f6f82008-01-28 21:18:06 -08001206 fa = fa_first;
Robert Olsson19baf832005-06-21 12:43:18 -07001207 }
1208 err = -ENOENT;
Thomas Graf4e902c52006-08-17 18:14:52 -07001209 if (!(cfg->fc_nlflags & NLM_F_CREATE))
Robert Olsson19baf832005-06-21 12:43:18 -07001210 goto out;
1211
1212 err = -ENOBUFS;
Christoph Lametere94b1762006-12-06 20:33:17 -08001213 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
Robert Olsson19baf832005-06-21 12:43:18 -07001214 if (new_fa == NULL)
1215 goto out;
1216
1217 new_fa->fa_info = fi;
1218 new_fa->fa_tos = tos;
Thomas Graf4e902c52006-08-17 18:14:52 -07001219 new_fa->fa_type = cfg->fc_type;
Robert Olsson19baf832005-06-21 12:43:18 -07001220 new_fa->fa_state = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001221 /*
1222 * Insert new entry to the list.
1223 */
1224
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001225 if (!fa_head) {
Stephen Hemmingerfea86ad2008-01-12 20:57:07 -08001226 fa_head = fib_insert_node(t, key, plen);
1227 if (unlikely(!fa_head)) {
1228 err = -ENOMEM;
Robert Olssonf835e472005-06-28 15:00:39 -07001229 goto out_free_new_fa;
Stephen Hemmingerfea86ad2008-01-12 20:57:07 -08001230 }
Robert Olssonf835e472005-06-28 15:00:39 -07001231 }
Robert Olsson19baf832005-06-21 12:43:18 -07001232
David S. Miller21d8c492011-04-14 14:49:37 -07001233 if (!plen)
1234 tb->tb_num_default++;
1235
Robert Olsson2373ce12005-08-25 13:01:29 -07001236 list_add_tail_rcu(&new_fa->fa_list,
1237 (fa ? &fa->fa_list : fa_head));
Robert Olsson19baf832005-06-21 12:43:18 -07001238
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001239 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Thomas Graf4e902c52006-08-17 18:14:52 -07001240 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen, tb->tb_id,
Milan Kocianb8f55832007-05-23 14:55:06 -07001241 &cfg->fc_nlinfo, 0);
Robert Olsson19baf832005-06-21 12:43:18 -07001242succeeded:
1243 return 0;
Robert Olssonf835e472005-06-28 15:00:39 -07001244
1245out_free_new_fa:
1246 kmem_cache_free(fn_alias_kmem, new_fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001247out:
1248 fib_release_info(fi);
Olof Johansson91b9a272005-08-09 20:24:39 -07001249err:
Robert Olsson19baf832005-06-21 12:43:18 -07001250 return err;
1251}
1252
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001253static inline t_key prefix_mismatch(t_key key, struct tnode *n)
1254{
1255 t_key prefix = n->key;
1256
1257 return (key ^ prefix) & (prefix | -prefix);
1258}
1259
Alexander Duyck345e9b52014-12-31 10:56:24 -08001260/* should be called with rcu_read_lock */
David S. Miller22bd5b92011-03-11 19:54:08 -05001261int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00001262 struct fib_result *res, int fib_flags)
Robert Olsson19baf832005-06-21 12:43:18 -07001263{
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001264 struct trie *t = (struct trie *)tb->tb_data;
Alexander Duyck8274a972014-12-31 10:55:29 -08001265#ifdef CONFIG_IP_FIB_TRIE_STATS
1266 struct trie_use_stats __percpu *stats = t->stats;
1267#endif
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001268 const t_key key = ntohl(flp->daddr);
1269 struct tnode *n, *pn;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001270 struct leaf_info *li;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001271 t_key cindex;
Robert Olsson19baf832005-06-21 12:43:18 -07001272
Robert Olsson2373ce12005-08-25 13:01:29 -07001273 n = rcu_dereference(t->trie);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001274 if (!n)
Alexander Duyck345e9b52014-12-31 10:56:24 -08001275 return -EAGAIN;
Robert Olsson19baf832005-06-21 12:43:18 -07001276
1277#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -08001278 this_cpu_inc(stats->gets);
Robert Olsson19baf832005-06-21 12:43:18 -07001279#endif
1280
Alexander Duyckadaf9812014-12-31 10:55:47 -08001281 pn = n;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001282 cindex = 0;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001283
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001284 /* Step 1: Travel to the longest prefix match in the trie */
1285 for (;;) {
1286 unsigned long index = get_index(key, n);
Robert Olsson19baf832005-06-21 12:43:18 -07001287
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001288 /* This bit of code is a bit tricky but it combines multiple
1289 * checks into a single check. The prefix consists of the
1290 * prefix plus zeros for the "bits" in the prefix. The index
1291 * is the difference between the key and this value. From
1292 * this we can actually derive several pieces of data.
Alexander Duyckb3832112015-01-22 15:51:08 -08001293 * if (index & (~0ul << bits))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001294 * we have a mismatch in skip bits and failed
Alexander Duyckb3832112015-01-22 15:51:08 -08001295 * else
1296 * we know the value is cindex
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001297 */
Alexander Duyckb3832112015-01-22 15:51:08 -08001298 if (index & (~0ul << n->bits))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001299 break;
Robert Olsson19baf832005-06-21 12:43:18 -07001300
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001301 /* we have found a leaf. Prefixes have already been compared */
1302 if (IS_LEAF(n))
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001303 goto found;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001304
1305 /* only record pn and cindex if we are going to be chopping
1306 * bits later. Otherwise we are just wasting cycles.
1307 */
Alexander Duyck5405afd2014-12-31 10:57:08 -08001308 if (n->slen > n->pos) {
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001309 pn = n;
1310 cindex = index;
Olof Johansson91b9a272005-08-09 20:24:39 -07001311 }
1312
Alexander Duyck21d1f112014-12-31 10:57:02 -08001313 n = tnode_get_child_rcu(n, index);
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001314 if (unlikely(!n))
Robert Olsson19baf832005-06-21 12:43:18 -07001315 goto backtrace;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001316 }
1317
1318 /* Step 2: Sort out leaves and begin backtracing for longest prefix */
1319 for (;;) {
1320 /* record the pointer where our next node pointer is stored */
1321 struct tnode __rcu **cptr = n->child;
1322
1323 /* This test verifies that none of the bits that differ
1324 * between the key and the prefix exist in the region of
1325 * the lsb and higher in the prefix.
1326 */
Alexander Duyck5405afd2014-12-31 10:57:08 -08001327 if (unlikely(prefix_mismatch(key, n)) || (n->slen == n->pos))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001328 goto backtrace;
1329
1330 /* exit out and process leaf */
1331 if (unlikely(IS_LEAF(n)))
1332 break;
1333
1334 /* Don't bother recording parent info. Since we are in
1335 * prefix match mode we will have to come back to wherever
1336 * we started this traversal anyway
1337 */
1338
1339 while ((n = rcu_dereference(*cptr)) == NULL) {
1340backtrace:
1341#ifdef CONFIG_IP_FIB_TRIE_STATS
1342 if (!n)
1343 this_cpu_inc(stats->null_node_hit);
1344#endif
1345 /* If we are at cindex 0 there are no more bits for
1346 * us to strip at this level so we must ascend back
1347 * up one level to see if there are any more bits to
1348 * be stripped there.
1349 */
1350 while (!cindex) {
1351 t_key pkey = pn->key;
1352
1353 pn = node_parent_rcu(pn);
1354 if (unlikely(!pn))
Alexander Duyck345e9b52014-12-31 10:56:24 -08001355 return -EAGAIN;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001356#ifdef CONFIG_IP_FIB_TRIE_STATS
1357 this_cpu_inc(stats->backtrack);
1358#endif
1359 /* Get Child's index */
1360 cindex = get_index(pkey, pn);
1361 }
1362
1363 /* strip the least significant bit from the cindex */
1364 cindex &= cindex - 1;
1365
1366 /* grab pointer for next child node */
1367 cptr = &pn->child[cindex];
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001368 }
Robert Olsson19baf832005-06-21 12:43:18 -07001369 }
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001370
Robert Olsson19baf832005-06-21 12:43:18 -07001371found:
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001372 /* Step 3: Process the leaf, if that fails fall back to backtracing */
Alexander Duyck345e9b52014-12-31 10:56:24 -08001373 hlist_for_each_entry_rcu(li, &n->list, hlist) {
1374 struct fib_alias *fa;
1375
1376 if ((key ^ n->key) & li->mask_plen)
1377 continue;
1378
1379 list_for_each_entry_rcu(fa, &li->falh, fa_list) {
1380 struct fib_info *fi = fa->fa_info;
1381 int nhsel, err;
1382
1383 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
1384 continue;
1385 if (fi->fib_dead)
1386 continue;
1387 if (fa->fa_info->fib_scope < flp->flowi4_scope)
1388 continue;
1389 fib_alias_accessed(fa);
1390 err = fib_props[fa->fa_type].error;
1391 if (unlikely(err < 0)) {
1392#ifdef CONFIG_IP_FIB_TRIE_STATS
1393 this_cpu_inc(stats->semantic_match_passed);
1394#endif
1395 return err;
1396 }
1397 if (fi->fib_flags & RTNH_F_DEAD)
1398 continue;
1399 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
1400 const struct fib_nh *nh = &fi->fib_nh[nhsel];
1401
1402 if (nh->nh_flags & RTNH_F_DEAD)
1403 continue;
1404 if (flp->flowi4_oif && flp->flowi4_oif != nh->nh_oif)
1405 continue;
1406
1407 if (!(fib_flags & FIB_LOOKUP_NOREF))
1408 atomic_inc(&fi->fib_clntref);
1409
1410 res->prefixlen = li->plen;
1411 res->nh_sel = nhsel;
1412 res->type = fa->fa_type;
1413 res->scope = fi->fib_scope;
1414 res->fi = fi;
1415 res->table = tb;
1416 res->fa_head = &li->falh;
1417#ifdef CONFIG_IP_FIB_TRIE_STATS
1418 this_cpu_inc(stats->semantic_match_passed);
1419#endif
1420 return err;
1421 }
1422 }
1423
1424#ifdef CONFIG_IP_FIB_TRIE_STATS
1425 this_cpu_inc(stats->semantic_match_miss);
1426#endif
1427 }
1428 goto backtrace;
Robert Olsson19baf832005-06-21 12:43:18 -07001429}
Florian Westphal6fc01432011-08-25 13:46:12 +02001430EXPORT_SYMBOL_GPL(fib_table_lookup);
Robert Olsson19baf832005-06-21 12:43:18 -07001431
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001432/*
1433 * Remove the leaf and return parent.
1434 */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001435static void trie_leaf_remove(struct trie *t, struct tnode *l)
Robert Olsson19baf832005-06-21 12:43:18 -07001436{
Alexander Duyck64c9b6f2014-12-31 10:55:35 -08001437 struct tnode *tp = node_parent(l);
Robert Olsson19baf832005-06-21 12:43:18 -07001438
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001439 pr_debug("entering trie_leaf_remove(%p)\n", l);
Robert Olsson19baf832005-06-21 12:43:18 -07001440
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001441 if (tp) {
Alexander Duyck836a0122014-12-31 10:56:06 -08001442 put_child(tp, get_index(l->key, tp), NULL);
Jarek Poplawski7b855762009-06-18 00:28:51 -07001443 trie_rebalance(t, tp);
Alexander Duyck836a0122014-12-31 10:56:06 -08001444 } else {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001445 RCU_INIT_POINTER(t->trie, NULL);
Alexander Duyck836a0122014-12-31 10:56:06 -08001446 }
Robert Olsson19baf832005-06-21 12:43:18 -07001447
Alexander Duyck37fd30f2014-12-31 10:55:41 -08001448 node_free(l);
Robert Olsson19baf832005-06-21 12:43:18 -07001449}
1450
Robert Olssond562f1f2007-03-26 14:22:22 -07001451/*
1452 * Caller must hold RTNL.
1453 */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001454int fib_table_delete(struct fib_table *tb, struct fib_config *cfg)
Robert Olsson19baf832005-06-21 12:43:18 -07001455{
1456 struct trie *t = (struct trie *) tb->tb_data;
1457 u32 key, mask;
Thomas Graf4e902c52006-08-17 18:14:52 -07001458 int plen = cfg->fc_dst_len;
1459 u8 tos = cfg->fc_tos;
Robert Olsson19baf832005-06-21 12:43:18 -07001460 struct fib_alias *fa, *fa_to_delete;
1461 struct list_head *fa_head;
Alexander Duyckadaf9812014-12-31 10:55:47 -08001462 struct tnode *l;
Olof Johansson91b9a272005-08-09 20:24:39 -07001463 struct leaf_info *li;
1464
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001465 if (plen > 32)
Robert Olsson19baf832005-06-21 12:43:18 -07001466 return -EINVAL;
1467
Thomas Graf4e902c52006-08-17 18:14:52 -07001468 key = ntohl(cfg->fc_dst);
Olof Johansson91b9a272005-08-09 20:24:39 -07001469 mask = ntohl(inet_make_mask(plen));
Robert Olsson19baf832005-06-21 12:43:18 -07001470
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001471 if (key & ~mask)
Robert Olsson19baf832005-06-21 12:43:18 -07001472 return -EINVAL;
1473
1474 key = key & mask;
1475 l = fib_find_node(t, key);
1476
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001477 if (!l)
Robert Olsson19baf832005-06-21 12:43:18 -07001478 return -ESRCH;
1479
Igor Maravicad5b3102012-08-13 10:26:08 +02001480 li = find_leaf_info(l, plen);
1481
1482 if (!li)
1483 return -ESRCH;
1484
1485 fa_head = &li->falh;
Robert Olsson19baf832005-06-21 12:43:18 -07001486 fa = fib_find_alias(fa_head, tos, 0);
1487
1488 if (!fa)
1489 return -ESRCH;
1490
Stephen Hemminger0c7770c2005-08-23 21:59:41 -07001491 pr_debug("Deleting %08x/%d tos=%d t=%p\n", key, plen, tos, t);
Robert Olsson19baf832005-06-21 12:43:18 -07001492
1493 fa_to_delete = NULL;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001494 fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
1495 list_for_each_entry_continue(fa, fa_head, fa_list) {
Robert Olsson19baf832005-06-21 12:43:18 -07001496 struct fib_info *fi = fa->fa_info;
1497
1498 if (fa->fa_tos != tos)
1499 break;
1500
Thomas Graf4e902c52006-08-17 18:14:52 -07001501 if ((!cfg->fc_type || fa->fa_type == cfg->fc_type) &&
1502 (cfg->fc_scope == RT_SCOPE_NOWHERE ||
David S. Miller37e826c2011-03-24 18:06:47 -07001503 fa->fa_info->fib_scope == cfg->fc_scope) &&
Julian Anastasov74cb3c12011-03-19 12:13:46 +00001504 (!cfg->fc_prefsrc ||
1505 fi->fib_prefsrc == cfg->fc_prefsrc) &&
Thomas Graf4e902c52006-08-17 18:14:52 -07001506 (!cfg->fc_protocol ||
1507 fi->fib_protocol == cfg->fc_protocol) &&
1508 fib_nh_match(cfg, fi) == 0) {
Robert Olsson19baf832005-06-21 12:43:18 -07001509 fa_to_delete = fa;
1510 break;
1511 }
1512 }
1513
Olof Johansson91b9a272005-08-09 20:24:39 -07001514 if (!fa_to_delete)
1515 return -ESRCH;
Robert Olsson19baf832005-06-21 12:43:18 -07001516
Olof Johansson91b9a272005-08-09 20:24:39 -07001517 fa = fa_to_delete;
Thomas Graf4e902c52006-08-17 18:14:52 -07001518 rtmsg_fib(RTM_DELROUTE, htonl(key), fa, plen, tb->tb_id,
Milan Kocianb8f55832007-05-23 14:55:06 -07001519 &cfg->fc_nlinfo, 0);
Robert Olsson19baf832005-06-21 12:43:18 -07001520
Robert Olsson2373ce12005-08-25 13:01:29 -07001521 list_del_rcu(&fa->fa_list);
Robert Olsson19baf832005-06-21 12:43:18 -07001522
David S. Miller21d8c492011-04-14 14:49:37 -07001523 if (!plen)
1524 tb->tb_num_default--;
1525
Olof Johansson91b9a272005-08-09 20:24:39 -07001526 if (list_empty(fa_head)) {
Alexander Duyck5405afd2014-12-31 10:57:08 -08001527 remove_leaf_info(l, li);
Olof Johansson91b9a272005-08-09 20:24:39 -07001528 free_leaf_info(li);
Robert Olsson2373ce12005-08-25 13:01:29 -07001529 }
Olof Johansson91b9a272005-08-09 20:24:39 -07001530
1531 if (hlist_empty(&l->list))
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001532 trie_leaf_remove(t, l);
Olof Johansson91b9a272005-08-09 20:24:39 -07001533
1534 if (fa->fa_state & FA_S_ACCESSED)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001535 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Olof Johansson91b9a272005-08-09 20:24:39 -07001536
Robert Olsson2373ce12005-08-25 13:01:29 -07001537 fib_release_info(fa->fa_info);
1538 alias_free_mem_rcu(fa);
Olof Johansson91b9a272005-08-09 20:24:39 -07001539 return 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001540}
1541
Stephen Hemmingeref3660c2008-04-10 03:46:12 -07001542static int trie_flush_list(struct list_head *head)
Robert Olsson19baf832005-06-21 12:43:18 -07001543{
1544 struct fib_alias *fa, *fa_node;
1545 int found = 0;
1546
1547 list_for_each_entry_safe(fa, fa_node, head, fa_list) {
1548 struct fib_info *fi = fa->fa_info;
Robert Olsson19baf832005-06-21 12:43:18 -07001549
Robert Olsson2373ce12005-08-25 13:01:29 -07001550 if (fi && (fi->fib_flags & RTNH_F_DEAD)) {
1551 list_del_rcu(&fa->fa_list);
1552 fib_release_info(fa->fa_info);
1553 alias_free_mem_rcu(fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001554 found++;
1555 }
1556 }
1557 return found;
1558}
1559
Alexander Duyckadaf9812014-12-31 10:55:47 -08001560static int trie_flush_leaf(struct tnode *l)
Robert Olsson19baf832005-06-21 12:43:18 -07001561{
1562 int found = 0;
1563 struct hlist_head *lih = &l->list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001564 struct hlist_node *tmp;
Robert Olsson19baf832005-06-21 12:43:18 -07001565 struct leaf_info *li = NULL;
1566
Sasha Levinb67bfe02013-02-27 17:06:00 -08001567 hlist_for_each_entry_safe(li, tmp, lih, hlist) {
Stephen Hemmingeref3660c2008-04-10 03:46:12 -07001568 found += trie_flush_list(&li->falh);
Robert Olsson19baf832005-06-21 12:43:18 -07001569
1570 if (list_empty(&li->falh)) {
Robert Olsson2373ce12005-08-25 13:01:29 -07001571 hlist_del_rcu(&li->hlist);
Robert Olsson19baf832005-06-21 12:43:18 -07001572 free_leaf_info(li);
1573 }
1574 }
1575 return found;
1576}
1577
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001578/*
1579 * Scan for the next right leaf starting at node p->child[idx]
1580 * Since we have back pointer, no recursion necessary.
1581 */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001582static struct tnode *leaf_walk_rcu(struct tnode *p, struct tnode *c)
Robert Olsson19baf832005-06-21 12:43:18 -07001583{
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001584 do {
Alexander Duyck98293e82014-12-31 10:56:18 -08001585 unsigned long idx = c ? idx = get_index(c->key, p) + 1 : 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001586
Alexander Duyck98293e82014-12-31 10:56:18 -08001587 while (idx < tnode_child_length(p)) {
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001588 c = tnode_get_child_rcu(p, idx++);
Robert Olsson2373ce12005-08-25 13:01:29 -07001589 if (!c)
Olof Johansson91b9a272005-08-09 20:24:39 -07001590 continue;
Robert Olsson19baf832005-06-21 12:43:18 -07001591
Eric Dumazetaab515d2013-08-05 11:18:49 -07001592 if (IS_LEAF(c))
Alexander Duyckadaf9812014-12-31 10:55:47 -08001593 return c;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001594
1595 /* Rescan start scanning in new node */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001596 p = c;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001597 idx = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001598 }
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001599
1600 /* Node empty, walk back up to parent */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001601 c = p;
Eric Dumazeta034ee32010-09-09 23:32:28 +00001602 } while ((p = node_parent_rcu(c)) != NULL);
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001603
1604 return NULL; /* Root of trie */
1605}
1606
Alexander Duyckadaf9812014-12-31 10:55:47 -08001607static struct tnode *trie_firstleaf(struct trie *t)
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001608{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001609 struct tnode *n = rcu_dereference_rtnl(t->trie);
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001610
1611 if (!n)
1612 return NULL;
1613
1614 if (IS_LEAF(n)) /* trie is just a leaf */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001615 return n;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001616
1617 return leaf_walk_rcu(n, NULL);
1618}
1619
Alexander Duyckadaf9812014-12-31 10:55:47 -08001620static struct tnode *trie_nextleaf(struct tnode *l)
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001621{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001622 struct tnode *p = node_parent_rcu(l);
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001623
1624 if (!p)
1625 return NULL; /* trie with just one leaf */
1626
Alexander Duyckadaf9812014-12-31 10:55:47 -08001627 return leaf_walk_rcu(p, l);
Robert Olsson19baf832005-06-21 12:43:18 -07001628}
1629
Alexander Duyckadaf9812014-12-31 10:55:47 -08001630static struct tnode *trie_leafindex(struct trie *t, int index)
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001631{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001632 struct tnode *l = trie_firstleaf(t);
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001633
Stephen Hemmingerec28cf72008-02-11 21:12:49 -08001634 while (l && index-- > 0)
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001635 l = trie_nextleaf(l);
Stephen Hemmingerec28cf72008-02-11 21:12:49 -08001636
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001637 return l;
1638}
1639
1640
Robert Olssond562f1f2007-03-26 14:22:22 -07001641/*
1642 * Caller must hold RTNL.
1643 */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001644int fib_table_flush(struct fib_table *tb)
Robert Olsson19baf832005-06-21 12:43:18 -07001645{
1646 struct trie *t = (struct trie *) tb->tb_data;
Alexander Duyckadaf9812014-12-31 10:55:47 -08001647 struct tnode *l, *ll = NULL;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001648 int found = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001649
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001650 for (l = trie_firstleaf(t); l; l = trie_nextleaf(l)) {
Stephen Hemmingeref3660c2008-04-10 03:46:12 -07001651 found += trie_flush_leaf(l);
Robert Olsson19baf832005-06-21 12:43:18 -07001652
1653 if (ll && hlist_empty(&ll->list))
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001654 trie_leaf_remove(t, ll);
Robert Olsson19baf832005-06-21 12:43:18 -07001655 ll = l;
1656 }
1657
1658 if (ll && hlist_empty(&ll->list))
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001659 trie_leaf_remove(t, ll);
Robert Olsson19baf832005-06-21 12:43:18 -07001660
Stephen Hemminger0c7770c2005-08-23 21:59:41 -07001661 pr_debug("trie_flush found=%d\n", found);
Robert Olsson19baf832005-06-21 12:43:18 -07001662 return found;
1663}
1664
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00001665void fib_free_table(struct fib_table *tb)
1666{
Alexander Duyck8274a972014-12-31 10:55:29 -08001667#ifdef CONFIG_IP_FIB_TRIE_STATS
1668 struct trie *t = (struct trie *)tb->tb_data;
1669
1670 free_percpu(t->stats);
1671#endif /* CONFIG_IP_FIB_TRIE_STATS */
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00001672 kfree(tb);
1673}
1674
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001675static int fn_trie_dump_fa(t_key key, int plen, struct list_head *fah,
1676 struct fib_table *tb,
Robert Olsson19baf832005-06-21 12:43:18 -07001677 struct sk_buff *skb, struct netlink_callback *cb)
1678{
1679 int i, s_i;
1680 struct fib_alias *fa;
Al Viro32ab5f82006-09-26 22:21:45 -07001681 __be32 xkey = htonl(key);
Robert Olsson19baf832005-06-21 12:43:18 -07001682
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001683 s_i = cb->args[5];
Robert Olsson19baf832005-06-21 12:43:18 -07001684 i = 0;
1685
Robert Olsson2373ce12005-08-25 13:01:29 -07001686 /* rcu_read_lock is hold by caller */
1687
1688 list_for_each_entry_rcu(fa, fah, fa_list) {
Robert Olsson19baf832005-06-21 12:43:18 -07001689 if (i < s_i) {
1690 i++;
1691 continue;
1692 }
Robert Olsson19baf832005-06-21 12:43:18 -07001693
Eric W. Biederman15e47302012-09-07 20:12:54 +00001694 if (fib_dump_info(skb, NETLINK_CB(cb->skb).portid,
Robert Olsson19baf832005-06-21 12:43:18 -07001695 cb->nlh->nlmsg_seq,
1696 RTM_NEWROUTE,
1697 tb->tb_id,
1698 fa->fa_type,
Thomas Grafbe403ea2006-08-17 18:15:17 -07001699 xkey,
Robert Olsson19baf832005-06-21 12:43:18 -07001700 plen,
1701 fa->fa_tos,
Stephen Hemminger64347f72008-01-22 21:55:01 -08001702 fa->fa_info, NLM_F_MULTI) < 0) {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001703 cb->args[5] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001704 return -1;
Olof Johansson91b9a272005-08-09 20:24:39 -07001705 }
Robert Olsson19baf832005-06-21 12:43:18 -07001706 i++;
1707 }
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001708 cb->args[5] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001709 return skb->len;
1710}
1711
Alexander Duyckadaf9812014-12-31 10:55:47 -08001712static int fn_trie_dump_leaf(struct tnode *l, struct fib_table *tb,
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001713 struct sk_buff *skb, struct netlink_callback *cb)
Robert Olsson19baf832005-06-21 12:43:18 -07001714{
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001715 struct leaf_info *li;
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001716 int i, s_i;
Robert Olsson19baf832005-06-21 12:43:18 -07001717
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001718 s_i = cb->args[4];
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001719 i = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001720
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001721 /* rcu_read_lock is hold by caller */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001722 hlist_for_each_entry_rcu(li, &l->list, hlist) {
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001723 if (i < s_i) {
1724 i++;
Robert Olsson19baf832005-06-21 12:43:18 -07001725 continue;
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001726 }
Robert Olsson19baf832005-06-21 12:43:18 -07001727
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001728 if (i > s_i)
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001729 cb->args[5] = 0;
Olof Johansson91b9a272005-08-09 20:24:39 -07001730
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001731 if (list_empty(&li->falh))
Robert Olsson19baf832005-06-21 12:43:18 -07001732 continue;
1733
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001734 if (fn_trie_dump_fa(l->key, li->plen, &li->falh, tb, skb, cb) < 0) {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001735 cb->args[4] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001736 return -1;
1737 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001738 i++;
Robert Olsson19baf832005-06-21 12:43:18 -07001739 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001740
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001741 cb->args[4] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001742 return skb->len;
1743}
1744
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001745int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
1746 struct netlink_callback *cb)
Robert Olsson19baf832005-06-21 12:43:18 -07001747{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001748 struct tnode *l;
Robert Olsson19baf832005-06-21 12:43:18 -07001749 struct trie *t = (struct trie *) tb->tb_data;
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001750 t_key key = cb->args[2];
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001751 int count = cb->args[3];
Robert Olsson19baf832005-06-21 12:43:18 -07001752
Robert Olsson2373ce12005-08-25 13:01:29 -07001753 rcu_read_lock();
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001754 /* Dump starting at last key.
1755 * Note: 0.0.0.0/0 (ie default) is first key.
1756 */
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001757 if (count == 0)
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001758 l = trie_firstleaf(t);
1759 else {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001760 /* Normally, continue from last key, but if that is missing
1761 * fallback to using slow rescan
1762 */
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001763 l = fib_find_node(t, key);
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001764 if (!l)
1765 l = trie_leafindex(t, count);
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001766 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001767
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001768 while (l) {
1769 cb->args[2] = l->key;
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001770 if (fn_trie_dump_leaf(l, tb, skb, cb) < 0) {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001771 cb->args[3] = count;
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001772 rcu_read_unlock();
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001773 return -1;
Robert Olsson19baf832005-06-21 12:43:18 -07001774 }
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001775
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001776 ++count;
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001777 l = trie_nextleaf(l);
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001778 memset(&cb->args[4], 0,
1779 sizeof(cb->args) - 4*sizeof(cb->args[0]));
Robert Olsson19baf832005-06-21 12:43:18 -07001780 }
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001781 cb->args[3] = count;
Robert Olsson2373ce12005-08-25 13:01:29 -07001782 rcu_read_unlock();
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001783
Robert Olsson19baf832005-06-21 12:43:18 -07001784 return skb->len;
Robert Olsson19baf832005-06-21 12:43:18 -07001785}
1786
David S. Miller5348ba82011-02-01 15:30:56 -08001787void __init fib_trie_init(void)
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001788{
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001789 fn_alias_kmem = kmem_cache_create("ip_fib_alias",
1790 sizeof(struct fib_alias),
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -08001791 0, SLAB_PANIC, NULL);
1792
1793 trie_leaf_kmem = kmem_cache_create("ip_fib_trie",
Alexander Duyckadaf9812014-12-31 10:55:47 -08001794 max(sizeof(struct tnode),
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -08001795 sizeof(struct leaf_info)),
1796 0, SLAB_PANIC, NULL);
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001797}
Robert Olsson19baf832005-06-21 12:43:18 -07001798
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001799
David S. Miller5348ba82011-02-01 15:30:56 -08001800struct fib_table *fib_trie_table(u32 id)
Robert Olsson19baf832005-06-21 12:43:18 -07001801{
1802 struct fib_table *tb;
1803 struct trie *t;
1804
Robert Olsson19baf832005-06-21 12:43:18 -07001805 tb = kmalloc(sizeof(struct fib_table) + sizeof(struct trie),
1806 GFP_KERNEL);
1807 if (tb == NULL)
1808 return NULL;
1809
1810 tb->tb_id = id;
Denis V. Lunev971b8932007-12-08 00:32:23 -08001811 tb->tb_default = -1;
David S. Miller21d8c492011-04-14 14:49:37 -07001812 tb->tb_num_default = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001813
1814 t = (struct trie *) tb->tb_data;
Alexander Duyck8274a972014-12-31 10:55:29 -08001815 RCU_INIT_POINTER(t->trie, NULL);
1816#ifdef CONFIG_IP_FIB_TRIE_STATS
1817 t->stats = alloc_percpu(struct trie_use_stats);
1818 if (!t->stats) {
1819 kfree(tb);
1820 tb = NULL;
1821 }
1822#endif
Robert Olsson19baf832005-06-21 12:43:18 -07001823
Robert Olsson19baf832005-06-21 12:43:18 -07001824 return tb;
1825}
1826
Robert Olsson19baf832005-06-21 12:43:18 -07001827#ifdef CONFIG_PROC_FS
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001828/* Depth first Trie walk iterator */
1829struct fib_trie_iter {
Denis V. Lunev1c340b22008-01-10 03:27:17 -08001830 struct seq_net_private p;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001831 struct fib_table *tb;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001832 struct tnode *tnode;
Eric Dumazeta034ee32010-09-09 23:32:28 +00001833 unsigned int index;
1834 unsigned int depth;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001835};
Robert Olsson19baf832005-06-21 12:43:18 -07001836
Alexander Duyckadaf9812014-12-31 10:55:47 -08001837static struct tnode *fib_trie_get_next(struct fib_trie_iter *iter)
Robert Olsson19baf832005-06-21 12:43:18 -07001838{
Alexander Duyck98293e82014-12-31 10:56:18 -08001839 unsigned long cindex = iter->index;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001840 struct tnode *tn = iter->tnode;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001841 struct tnode *p;
1842
Eric W. Biederman6640e692007-01-24 14:42:04 -08001843 /* A single entry routing table */
1844 if (!tn)
1845 return NULL;
1846
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001847 pr_debug("get_next iter={node=%p index=%d depth=%d}\n",
1848 iter->tnode, iter->index, iter->depth);
1849rescan:
Alexander Duyck98293e82014-12-31 10:56:18 -08001850 while (cindex < tnode_child_length(tn)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08001851 struct tnode *n = tnode_get_child_rcu(tn, cindex);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001852
1853 if (n) {
1854 if (IS_LEAF(n)) {
1855 iter->tnode = tn;
1856 iter->index = cindex + 1;
1857 } else {
1858 /* push down one level */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001859 iter->tnode = n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001860 iter->index = 0;
1861 ++iter->depth;
1862 }
1863 return n;
1864 }
1865
1866 ++cindex;
1867 }
1868
1869 /* Current node exhausted, pop back up */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001870 p = node_parent_rcu(tn);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001871 if (p) {
Alexander Duycke9b44012014-12-31 10:56:12 -08001872 cindex = get_index(tn->key, p) + 1;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001873 tn = p;
1874 --iter->depth;
1875 goto rescan;
1876 }
1877
1878 /* got root? */
Robert Olsson19baf832005-06-21 12:43:18 -07001879 return NULL;
1880}
1881
Alexander Duyckadaf9812014-12-31 10:55:47 -08001882static struct tnode *fib_trie_get_first(struct fib_trie_iter *iter,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001883 struct trie *t)
Robert Olsson19baf832005-06-21 12:43:18 -07001884{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001885 struct tnode *n;
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08001886
Stephen Hemminger132adf52007-03-08 20:44:43 -08001887 if (!t)
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08001888 return NULL;
1889
1890 n = rcu_dereference(t->trie);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001891 if (!n)
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08001892 return NULL;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001893
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001894 if (IS_TNODE(n)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08001895 iter->tnode = n;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001896 iter->index = 0;
1897 iter->depth = 1;
1898 } else {
1899 iter->tnode = NULL;
1900 iter->index = 0;
1901 iter->depth = 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001902 }
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001903
1904 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07001905}
1906
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001907static void trie_collect_stats(struct trie *t, struct trie_stat *s)
Robert Olsson19baf832005-06-21 12:43:18 -07001908{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001909 struct tnode *n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001910 struct fib_trie_iter iter;
Robert Olsson19baf832005-06-21 12:43:18 -07001911
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001912 memset(s, 0, sizeof(*s));
Robert Olsson19baf832005-06-21 12:43:18 -07001913
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001914 rcu_read_lock();
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001915 for (n = fib_trie_get_first(&iter, t); n; n = fib_trie_get_next(&iter)) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001916 if (IS_LEAF(n)) {
Stephen Hemminger93672292008-01-22 21:54:05 -08001917 struct leaf_info *li;
Stephen Hemminger93672292008-01-22 21:54:05 -08001918
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001919 s->leaves++;
1920 s->totdepth += iter.depth;
1921 if (iter.depth > s->maxdepth)
1922 s->maxdepth = iter.depth;
Stephen Hemminger93672292008-01-22 21:54:05 -08001923
Alexander Duyckadaf9812014-12-31 10:55:47 -08001924 hlist_for_each_entry_rcu(li, &n->list, hlist)
Stephen Hemminger93672292008-01-22 21:54:05 -08001925 ++s->prefixes;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001926 } else {
Alexander Duyck98293e82014-12-31 10:56:18 -08001927 unsigned long i;
Robert Olsson19baf832005-06-21 12:43:18 -07001928
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001929 s->tnodes++;
Alexander Duyckadaf9812014-12-31 10:55:47 -08001930 if (n->bits < MAX_STAT_DEPTH)
1931 s->nodesizes[n->bits]++;
Robert Olsson06ef9212006-03-20 21:35:01 -08001932
Alexander Duyck21d1f112014-12-31 10:57:02 -08001933 for (i = tnode_child_length(n); i--;) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08001934 if (!rcu_access_pointer(n->child[i]))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001935 s->nullpointers++;
Alexander Duyck98293e82014-12-31 10:56:18 -08001936 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001937 }
1938 }
1939 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07001940}
1941
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001942/*
Robert Olsson19baf832005-06-21 12:43:18 -07001943 * This outputs /proc/net/fib_triestats
Robert Olsson19baf832005-06-21 12:43:18 -07001944 */
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001945static void trie_show_stats(struct seq_file *seq, struct trie_stat *stat)
Robert Olsson19baf832005-06-21 12:43:18 -07001946{
Eric Dumazeta034ee32010-09-09 23:32:28 +00001947 unsigned int i, max, pointers, bytes, avdepth;
Robert Olsson19baf832005-06-21 12:43:18 -07001948
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001949 if (stat->leaves)
1950 avdepth = stat->totdepth*100 / stat->leaves;
1951 else
1952 avdepth = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001953
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001954 seq_printf(seq, "\tAver depth: %u.%02d\n",
1955 avdepth / 100, avdepth % 100);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001956 seq_printf(seq, "\tMax depth: %u\n", stat->maxdepth);
Robert Olsson19baf832005-06-21 12:43:18 -07001957
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001958 seq_printf(seq, "\tLeaves: %u\n", stat->leaves);
Alexander Duyckadaf9812014-12-31 10:55:47 -08001959 bytes = sizeof(struct tnode) * stat->leaves;
Stephen Hemminger93672292008-01-22 21:54:05 -08001960
1961 seq_printf(seq, "\tPrefixes: %u\n", stat->prefixes);
1962 bytes += sizeof(struct leaf_info) * stat->prefixes;
1963
Stephen Hemminger187b5182008-01-12 20:55:55 -08001964 seq_printf(seq, "\tInternal nodes: %u\n\t", stat->tnodes);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001965 bytes += sizeof(struct tnode) * stat->tnodes;
Robert Olsson19baf832005-06-21 12:43:18 -07001966
Robert Olsson06ef9212006-03-20 21:35:01 -08001967 max = MAX_STAT_DEPTH;
1968 while (max > 0 && stat->nodesizes[max-1] == 0)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001969 max--;
Robert Olsson19baf832005-06-21 12:43:18 -07001970
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001971 pointers = 0;
Jerry Snitselaarf585a992013-07-22 12:01:58 -07001972 for (i = 1; i < max; i++)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001973 if (stat->nodesizes[i] != 0) {
Stephen Hemminger187b5182008-01-12 20:55:55 -08001974 seq_printf(seq, " %u: %u", i, stat->nodesizes[i]);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001975 pointers += (1<<i) * stat->nodesizes[i];
1976 }
1977 seq_putc(seq, '\n');
Stephen Hemminger187b5182008-01-12 20:55:55 -08001978 seq_printf(seq, "\tPointers: %u\n", pointers);
Robert Olsson19baf832005-06-21 12:43:18 -07001979
Alexander Duyckadaf9812014-12-31 10:55:47 -08001980 bytes += sizeof(struct tnode *) * pointers;
Stephen Hemminger187b5182008-01-12 20:55:55 -08001981 seq_printf(seq, "Null ptrs: %u\n", stat->nullpointers);
1982 seq_printf(seq, "Total size: %u kB\n", (bytes + 1023) / 1024);
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08001983}
Robert Olsson19baf832005-06-21 12:43:18 -07001984
1985#ifdef CONFIG_IP_FIB_TRIE_STATS
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08001986static void trie_show_usage(struct seq_file *seq,
Alexander Duyck8274a972014-12-31 10:55:29 -08001987 const struct trie_use_stats __percpu *stats)
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08001988{
Alexander Duyck8274a972014-12-31 10:55:29 -08001989 struct trie_use_stats s = { 0 };
1990 int cpu;
1991
1992 /* loop through all of the CPUs and gather up the stats */
1993 for_each_possible_cpu(cpu) {
1994 const struct trie_use_stats *pcpu = per_cpu_ptr(stats, cpu);
1995
1996 s.gets += pcpu->gets;
1997 s.backtrack += pcpu->backtrack;
1998 s.semantic_match_passed += pcpu->semantic_match_passed;
1999 s.semantic_match_miss += pcpu->semantic_match_miss;
2000 s.null_node_hit += pcpu->null_node_hit;
2001 s.resize_node_skipped += pcpu->resize_node_skipped;
2002 }
2003
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002004 seq_printf(seq, "\nCounters:\n---------\n");
Alexander Duyck8274a972014-12-31 10:55:29 -08002005 seq_printf(seq, "gets = %u\n", s.gets);
2006 seq_printf(seq, "backtracks = %u\n", s.backtrack);
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002007 seq_printf(seq, "semantic match passed = %u\n",
Alexander Duyck8274a972014-12-31 10:55:29 -08002008 s.semantic_match_passed);
2009 seq_printf(seq, "semantic match miss = %u\n", s.semantic_match_miss);
2010 seq_printf(seq, "null node hit= %u\n", s.null_node_hit);
2011 seq_printf(seq, "skipped node resize = %u\n\n", s.resize_node_skipped);
Robert Olsson19baf832005-06-21 12:43:18 -07002012}
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002013#endif /* CONFIG_IP_FIB_TRIE_STATS */
2014
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002015static void fib_table_print(struct seq_file *seq, struct fib_table *tb)
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002016{
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002017 if (tb->tb_id == RT_TABLE_LOCAL)
2018 seq_puts(seq, "Local:\n");
2019 else if (tb->tb_id == RT_TABLE_MAIN)
2020 seq_puts(seq, "Main:\n");
2021 else
2022 seq_printf(seq, "Id %d:\n", tb->tb_id);
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002023}
Robert Olsson19baf832005-06-21 12:43:18 -07002024
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002025
Robert Olsson19baf832005-06-21 12:43:18 -07002026static int fib_triestat_seq_show(struct seq_file *seq, void *v)
2027{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002028 struct net *net = (struct net *)seq->private;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002029 unsigned int h;
Eric W. Biederman877a9bf2007-12-07 00:47:47 -08002030
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002031 seq_printf(seq,
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002032 "Basic info: size of leaf:"
2033 " %Zd bytes, size of tnode: %Zd bytes.\n",
Alexander Duyckadaf9812014-12-31 10:55:47 -08002034 sizeof(struct tnode), sizeof(struct tnode));
Olof Johansson91b9a272005-08-09 20:24:39 -07002035
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002036 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2037 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002038 struct fib_table *tb;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002039
Sasha Levinb67bfe02013-02-27 17:06:00 -08002040 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002041 struct trie *t = (struct trie *) tb->tb_data;
2042 struct trie_stat stat;
2043
2044 if (!t)
2045 continue;
2046
2047 fib_table_print(seq, tb);
2048
2049 trie_collect_stats(t, &stat);
2050 trie_show_stats(seq, &stat);
2051#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -08002052 trie_show_usage(seq, t->stats);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002053#endif
2054 }
2055 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002056
Robert Olsson19baf832005-06-21 12:43:18 -07002057 return 0;
2058}
2059
Robert Olsson19baf832005-06-21 12:43:18 -07002060static int fib_triestat_seq_open(struct inode *inode, struct file *file)
2061{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002062 return single_open_net(inode, file, fib_triestat_seq_show);
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002063}
2064
Arjan van de Ven9a321442007-02-12 00:55:35 -08002065static const struct file_operations fib_triestat_fops = {
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07002066 .owner = THIS_MODULE,
2067 .open = fib_triestat_seq_open,
2068 .read = seq_read,
2069 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002070 .release = single_release_net,
Robert Olsson19baf832005-06-21 12:43:18 -07002071};
2072
Alexander Duyckadaf9812014-12-31 10:55:47 -08002073static struct tnode *fib_trie_get_idx(struct seq_file *seq, loff_t pos)
Robert Olsson19baf832005-06-21 12:43:18 -07002074{
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002075 struct fib_trie_iter *iter = seq->private;
2076 struct net *net = seq_file_net(seq);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002077 loff_t idx = 0;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002078 unsigned int h;
Robert Olsson19baf832005-06-21 12:43:18 -07002079
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002080 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2081 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002082 struct fib_table *tb;
2083
Sasha Levinb67bfe02013-02-27 17:06:00 -08002084 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002085 struct tnode *n;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002086
2087 for (n = fib_trie_get_first(iter,
2088 (struct trie *) tb->tb_data);
2089 n; n = fib_trie_get_next(iter))
2090 if (pos == idx++) {
2091 iter->tb = tb;
2092 return n;
2093 }
2094 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002095 }
Robert Olsson19baf832005-06-21 12:43:18 -07002096
Robert Olsson19baf832005-06-21 12:43:18 -07002097 return NULL;
2098}
2099
2100static void *fib_trie_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08002101 __acquires(RCU)
Robert Olsson19baf832005-06-21 12:43:18 -07002102{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002103 rcu_read_lock();
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002104 return fib_trie_get_idx(seq, *pos);
Robert Olsson19baf832005-06-21 12:43:18 -07002105}
2106
2107static void *fib_trie_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2108{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002109 struct fib_trie_iter *iter = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002110 struct net *net = seq_file_net(seq);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002111 struct fib_table *tb = iter->tb;
2112 struct hlist_node *tb_node;
2113 unsigned int h;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002114 struct tnode *n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002115
Robert Olsson19baf832005-06-21 12:43:18 -07002116 ++*pos;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002117 /* next node in same table */
2118 n = fib_trie_get_next(iter);
2119 if (n)
2120 return n;
Olof Johansson91b9a272005-08-09 20:24:39 -07002121
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002122 /* walk rest of this hash chain */
2123 h = tb->tb_id & (FIB_TABLE_HASHSZ - 1);
Eric Dumazet0a5c0472011-03-31 01:51:35 -07002124 while ((tb_node = rcu_dereference(hlist_next_rcu(&tb->tb_hlist)))) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002125 tb = hlist_entry(tb_node, struct fib_table, tb_hlist);
2126 n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
2127 if (n)
2128 goto found;
2129 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002130
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002131 /* new hash chain */
2132 while (++h < FIB_TABLE_HASHSZ) {
2133 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002134 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002135 n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
2136 if (n)
2137 goto found;
2138 }
2139 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002140 return NULL;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002141
2142found:
2143 iter->tb = tb;
2144 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07002145}
2146
2147static void fib_trie_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08002148 __releases(RCU)
Robert Olsson19baf832005-06-21 12:43:18 -07002149{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002150 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07002151}
2152
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002153static void seq_indent(struct seq_file *seq, int n)
2154{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002155 while (n-- > 0)
2156 seq_puts(seq, " ");
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002157}
Robert Olsson19baf832005-06-21 12:43:18 -07002158
Eric Dumazet28d36e32008-01-14 23:09:56 -08002159static inline const char *rtn_scope(char *buf, size_t len, enum rt_scope_t s)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002160{
Stephen Hemminger132adf52007-03-08 20:44:43 -08002161 switch (s) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002162 case RT_SCOPE_UNIVERSE: return "universe";
2163 case RT_SCOPE_SITE: return "site";
2164 case RT_SCOPE_LINK: return "link";
2165 case RT_SCOPE_HOST: return "host";
2166 case RT_SCOPE_NOWHERE: return "nowhere";
2167 default:
Eric Dumazet28d36e32008-01-14 23:09:56 -08002168 snprintf(buf, len, "scope=%d", s);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002169 return buf;
2170 }
2171}
2172
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -07002173static const char *const rtn_type_names[__RTN_MAX] = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002174 [RTN_UNSPEC] = "UNSPEC",
2175 [RTN_UNICAST] = "UNICAST",
2176 [RTN_LOCAL] = "LOCAL",
2177 [RTN_BROADCAST] = "BROADCAST",
2178 [RTN_ANYCAST] = "ANYCAST",
2179 [RTN_MULTICAST] = "MULTICAST",
2180 [RTN_BLACKHOLE] = "BLACKHOLE",
2181 [RTN_UNREACHABLE] = "UNREACHABLE",
2182 [RTN_PROHIBIT] = "PROHIBIT",
2183 [RTN_THROW] = "THROW",
2184 [RTN_NAT] = "NAT",
2185 [RTN_XRESOLVE] = "XRESOLVE",
2186};
2187
Eric Dumazeta034ee32010-09-09 23:32:28 +00002188static inline const char *rtn_type(char *buf, size_t len, unsigned int t)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002189{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002190 if (t < __RTN_MAX && rtn_type_names[t])
2191 return rtn_type_names[t];
Eric Dumazet28d36e32008-01-14 23:09:56 -08002192 snprintf(buf, len, "type %u", t);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002193 return buf;
2194}
2195
2196/* Pretty print the trie */
Robert Olsson19baf832005-06-21 12:43:18 -07002197static int fib_trie_seq_show(struct seq_file *seq, void *v)
2198{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002199 const struct fib_trie_iter *iter = seq->private;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002200 struct tnode *n = v;
Robert Olsson19baf832005-06-21 12:43:18 -07002201
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002202 if (!node_parent_rcu(n))
2203 fib_table_print(seq, iter->tb);
Robert Olsson095b8502007-01-26 19:06:01 -08002204
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002205 if (IS_TNODE(n)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002206 __be32 prf = htonl(n->key);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002207
Alexander Duycke9b44012014-12-31 10:56:12 -08002208 seq_indent(seq, iter->depth-1);
2209 seq_printf(seq, " +-- %pI4/%zu %u %u %u\n",
2210 &prf, KEYLENGTH - n->pos - n->bits, n->bits,
2211 n->full_children, n->empty_children);
Olof Johansson91b9a272005-08-09 20:24:39 -07002212 } else {
Stephen Hemminger13280422008-01-22 21:54:37 -08002213 struct leaf_info *li;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002214 __be32 val = htonl(n->key);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002215
2216 seq_indent(seq, iter->depth);
Harvey Harrison673d57e2008-10-31 00:53:57 -07002217 seq_printf(seq, " |-- %pI4\n", &val);
Eric Dumazet28d36e32008-01-14 23:09:56 -08002218
Alexander Duyckadaf9812014-12-31 10:55:47 -08002219 hlist_for_each_entry_rcu(li, &n->list, hlist) {
Stephen Hemminger13280422008-01-22 21:54:37 -08002220 struct fib_alias *fa;
Eric Dumazet28d36e32008-01-14 23:09:56 -08002221
Stephen Hemminger13280422008-01-22 21:54:37 -08002222 list_for_each_entry_rcu(fa, &li->falh, fa_list) {
2223 char buf1[32], buf2[32];
Eric Dumazet28d36e32008-01-14 23:09:56 -08002224
Stephen Hemminger13280422008-01-22 21:54:37 -08002225 seq_indent(seq, iter->depth+1);
2226 seq_printf(seq, " /%d %s %s", li->plen,
2227 rtn_scope(buf1, sizeof(buf1),
David S. Miller37e826c2011-03-24 18:06:47 -07002228 fa->fa_info->fib_scope),
Stephen Hemminger13280422008-01-22 21:54:37 -08002229 rtn_type(buf2, sizeof(buf2),
2230 fa->fa_type));
2231 if (fa->fa_tos)
Denis V. Lunevb9c4d822008-02-05 02:58:45 -08002232 seq_printf(seq, " tos=%d", fa->fa_tos);
Stephen Hemminger13280422008-01-22 21:54:37 -08002233 seq_putc(seq, '\n');
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002234 }
2235 }
Robert Olsson19baf832005-06-21 12:43:18 -07002236 }
2237
2238 return 0;
2239}
2240
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002241static const struct seq_operations fib_trie_seq_ops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002242 .start = fib_trie_seq_start,
2243 .next = fib_trie_seq_next,
2244 .stop = fib_trie_seq_stop,
2245 .show = fib_trie_seq_show,
Robert Olsson19baf832005-06-21 12:43:18 -07002246};
2247
2248static int fib_trie_seq_open(struct inode *inode, struct file *file)
2249{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002250 return seq_open_net(inode, file, &fib_trie_seq_ops,
2251 sizeof(struct fib_trie_iter));
Robert Olsson19baf832005-06-21 12:43:18 -07002252}
2253
Arjan van de Ven9a321442007-02-12 00:55:35 -08002254static const struct file_operations fib_trie_fops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002255 .owner = THIS_MODULE,
2256 .open = fib_trie_seq_open,
2257 .read = seq_read,
2258 .llseek = seq_lseek,
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002259 .release = seq_release_net,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002260};
2261
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002262struct fib_route_iter {
2263 struct seq_net_private p;
2264 struct trie *main_trie;
2265 loff_t pos;
2266 t_key key;
2267};
2268
Alexander Duyckadaf9812014-12-31 10:55:47 -08002269static struct tnode *fib_route_get_idx(struct fib_route_iter *iter, loff_t pos)
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002270{
Alexander Duyckadaf9812014-12-31 10:55:47 -08002271 struct tnode *l = NULL;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002272 struct trie *t = iter->main_trie;
2273
2274 /* use cache location of last found key */
2275 if (iter->pos > 0 && pos >= iter->pos && (l = fib_find_node(t, iter->key)))
2276 pos -= iter->pos;
2277 else {
2278 iter->pos = 0;
2279 l = trie_firstleaf(t);
2280 }
2281
2282 while (l && pos-- > 0) {
2283 iter->pos++;
2284 l = trie_nextleaf(l);
2285 }
2286
2287 if (l)
2288 iter->key = pos; /* remember it */
2289 else
2290 iter->pos = 0; /* forget it */
2291
2292 return l;
2293}
2294
2295static void *fib_route_seq_start(struct seq_file *seq, loff_t *pos)
2296 __acquires(RCU)
2297{
2298 struct fib_route_iter *iter = seq->private;
2299 struct fib_table *tb;
2300
2301 rcu_read_lock();
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002302 tb = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN);
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002303 if (!tb)
2304 return NULL;
2305
2306 iter->main_trie = (struct trie *) tb->tb_data;
2307 if (*pos == 0)
2308 return SEQ_START_TOKEN;
2309 else
2310 return fib_route_get_idx(iter, *pos - 1);
2311}
2312
2313static void *fib_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2314{
2315 struct fib_route_iter *iter = seq->private;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002316 struct tnode *l = v;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002317
2318 ++*pos;
2319 if (v == SEQ_START_TOKEN) {
2320 iter->pos = 0;
2321 l = trie_firstleaf(iter->main_trie);
2322 } else {
2323 iter->pos++;
2324 l = trie_nextleaf(l);
2325 }
2326
2327 if (l)
2328 iter->key = l->key;
2329 else
2330 iter->pos = 0;
2331 return l;
2332}
2333
2334static void fib_route_seq_stop(struct seq_file *seq, void *v)
2335 __releases(RCU)
2336{
2337 rcu_read_unlock();
2338}
2339
Eric Dumazeta034ee32010-09-09 23:32:28 +00002340static unsigned int fib_flag_trans(int type, __be32 mask, const struct fib_info *fi)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002341{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002342 unsigned int flags = 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002343
Eric Dumazeta034ee32010-09-09 23:32:28 +00002344 if (type == RTN_UNREACHABLE || type == RTN_PROHIBIT)
2345 flags = RTF_REJECT;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002346 if (fi && fi->fib_nh->nh_gw)
2347 flags |= RTF_GATEWAY;
Al Viro32ab5f82006-09-26 22:21:45 -07002348 if (mask == htonl(0xFFFFFFFF))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002349 flags |= RTF_HOST;
2350 flags |= RTF_UP;
2351 return flags;
2352}
2353
2354/*
2355 * This outputs /proc/net/route.
2356 * The format of the file is not supposed to be changed
Eric Dumazeta034ee32010-09-09 23:32:28 +00002357 * and needs to be same as fib_hash output to avoid breaking
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002358 * legacy utilities
2359 */
2360static int fib_route_seq_show(struct seq_file *seq, void *v)
2361{
Alexander Duyckadaf9812014-12-31 10:55:47 -08002362 struct tnode *l = v;
Stephen Hemminger13280422008-01-22 21:54:37 -08002363 struct leaf_info *li;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002364
2365 if (v == SEQ_START_TOKEN) {
2366 seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
2367 "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
2368 "\tWindow\tIRTT");
2369 return 0;
2370 }
2371
Sasha Levinb67bfe02013-02-27 17:06:00 -08002372 hlist_for_each_entry_rcu(li, &l->list, hlist) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002373 struct fib_alias *fa;
Al Viro32ab5f82006-09-26 22:21:45 -07002374 __be32 mask, prefix;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002375
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002376 mask = inet_make_mask(li->plen);
2377 prefix = htonl(l->key);
2378
2379 list_for_each_entry_rcu(fa, &li->falh, fa_list) {
Herbert Xu1371e372005-10-15 09:42:39 +10002380 const struct fib_info *fi = fa->fa_info;
Eric Dumazeta034ee32010-09-09 23:32:28 +00002381 unsigned int flags = fib_flag_trans(fa->fa_type, mask, fi);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002382
2383 if (fa->fa_type == RTN_BROADCAST
2384 || fa->fa_type == RTN_MULTICAST)
2385 continue;
2386
Tetsuo Handa652586d2013-11-14 14:31:57 -08002387 seq_setwidth(seq, 127);
2388
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002389 if (fi)
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002390 seq_printf(seq,
2391 "%s\t%08X\t%08X\t%04X\t%d\t%u\t"
Tetsuo Handa652586d2013-11-14 14:31:57 -08002392 "%d\t%08X\t%d\t%u\t%u",
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002393 fi->fib_dev ? fi->fib_dev->name : "*",
2394 prefix,
2395 fi->fib_nh->nh_gw, flags, 0, 0,
2396 fi->fib_priority,
2397 mask,
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002398 (fi->fib_advmss ?
2399 fi->fib_advmss + 40 : 0),
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002400 fi->fib_window,
Tetsuo Handa652586d2013-11-14 14:31:57 -08002401 fi->fib_rtt >> 3);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002402 else
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002403 seq_printf(seq,
2404 "*\t%08X\t%08X\t%04X\t%d\t%u\t"
Tetsuo Handa652586d2013-11-14 14:31:57 -08002405 "%d\t%08X\t%d\t%u\t%u",
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002406 prefix, 0, flags, 0, 0, 0,
Tetsuo Handa652586d2013-11-14 14:31:57 -08002407 mask, 0, 0, 0);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002408
Tetsuo Handa652586d2013-11-14 14:31:57 -08002409 seq_pad(seq, '\n');
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002410 }
2411 }
2412
2413 return 0;
2414}
2415
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002416static const struct seq_operations fib_route_seq_ops = {
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002417 .start = fib_route_seq_start,
2418 .next = fib_route_seq_next,
2419 .stop = fib_route_seq_stop,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002420 .show = fib_route_seq_show,
2421};
2422
2423static int fib_route_seq_open(struct inode *inode, struct file *file)
2424{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002425 return seq_open_net(inode, file, &fib_route_seq_ops,
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002426 sizeof(struct fib_route_iter));
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002427}
2428
Arjan van de Ven9a321442007-02-12 00:55:35 -08002429static const struct file_operations fib_route_fops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002430 .owner = THIS_MODULE,
2431 .open = fib_route_seq_open,
2432 .read = seq_read,
2433 .llseek = seq_lseek,
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002434 .release = seq_release_net,
Robert Olsson19baf832005-06-21 12:43:18 -07002435};
2436
Denis V. Lunev61a02652008-01-10 03:21:09 -08002437int __net_init fib_proc_init(struct net *net)
Robert Olsson19baf832005-06-21 12:43:18 -07002438{
Gao fengd4beaa62013-02-18 01:34:54 +00002439 if (!proc_create("fib_trie", S_IRUGO, net->proc_net, &fib_trie_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002440 goto out1;
2441
Gao fengd4beaa62013-02-18 01:34:54 +00002442 if (!proc_create("fib_triestat", S_IRUGO, net->proc_net,
2443 &fib_triestat_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002444 goto out2;
2445
Gao fengd4beaa62013-02-18 01:34:54 +00002446 if (!proc_create("route", S_IRUGO, net->proc_net, &fib_route_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002447 goto out3;
2448
Robert Olsson19baf832005-06-21 12:43:18 -07002449 return 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002450
2451out3:
Gao fengece31ff2013-02-18 01:34:56 +00002452 remove_proc_entry("fib_triestat", net->proc_net);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002453out2:
Gao fengece31ff2013-02-18 01:34:56 +00002454 remove_proc_entry("fib_trie", net->proc_net);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002455out1:
2456 return -ENOMEM;
Robert Olsson19baf832005-06-21 12:43:18 -07002457}
2458
Denis V. Lunev61a02652008-01-10 03:21:09 -08002459void __net_exit fib_proc_exit(struct net *net)
Robert Olsson19baf832005-06-21 12:43:18 -07002460{
Gao fengece31ff2013-02-18 01:34:56 +00002461 remove_proc_entry("fib_trie", net->proc_net);
2462 remove_proc_entry("fib_triestat", net->proc_net);
2463 remove_proc_entry("route", net->proc_net);
Robert Olsson19baf832005-06-21 12:43:18 -07002464}
2465
2466#endif /* CONFIG_PROC_FS */