blob: f17e2239b7b9d3996ad1d73812b26e14a01006cb [file] [log] [blame]
Robert Olsson19baf832005-06-21 12:43:18 -07001/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version
5 * 2 of the License, or (at your option) any later version.
6 *
7 * Robert Olsson <robert.olsson@its.uu.se> Uppsala Universitet
8 * & Swedish University of Agricultural Sciences.
9 *
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090010 * Jens Laas <jens.laas@data.slu.se> Swedish University of
Robert Olsson19baf832005-06-21 12:43:18 -070011 * Agricultural Sciences.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090012 *
Robert Olsson19baf832005-06-21 12:43:18 -070013 * Hans Liss <hans.liss@its.uu.se> Uppsala Universitet
14 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015 * This work is based on the LPC-trie which is originally described in:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090016 *
Robert Olsson19baf832005-06-21 12:43:18 -070017 * An experimental study of compression methods for dynamic tries
18 * Stefan Nilsson and Matti Tikkanen. Algorithmica, 33(1):19-33, 2002.
Justin P. Mattock631dd1a2010-10-18 11:03:14 +020019 * http://www.csc.kth.se/~snilsson/software/dyntrie2/
Robert Olsson19baf832005-06-21 12:43:18 -070020 *
21 *
22 * IP-address lookup using LC-tries. Stefan Nilsson and Gunnar Karlsson
23 * IEEE Journal on Selected Areas in Communications, 17(6):1083-1092, June 1999
24 *
Robert Olsson19baf832005-06-21 12:43:18 -070025 *
26 * Code from fib_hash has been reused which includes the following header:
27 *
28 *
29 * INET An implementation of the TCP/IP protocol suite for the LINUX
30 * operating system. INET is implemented using the BSD Socket
31 * interface as the means of communication with the user level.
32 *
33 * IPv4 FIB: lookup engine and maintenance routines.
34 *
35 *
36 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
37 *
38 * This program is free software; you can redistribute it and/or
39 * modify it under the terms of the GNU General Public License
40 * as published by the Free Software Foundation; either version
41 * 2 of the License, or (at your option) any later version.
Robert Olssonfd966252005-12-22 11:25:10 -080042 *
43 * Substantial contributions to this work comes from:
44 *
45 * David S. Miller, <davem@davemloft.net>
46 * Stephen Hemminger <shemminger@osdl.org>
47 * Paul E. McKenney <paulmck@us.ibm.com>
48 * Patrick McHardy <kaber@trash.net>
Robert Olsson19baf832005-06-21 12:43:18 -070049 */
50
Jens Låås80b71b82009-08-28 23:57:15 -070051#define VERSION "0.409"
Robert Olsson19baf832005-06-21 12:43:18 -070052
Robert Olsson19baf832005-06-21 12:43:18 -070053#include <asm/uaccess.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070054#include <linux/bitops.h>
Robert Olsson19baf832005-06-21 12:43:18 -070055#include <linux/types.h>
56#include <linux/kernel.h>
Robert Olsson19baf832005-06-21 12:43:18 -070057#include <linux/mm.h>
58#include <linux/string.h>
59#include <linux/socket.h>
60#include <linux/sockios.h>
61#include <linux/errno.h>
62#include <linux/in.h>
63#include <linux/inet.h>
Stephen Hemmingercd8787a2006-01-03 14:38:34 -080064#include <linux/inetdevice.h>
Robert Olsson19baf832005-06-21 12:43:18 -070065#include <linux/netdevice.h>
66#include <linux/if_arp.h>
67#include <linux/proc_fs.h>
Robert Olsson2373ce12005-08-25 13:01:29 -070068#include <linux/rcupdate.h>
Robert Olsson19baf832005-06-21 12:43:18 -070069#include <linux/skbuff.h>
70#include <linux/netlink.h>
71#include <linux/init.h>
72#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090073#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040074#include <linux/export.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020075#include <net/net_namespace.h>
Robert Olsson19baf832005-06-21 12:43:18 -070076#include <net/ip.h>
77#include <net/protocol.h>
78#include <net/route.h>
79#include <net/tcp.h>
80#include <net/sock.h>
81#include <net/ip_fib.h>
82#include "fib_lookup.h"
83
Robert Olsson06ef9212006-03-20 21:35:01 -080084#define MAX_STAT_DEPTH 32
Robert Olsson19baf832005-06-21 12:43:18 -070085
Alexander Duyck95f60ea2015-01-22 15:51:26 -080086#define KEYLENGTH (8*sizeof(t_key))
87#define KEY_MAX ((t_key)~0)
Robert Olsson19baf832005-06-21 12:43:18 -070088
Robert Olsson19baf832005-06-21 12:43:18 -070089typedef unsigned int t_key;
90
Alexander Duyck64c9b6f2014-12-31 10:55:35 -080091#define IS_TNODE(n) ((n)->bits)
92#define IS_LEAF(n) (!(n)->bits)
Robert Olsson2373ce12005-08-25 13:01:29 -070093
Alexander Duycke9b44012014-12-31 10:56:12 -080094#define get_index(_key, _kv) (((_key) ^ (_kv)->key) >> (_kv)->pos)
Alexander Duyck9f9e6362014-12-31 10:55:54 -080095
Alexander Duyck64c9b6f2014-12-31 10:55:35 -080096struct tnode {
97 t_key key;
98 unsigned char bits; /* 2log(KEYLENGTH) bits needed */
99 unsigned char pos; /* 2log(KEYLENGTH) bits needed */
Alexander Duyck5405afd2014-12-31 10:57:08 -0800100 unsigned char slen;
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800101 struct tnode __rcu *parent;
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800102 struct rcu_head rcu;
Alexander Duyckadaf9812014-12-31 10:55:47 -0800103 union {
104 /* The fields in this struct are valid if bits > 0 (TNODE) */
105 struct {
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800106 t_key empty_children; /* KEYLENGTH bits needed */
107 t_key full_children; /* KEYLENGTH bits needed */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800108 struct tnode __rcu *child[0];
109 };
110 /* This list pointer if valid if bits == 0 (LEAF) */
111 struct hlist_head list;
112 };
Robert Olsson19baf832005-06-21 12:43:18 -0700113};
114
115struct leaf_info {
116 struct hlist_node hlist;
117 int plen;
Eric Dumazet5c745012011-07-18 03:16:33 +0000118 u32 mask_plen; /* ntohl(inet_make_mask(plen)) */
Alexander Duyck56315f92015-02-25 15:31:31 -0800119 struct hlist_head falh;
Eric Dumazet5c745012011-07-18 03:16:33 +0000120 struct rcu_head rcu;
Robert Olsson19baf832005-06-21 12:43:18 -0700121};
122
Robert Olsson19baf832005-06-21 12:43:18 -0700123#ifdef CONFIG_IP_FIB_TRIE_STATS
124struct trie_use_stats {
125 unsigned int gets;
126 unsigned int backtrack;
127 unsigned int semantic_match_passed;
128 unsigned int semantic_match_miss;
129 unsigned int null_node_hit;
Robert Olsson2f368952005-07-05 15:02:40 -0700130 unsigned int resize_node_skipped;
Robert Olsson19baf832005-06-21 12:43:18 -0700131};
132#endif
133
134struct trie_stat {
135 unsigned int totdepth;
136 unsigned int maxdepth;
137 unsigned int tnodes;
138 unsigned int leaves;
139 unsigned int nullpointers;
Stephen Hemminger93672292008-01-22 21:54:05 -0800140 unsigned int prefixes;
Robert Olsson06ef9212006-03-20 21:35:01 -0800141 unsigned int nodesizes[MAX_STAT_DEPTH];
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700142};
Robert Olsson19baf832005-06-21 12:43:18 -0700143
144struct trie {
Alexander Duyckadaf9812014-12-31 10:55:47 -0800145 struct tnode __rcu *trie;
Robert Olsson19baf832005-06-21 12:43:18 -0700146#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -0800147 struct trie_use_stats __percpu *stats;
Robert Olsson19baf832005-06-21 12:43:18 -0700148#endif
Robert Olsson19baf832005-06-21 12:43:18 -0700149};
150
Alexander Duyckff181ed2014-12-31 10:56:43 -0800151static void resize(struct trie *t, struct tnode *tn);
Jarek Poplawskic3059472009-07-14 08:33:08 +0000152static size_t tnode_free_size;
153
154/*
155 * synchronize_rcu after call_rcu for that many pages; it should be especially
156 * useful before resizing the root node with PREEMPT_NONE configs; the value was
157 * obtained experimentally, aiming to avoid visible slowdown.
158 */
159static const int sync_pages = 128;
Robert Olsson19baf832005-06-21 12:43:18 -0700160
Christoph Lametere18b8902006-12-06 20:33:20 -0800161static struct kmem_cache *fn_alias_kmem __read_mostly;
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -0800162static struct kmem_cache *trie_leaf_kmem __read_mostly;
Robert Olsson19baf832005-06-21 12:43:18 -0700163
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800164/* caller must hold RTNL */
165#define node_parent(n) rtnl_dereference((n)->parent)
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700166
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800167/* caller must hold RCU read lock or RTNL */
168#define node_parent_rcu(n) rcu_dereference_rtnl((n)->parent)
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700169
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800170/* wrapper for rcu_assign_pointer */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800171static inline void node_set_parent(struct tnode *n, struct tnode *tp)
Stephen Hemminger06801912007-08-10 15:22:13 -0700172{
Alexander Duyckadaf9812014-12-31 10:55:47 -0800173 if (n)
174 rcu_assign_pointer(n->parent, tp);
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800175}
176
177#define NODE_INIT_PARENT(n, p) RCU_INIT_POINTER((n)->parent, p)
178
179/* This provides us with the number of children in this node, in the case of a
180 * leaf this will return 0 meaning none of the children are accessible.
181 */
Alexander Duyck98293e82014-12-31 10:56:18 -0800182static inline unsigned long tnode_child_length(const struct tnode *tn)
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800183{
184 return (1ul << tn->bits) & ~(1ul);
Stephen Hemminger06801912007-08-10 15:22:13 -0700185}
Robert Olsson2373ce12005-08-25 13:01:29 -0700186
Alexander Duyck98293e82014-12-31 10:56:18 -0800187/* caller must hold RTNL */
188static inline struct tnode *tnode_get_child(const struct tnode *tn,
189 unsigned long i)
Robert Olsson19baf832005-06-21 12:43:18 -0700190{
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700191 return rtnl_dereference(tn->child[i]);
Eric Dumazetb59cfbf2008-01-18 03:31:36 -0800192}
193
Alexander Duyck98293e82014-12-31 10:56:18 -0800194/* caller must hold RCU read lock or RTNL */
195static inline struct tnode *tnode_get_child_rcu(const struct tnode *tn,
196 unsigned long i)
Eric Dumazetb59cfbf2008-01-18 03:31:36 -0800197{
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700198 return rcu_dereference_rtnl(tn->child[i]);
Robert Olsson19baf832005-06-21 12:43:18 -0700199}
200
Alexander Duycke9b44012014-12-31 10:56:12 -0800201/* To understand this stuff, an understanding of keys and all their bits is
202 * necessary. Every node in the trie has a key associated with it, but not
203 * all of the bits in that key are significant.
204 *
205 * Consider a node 'n' and its parent 'tp'.
206 *
207 * If n is a leaf, every bit in its key is significant. Its presence is
208 * necessitated by path compression, since during a tree traversal (when
209 * searching for a leaf - unless we are doing an insertion) we will completely
210 * ignore all skipped bits we encounter. Thus we need to verify, at the end of
211 * a potentially successful search, that we have indeed been walking the
212 * correct key path.
213 *
214 * Note that we can never "miss" the correct key in the tree if present by
215 * following the wrong path. Path compression ensures that segments of the key
216 * that are the same for all keys with a given prefix are skipped, but the
217 * skipped part *is* identical for each node in the subtrie below the skipped
218 * bit! trie_insert() in this implementation takes care of that.
219 *
220 * if n is an internal node - a 'tnode' here, the various parts of its key
221 * have many different meanings.
222 *
223 * Example:
224 * _________________________________________________________________
225 * | i | i | i | i | i | i | i | N | N | N | S | S | S | S | S | C |
226 * -----------------------------------------------------------------
227 * 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
228 *
229 * _________________________________________________________________
230 * | C | C | C | u | u | u | u | u | u | u | u | u | u | u | u | u |
231 * -----------------------------------------------------------------
232 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
233 *
234 * tp->pos = 22
235 * tp->bits = 3
236 * n->pos = 13
237 * n->bits = 4
238 *
239 * First, let's just ignore the bits that come before the parent tp, that is
240 * the bits from (tp->pos + tp->bits) to 31. They are *known* but at this
241 * point we do not use them for anything.
242 *
243 * The bits from (tp->pos) to (tp->pos + tp->bits - 1) - "N", above - are the
244 * index into the parent's child array. That is, they will be used to find
245 * 'n' among tp's children.
246 *
247 * The bits from (n->pos + n->bits) to (tn->pos - 1) - "S" - are skipped bits
248 * for the node n.
249 *
250 * All the bits we have seen so far are significant to the node n. The rest
251 * of the bits are really not needed or indeed known in n->key.
252 *
253 * The bits from (n->pos) to (n->pos + n->bits - 1) - "C" - are the index into
254 * n's child array, and will of course be different for each child.
255 *
256 * The rest of the bits, from 0 to (n->pos + n->bits), are completely unknown
257 * at this point.
258 */
Robert Olsson19baf832005-06-21 12:43:18 -0700259
Denis V. Lunevf5026fa2007-12-13 09:47:57 -0800260static const int halve_threshold = 25;
261static const int inflate_threshold = 50;
Jarek Poplawski345aa032009-07-07 19:39:16 -0700262static const int halve_threshold_root = 15;
Jens Låås80b71b82009-08-28 23:57:15 -0700263static const int inflate_threshold_root = 30;
Robert Olsson2373ce12005-08-25 13:01:29 -0700264
265static void __alias_free_mem(struct rcu_head *head)
266{
267 struct fib_alias *fa = container_of(head, struct fib_alias, rcu);
268 kmem_cache_free(fn_alias_kmem, fa);
269}
270
271static inline void alias_free_mem_rcu(struct fib_alias *fa)
272{
273 call_rcu(&fa->rcu, __alias_free_mem);
274}
275
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800276#define TNODE_KMALLOC_MAX \
Alexander Duyckadaf9812014-12-31 10:55:47 -0800277 ilog2((PAGE_SIZE - sizeof(struct tnode)) / sizeof(struct tnode *))
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800278
279static void __node_free_rcu(struct rcu_head *head)
Robert Olsson2373ce12005-08-25 13:01:29 -0700280{
Alexander Duyckadaf9812014-12-31 10:55:47 -0800281 struct tnode *n = container_of(head, struct tnode, rcu);
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800282
283 if (IS_LEAF(n))
284 kmem_cache_free(trie_leaf_kmem, n);
285 else if (n->bits <= TNODE_KMALLOC_MAX)
286 kfree(n);
287 else
288 vfree(n);
Robert Olsson2373ce12005-08-25 13:01:29 -0700289}
290
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800291#define node_free(n) call_rcu(&n->rcu, __node_free_rcu)
Stephen Hemminger387a5482008-04-10 03:47:34 -0700292
Robert Olsson2373ce12005-08-25 13:01:29 -0700293static inline void free_leaf_info(struct leaf_info *leaf)
294{
Lai Jiangshanbceb0f42011-03-18 11:42:34 +0800295 kfree_rcu(leaf, rcu);
Robert Olsson2373ce12005-08-25 13:01:29 -0700296}
297
Eric Dumazet8d965442008-01-13 22:31:44 -0800298static struct tnode *tnode_alloc(size_t size)
Robert Olsson2373ce12005-08-25 13:01:29 -0700299{
Robert Olsson2373ce12005-08-25 13:01:29 -0700300 if (size <= PAGE_SIZE)
Eric Dumazet8d965442008-01-13 22:31:44 -0800301 return kzalloc(size, GFP_KERNEL);
Stephen Hemminger15be75c2008-04-10 02:56:38 -0700302 else
Eric Dumazet7a1c8e52010-11-20 07:46:35 +0000303 return vzalloc(size);
Stephen Hemminger15be75c2008-04-10 02:56:38 -0700304}
Robert Olsson2373ce12005-08-25 13:01:29 -0700305
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800306static inline void empty_child_inc(struct tnode *n)
307{
308 ++n->empty_children ? : ++n->full_children;
309}
310
311static inline void empty_child_dec(struct tnode *n)
312{
313 n->empty_children-- ? : n->full_children--;
314}
315
Alexander Duyckadaf9812014-12-31 10:55:47 -0800316static struct tnode *leaf_new(t_key key)
Robert Olsson19baf832005-06-21 12:43:18 -0700317{
Alexander Duyckadaf9812014-12-31 10:55:47 -0800318 struct tnode *l = kmem_cache_alloc(trie_leaf_kmem, GFP_KERNEL);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700319 if (l) {
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800320 l->parent = NULL;
321 /* set key and pos to reflect full key value
322 * any trailing zeros in the key should be ignored
323 * as the nodes are searched
324 */
325 l->key = key;
Alexander Duyck5405afd2014-12-31 10:57:08 -0800326 l->slen = 0;
Alexander Duycke9b44012014-12-31 10:56:12 -0800327 l->pos = 0;
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800328 /* set bits to 0 indicating we are not a tnode */
329 l->bits = 0;
330
Robert Olsson19baf832005-06-21 12:43:18 -0700331 INIT_HLIST_HEAD(&l->list);
332 }
333 return l;
334}
335
336static struct leaf_info *leaf_info_new(int plen)
337{
338 struct leaf_info *li = kmalloc(sizeof(struct leaf_info), GFP_KERNEL);
Robert Olsson2373ce12005-08-25 13:01:29 -0700339 if (li) {
340 li->plen = plen;
Eric Dumazet5c745012011-07-18 03:16:33 +0000341 li->mask_plen = ntohl(inet_make_mask(plen));
Alexander Duyck56315f92015-02-25 15:31:31 -0800342 INIT_HLIST_HEAD(&li->falh);
Patrick McHardyf0e36f82005-07-05 14:44:55 -0700343 }
Robert Olsson2373ce12005-08-25 13:01:29 -0700344 return li;
Patrick McHardyf0e36f82005-07-05 14:44:55 -0700345}
346
Stephen Hemmingera07f5f52008-01-22 21:53:36 -0800347static struct tnode *tnode_new(t_key key, int pos, int bits)
Robert Olsson19baf832005-06-21 12:43:18 -0700348{
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800349 size_t sz = offsetof(struct tnode, child[1ul << bits]);
Patrick McHardyf0e36f82005-07-05 14:44:55 -0700350 struct tnode *tn = tnode_alloc(sz);
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800351 unsigned int shift = pos + bits;
352
353 /* verify bits and pos their msb bits clear and values are valid */
354 BUG_ON(!bits || (shift > KEYLENGTH));
Robert Olsson19baf832005-06-21 12:43:18 -0700355
Olof Johansson91b9a272005-08-09 20:24:39 -0700356 if (tn) {
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800357 tn->parent = NULL;
Alexander Duyck5405afd2014-12-31 10:57:08 -0800358 tn->slen = pos;
Robert Olsson19baf832005-06-21 12:43:18 -0700359 tn->pos = pos;
360 tn->bits = bits;
Alexander Duycke9b44012014-12-31 10:56:12 -0800361 tn->key = (shift < KEYLENGTH) ? (key >> shift) << shift : 0;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800362 if (bits == KEYLENGTH)
363 tn->full_children = 1;
364 else
365 tn->empty_children = 1ul << bits;
Robert Olsson19baf832005-06-21 12:43:18 -0700366 }
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700367
Eric Dumazeta034ee32010-09-09 23:32:28 +0000368 pr_debug("AT %p s=%zu %zu\n", tn, sizeof(struct tnode),
Alexander Duyckadaf9812014-12-31 10:55:47 -0800369 sizeof(struct tnode *) << bits);
Robert Olsson19baf832005-06-21 12:43:18 -0700370 return tn;
371}
372
Alexander Duycke9b44012014-12-31 10:56:12 -0800373/* Check whether a tnode 'n' is "full", i.e. it is an internal node
Robert Olsson19baf832005-06-21 12:43:18 -0700374 * and no bits are skipped. See discussion in dyntree paper p. 6
375 */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800376static inline int tnode_full(const struct tnode *tn, const struct tnode *n)
Robert Olsson19baf832005-06-21 12:43:18 -0700377{
Alexander Duycke9b44012014-12-31 10:56:12 -0800378 return n && ((n->pos + n->bits) == tn->pos) && IS_TNODE(n);
Robert Olsson19baf832005-06-21 12:43:18 -0700379}
380
Alexander Duyckff181ed2014-12-31 10:56:43 -0800381/* Add a child at position i overwriting the old value.
382 * Update the value of full_children and empty_children.
383 */
384static void put_child(struct tnode *tn, unsigned long i, struct tnode *n)
Robert Olsson19baf832005-06-21 12:43:18 -0700385{
Alexander Duyck21d1f112014-12-31 10:57:02 -0800386 struct tnode *chi = tnode_get_child(tn, i);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800387 int isfull, wasfull;
Robert Olsson19baf832005-06-21 12:43:18 -0700388
Alexander Duyck98293e82014-12-31 10:56:18 -0800389 BUG_ON(i >= tnode_child_length(tn));
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700390
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800391 /* update emptyChildren, overflow into fullChildren */
Robert Olsson19baf832005-06-21 12:43:18 -0700392 if (n == NULL && chi != NULL)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800393 empty_child_inc(tn);
394 if (n != NULL && chi == NULL)
395 empty_child_dec(tn);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700396
Robert Olsson19baf832005-06-21 12:43:18 -0700397 /* update fullChildren */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800398 wasfull = tnode_full(tn, chi);
Robert Olsson19baf832005-06-21 12:43:18 -0700399 isfull = tnode_full(tn, n);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800400
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700401 if (wasfull && !isfull)
Robert Olsson19baf832005-06-21 12:43:18 -0700402 tn->full_children--;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700403 else if (!wasfull && isfull)
Robert Olsson19baf832005-06-21 12:43:18 -0700404 tn->full_children++;
Olof Johansson91b9a272005-08-09 20:24:39 -0700405
Alexander Duyck5405afd2014-12-31 10:57:08 -0800406 if (n && (tn->slen < n->slen))
407 tn->slen = n->slen;
408
Eric Dumazetcf778b02012-01-12 04:41:32 +0000409 rcu_assign_pointer(tn->child[i], n);
Robert Olsson19baf832005-06-21 12:43:18 -0700410}
411
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800412static void update_children(struct tnode *tn)
413{
414 unsigned long i;
415
416 /* update all of the child parent pointers */
417 for (i = tnode_child_length(tn); i;) {
418 struct tnode *inode = tnode_get_child(tn, --i);
419
420 if (!inode)
421 continue;
422
423 /* Either update the children of a tnode that
424 * already belongs to us or update the child
425 * to point to ourselves.
426 */
427 if (node_parent(inode) == tn)
428 update_children(inode);
429 else
430 node_set_parent(inode, tn);
431 }
432}
433
434static inline void put_child_root(struct tnode *tp, struct trie *t,
435 t_key key, struct tnode *n)
Alexander Duyck836a0122014-12-31 10:56:06 -0800436{
437 if (tp)
438 put_child(tp, get_index(key, tp), n);
439 else
440 rcu_assign_pointer(t->trie, n);
441}
442
Alexander Duyckfc86a932014-12-31 10:56:49 -0800443static inline void tnode_free_init(struct tnode *tn)
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700444{
Alexander Duyckfc86a932014-12-31 10:56:49 -0800445 tn->rcu.next = NULL;
446}
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700447
Alexander Duyckfc86a932014-12-31 10:56:49 -0800448static inline void tnode_free_append(struct tnode *tn, struct tnode *n)
449{
450 n->rcu.next = tn->rcu.next;
451 tn->rcu.next = &n->rcu;
452}
453
454static void tnode_free(struct tnode *tn)
455{
456 struct callback_head *head = &tn->rcu;
457
458 while (head) {
459 head = head->next;
460 tnode_free_size += offsetof(struct tnode, child[1 << tn->bits]);
461 node_free(tn);
462
463 tn = container_of(head, struct tnode, rcu);
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700464 }
Alexander Duyckfc86a932014-12-31 10:56:49 -0800465
466 if (tnode_free_size >= PAGE_SIZE * sync_pages) {
467 tnode_free_size = 0;
468 synchronize_rcu();
469 }
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700470}
471
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800472static void replace(struct trie *t, struct tnode *oldtnode, struct tnode *tn)
473{
474 struct tnode *tp = node_parent(oldtnode);
475 unsigned long i;
476
477 /* setup the parent pointer out of and back into this node */
478 NODE_INIT_PARENT(tn, tp);
479 put_child_root(tp, t, tn->key, tn);
480
481 /* update all of the child parent pointers */
482 update_children(tn);
483
484 /* all pointers should be clean so we are done */
485 tnode_free(oldtnode);
486
487 /* resize children now that oldtnode is freed */
488 for (i = tnode_child_length(tn); i;) {
489 struct tnode *inode = tnode_get_child(tn, --i);
490
491 /* resize child node */
492 if (tnode_full(tn, inode))
493 resize(t, inode);
494 }
495}
496
Alexander Duyckff181ed2014-12-31 10:56:43 -0800497static int inflate(struct trie *t, struct tnode *oldtnode)
Robert Olsson19baf832005-06-21 12:43:18 -0700498{
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800499 struct tnode *tn;
500 unsigned long i;
Alexander Duycke9b44012014-12-31 10:56:12 -0800501 t_key m;
Robert Olsson19baf832005-06-21 12:43:18 -0700502
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700503 pr_debug("In inflate\n");
Robert Olsson19baf832005-06-21 12:43:18 -0700504
Alexander Duycke9b44012014-12-31 10:56:12 -0800505 tn = tnode_new(oldtnode->key, oldtnode->pos - 1, oldtnode->bits + 1);
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700506 if (!tn)
Alexander Duyckff181ed2014-12-31 10:56:43 -0800507 return -ENOMEM;
Robert Olsson2f368952005-07-05 15:02:40 -0700508
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800509 /* prepare oldtnode to be freed */
510 tnode_free_init(oldtnode);
511
Alexander Duyck12c081a2014-12-31 10:56:55 -0800512 /* Assemble all of the pointers in our cluster, in this case that
513 * represents all of the pointers out of our allocated nodes that
514 * point to existing tnodes and the links between our allocated
515 * nodes.
Robert Olsson2f368952005-07-05 15:02:40 -0700516 */
Alexander Duyck12c081a2014-12-31 10:56:55 -0800517 for (i = tnode_child_length(oldtnode), m = 1u << tn->pos; i;) {
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800518 struct tnode *inode = tnode_get_child(oldtnode, --i);
519 struct tnode *node0, *node1;
520 unsigned long j, k;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700521
Robert Olsson19baf832005-06-21 12:43:18 -0700522 /* An empty child */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800523 if (inode == NULL)
Robert Olsson19baf832005-06-21 12:43:18 -0700524 continue;
525
526 /* A leaf or an internal node with skipped bits */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800527 if (!tnode_full(oldtnode, inode)) {
Alexander Duycke9b44012014-12-31 10:56:12 -0800528 put_child(tn, get_index(inode->key, tn), inode);
Robert Olsson19baf832005-06-21 12:43:18 -0700529 continue;
530 }
531
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800532 /* drop the node in the old tnode free list */
533 tnode_free_append(oldtnode, inode);
534
Robert Olsson19baf832005-06-21 12:43:18 -0700535 /* An internal node with two children */
Robert Olsson19baf832005-06-21 12:43:18 -0700536 if (inode->bits == 1) {
Alexander Duyck12c081a2014-12-31 10:56:55 -0800537 put_child(tn, 2 * i + 1, tnode_get_child(inode, 1));
538 put_child(tn, 2 * i, tnode_get_child(inode, 0));
Olof Johansson91b9a272005-08-09 20:24:39 -0700539 continue;
Robert Olsson19baf832005-06-21 12:43:18 -0700540 }
541
Olof Johansson91b9a272005-08-09 20:24:39 -0700542 /* We will replace this node 'inode' with two new
Alexander Duyck12c081a2014-12-31 10:56:55 -0800543 * ones, 'node0' and 'node1', each with half of the
Olof Johansson91b9a272005-08-09 20:24:39 -0700544 * original children. The two new nodes will have
545 * a position one bit further down the key and this
546 * means that the "significant" part of their keys
547 * (see the discussion near the top of this file)
548 * will differ by one bit, which will be "0" in
Alexander Duyck12c081a2014-12-31 10:56:55 -0800549 * node0's key and "1" in node1's key. Since we are
Olof Johansson91b9a272005-08-09 20:24:39 -0700550 * moving the key position by one step, the bit that
551 * we are moving away from - the bit at position
Alexander Duyck12c081a2014-12-31 10:56:55 -0800552 * (tn->pos) - is the one that will differ between
553 * node0 and node1. So... we synthesize that bit in the
554 * two new keys.
Olof Johansson91b9a272005-08-09 20:24:39 -0700555 */
Alexander Duyck12c081a2014-12-31 10:56:55 -0800556 node1 = tnode_new(inode->key | m, inode->pos, inode->bits - 1);
557 if (!node1)
558 goto nomem;
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800559 node0 = tnode_new(inode->key, inode->pos, inode->bits - 1);
Robert Olsson19baf832005-06-21 12:43:18 -0700560
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800561 tnode_free_append(tn, node1);
Alexander Duyck12c081a2014-12-31 10:56:55 -0800562 if (!node0)
563 goto nomem;
564 tnode_free_append(tn, node0);
Robert Olsson19baf832005-06-21 12:43:18 -0700565
Alexander Duyck12c081a2014-12-31 10:56:55 -0800566 /* populate child pointers in new nodes */
567 for (k = tnode_child_length(inode), j = k / 2; j;) {
568 put_child(node1, --j, tnode_get_child(inode, --k));
569 put_child(node0, j, tnode_get_child(inode, j));
570 put_child(node1, --j, tnode_get_child(inode, --k));
571 put_child(node0, j, tnode_get_child(inode, j));
Robert Olsson19baf832005-06-21 12:43:18 -0700572 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800573
Alexander Duyck12c081a2014-12-31 10:56:55 -0800574 /* link new nodes to parent */
575 NODE_INIT_PARENT(node1, tn);
576 NODE_INIT_PARENT(node0, tn);
Olof Johansson91b9a272005-08-09 20:24:39 -0700577
Alexander Duyck12c081a2014-12-31 10:56:55 -0800578 /* link parent to nodes */
579 put_child(tn, 2 * i + 1, node1);
580 put_child(tn, 2 * i, node0);
Robert Olsson19baf832005-06-21 12:43:18 -0700581 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800582
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800583 /* setup the parent pointers into and out of this node */
584 replace(t, oldtnode, tn);
Alexander Duyckfc86a932014-12-31 10:56:49 -0800585
Alexander Duyckff181ed2014-12-31 10:56:43 -0800586 return 0;
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700587nomem:
Alexander Duyckfc86a932014-12-31 10:56:49 -0800588 /* all pointers should be clean so we are done */
589 tnode_free(tn);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800590 return -ENOMEM;
Robert Olsson19baf832005-06-21 12:43:18 -0700591}
592
Alexander Duyckff181ed2014-12-31 10:56:43 -0800593static int halve(struct trie *t, struct tnode *oldtnode)
Robert Olsson19baf832005-06-21 12:43:18 -0700594{
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800595 struct tnode *tn;
Alexander Duyck12c081a2014-12-31 10:56:55 -0800596 unsigned long i;
Robert Olsson19baf832005-06-21 12:43:18 -0700597
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700598 pr_debug("In halve\n");
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700599
Alexander Duycke9b44012014-12-31 10:56:12 -0800600 tn = tnode_new(oldtnode->key, oldtnode->pos + 1, oldtnode->bits - 1);
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700601 if (!tn)
Alexander Duyckff181ed2014-12-31 10:56:43 -0800602 return -ENOMEM;
Robert Olsson2f368952005-07-05 15:02:40 -0700603
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800604 /* prepare oldtnode to be freed */
605 tnode_free_init(oldtnode);
606
Alexander Duyck12c081a2014-12-31 10:56:55 -0800607 /* Assemble all of the pointers in our cluster, in this case that
608 * represents all of the pointers out of our allocated nodes that
609 * point to existing tnodes and the links between our allocated
610 * nodes.
Robert Olsson2f368952005-07-05 15:02:40 -0700611 */
Alexander Duyck12c081a2014-12-31 10:56:55 -0800612 for (i = tnode_child_length(oldtnode); i;) {
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800613 struct tnode *node1 = tnode_get_child(oldtnode, --i);
614 struct tnode *node0 = tnode_get_child(oldtnode, --i);
615 struct tnode *inode;
Robert Olsson2f368952005-07-05 15:02:40 -0700616
Alexander Duyck12c081a2014-12-31 10:56:55 -0800617 /* At least one of the children is empty */
618 if (!node1 || !node0) {
619 put_child(tn, i / 2, node1 ? : node0);
620 continue;
Robert Olsson2f368952005-07-05 15:02:40 -0700621 }
Robert Olsson2f368952005-07-05 15:02:40 -0700622
Alexander Duyck12c081a2014-12-31 10:56:55 -0800623 /* Two nonempty children */
624 inode = tnode_new(node0->key, oldtnode->pos, 1);
625 if (!inode) {
626 tnode_free(tn);
627 return -ENOMEM;
628 }
629 tnode_free_append(tn, inode);
630
631 /* initialize pointers out of node */
632 put_child(inode, 1, node1);
633 put_child(inode, 0, node0);
634 NODE_INIT_PARENT(inode, tn);
635
636 /* link parent to node */
637 put_child(tn, i / 2, inode);
Robert Olsson2f368952005-07-05 15:02:40 -0700638 }
Robert Olsson19baf832005-06-21 12:43:18 -0700639
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800640 /* setup the parent pointers into and out of this node */
641 replace(t, oldtnode, tn);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800642
643 return 0;
Robert Olsson19baf832005-06-21 12:43:18 -0700644}
645
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800646static void collapse(struct trie *t, struct tnode *oldtnode)
647{
648 struct tnode *n, *tp;
649 unsigned long i;
650
651 /* scan the tnode looking for that one child that might still exist */
652 for (n = NULL, i = tnode_child_length(oldtnode); !n && i;)
653 n = tnode_get_child(oldtnode, --i);
654
655 /* compress one level */
656 tp = node_parent(oldtnode);
657 put_child_root(tp, t, oldtnode->key, n);
658 node_set_parent(n, tp);
659
660 /* drop dead node */
661 node_free(oldtnode);
662}
663
Alexander Duyck5405afd2014-12-31 10:57:08 -0800664static unsigned char update_suffix(struct tnode *tn)
665{
666 unsigned char slen = tn->pos;
667 unsigned long stride, i;
668
669 /* search though the list of children looking for nodes that might
670 * have a suffix greater than the one we currently have. This is
671 * why we start with a stride of 2 since a stride of 1 would
672 * represent the nodes with suffix length equal to tn->pos
673 */
674 for (i = 0, stride = 0x2ul ; i < tnode_child_length(tn); i += stride) {
675 struct tnode *n = tnode_get_child(tn, i);
676
677 if (!n || (n->slen <= slen))
678 continue;
679
680 /* update stride and slen based on new value */
681 stride <<= (n->slen - slen);
682 slen = n->slen;
683 i &= ~(stride - 1);
684
685 /* if slen covers all but the last bit we can stop here
686 * there will be nothing longer than that since only node
687 * 0 and 1 << (bits - 1) could have that as their suffix
688 * length.
689 */
690 if ((slen + 1) >= (tn->pos + tn->bits))
691 break;
692 }
693
694 tn->slen = slen;
695
696 return slen;
697}
698
Alexander Duyckf05a4812014-12-31 10:56:37 -0800699/* From "Implementing a dynamic compressed trie" by Stefan Nilsson of
700 * the Helsinki University of Technology and Matti Tikkanen of Nokia
701 * Telecommunications, page 6:
702 * "A node is doubled if the ratio of non-empty children to all
703 * children in the *doubled* node is at least 'high'."
704 *
705 * 'high' in this instance is the variable 'inflate_threshold'. It
706 * is expressed as a percentage, so we multiply it with
707 * tnode_child_length() and instead of multiplying by 2 (since the
708 * child array will be doubled by inflate()) and multiplying
709 * the left-hand side by 100 (to handle the percentage thing) we
710 * multiply the left-hand side by 50.
711 *
712 * The left-hand side may look a bit weird: tnode_child_length(tn)
713 * - tn->empty_children is of course the number of non-null children
714 * in the current node. tn->full_children is the number of "full"
715 * children, that is non-null tnodes with a skip value of 0.
716 * All of those will be doubled in the resulting inflated tnode, so
717 * we just count them one extra time here.
718 *
719 * A clearer way to write this would be:
720 *
721 * to_be_doubled = tn->full_children;
722 * not_to_be_doubled = tnode_child_length(tn) - tn->empty_children -
723 * tn->full_children;
724 *
725 * new_child_length = tnode_child_length(tn) * 2;
726 *
727 * new_fill_factor = 100 * (not_to_be_doubled + 2*to_be_doubled) /
728 * new_child_length;
729 * if (new_fill_factor >= inflate_threshold)
730 *
731 * ...and so on, tho it would mess up the while () loop.
732 *
733 * anyway,
734 * 100 * (not_to_be_doubled + 2*to_be_doubled) / new_child_length >=
735 * inflate_threshold
736 *
737 * avoid a division:
738 * 100 * (not_to_be_doubled + 2*to_be_doubled) >=
739 * inflate_threshold * new_child_length
740 *
741 * expand not_to_be_doubled and to_be_doubled, and shorten:
742 * 100 * (tnode_child_length(tn) - tn->empty_children +
743 * tn->full_children) >= inflate_threshold * new_child_length
744 *
745 * expand new_child_length:
746 * 100 * (tnode_child_length(tn) - tn->empty_children +
747 * tn->full_children) >=
748 * inflate_threshold * tnode_child_length(tn) * 2
749 *
750 * shorten again:
751 * 50 * (tn->full_children + tnode_child_length(tn) -
752 * tn->empty_children) >= inflate_threshold *
753 * tnode_child_length(tn)
754 *
755 */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800756static bool should_inflate(const struct tnode *tp, const struct tnode *tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800757{
758 unsigned long used = tnode_child_length(tn);
759 unsigned long threshold = used;
760
761 /* Keep root node larger */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800762 threshold *= tp ? inflate_threshold : inflate_threshold_root;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800763 used -= tn->empty_children;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800764 used += tn->full_children;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800765
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800766 /* if bits == KEYLENGTH then pos = 0, and will fail below */
767
768 return (used > 1) && tn->pos && ((50 * used) >= threshold);
Alexander Duyckf05a4812014-12-31 10:56:37 -0800769}
770
Alexander Duyckff181ed2014-12-31 10:56:43 -0800771static bool should_halve(const struct tnode *tp, const struct tnode *tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800772{
773 unsigned long used = tnode_child_length(tn);
774 unsigned long threshold = used;
775
776 /* Keep root node larger */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800777 threshold *= tp ? halve_threshold : halve_threshold_root;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800778 used -= tn->empty_children;
779
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800780 /* if bits == KEYLENGTH then used = 100% on wrap, and will fail below */
781
782 return (used > 1) && (tn->bits > 1) && ((100 * used) < threshold);
783}
784
785static bool should_collapse(const struct tnode *tn)
786{
787 unsigned long used = tnode_child_length(tn);
788
789 used -= tn->empty_children;
790
791 /* account for bits == KEYLENGTH case */
792 if ((tn->bits == KEYLENGTH) && tn->full_children)
793 used -= KEY_MAX;
794
795 /* One child or none, time to drop us from the trie */
796 return used < 2;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800797}
798
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800799#define MAX_WORK 10
Alexander Duyckff181ed2014-12-31 10:56:43 -0800800static void resize(struct trie *t, struct tnode *tn)
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800801{
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800802 struct tnode *tp = node_parent(tn);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800803 struct tnode __rcu **cptr;
Alexander Duycka80e89d2015-01-22 15:51:20 -0800804 int max_work = MAX_WORK;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800805
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800806 pr_debug("In tnode_resize %p inflate_threshold=%d threshold=%d\n",
807 tn, inflate_threshold, halve_threshold);
808
Alexander Duyckff181ed2014-12-31 10:56:43 -0800809 /* track the tnode via the pointer from the parent instead of
810 * doing it ourselves. This way we can let RCU fully do its
811 * thing without us interfering
812 */
813 cptr = tp ? &tp->child[get_index(tn->key, tp)] : &t->trie;
814 BUG_ON(tn != rtnl_dereference(*cptr));
815
Alexander Duyckf05a4812014-12-31 10:56:37 -0800816 /* Double as long as the resulting node has a number of
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800817 * nonempty nodes that are above the threshold.
818 */
Alexander Duycka80e89d2015-01-22 15:51:20 -0800819 while (should_inflate(tp, tn) && max_work) {
Alexander Duyckff181ed2014-12-31 10:56:43 -0800820 if (inflate(t, tn)) {
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800821#ifdef CONFIG_IP_FIB_TRIE_STATS
822 this_cpu_inc(t->stats->resize_node_skipped);
823#endif
824 break;
825 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800826
Alexander Duycka80e89d2015-01-22 15:51:20 -0800827 max_work--;
Alexander Duyckff181ed2014-12-31 10:56:43 -0800828 tn = rtnl_dereference(*cptr);
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800829 }
830
831 /* Return if at least one inflate is run */
832 if (max_work != MAX_WORK)
Alexander Duyckff181ed2014-12-31 10:56:43 -0800833 return;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800834
Alexander Duyckf05a4812014-12-31 10:56:37 -0800835 /* Halve as long as the number of empty children in this
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800836 * node is above threshold.
837 */
Alexander Duycka80e89d2015-01-22 15:51:20 -0800838 while (should_halve(tp, tn) && max_work) {
Alexander Duyckff181ed2014-12-31 10:56:43 -0800839 if (halve(t, tn)) {
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800840#ifdef CONFIG_IP_FIB_TRIE_STATS
841 this_cpu_inc(t->stats->resize_node_skipped);
842#endif
843 break;
844 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800845
Alexander Duycka80e89d2015-01-22 15:51:20 -0800846 max_work--;
Alexander Duyckff181ed2014-12-31 10:56:43 -0800847 tn = rtnl_dereference(*cptr);
848 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800849
850 /* Only one child remains */
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800851 if (should_collapse(tn)) {
852 collapse(t, tn);
Alexander Duyck5405afd2014-12-31 10:57:08 -0800853 return;
854 }
855
856 /* Return if at least one deflate was run */
857 if (max_work != MAX_WORK)
858 return;
859
860 /* push the suffix length to the parent node */
861 if (tn->slen > tn->pos) {
862 unsigned char slen = update_suffix(tn);
863
864 if (tp && (slen > tp->slen))
865 tp->slen = slen;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800866 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800867}
868
Robert Olsson772cb712005-09-19 15:31:18 -0700869/* readside must use rcu_read_lock currently dump routines
Robert Olsson2373ce12005-08-25 13:01:29 -0700870 via get_fa_head and dump */
871
Alexander Duyckadaf9812014-12-31 10:55:47 -0800872static struct leaf_info *find_leaf_info(struct tnode *l, int plen)
Robert Olsson19baf832005-06-21 12:43:18 -0700873{
Robert Olsson772cb712005-09-19 15:31:18 -0700874 struct hlist_head *head = &l->list;
Robert Olsson19baf832005-06-21 12:43:18 -0700875 struct leaf_info *li;
876
Sasha Levinb67bfe02013-02-27 17:06:00 -0800877 hlist_for_each_entry_rcu(li, head, hlist)
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700878 if (li->plen == plen)
Robert Olsson19baf832005-06-21 12:43:18 -0700879 return li;
Olof Johansson91b9a272005-08-09 20:24:39 -0700880
Robert Olsson19baf832005-06-21 12:43:18 -0700881 return NULL;
882}
883
Alexander Duyck56315f92015-02-25 15:31:31 -0800884static inline struct hlist_head *get_fa_head(struct tnode *l, int plen)
Robert Olsson19baf832005-06-21 12:43:18 -0700885{
Robert Olsson772cb712005-09-19 15:31:18 -0700886 struct leaf_info *li = find_leaf_info(l, plen);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700887
Olof Johansson91b9a272005-08-09 20:24:39 -0700888 if (!li)
889 return NULL;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700890
Olof Johansson91b9a272005-08-09 20:24:39 -0700891 return &li->falh;
Robert Olsson19baf832005-06-21 12:43:18 -0700892}
893
Alexander Duyck5405afd2014-12-31 10:57:08 -0800894static void leaf_pull_suffix(struct tnode *l)
Robert Olsson19baf832005-06-21 12:43:18 -0700895{
Alexander Duyck5405afd2014-12-31 10:57:08 -0800896 struct tnode *tp = node_parent(l);
897
898 while (tp && (tp->slen > tp->pos) && (tp->slen > l->slen)) {
899 if (update_suffix(tp) > l->slen)
900 break;
901 tp = node_parent(tp);
902 }
903}
904
905static void leaf_push_suffix(struct tnode *l)
906{
907 struct tnode *tn = node_parent(l);
908
909 /* if this is a new leaf then tn will be NULL and we can sort
910 * out parent suffix lengths as a part of trie_rebalance
911 */
912 while (tn && (tn->slen < l->slen)) {
913 tn->slen = l->slen;
914 tn = node_parent(tn);
915 }
916}
917
918static void remove_leaf_info(struct tnode *l, struct leaf_info *old)
919{
Alexander Duyck64c62722015-01-22 15:51:45 -0800920 /* record the location of the previous list_info entry */
921 struct hlist_node **pprev = old->hlist.pprev;
922 struct leaf_info *li = hlist_entry(pprev, typeof(*li), hlist.next);
Alexander Duyck5405afd2014-12-31 10:57:08 -0800923
924 /* remove the leaf info from the list */
925 hlist_del_rcu(&old->hlist);
926
Alexander Duyck64c62722015-01-22 15:51:45 -0800927 /* only access li if it is pointing at the last valid hlist_node */
928 if (hlist_empty(&l->list) || (*pprev))
Alexander Duyck5405afd2014-12-31 10:57:08 -0800929 return;
930
Alexander Duyck64c62722015-01-22 15:51:45 -0800931 /* update the trie with the latest suffix length */
932 l->slen = KEYLENGTH - li->plen;
933 leaf_pull_suffix(l);
Alexander Duyck5405afd2014-12-31 10:57:08 -0800934}
935
936static void insert_leaf_info(struct tnode *l, struct leaf_info *new)
937{
938 struct hlist_head *head = &l->list;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900939 struct leaf_info *li = NULL, *last = NULL;
Robert Olsson19baf832005-06-21 12:43:18 -0700940
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900941 if (hlist_empty(head)) {
942 hlist_add_head_rcu(&new->hlist, head);
943 } else {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800944 hlist_for_each_entry(li, head, hlist) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900945 if (new->plen > li->plen)
946 break;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700947
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900948 last = li;
949 }
950 if (last)
Ken Helias1d023282014-08-06 16:09:16 -0700951 hlist_add_behind_rcu(&new->hlist, &last->hlist);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900952 else
953 hlist_add_before_rcu(&new->hlist, &li->hlist);
954 }
Alexander Duyck5405afd2014-12-31 10:57:08 -0800955
956 /* if we added to the tail node then we need to update slen */
Alexander Duyck64c62722015-01-22 15:51:45 -0800957 if (l->slen < (KEYLENGTH - new->plen)) {
Alexander Duyck5405afd2014-12-31 10:57:08 -0800958 l->slen = KEYLENGTH - new->plen;
959 leaf_push_suffix(l);
960 }
Robert Olsson19baf832005-06-21 12:43:18 -0700961}
962
Robert Olsson2373ce12005-08-25 13:01:29 -0700963/* rcu_read_lock needs to be hold by caller from readside */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800964static struct tnode *fib_find_node(struct trie *t, u32 key)
Robert Olsson19baf832005-06-21 12:43:18 -0700965{
Alexander Duyckadaf9812014-12-31 10:55:47 -0800966 struct tnode *n = rcu_dereference_rtnl(t->trie);
Robert Olsson19baf832005-06-21 12:43:18 -0700967
Alexander Duyck939afb02014-12-31 10:56:00 -0800968 while (n) {
969 unsigned long index = get_index(key, n);
970
971 /* This bit of code is a bit tricky but it combines multiple
972 * checks into a single check. The prefix consists of the
973 * prefix plus zeros for the bits in the cindex. The index
974 * is the difference between the key and this value. From
975 * this we can actually derive several pieces of data.
Alexander Duyckb3832112015-01-22 15:51:08 -0800976 * if (index & (~0ul << bits))
Alexander Duyck939afb02014-12-31 10:56:00 -0800977 * we have a mismatch in skip bits and failed
Alexander Duyckb3832112015-01-22 15:51:08 -0800978 * else
979 * we know the value is cindex
Alexander Duyck939afb02014-12-31 10:56:00 -0800980 */
Alexander Duyckb3832112015-01-22 15:51:08 -0800981 if (index & (~0ul << n->bits))
Alexander Duyck939afb02014-12-31 10:56:00 -0800982 return NULL;
983
984 /* we have found a leaf. Prefixes have already been compared */
985 if (IS_LEAF(n))
Robert Olsson19baf832005-06-21 12:43:18 -0700986 break;
Alexander Duyck939afb02014-12-31 10:56:00 -0800987
Alexander Duyck21d1f112014-12-31 10:57:02 -0800988 n = tnode_get_child_rcu(n, index);
Robert Olsson19baf832005-06-21 12:43:18 -0700989 }
Robert Olsson19baf832005-06-21 12:43:18 -0700990
Alexander Duyck939afb02014-12-31 10:56:00 -0800991 return n;
Robert Olsson19baf832005-06-21 12:43:18 -0700992}
993
Alexander Duyck02525362015-01-22 15:51:39 -0800994/* Return the first fib alias matching TOS with
995 * priority less than or equal to PRIO.
996 */
Alexander Duyck56315f92015-02-25 15:31:31 -0800997static struct fib_alias *fib_find_alias(struct hlist_head *fah, u8 tos,
998 u32 prio)
Alexander Duyck02525362015-01-22 15:51:39 -0800999{
1000 struct fib_alias *fa;
1001
1002 if (!fah)
1003 return NULL;
1004
Alexander Duyck56315f92015-02-25 15:31:31 -08001005 hlist_for_each_entry(fa, fah, fa_list) {
Alexander Duyck02525362015-01-22 15:51:39 -08001006 if (fa->fa_tos > tos)
1007 continue;
1008 if (fa->fa_info->fib_priority >= prio || fa->fa_tos < tos)
1009 return fa;
1010 }
1011
1012 return NULL;
1013}
1014
Jarek Poplawski7b855762009-06-18 00:28:51 -07001015static void trie_rebalance(struct trie *t, struct tnode *tn)
Robert Olsson19baf832005-06-21 12:43:18 -07001016{
Stephen Hemminger06801912007-08-10 15:22:13 -07001017 struct tnode *tp;
Robert Olsson19baf832005-06-21 12:43:18 -07001018
Alexander Duyckff181ed2014-12-31 10:56:43 -08001019 while ((tp = node_parent(tn)) != NULL) {
1020 resize(t, tn);
Stephen Hemminger06801912007-08-10 15:22:13 -07001021 tn = tp;
Robert Olsson19baf832005-06-21 12:43:18 -07001022 }
Stephen Hemminger06801912007-08-10 15:22:13 -07001023
Robert Olsson19baf832005-06-21 12:43:18 -07001024 /* Handle last (top) tnode */
Jarek Poplawski7b855762009-06-18 00:28:51 -07001025 if (IS_TNODE(tn))
Alexander Duyckff181ed2014-12-31 10:56:43 -08001026 resize(t, tn);
Robert Olsson19baf832005-06-21 12:43:18 -07001027}
1028
Robert Olsson2373ce12005-08-25 13:01:29 -07001029/* only used from updater-side */
1030
Alexander Duyck56315f92015-02-25 15:31:31 -08001031static struct hlist_head *fib_insert_node(struct trie *t, u32 key, int plen)
Robert Olsson19baf832005-06-21 12:43:18 -07001032{
Alexander Duyck56315f92015-02-25 15:31:31 -08001033 struct hlist_head *fa_head = NULL;
Alexander Duyck836a0122014-12-31 10:56:06 -08001034 struct tnode *l, *n, *tp = NULL;
Robert Olsson19baf832005-06-21 12:43:18 -07001035 struct leaf_info *li;
Robert Olsson19baf832005-06-21 12:43:18 -07001036
Alexander Duyck836a0122014-12-31 10:56:06 -08001037 li = leaf_info_new(plen);
1038 if (!li)
1039 return NULL;
1040 fa_head = &li->falh;
1041
Eric Dumazet0a5c0472011-03-31 01:51:35 -07001042 n = rtnl_dereference(t->trie);
Robert Olsson19baf832005-06-21 12:43:18 -07001043
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001044 /* If we point to NULL, stop. Either the tree is empty and we should
1045 * just put a new leaf in if, or we have reached an empty child slot,
Robert Olsson19baf832005-06-21 12:43:18 -07001046 * and we should just put our new leaf in that.
Robert Olsson19baf832005-06-21 12:43:18 -07001047 *
Alexander Duyck836a0122014-12-31 10:56:06 -08001048 * If we hit a node with a key that does't match then we should stop
1049 * and create a new tnode to replace that node and insert ourselves
1050 * and the other node into the new tnode.
Robert Olsson19baf832005-06-21 12:43:18 -07001051 */
Alexander Duyck836a0122014-12-31 10:56:06 -08001052 while (n) {
1053 unsigned long index = get_index(key, n);
Robert Olsson19baf832005-06-21 12:43:18 -07001054
Alexander Duyck836a0122014-12-31 10:56:06 -08001055 /* This bit of code is a bit tricky but it combines multiple
1056 * checks into a single check. The prefix consists of the
1057 * prefix plus zeros for the "bits" in the prefix. The index
1058 * is the difference between the key and this value. From
1059 * this we can actually derive several pieces of data.
1060 * if !(index >> bits)
1061 * we know the value is child index
1062 * else
1063 * we have a mismatch in skip bits and failed
Robert Olsson19baf832005-06-21 12:43:18 -07001064 */
Alexander Duyck836a0122014-12-31 10:56:06 -08001065 if (index >> n->bits)
1066 break;
Robert Olsson19baf832005-06-21 12:43:18 -07001067
Alexander Duyck836a0122014-12-31 10:56:06 -08001068 /* we have found a leaf. Prefixes have already been compared */
1069 if (IS_LEAF(n)) {
1070 /* Case 1: n is a leaf, and prefixes match*/
Alexander Duyck5405afd2014-12-31 10:57:08 -08001071 insert_leaf_info(n, li);
Alexander Duyck836a0122014-12-31 10:56:06 -08001072 return fa_head;
Robert Olsson19baf832005-06-21 12:43:18 -07001073 }
Robert Olsson19baf832005-06-21 12:43:18 -07001074
Alexander Duyck836a0122014-12-31 10:56:06 -08001075 tp = n;
Alexander Duyck21d1f112014-12-31 10:57:02 -08001076 n = tnode_get_child_rcu(n, index);
Alexander Duyck836a0122014-12-31 10:56:06 -08001077 }
1078
1079 l = leaf_new(key);
1080 if (!l) {
1081 free_leaf_info(li);
1082 return NULL;
1083 }
1084
Alexander Duyck5405afd2014-12-31 10:57:08 -08001085 insert_leaf_info(l, li);
Alexander Duyck836a0122014-12-31 10:56:06 -08001086
1087 /* Case 2: n is a LEAF or a TNODE and the key doesn't match.
1088 *
1089 * Add a new tnode here
1090 * first tnode need some special handling
1091 * leaves us in position for handling as case 3
1092 */
1093 if (n) {
1094 struct tnode *tn;
Alexander Duyck836a0122014-12-31 10:56:06 -08001095
Alexander Duycke9b44012014-12-31 10:56:12 -08001096 tn = tnode_new(key, __fls(key ^ n->key), 1);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001097 if (!tn) {
Robert Olssonf835e472005-06-28 15:00:39 -07001098 free_leaf_info(li);
Alexander Duyck37fd30f2014-12-31 10:55:41 -08001099 node_free(l);
Stephen Hemmingerfea86ad2008-01-12 20:57:07 -08001100 return NULL;
Olof Johansson91b9a272005-08-09 20:24:39 -07001101 }
1102
Alexander Duyck836a0122014-12-31 10:56:06 -08001103 /* initialize routes out of node */
1104 NODE_INIT_PARENT(tn, tp);
1105 put_child(tn, get_index(key, tn) ^ 1, n);
Robert Olsson19baf832005-06-21 12:43:18 -07001106
Alexander Duyck836a0122014-12-31 10:56:06 -08001107 /* start adding routes into the node */
1108 put_child_root(tp, t, key, tn);
1109 node_set_parent(n, tn);
Robert Olsson19baf832005-06-21 12:43:18 -07001110
Alexander Duyck836a0122014-12-31 10:56:06 -08001111 /* parent now has a NULL spot where the leaf can go */
Alexander Duycke962f302014-12-10 21:49:22 -08001112 tp = tn;
Robert Olsson19baf832005-06-21 12:43:18 -07001113 }
Olof Johansson91b9a272005-08-09 20:24:39 -07001114
Alexander Duyck836a0122014-12-31 10:56:06 -08001115 /* Case 3: n is NULL, and will just insert a new leaf */
1116 if (tp) {
1117 NODE_INIT_PARENT(l, tp);
1118 put_child(tp, get_index(key, tp), l);
1119 trie_rebalance(t, tp);
1120 } else {
1121 rcu_assign_pointer(t->trie, l);
1122 }
Olof Johansson91b9a272005-08-09 20:24:39 -07001123
Robert Olsson19baf832005-06-21 12:43:18 -07001124 return fa_head;
1125}
1126
Robert Olssond562f1f2007-03-26 14:22:22 -07001127/*
1128 * Caller must hold RTNL.
1129 */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001130int fib_table_insert(struct fib_table *tb, struct fib_config *cfg)
Robert Olsson19baf832005-06-21 12:43:18 -07001131{
1132 struct trie *t = (struct trie *) tb->tb_data;
1133 struct fib_alias *fa, *new_fa;
Alexander Duyck56315f92015-02-25 15:31:31 -08001134 struct hlist_head *fa_head = NULL;
Robert Olsson19baf832005-06-21 12:43:18 -07001135 struct fib_info *fi;
Thomas Graf4e902c52006-08-17 18:14:52 -07001136 int plen = cfg->fc_dst_len;
1137 u8 tos = cfg->fc_tos;
Robert Olsson19baf832005-06-21 12:43:18 -07001138 u32 key, mask;
1139 int err;
Alexander Duyckadaf9812014-12-31 10:55:47 -08001140 struct tnode *l;
Robert Olsson19baf832005-06-21 12:43:18 -07001141
1142 if (plen > 32)
1143 return -EINVAL;
1144
Thomas Graf4e902c52006-08-17 18:14:52 -07001145 key = ntohl(cfg->fc_dst);
Robert Olsson19baf832005-06-21 12:43:18 -07001146
Patrick McHardy2dfe55b2006-08-10 23:08:33 -07001147 pr_debug("Insert table=%u %08x/%d\n", tb->tb_id, key, plen);
Robert Olsson19baf832005-06-21 12:43:18 -07001148
Olof Johansson91b9a272005-08-09 20:24:39 -07001149 mask = ntohl(inet_make_mask(plen));
Robert Olsson19baf832005-06-21 12:43:18 -07001150
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001151 if (key & ~mask)
Robert Olsson19baf832005-06-21 12:43:18 -07001152 return -EINVAL;
1153
1154 key = key & mask;
1155
Thomas Graf4e902c52006-08-17 18:14:52 -07001156 fi = fib_create_info(cfg);
1157 if (IS_ERR(fi)) {
1158 err = PTR_ERR(fi);
Robert Olsson19baf832005-06-21 12:43:18 -07001159 goto err;
Thomas Graf4e902c52006-08-17 18:14:52 -07001160 }
Robert Olsson19baf832005-06-21 12:43:18 -07001161
1162 l = fib_find_node(t, key);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001163 fa = NULL;
Robert Olsson19baf832005-06-21 12:43:18 -07001164
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001165 if (l) {
Robert Olsson19baf832005-06-21 12:43:18 -07001166 fa_head = get_fa_head(l, plen);
1167 fa = fib_find_alias(fa_head, tos, fi->fib_priority);
1168 }
1169
1170 /* Now fa, if non-NULL, points to the first fib alias
1171 * with the same keys [prefix,tos,priority], if such key already
1172 * exists or to the node before which we will insert new one.
1173 *
1174 * If fa is NULL, we will need to allocate a new one and
Alexander Duyck56315f92015-02-25 15:31:31 -08001175 * insert to the tail of the section matching the suffix length
1176 * of the new alias.
Robert Olsson19baf832005-06-21 12:43:18 -07001177 */
1178
Julian Anastasov936f6f82008-01-28 21:18:06 -08001179 if (fa && fa->fa_tos == tos &&
1180 fa->fa_info->fib_priority == fi->fib_priority) {
1181 struct fib_alias *fa_first, *fa_match;
Robert Olsson19baf832005-06-21 12:43:18 -07001182
1183 err = -EEXIST;
Thomas Graf4e902c52006-08-17 18:14:52 -07001184 if (cfg->fc_nlflags & NLM_F_EXCL)
Robert Olsson19baf832005-06-21 12:43:18 -07001185 goto out;
1186
Julian Anastasov936f6f82008-01-28 21:18:06 -08001187 /* We have 2 goals:
1188 * 1. Find exact match for type, scope, fib_info to avoid
1189 * duplicate routes
1190 * 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it
1191 */
1192 fa_match = NULL;
1193 fa_first = fa;
Alexander Duyck56315f92015-02-25 15:31:31 -08001194 hlist_for_each_entry_from(fa, fa_list) {
Julian Anastasov936f6f82008-01-28 21:18:06 -08001195 if (fa->fa_tos != tos)
1196 break;
1197 if (fa->fa_info->fib_priority != fi->fib_priority)
1198 break;
1199 if (fa->fa_type == cfg->fc_type &&
Julian Anastasov936f6f82008-01-28 21:18:06 -08001200 fa->fa_info == fi) {
1201 fa_match = fa;
1202 break;
1203 }
1204 }
1205
Thomas Graf4e902c52006-08-17 18:14:52 -07001206 if (cfg->fc_nlflags & NLM_F_REPLACE) {
Robert Olsson19baf832005-06-21 12:43:18 -07001207 struct fib_info *fi_drop;
1208 u8 state;
1209
Julian Anastasov936f6f82008-01-28 21:18:06 -08001210 fa = fa_first;
1211 if (fa_match) {
1212 if (fa == fa_match)
1213 err = 0;
Joonwoo Park67250332008-01-18 03:45:18 -08001214 goto out;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001215 }
Robert Olsson2373ce12005-08-25 13:01:29 -07001216 err = -ENOBUFS;
Christoph Lametere94b1762006-12-06 20:33:17 -08001217 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
Robert Olsson2373ce12005-08-25 13:01:29 -07001218 if (new_fa == NULL)
1219 goto out;
Robert Olsson19baf832005-06-21 12:43:18 -07001220
1221 fi_drop = fa->fa_info;
Robert Olsson2373ce12005-08-25 13:01:29 -07001222 new_fa->fa_tos = fa->fa_tos;
1223 new_fa->fa_info = fi;
Thomas Graf4e902c52006-08-17 18:14:52 -07001224 new_fa->fa_type = cfg->fc_type;
Robert Olsson19baf832005-06-21 12:43:18 -07001225 state = fa->fa_state;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001226 new_fa->fa_state = state & ~FA_S_ACCESSED;
Robert Olsson19baf832005-06-21 12:43:18 -07001227
Alexander Duyck56315f92015-02-25 15:31:31 -08001228 hlist_replace_rcu(&fa->fa_list, &new_fa->fa_list);
Robert Olsson2373ce12005-08-25 13:01:29 -07001229 alias_free_mem_rcu(fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001230
1231 fib_release_info(fi_drop);
1232 if (state & FA_S_ACCESSED)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001233 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Milan Kocianb8f55832007-05-23 14:55:06 -07001234 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen,
1235 tb->tb_id, &cfg->fc_nlinfo, NLM_F_REPLACE);
Robert Olsson19baf832005-06-21 12:43:18 -07001236
Olof Johansson91b9a272005-08-09 20:24:39 -07001237 goto succeeded;
Robert Olsson19baf832005-06-21 12:43:18 -07001238 }
1239 /* Error if we find a perfect match which
1240 * uses the same scope, type, and nexthop
1241 * information.
1242 */
Julian Anastasov936f6f82008-01-28 21:18:06 -08001243 if (fa_match)
1244 goto out;
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001245
Thomas Graf4e902c52006-08-17 18:14:52 -07001246 if (!(cfg->fc_nlflags & NLM_F_APPEND))
Julian Anastasov936f6f82008-01-28 21:18:06 -08001247 fa = fa_first;
Robert Olsson19baf832005-06-21 12:43:18 -07001248 }
1249 err = -ENOENT;
Thomas Graf4e902c52006-08-17 18:14:52 -07001250 if (!(cfg->fc_nlflags & NLM_F_CREATE))
Robert Olsson19baf832005-06-21 12:43:18 -07001251 goto out;
1252
1253 err = -ENOBUFS;
Christoph Lametere94b1762006-12-06 20:33:17 -08001254 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
Robert Olsson19baf832005-06-21 12:43:18 -07001255 if (new_fa == NULL)
1256 goto out;
1257
1258 new_fa->fa_info = fi;
1259 new_fa->fa_tos = tos;
Thomas Graf4e902c52006-08-17 18:14:52 -07001260 new_fa->fa_type = cfg->fc_type;
Robert Olsson19baf832005-06-21 12:43:18 -07001261 new_fa->fa_state = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001262 /*
1263 * Insert new entry to the list.
1264 */
1265
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001266 if (!fa_head) {
Stephen Hemmingerfea86ad2008-01-12 20:57:07 -08001267 fa_head = fib_insert_node(t, key, plen);
1268 if (unlikely(!fa_head)) {
1269 err = -ENOMEM;
Robert Olssonf835e472005-06-28 15:00:39 -07001270 goto out_free_new_fa;
Stephen Hemmingerfea86ad2008-01-12 20:57:07 -08001271 }
Robert Olssonf835e472005-06-28 15:00:39 -07001272 }
Robert Olsson19baf832005-06-21 12:43:18 -07001273
David S. Miller21d8c492011-04-14 14:49:37 -07001274 if (!plen)
1275 tb->tb_num_default++;
1276
Alexander Duyck56315f92015-02-25 15:31:31 -08001277 if (fa) {
1278 hlist_add_before_rcu(&new_fa->fa_list, &fa->fa_list);
1279 } else {
1280 struct fib_alias *last;
1281
1282 hlist_for_each_entry(last, fa_head, fa_list)
1283 fa = last;
1284
1285 if (fa)
1286 hlist_add_behind_rcu(&new_fa->fa_list, &fa->fa_list);
1287 else
1288 hlist_add_head_rcu(&new_fa->fa_list, fa_head);
1289 }
Robert Olsson19baf832005-06-21 12:43:18 -07001290
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001291 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Thomas Graf4e902c52006-08-17 18:14:52 -07001292 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen, tb->tb_id,
Milan Kocianb8f55832007-05-23 14:55:06 -07001293 &cfg->fc_nlinfo, 0);
Robert Olsson19baf832005-06-21 12:43:18 -07001294succeeded:
1295 return 0;
Robert Olssonf835e472005-06-28 15:00:39 -07001296
1297out_free_new_fa:
1298 kmem_cache_free(fn_alias_kmem, new_fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001299out:
1300 fib_release_info(fi);
Olof Johansson91b9a272005-08-09 20:24:39 -07001301err:
Robert Olsson19baf832005-06-21 12:43:18 -07001302 return err;
1303}
1304
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001305static inline t_key prefix_mismatch(t_key key, struct tnode *n)
1306{
1307 t_key prefix = n->key;
1308
1309 return (key ^ prefix) & (prefix | -prefix);
1310}
1311
Alexander Duyck345e9b52014-12-31 10:56:24 -08001312/* should be called with rcu_read_lock */
David S. Miller22bd5b92011-03-11 19:54:08 -05001313int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00001314 struct fib_result *res, int fib_flags)
Robert Olsson19baf832005-06-21 12:43:18 -07001315{
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001316 struct trie *t = (struct trie *)tb->tb_data;
Alexander Duyck8274a972014-12-31 10:55:29 -08001317#ifdef CONFIG_IP_FIB_TRIE_STATS
1318 struct trie_use_stats __percpu *stats = t->stats;
1319#endif
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001320 const t_key key = ntohl(flp->daddr);
1321 struct tnode *n, *pn;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001322 struct leaf_info *li;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001323 t_key cindex;
Robert Olsson19baf832005-06-21 12:43:18 -07001324
Robert Olsson2373ce12005-08-25 13:01:29 -07001325 n = rcu_dereference(t->trie);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001326 if (!n)
Alexander Duyck345e9b52014-12-31 10:56:24 -08001327 return -EAGAIN;
Robert Olsson19baf832005-06-21 12:43:18 -07001328
1329#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -08001330 this_cpu_inc(stats->gets);
Robert Olsson19baf832005-06-21 12:43:18 -07001331#endif
1332
Alexander Duyckadaf9812014-12-31 10:55:47 -08001333 pn = n;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001334 cindex = 0;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001335
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001336 /* Step 1: Travel to the longest prefix match in the trie */
1337 for (;;) {
1338 unsigned long index = get_index(key, n);
Robert Olsson19baf832005-06-21 12:43:18 -07001339
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001340 /* This bit of code is a bit tricky but it combines multiple
1341 * checks into a single check. The prefix consists of the
1342 * prefix plus zeros for the "bits" in the prefix. The index
1343 * is the difference between the key and this value. From
1344 * this we can actually derive several pieces of data.
Alexander Duyckb3832112015-01-22 15:51:08 -08001345 * if (index & (~0ul << bits))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001346 * we have a mismatch in skip bits and failed
Alexander Duyckb3832112015-01-22 15:51:08 -08001347 * else
1348 * we know the value is cindex
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001349 */
Alexander Duyckb3832112015-01-22 15:51:08 -08001350 if (index & (~0ul << n->bits))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001351 break;
Robert Olsson19baf832005-06-21 12:43:18 -07001352
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001353 /* we have found a leaf. Prefixes have already been compared */
1354 if (IS_LEAF(n))
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001355 goto found;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001356
1357 /* only record pn and cindex if we are going to be chopping
1358 * bits later. Otherwise we are just wasting cycles.
1359 */
Alexander Duyck5405afd2014-12-31 10:57:08 -08001360 if (n->slen > n->pos) {
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001361 pn = n;
1362 cindex = index;
Olof Johansson91b9a272005-08-09 20:24:39 -07001363 }
1364
Alexander Duyck21d1f112014-12-31 10:57:02 -08001365 n = tnode_get_child_rcu(n, index);
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001366 if (unlikely(!n))
Robert Olsson19baf832005-06-21 12:43:18 -07001367 goto backtrace;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001368 }
1369
1370 /* Step 2: Sort out leaves and begin backtracing for longest prefix */
1371 for (;;) {
1372 /* record the pointer where our next node pointer is stored */
1373 struct tnode __rcu **cptr = n->child;
1374
1375 /* This test verifies that none of the bits that differ
1376 * between the key and the prefix exist in the region of
1377 * the lsb and higher in the prefix.
1378 */
Alexander Duyck5405afd2014-12-31 10:57:08 -08001379 if (unlikely(prefix_mismatch(key, n)) || (n->slen == n->pos))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001380 goto backtrace;
1381
1382 /* exit out and process leaf */
1383 if (unlikely(IS_LEAF(n)))
1384 break;
1385
1386 /* Don't bother recording parent info. Since we are in
1387 * prefix match mode we will have to come back to wherever
1388 * we started this traversal anyway
1389 */
1390
1391 while ((n = rcu_dereference(*cptr)) == NULL) {
1392backtrace:
1393#ifdef CONFIG_IP_FIB_TRIE_STATS
1394 if (!n)
1395 this_cpu_inc(stats->null_node_hit);
1396#endif
1397 /* If we are at cindex 0 there are no more bits for
1398 * us to strip at this level so we must ascend back
1399 * up one level to see if there are any more bits to
1400 * be stripped there.
1401 */
1402 while (!cindex) {
1403 t_key pkey = pn->key;
1404
1405 pn = node_parent_rcu(pn);
1406 if (unlikely(!pn))
Alexander Duyck345e9b52014-12-31 10:56:24 -08001407 return -EAGAIN;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001408#ifdef CONFIG_IP_FIB_TRIE_STATS
1409 this_cpu_inc(stats->backtrack);
1410#endif
1411 /* Get Child's index */
1412 cindex = get_index(pkey, pn);
1413 }
1414
1415 /* strip the least significant bit from the cindex */
1416 cindex &= cindex - 1;
1417
1418 /* grab pointer for next child node */
1419 cptr = &pn->child[cindex];
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001420 }
Robert Olsson19baf832005-06-21 12:43:18 -07001421 }
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001422
Robert Olsson19baf832005-06-21 12:43:18 -07001423found:
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001424 /* Step 3: Process the leaf, if that fails fall back to backtracing */
Alexander Duyck345e9b52014-12-31 10:56:24 -08001425 hlist_for_each_entry_rcu(li, &n->list, hlist) {
1426 struct fib_alias *fa;
1427
1428 if ((key ^ n->key) & li->mask_plen)
1429 continue;
1430
Alexander Duyck56315f92015-02-25 15:31:31 -08001431 hlist_for_each_entry_rcu(fa, &li->falh, fa_list) {
Alexander Duyck345e9b52014-12-31 10:56:24 -08001432 struct fib_info *fi = fa->fa_info;
1433 int nhsel, err;
1434
1435 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
1436 continue;
1437 if (fi->fib_dead)
1438 continue;
1439 if (fa->fa_info->fib_scope < flp->flowi4_scope)
1440 continue;
1441 fib_alias_accessed(fa);
1442 err = fib_props[fa->fa_type].error;
1443 if (unlikely(err < 0)) {
1444#ifdef CONFIG_IP_FIB_TRIE_STATS
1445 this_cpu_inc(stats->semantic_match_passed);
1446#endif
1447 return err;
1448 }
1449 if (fi->fib_flags & RTNH_F_DEAD)
1450 continue;
1451 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
1452 const struct fib_nh *nh = &fi->fib_nh[nhsel];
1453
1454 if (nh->nh_flags & RTNH_F_DEAD)
1455 continue;
1456 if (flp->flowi4_oif && flp->flowi4_oif != nh->nh_oif)
1457 continue;
1458
1459 if (!(fib_flags & FIB_LOOKUP_NOREF))
1460 atomic_inc(&fi->fib_clntref);
1461
1462 res->prefixlen = li->plen;
1463 res->nh_sel = nhsel;
1464 res->type = fa->fa_type;
1465 res->scope = fi->fib_scope;
1466 res->fi = fi;
1467 res->table = tb;
1468 res->fa_head = &li->falh;
1469#ifdef CONFIG_IP_FIB_TRIE_STATS
1470 this_cpu_inc(stats->semantic_match_passed);
1471#endif
1472 return err;
1473 }
1474 }
1475
1476#ifdef CONFIG_IP_FIB_TRIE_STATS
1477 this_cpu_inc(stats->semantic_match_miss);
1478#endif
1479 }
1480 goto backtrace;
Robert Olsson19baf832005-06-21 12:43:18 -07001481}
Florian Westphal6fc01432011-08-25 13:46:12 +02001482EXPORT_SYMBOL_GPL(fib_table_lookup);
Robert Olsson19baf832005-06-21 12:43:18 -07001483
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001484/*
1485 * Remove the leaf and return parent.
1486 */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001487static void trie_leaf_remove(struct trie *t, struct tnode *l)
Robert Olsson19baf832005-06-21 12:43:18 -07001488{
Alexander Duyck64c9b6f2014-12-31 10:55:35 -08001489 struct tnode *tp = node_parent(l);
Robert Olsson19baf832005-06-21 12:43:18 -07001490
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001491 pr_debug("entering trie_leaf_remove(%p)\n", l);
Robert Olsson19baf832005-06-21 12:43:18 -07001492
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001493 if (tp) {
Alexander Duyck836a0122014-12-31 10:56:06 -08001494 put_child(tp, get_index(l->key, tp), NULL);
Jarek Poplawski7b855762009-06-18 00:28:51 -07001495 trie_rebalance(t, tp);
Alexander Duyck836a0122014-12-31 10:56:06 -08001496 } else {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001497 RCU_INIT_POINTER(t->trie, NULL);
Alexander Duyck836a0122014-12-31 10:56:06 -08001498 }
Robert Olsson19baf832005-06-21 12:43:18 -07001499
Alexander Duyck37fd30f2014-12-31 10:55:41 -08001500 node_free(l);
Robert Olsson19baf832005-06-21 12:43:18 -07001501}
1502
Robert Olssond562f1f2007-03-26 14:22:22 -07001503/*
1504 * Caller must hold RTNL.
1505 */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001506int fib_table_delete(struct fib_table *tb, struct fib_config *cfg)
Robert Olsson19baf832005-06-21 12:43:18 -07001507{
1508 struct trie *t = (struct trie *) tb->tb_data;
1509 u32 key, mask;
Thomas Graf4e902c52006-08-17 18:14:52 -07001510 int plen = cfg->fc_dst_len;
1511 u8 tos = cfg->fc_tos;
Robert Olsson19baf832005-06-21 12:43:18 -07001512 struct fib_alias *fa, *fa_to_delete;
Alexander Duyck56315f92015-02-25 15:31:31 -08001513 struct hlist_head *fa_head;
Alexander Duyckadaf9812014-12-31 10:55:47 -08001514 struct tnode *l;
Olof Johansson91b9a272005-08-09 20:24:39 -07001515 struct leaf_info *li;
1516
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001517 if (plen > 32)
Robert Olsson19baf832005-06-21 12:43:18 -07001518 return -EINVAL;
1519
Thomas Graf4e902c52006-08-17 18:14:52 -07001520 key = ntohl(cfg->fc_dst);
Olof Johansson91b9a272005-08-09 20:24:39 -07001521 mask = ntohl(inet_make_mask(plen));
Robert Olsson19baf832005-06-21 12:43:18 -07001522
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001523 if (key & ~mask)
Robert Olsson19baf832005-06-21 12:43:18 -07001524 return -EINVAL;
1525
1526 key = key & mask;
1527 l = fib_find_node(t, key);
1528
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001529 if (!l)
Robert Olsson19baf832005-06-21 12:43:18 -07001530 return -ESRCH;
1531
Igor Maravicad5b3102012-08-13 10:26:08 +02001532 li = find_leaf_info(l, plen);
1533
1534 if (!li)
1535 return -ESRCH;
1536
1537 fa_head = &li->falh;
Robert Olsson19baf832005-06-21 12:43:18 -07001538 fa = fib_find_alias(fa_head, tos, 0);
1539
1540 if (!fa)
1541 return -ESRCH;
1542
Stephen Hemminger0c7770c2005-08-23 21:59:41 -07001543 pr_debug("Deleting %08x/%d tos=%d t=%p\n", key, plen, tos, t);
Robert Olsson19baf832005-06-21 12:43:18 -07001544
1545 fa_to_delete = NULL;
Alexander Duyck56315f92015-02-25 15:31:31 -08001546 hlist_for_each_entry_from(fa, fa_list) {
Robert Olsson19baf832005-06-21 12:43:18 -07001547 struct fib_info *fi = fa->fa_info;
1548
1549 if (fa->fa_tos != tos)
1550 break;
1551
Thomas Graf4e902c52006-08-17 18:14:52 -07001552 if ((!cfg->fc_type || fa->fa_type == cfg->fc_type) &&
1553 (cfg->fc_scope == RT_SCOPE_NOWHERE ||
David S. Miller37e826c2011-03-24 18:06:47 -07001554 fa->fa_info->fib_scope == cfg->fc_scope) &&
Julian Anastasov74cb3c12011-03-19 12:13:46 +00001555 (!cfg->fc_prefsrc ||
1556 fi->fib_prefsrc == cfg->fc_prefsrc) &&
Thomas Graf4e902c52006-08-17 18:14:52 -07001557 (!cfg->fc_protocol ||
1558 fi->fib_protocol == cfg->fc_protocol) &&
1559 fib_nh_match(cfg, fi) == 0) {
Robert Olsson19baf832005-06-21 12:43:18 -07001560 fa_to_delete = fa;
1561 break;
1562 }
1563 }
1564
Olof Johansson91b9a272005-08-09 20:24:39 -07001565 if (!fa_to_delete)
1566 return -ESRCH;
Robert Olsson19baf832005-06-21 12:43:18 -07001567
Olof Johansson91b9a272005-08-09 20:24:39 -07001568 fa = fa_to_delete;
Thomas Graf4e902c52006-08-17 18:14:52 -07001569 rtmsg_fib(RTM_DELROUTE, htonl(key), fa, plen, tb->tb_id,
Milan Kocianb8f55832007-05-23 14:55:06 -07001570 &cfg->fc_nlinfo, 0);
Robert Olsson19baf832005-06-21 12:43:18 -07001571
Alexander Duyck56315f92015-02-25 15:31:31 -08001572 hlist_del_rcu(&fa->fa_list);
Robert Olsson19baf832005-06-21 12:43:18 -07001573
David S. Miller21d8c492011-04-14 14:49:37 -07001574 if (!plen)
1575 tb->tb_num_default--;
1576
Alexander Duyck56315f92015-02-25 15:31:31 -08001577 if (hlist_empty(fa_head)) {
Alexander Duyck5405afd2014-12-31 10:57:08 -08001578 remove_leaf_info(l, li);
Olof Johansson91b9a272005-08-09 20:24:39 -07001579 free_leaf_info(li);
Robert Olsson2373ce12005-08-25 13:01:29 -07001580 }
Olof Johansson91b9a272005-08-09 20:24:39 -07001581
1582 if (hlist_empty(&l->list))
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001583 trie_leaf_remove(t, l);
Olof Johansson91b9a272005-08-09 20:24:39 -07001584
1585 if (fa->fa_state & FA_S_ACCESSED)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001586 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Olof Johansson91b9a272005-08-09 20:24:39 -07001587
Robert Olsson2373ce12005-08-25 13:01:29 -07001588 fib_release_info(fa->fa_info);
1589 alias_free_mem_rcu(fa);
Olof Johansson91b9a272005-08-09 20:24:39 -07001590 return 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001591}
1592
Alexander Duyck56315f92015-02-25 15:31:31 -08001593static int trie_flush_list(struct hlist_head *head)
Robert Olsson19baf832005-06-21 12:43:18 -07001594{
Alexander Duyck56315f92015-02-25 15:31:31 -08001595 struct hlist_node *tmp;
1596 struct fib_alias *fa;
Robert Olsson19baf832005-06-21 12:43:18 -07001597 int found = 0;
1598
Alexander Duyck56315f92015-02-25 15:31:31 -08001599 hlist_for_each_entry_safe(fa, tmp, head, fa_list) {
Robert Olsson19baf832005-06-21 12:43:18 -07001600 struct fib_info *fi = fa->fa_info;
Robert Olsson19baf832005-06-21 12:43:18 -07001601
Robert Olsson2373ce12005-08-25 13:01:29 -07001602 if (fi && (fi->fib_flags & RTNH_F_DEAD)) {
Alexander Duyck56315f92015-02-25 15:31:31 -08001603 hlist_del_rcu(&fa->fa_list);
Robert Olsson2373ce12005-08-25 13:01:29 -07001604 fib_release_info(fa->fa_info);
1605 alias_free_mem_rcu(fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001606 found++;
1607 }
1608 }
1609 return found;
1610}
1611
Alexander Duyckadaf9812014-12-31 10:55:47 -08001612static int trie_flush_leaf(struct tnode *l)
Robert Olsson19baf832005-06-21 12:43:18 -07001613{
1614 int found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001615 struct hlist_node *tmp;
Alexander Duyck56315f92015-02-25 15:31:31 -08001616 struct leaf_info *li;
Alexander Duyck64c62722015-01-22 15:51:45 -08001617 unsigned char plen = KEYLENGTH;
Robert Olsson19baf832005-06-21 12:43:18 -07001618
Alexander Duyck56315f92015-02-25 15:31:31 -08001619 hlist_for_each_entry_safe(li, tmp, &l->list, hlist) {
Stephen Hemmingeref3660c2008-04-10 03:46:12 -07001620 found += trie_flush_list(&li->falh);
Robert Olsson19baf832005-06-21 12:43:18 -07001621
Alexander Duyck56315f92015-02-25 15:31:31 -08001622 if (hlist_empty(&li->falh)) {
Robert Olsson2373ce12005-08-25 13:01:29 -07001623 hlist_del_rcu(&li->hlist);
Robert Olsson19baf832005-06-21 12:43:18 -07001624 free_leaf_info(li);
Alexander Duyck64c62722015-01-22 15:51:45 -08001625 continue;
Robert Olsson19baf832005-06-21 12:43:18 -07001626 }
Alexander Duyck64c62722015-01-22 15:51:45 -08001627
1628 plen = li->plen;
Robert Olsson19baf832005-06-21 12:43:18 -07001629 }
Alexander Duyck64c62722015-01-22 15:51:45 -08001630
1631 l->slen = KEYLENGTH - plen;
1632
Robert Olsson19baf832005-06-21 12:43:18 -07001633 return found;
1634}
1635
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001636/*
1637 * Scan for the next right leaf starting at node p->child[idx]
1638 * Since we have back pointer, no recursion necessary.
1639 */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001640static struct tnode *leaf_walk_rcu(struct tnode *p, struct tnode *c)
Robert Olsson19baf832005-06-21 12:43:18 -07001641{
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001642 do {
Alexander Duyck98293e82014-12-31 10:56:18 -08001643 unsigned long idx = c ? idx = get_index(c->key, p) + 1 : 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001644
Alexander Duyck98293e82014-12-31 10:56:18 -08001645 while (idx < tnode_child_length(p)) {
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001646 c = tnode_get_child_rcu(p, idx++);
Robert Olsson2373ce12005-08-25 13:01:29 -07001647 if (!c)
Olof Johansson91b9a272005-08-09 20:24:39 -07001648 continue;
Robert Olsson19baf832005-06-21 12:43:18 -07001649
Eric Dumazetaab515d2013-08-05 11:18:49 -07001650 if (IS_LEAF(c))
Alexander Duyckadaf9812014-12-31 10:55:47 -08001651 return c;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001652
1653 /* Rescan start scanning in new node */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001654 p = c;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001655 idx = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001656 }
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001657
1658 /* Node empty, walk back up to parent */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001659 c = p;
Eric Dumazeta034ee32010-09-09 23:32:28 +00001660 } while ((p = node_parent_rcu(c)) != NULL);
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001661
1662 return NULL; /* Root of trie */
1663}
1664
Alexander Duyckadaf9812014-12-31 10:55:47 -08001665static struct tnode *trie_firstleaf(struct trie *t)
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001666{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001667 struct tnode *n = rcu_dereference_rtnl(t->trie);
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001668
1669 if (!n)
1670 return NULL;
1671
1672 if (IS_LEAF(n)) /* trie is just a leaf */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001673 return n;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001674
1675 return leaf_walk_rcu(n, NULL);
1676}
1677
Alexander Duyckadaf9812014-12-31 10:55:47 -08001678static struct tnode *trie_nextleaf(struct tnode *l)
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001679{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001680 struct tnode *p = node_parent_rcu(l);
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001681
1682 if (!p)
1683 return NULL; /* trie with just one leaf */
1684
Alexander Duyckadaf9812014-12-31 10:55:47 -08001685 return leaf_walk_rcu(p, l);
Robert Olsson19baf832005-06-21 12:43:18 -07001686}
1687
Alexander Duyckadaf9812014-12-31 10:55:47 -08001688static struct tnode *trie_leafindex(struct trie *t, int index)
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001689{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001690 struct tnode *l = trie_firstleaf(t);
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001691
Stephen Hemmingerec28cf72008-02-11 21:12:49 -08001692 while (l && index-- > 0)
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001693 l = trie_nextleaf(l);
Stephen Hemmingerec28cf72008-02-11 21:12:49 -08001694
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001695 return l;
1696}
1697
1698
Robert Olssond562f1f2007-03-26 14:22:22 -07001699/*
1700 * Caller must hold RTNL.
1701 */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001702int fib_table_flush(struct fib_table *tb)
Robert Olsson19baf832005-06-21 12:43:18 -07001703{
1704 struct trie *t = (struct trie *) tb->tb_data;
Alexander Duyckadaf9812014-12-31 10:55:47 -08001705 struct tnode *l, *ll = NULL;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001706 int found = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001707
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001708 for (l = trie_firstleaf(t); l; l = trie_nextleaf(l)) {
Stephen Hemmingeref3660c2008-04-10 03:46:12 -07001709 found += trie_flush_leaf(l);
Robert Olsson19baf832005-06-21 12:43:18 -07001710
Alexander Duyck64c62722015-01-22 15:51:45 -08001711 if (ll) {
1712 if (hlist_empty(&ll->list))
1713 trie_leaf_remove(t, ll);
1714 else
1715 leaf_pull_suffix(ll);
1716 }
1717
Robert Olsson19baf832005-06-21 12:43:18 -07001718 ll = l;
1719 }
1720
Alexander Duyck64c62722015-01-22 15:51:45 -08001721 if (ll) {
1722 if (hlist_empty(&ll->list))
1723 trie_leaf_remove(t, ll);
1724 else
1725 leaf_pull_suffix(ll);
1726 }
Robert Olsson19baf832005-06-21 12:43:18 -07001727
Stephen Hemminger0c7770c2005-08-23 21:59:41 -07001728 pr_debug("trie_flush found=%d\n", found);
Robert Olsson19baf832005-06-21 12:43:18 -07001729 return found;
1730}
1731
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00001732void fib_free_table(struct fib_table *tb)
1733{
Alexander Duyck8274a972014-12-31 10:55:29 -08001734#ifdef CONFIG_IP_FIB_TRIE_STATS
1735 struct trie *t = (struct trie *)tb->tb_data;
1736
1737 free_percpu(t->stats);
1738#endif /* CONFIG_IP_FIB_TRIE_STATS */
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00001739 kfree(tb);
1740}
1741
Alexander Duyck56315f92015-02-25 15:31:31 -08001742static int fn_trie_dump_fa(t_key key, int plen, struct hlist_head *fah,
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001743 struct fib_table *tb,
Robert Olsson19baf832005-06-21 12:43:18 -07001744 struct sk_buff *skb, struct netlink_callback *cb)
1745{
1746 int i, s_i;
1747 struct fib_alias *fa;
Al Viro32ab5f82006-09-26 22:21:45 -07001748 __be32 xkey = htonl(key);
Robert Olsson19baf832005-06-21 12:43:18 -07001749
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001750 s_i = cb->args[5];
Robert Olsson19baf832005-06-21 12:43:18 -07001751 i = 0;
1752
Robert Olsson2373ce12005-08-25 13:01:29 -07001753 /* rcu_read_lock is hold by caller */
1754
Alexander Duyck56315f92015-02-25 15:31:31 -08001755 hlist_for_each_entry_rcu(fa, fah, fa_list) {
Robert Olsson19baf832005-06-21 12:43:18 -07001756 if (i < s_i) {
1757 i++;
1758 continue;
1759 }
Robert Olsson19baf832005-06-21 12:43:18 -07001760
Eric W. Biederman15e47302012-09-07 20:12:54 +00001761 if (fib_dump_info(skb, NETLINK_CB(cb->skb).portid,
Robert Olsson19baf832005-06-21 12:43:18 -07001762 cb->nlh->nlmsg_seq,
1763 RTM_NEWROUTE,
1764 tb->tb_id,
1765 fa->fa_type,
Thomas Grafbe403ea2006-08-17 18:15:17 -07001766 xkey,
Robert Olsson19baf832005-06-21 12:43:18 -07001767 plen,
1768 fa->fa_tos,
Stephen Hemminger64347f72008-01-22 21:55:01 -08001769 fa->fa_info, NLM_F_MULTI) < 0) {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001770 cb->args[5] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001771 return -1;
Olof Johansson91b9a272005-08-09 20:24:39 -07001772 }
Robert Olsson19baf832005-06-21 12:43:18 -07001773 i++;
1774 }
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001775 cb->args[5] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001776 return skb->len;
1777}
1778
Alexander Duyckadaf9812014-12-31 10:55:47 -08001779static int fn_trie_dump_leaf(struct tnode *l, struct fib_table *tb,
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001780 struct sk_buff *skb, struct netlink_callback *cb)
Robert Olsson19baf832005-06-21 12:43:18 -07001781{
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001782 struct leaf_info *li;
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001783 int i, s_i;
Robert Olsson19baf832005-06-21 12:43:18 -07001784
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001785 s_i = cb->args[4];
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001786 i = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001787
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001788 /* rcu_read_lock is hold by caller */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001789 hlist_for_each_entry_rcu(li, &l->list, hlist) {
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001790 if (i < s_i) {
1791 i++;
Robert Olsson19baf832005-06-21 12:43:18 -07001792 continue;
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001793 }
Robert Olsson19baf832005-06-21 12:43:18 -07001794
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001795 if (i > s_i)
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001796 cb->args[5] = 0;
Olof Johansson91b9a272005-08-09 20:24:39 -07001797
Alexander Duyck56315f92015-02-25 15:31:31 -08001798 if (hlist_empty(&li->falh))
Robert Olsson19baf832005-06-21 12:43:18 -07001799 continue;
1800
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001801 if (fn_trie_dump_fa(l->key, li->plen, &li->falh, tb, skb, cb) < 0) {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001802 cb->args[4] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001803 return -1;
1804 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001805 i++;
Robert Olsson19baf832005-06-21 12:43:18 -07001806 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001807
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001808 cb->args[4] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001809 return skb->len;
1810}
1811
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001812int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
1813 struct netlink_callback *cb)
Robert Olsson19baf832005-06-21 12:43:18 -07001814{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001815 struct tnode *l;
Robert Olsson19baf832005-06-21 12:43:18 -07001816 struct trie *t = (struct trie *) tb->tb_data;
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001817 t_key key = cb->args[2];
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001818 int count = cb->args[3];
Robert Olsson19baf832005-06-21 12:43:18 -07001819
Robert Olsson2373ce12005-08-25 13:01:29 -07001820 rcu_read_lock();
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001821 /* Dump starting at last key.
1822 * Note: 0.0.0.0/0 (ie default) is first key.
1823 */
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001824 if (count == 0)
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001825 l = trie_firstleaf(t);
1826 else {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001827 /* Normally, continue from last key, but if that is missing
1828 * fallback to using slow rescan
1829 */
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001830 l = fib_find_node(t, key);
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001831 if (!l)
1832 l = trie_leafindex(t, count);
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001833 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001834
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001835 while (l) {
1836 cb->args[2] = l->key;
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001837 if (fn_trie_dump_leaf(l, tb, skb, cb) < 0) {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001838 cb->args[3] = count;
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001839 rcu_read_unlock();
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001840 return -1;
Robert Olsson19baf832005-06-21 12:43:18 -07001841 }
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001842
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001843 ++count;
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001844 l = trie_nextleaf(l);
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001845 memset(&cb->args[4], 0,
1846 sizeof(cb->args) - 4*sizeof(cb->args[0]));
Robert Olsson19baf832005-06-21 12:43:18 -07001847 }
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001848 cb->args[3] = count;
Robert Olsson2373ce12005-08-25 13:01:29 -07001849 rcu_read_unlock();
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001850
Robert Olsson19baf832005-06-21 12:43:18 -07001851 return skb->len;
Robert Olsson19baf832005-06-21 12:43:18 -07001852}
1853
David S. Miller5348ba82011-02-01 15:30:56 -08001854void __init fib_trie_init(void)
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001855{
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001856 fn_alias_kmem = kmem_cache_create("ip_fib_alias",
1857 sizeof(struct fib_alias),
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -08001858 0, SLAB_PANIC, NULL);
1859
1860 trie_leaf_kmem = kmem_cache_create("ip_fib_trie",
Alexander Duyckadaf9812014-12-31 10:55:47 -08001861 max(sizeof(struct tnode),
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -08001862 sizeof(struct leaf_info)),
1863 0, SLAB_PANIC, NULL);
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001864}
Robert Olsson19baf832005-06-21 12:43:18 -07001865
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001866
David S. Miller5348ba82011-02-01 15:30:56 -08001867struct fib_table *fib_trie_table(u32 id)
Robert Olsson19baf832005-06-21 12:43:18 -07001868{
1869 struct fib_table *tb;
1870 struct trie *t;
1871
Robert Olsson19baf832005-06-21 12:43:18 -07001872 tb = kmalloc(sizeof(struct fib_table) + sizeof(struct trie),
1873 GFP_KERNEL);
1874 if (tb == NULL)
1875 return NULL;
1876
1877 tb->tb_id = id;
Denis V. Lunev971b8932007-12-08 00:32:23 -08001878 tb->tb_default = -1;
David S. Miller21d8c492011-04-14 14:49:37 -07001879 tb->tb_num_default = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001880
1881 t = (struct trie *) tb->tb_data;
Alexander Duyck8274a972014-12-31 10:55:29 -08001882 RCU_INIT_POINTER(t->trie, NULL);
1883#ifdef CONFIG_IP_FIB_TRIE_STATS
1884 t->stats = alloc_percpu(struct trie_use_stats);
1885 if (!t->stats) {
1886 kfree(tb);
1887 tb = NULL;
1888 }
1889#endif
Robert Olsson19baf832005-06-21 12:43:18 -07001890
Robert Olsson19baf832005-06-21 12:43:18 -07001891 return tb;
1892}
1893
Robert Olsson19baf832005-06-21 12:43:18 -07001894#ifdef CONFIG_PROC_FS
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001895/* Depth first Trie walk iterator */
1896struct fib_trie_iter {
Denis V. Lunev1c340b22008-01-10 03:27:17 -08001897 struct seq_net_private p;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001898 struct fib_table *tb;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001899 struct tnode *tnode;
Eric Dumazeta034ee32010-09-09 23:32:28 +00001900 unsigned int index;
1901 unsigned int depth;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001902};
Robert Olsson19baf832005-06-21 12:43:18 -07001903
Alexander Duyckadaf9812014-12-31 10:55:47 -08001904static struct tnode *fib_trie_get_next(struct fib_trie_iter *iter)
Robert Olsson19baf832005-06-21 12:43:18 -07001905{
Alexander Duyck98293e82014-12-31 10:56:18 -08001906 unsigned long cindex = iter->index;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001907 struct tnode *tn = iter->tnode;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001908 struct tnode *p;
1909
Eric W. Biederman6640e692007-01-24 14:42:04 -08001910 /* A single entry routing table */
1911 if (!tn)
1912 return NULL;
1913
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001914 pr_debug("get_next iter={node=%p index=%d depth=%d}\n",
1915 iter->tnode, iter->index, iter->depth);
1916rescan:
Alexander Duyck98293e82014-12-31 10:56:18 -08001917 while (cindex < tnode_child_length(tn)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08001918 struct tnode *n = tnode_get_child_rcu(tn, cindex);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001919
1920 if (n) {
1921 if (IS_LEAF(n)) {
1922 iter->tnode = tn;
1923 iter->index = cindex + 1;
1924 } else {
1925 /* push down one level */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001926 iter->tnode = n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001927 iter->index = 0;
1928 ++iter->depth;
1929 }
1930 return n;
1931 }
1932
1933 ++cindex;
1934 }
1935
1936 /* Current node exhausted, pop back up */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001937 p = node_parent_rcu(tn);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001938 if (p) {
Alexander Duycke9b44012014-12-31 10:56:12 -08001939 cindex = get_index(tn->key, p) + 1;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001940 tn = p;
1941 --iter->depth;
1942 goto rescan;
1943 }
1944
1945 /* got root? */
Robert Olsson19baf832005-06-21 12:43:18 -07001946 return NULL;
1947}
1948
Alexander Duyckadaf9812014-12-31 10:55:47 -08001949static struct tnode *fib_trie_get_first(struct fib_trie_iter *iter,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001950 struct trie *t)
Robert Olsson19baf832005-06-21 12:43:18 -07001951{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001952 struct tnode *n;
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08001953
Stephen Hemminger132adf52007-03-08 20:44:43 -08001954 if (!t)
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08001955 return NULL;
1956
1957 n = rcu_dereference(t->trie);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001958 if (!n)
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08001959 return NULL;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001960
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001961 if (IS_TNODE(n)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08001962 iter->tnode = n;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001963 iter->index = 0;
1964 iter->depth = 1;
1965 } else {
1966 iter->tnode = NULL;
1967 iter->index = 0;
1968 iter->depth = 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001969 }
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001970
1971 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07001972}
1973
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001974static void trie_collect_stats(struct trie *t, struct trie_stat *s)
Robert Olsson19baf832005-06-21 12:43:18 -07001975{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001976 struct tnode *n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001977 struct fib_trie_iter iter;
Robert Olsson19baf832005-06-21 12:43:18 -07001978
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001979 memset(s, 0, sizeof(*s));
Robert Olsson19baf832005-06-21 12:43:18 -07001980
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001981 rcu_read_lock();
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001982 for (n = fib_trie_get_first(&iter, t); n; n = fib_trie_get_next(&iter)) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001983 if (IS_LEAF(n)) {
Stephen Hemminger93672292008-01-22 21:54:05 -08001984 struct leaf_info *li;
Stephen Hemminger93672292008-01-22 21:54:05 -08001985
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001986 s->leaves++;
1987 s->totdepth += iter.depth;
1988 if (iter.depth > s->maxdepth)
1989 s->maxdepth = iter.depth;
Stephen Hemminger93672292008-01-22 21:54:05 -08001990
Alexander Duyckadaf9812014-12-31 10:55:47 -08001991 hlist_for_each_entry_rcu(li, &n->list, hlist)
Stephen Hemminger93672292008-01-22 21:54:05 -08001992 ++s->prefixes;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001993 } else {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001994 s->tnodes++;
Alexander Duyckadaf9812014-12-31 10:55:47 -08001995 if (n->bits < MAX_STAT_DEPTH)
1996 s->nodesizes[n->bits]++;
Alexander Duyck30cfe7c2015-01-22 15:51:33 -08001997 s->nullpointers += n->empty_children;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001998 }
1999 }
2000 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07002001}
2002
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07002003/*
Robert Olsson19baf832005-06-21 12:43:18 -07002004 * This outputs /proc/net/fib_triestats
Robert Olsson19baf832005-06-21 12:43:18 -07002005 */
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002006static void trie_show_stats(struct seq_file *seq, struct trie_stat *stat)
Robert Olsson19baf832005-06-21 12:43:18 -07002007{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002008 unsigned int i, max, pointers, bytes, avdepth;
Robert Olsson19baf832005-06-21 12:43:18 -07002009
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002010 if (stat->leaves)
2011 avdepth = stat->totdepth*100 / stat->leaves;
2012 else
2013 avdepth = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07002014
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002015 seq_printf(seq, "\tAver depth: %u.%02d\n",
2016 avdepth / 100, avdepth % 100);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002017 seq_printf(seq, "\tMax depth: %u\n", stat->maxdepth);
Robert Olsson19baf832005-06-21 12:43:18 -07002018
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002019 seq_printf(seq, "\tLeaves: %u\n", stat->leaves);
Alexander Duyckadaf9812014-12-31 10:55:47 -08002020 bytes = sizeof(struct tnode) * stat->leaves;
Stephen Hemminger93672292008-01-22 21:54:05 -08002021
2022 seq_printf(seq, "\tPrefixes: %u\n", stat->prefixes);
2023 bytes += sizeof(struct leaf_info) * stat->prefixes;
2024
Stephen Hemminger187b5182008-01-12 20:55:55 -08002025 seq_printf(seq, "\tInternal nodes: %u\n\t", stat->tnodes);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002026 bytes += sizeof(struct tnode) * stat->tnodes;
Robert Olsson19baf832005-06-21 12:43:18 -07002027
Robert Olsson06ef9212006-03-20 21:35:01 -08002028 max = MAX_STAT_DEPTH;
2029 while (max > 0 && stat->nodesizes[max-1] == 0)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002030 max--;
Robert Olsson19baf832005-06-21 12:43:18 -07002031
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002032 pointers = 0;
Jerry Snitselaarf585a992013-07-22 12:01:58 -07002033 for (i = 1; i < max; i++)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002034 if (stat->nodesizes[i] != 0) {
Stephen Hemminger187b5182008-01-12 20:55:55 -08002035 seq_printf(seq, " %u: %u", i, stat->nodesizes[i]);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002036 pointers += (1<<i) * stat->nodesizes[i];
2037 }
2038 seq_putc(seq, '\n');
Stephen Hemminger187b5182008-01-12 20:55:55 -08002039 seq_printf(seq, "\tPointers: %u\n", pointers);
Robert Olsson19baf832005-06-21 12:43:18 -07002040
Alexander Duyckadaf9812014-12-31 10:55:47 -08002041 bytes += sizeof(struct tnode *) * pointers;
Stephen Hemminger187b5182008-01-12 20:55:55 -08002042 seq_printf(seq, "Null ptrs: %u\n", stat->nullpointers);
2043 seq_printf(seq, "Total size: %u kB\n", (bytes + 1023) / 1024);
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002044}
Robert Olsson19baf832005-06-21 12:43:18 -07002045
2046#ifdef CONFIG_IP_FIB_TRIE_STATS
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002047static void trie_show_usage(struct seq_file *seq,
Alexander Duyck8274a972014-12-31 10:55:29 -08002048 const struct trie_use_stats __percpu *stats)
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002049{
Alexander Duyck8274a972014-12-31 10:55:29 -08002050 struct trie_use_stats s = { 0 };
2051 int cpu;
2052
2053 /* loop through all of the CPUs and gather up the stats */
2054 for_each_possible_cpu(cpu) {
2055 const struct trie_use_stats *pcpu = per_cpu_ptr(stats, cpu);
2056
2057 s.gets += pcpu->gets;
2058 s.backtrack += pcpu->backtrack;
2059 s.semantic_match_passed += pcpu->semantic_match_passed;
2060 s.semantic_match_miss += pcpu->semantic_match_miss;
2061 s.null_node_hit += pcpu->null_node_hit;
2062 s.resize_node_skipped += pcpu->resize_node_skipped;
2063 }
2064
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002065 seq_printf(seq, "\nCounters:\n---------\n");
Alexander Duyck8274a972014-12-31 10:55:29 -08002066 seq_printf(seq, "gets = %u\n", s.gets);
2067 seq_printf(seq, "backtracks = %u\n", s.backtrack);
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002068 seq_printf(seq, "semantic match passed = %u\n",
Alexander Duyck8274a972014-12-31 10:55:29 -08002069 s.semantic_match_passed);
2070 seq_printf(seq, "semantic match miss = %u\n", s.semantic_match_miss);
2071 seq_printf(seq, "null node hit= %u\n", s.null_node_hit);
2072 seq_printf(seq, "skipped node resize = %u\n\n", s.resize_node_skipped);
Robert Olsson19baf832005-06-21 12:43:18 -07002073}
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002074#endif /* CONFIG_IP_FIB_TRIE_STATS */
2075
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002076static void fib_table_print(struct seq_file *seq, struct fib_table *tb)
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002077{
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002078 if (tb->tb_id == RT_TABLE_LOCAL)
2079 seq_puts(seq, "Local:\n");
2080 else if (tb->tb_id == RT_TABLE_MAIN)
2081 seq_puts(seq, "Main:\n");
2082 else
2083 seq_printf(seq, "Id %d:\n", tb->tb_id);
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002084}
Robert Olsson19baf832005-06-21 12:43:18 -07002085
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002086
Robert Olsson19baf832005-06-21 12:43:18 -07002087static int fib_triestat_seq_show(struct seq_file *seq, void *v)
2088{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002089 struct net *net = (struct net *)seq->private;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002090 unsigned int h;
Eric W. Biederman877a9bf2007-12-07 00:47:47 -08002091
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002092 seq_printf(seq,
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002093 "Basic info: size of leaf:"
2094 " %Zd bytes, size of tnode: %Zd bytes.\n",
Alexander Duyckadaf9812014-12-31 10:55:47 -08002095 sizeof(struct tnode), sizeof(struct tnode));
Olof Johansson91b9a272005-08-09 20:24:39 -07002096
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002097 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2098 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002099 struct fib_table *tb;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002100
Sasha Levinb67bfe02013-02-27 17:06:00 -08002101 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002102 struct trie *t = (struct trie *) tb->tb_data;
2103 struct trie_stat stat;
2104
2105 if (!t)
2106 continue;
2107
2108 fib_table_print(seq, tb);
2109
2110 trie_collect_stats(t, &stat);
2111 trie_show_stats(seq, &stat);
2112#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -08002113 trie_show_usage(seq, t->stats);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002114#endif
2115 }
2116 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002117
Robert Olsson19baf832005-06-21 12:43:18 -07002118 return 0;
2119}
2120
Robert Olsson19baf832005-06-21 12:43:18 -07002121static int fib_triestat_seq_open(struct inode *inode, struct file *file)
2122{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002123 return single_open_net(inode, file, fib_triestat_seq_show);
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002124}
2125
Arjan van de Ven9a321442007-02-12 00:55:35 -08002126static const struct file_operations fib_triestat_fops = {
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07002127 .owner = THIS_MODULE,
2128 .open = fib_triestat_seq_open,
2129 .read = seq_read,
2130 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002131 .release = single_release_net,
Robert Olsson19baf832005-06-21 12:43:18 -07002132};
2133
Alexander Duyckadaf9812014-12-31 10:55:47 -08002134static struct tnode *fib_trie_get_idx(struct seq_file *seq, loff_t pos)
Robert Olsson19baf832005-06-21 12:43:18 -07002135{
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002136 struct fib_trie_iter *iter = seq->private;
2137 struct net *net = seq_file_net(seq);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002138 loff_t idx = 0;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002139 unsigned int h;
Robert Olsson19baf832005-06-21 12:43:18 -07002140
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002141 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2142 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002143 struct fib_table *tb;
2144
Sasha Levinb67bfe02013-02-27 17:06:00 -08002145 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002146 struct tnode *n;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002147
2148 for (n = fib_trie_get_first(iter,
2149 (struct trie *) tb->tb_data);
2150 n; n = fib_trie_get_next(iter))
2151 if (pos == idx++) {
2152 iter->tb = tb;
2153 return n;
2154 }
2155 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002156 }
Robert Olsson19baf832005-06-21 12:43:18 -07002157
Robert Olsson19baf832005-06-21 12:43:18 -07002158 return NULL;
2159}
2160
2161static void *fib_trie_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08002162 __acquires(RCU)
Robert Olsson19baf832005-06-21 12:43:18 -07002163{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002164 rcu_read_lock();
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002165 return fib_trie_get_idx(seq, *pos);
Robert Olsson19baf832005-06-21 12:43:18 -07002166}
2167
2168static void *fib_trie_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2169{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002170 struct fib_trie_iter *iter = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002171 struct net *net = seq_file_net(seq);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002172 struct fib_table *tb = iter->tb;
2173 struct hlist_node *tb_node;
2174 unsigned int h;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002175 struct tnode *n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002176
Robert Olsson19baf832005-06-21 12:43:18 -07002177 ++*pos;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002178 /* next node in same table */
2179 n = fib_trie_get_next(iter);
2180 if (n)
2181 return n;
Olof Johansson91b9a272005-08-09 20:24:39 -07002182
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002183 /* walk rest of this hash chain */
2184 h = tb->tb_id & (FIB_TABLE_HASHSZ - 1);
Eric Dumazet0a5c0472011-03-31 01:51:35 -07002185 while ((tb_node = rcu_dereference(hlist_next_rcu(&tb->tb_hlist)))) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002186 tb = hlist_entry(tb_node, struct fib_table, tb_hlist);
2187 n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
2188 if (n)
2189 goto found;
2190 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002191
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002192 /* new hash chain */
2193 while (++h < FIB_TABLE_HASHSZ) {
2194 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002195 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002196 n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
2197 if (n)
2198 goto found;
2199 }
2200 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002201 return NULL;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002202
2203found:
2204 iter->tb = tb;
2205 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07002206}
2207
2208static void fib_trie_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08002209 __releases(RCU)
Robert Olsson19baf832005-06-21 12:43:18 -07002210{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002211 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07002212}
2213
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002214static void seq_indent(struct seq_file *seq, int n)
2215{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002216 while (n-- > 0)
2217 seq_puts(seq, " ");
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002218}
Robert Olsson19baf832005-06-21 12:43:18 -07002219
Eric Dumazet28d36e32008-01-14 23:09:56 -08002220static inline const char *rtn_scope(char *buf, size_t len, enum rt_scope_t s)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002221{
Stephen Hemminger132adf52007-03-08 20:44:43 -08002222 switch (s) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002223 case RT_SCOPE_UNIVERSE: return "universe";
2224 case RT_SCOPE_SITE: return "site";
2225 case RT_SCOPE_LINK: return "link";
2226 case RT_SCOPE_HOST: return "host";
2227 case RT_SCOPE_NOWHERE: return "nowhere";
2228 default:
Eric Dumazet28d36e32008-01-14 23:09:56 -08002229 snprintf(buf, len, "scope=%d", s);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002230 return buf;
2231 }
2232}
2233
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -07002234static const char *const rtn_type_names[__RTN_MAX] = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002235 [RTN_UNSPEC] = "UNSPEC",
2236 [RTN_UNICAST] = "UNICAST",
2237 [RTN_LOCAL] = "LOCAL",
2238 [RTN_BROADCAST] = "BROADCAST",
2239 [RTN_ANYCAST] = "ANYCAST",
2240 [RTN_MULTICAST] = "MULTICAST",
2241 [RTN_BLACKHOLE] = "BLACKHOLE",
2242 [RTN_UNREACHABLE] = "UNREACHABLE",
2243 [RTN_PROHIBIT] = "PROHIBIT",
2244 [RTN_THROW] = "THROW",
2245 [RTN_NAT] = "NAT",
2246 [RTN_XRESOLVE] = "XRESOLVE",
2247};
2248
Eric Dumazeta034ee32010-09-09 23:32:28 +00002249static inline const char *rtn_type(char *buf, size_t len, unsigned int t)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002250{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002251 if (t < __RTN_MAX && rtn_type_names[t])
2252 return rtn_type_names[t];
Eric Dumazet28d36e32008-01-14 23:09:56 -08002253 snprintf(buf, len, "type %u", t);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002254 return buf;
2255}
2256
2257/* Pretty print the trie */
Robert Olsson19baf832005-06-21 12:43:18 -07002258static int fib_trie_seq_show(struct seq_file *seq, void *v)
2259{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002260 const struct fib_trie_iter *iter = seq->private;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002261 struct tnode *n = v;
Robert Olsson19baf832005-06-21 12:43:18 -07002262
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002263 if (!node_parent_rcu(n))
2264 fib_table_print(seq, iter->tb);
Robert Olsson095b8502007-01-26 19:06:01 -08002265
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002266 if (IS_TNODE(n)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002267 __be32 prf = htonl(n->key);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002268
Alexander Duycke9b44012014-12-31 10:56:12 -08002269 seq_indent(seq, iter->depth-1);
2270 seq_printf(seq, " +-- %pI4/%zu %u %u %u\n",
2271 &prf, KEYLENGTH - n->pos - n->bits, n->bits,
2272 n->full_children, n->empty_children);
Olof Johansson91b9a272005-08-09 20:24:39 -07002273 } else {
Stephen Hemminger13280422008-01-22 21:54:37 -08002274 struct leaf_info *li;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002275 __be32 val = htonl(n->key);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002276
2277 seq_indent(seq, iter->depth);
Harvey Harrison673d57e2008-10-31 00:53:57 -07002278 seq_printf(seq, " |-- %pI4\n", &val);
Eric Dumazet28d36e32008-01-14 23:09:56 -08002279
Alexander Duyckadaf9812014-12-31 10:55:47 -08002280 hlist_for_each_entry_rcu(li, &n->list, hlist) {
Stephen Hemminger13280422008-01-22 21:54:37 -08002281 struct fib_alias *fa;
Eric Dumazet28d36e32008-01-14 23:09:56 -08002282
Alexander Duyck56315f92015-02-25 15:31:31 -08002283 hlist_for_each_entry_rcu(fa, &li->falh, fa_list) {
Stephen Hemminger13280422008-01-22 21:54:37 -08002284 char buf1[32], buf2[32];
Eric Dumazet28d36e32008-01-14 23:09:56 -08002285
Stephen Hemminger13280422008-01-22 21:54:37 -08002286 seq_indent(seq, iter->depth+1);
2287 seq_printf(seq, " /%d %s %s", li->plen,
2288 rtn_scope(buf1, sizeof(buf1),
David S. Miller37e826c2011-03-24 18:06:47 -07002289 fa->fa_info->fib_scope),
Stephen Hemminger13280422008-01-22 21:54:37 -08002290 rtn_type(buf2, sizeof(buf2),
2291 fa->fa_type));
2292 if (fa->fa_tos)
Denis V. Lunevb9c4d822008-02-05 02:58:45 -08002293 seq_printf(seq, " tos=%d", fa->fa_tos);
Stephen Hemminger13280422008-01-22 21:54:37 -08002294 seq_putc(seq, '\n');
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002295 }
2296 }
Robert Olsson19baf832005-06-21 12:43:18 -07002297 }
2298
2299 return 0;
2300}
2301
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002302static const struct seq_operations fib_trie_seq_ops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002303 .start = fib_trie_seq_start,
2304 .next = fib_trie_seq_next,
2305 .stop = fib_trie_seq_stop,
2306 .show = fib_trie_seq_show,
Robert Olsson19baf832005-06-21 12:43:18 -07002307};
2308
2309static int fib_trie_seq_open(struct inode *inode, struct file *file)
2310{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002311 return seq_open_net(inode, file, &fib_trie_seq_ops,
2312 sizeof(struct fib_trie_iter));
Robert Olsson19baf832005-06-21 12:43:18 -07002313}
2314
Arjan van de Ven9a321442007-02-12 00:55:35 -08002315static const struct file_operations fib_trie_fops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002316 .owner = THIS_MODULE,
2317 .open = fib_trie_seq_open,
2318 .read = seq_read,
2319 .llseek = seq_lseek,
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002320 .release = seq_release_net,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002321};
2322
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002323struct fib_route_iter {
2324 struct seq_net_private p;
2325 struct trie *main_trie;
2326 loff_t pos;
2327 t_key key;
2328};
2329
Alexander Duyckadaf9812014-12-31 10:55:47 -08002330static struct tnode *fib_route_get_idx(struct fib_route_iter *iter, loff_t pos)
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002331{
Alexander Duyckadaf9812014-12-31 10:55:47 -08002332 struct tnode *l = NULL;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002333 struct trie *t = iter->main_trie;
2334
2335 /* use cache location of last found key */
2336 if (iter->pos > 0 && pos >= iter->pos && (l = fib_find_node(t, iter->key)))
2337 pos -= iter->pos;
2338 else {
2339 iter->pos = 0;
2340 l = trie_firstleaf(t);
2341 }
2342
2343 while (l && pos-- > 0) {
2344 iter->pos++;
2345 l = trie_nextleaf(l);
2346 }
2347
2348 if (l)
2349 iter->key = pos; /* remember it */
2350 else
2351 iter->pos = 0; /* forget it */
2352
2353 return l;
2354}
2355
2356static void *fib_route_seq_start(struct seq_file *seq, loff_t *pos)
2357 __acquires(RCU)
2358{
2359 struct fib_route_iter *iter = seq->private;
2360 struct fib_table *tb;
2361
2362 rcu_read_lock();
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002363 tb = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN);
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002364 if (!tb)
2365 return NULL;
2366
2367 iter->main_trie = (struct trie *) tb->tb_data;
2368 if (*pos == 0)
2369 return SEQ_START_TOKEN;
2370 else
2371 return fib_route_get_idx(iter, *pos - 1);
2372}
2373
2374static void *fib_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2375{
2376 struct fib_route_iter *iter = seq->private;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002377 struct tnode *l = v;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002378
2379 ++*pos;
2380 if (v == SEQ_START_TOKEN) {
2381 iter->pos = 0;
2382 l = trie_firstleaf(iter->main_trie);
2383 } else {
2384 iter->pos++;
2385 l = trie_nextleaf(l);
2386 }
2387
2388 if (l)
2389 iter->key = l->key;
2390 else
2391 iter->pos = 0;
2392 return l;
2393}
2394
2395static void fib_route_seq_stop(struct seq_file *seq, void *v)
2396 __releases(RCU)
2397{
2398 rcu_read_unlock();
2399}
2400
Eric Dumazeta034ee32010-09-09 23:32:28 +00002401static unsigned int fib_flag_trans(int type, __be32 mask, const struct fib_info *fi)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002402{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002403 unsigned int flags = 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002404
Eric Dumazeta034ee32010-09-09 23:32:28 +00002405 if (type == RTN_UNREACHABLE || type == RTN_PROHIBIT)
2406 flags = RTF_REJECT;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002407 if (fi && fi->fib_nh->nh_gw)
2408 flags |= RTF_GATEWAY;
Al Viro32ab5f82006-09-26 22:21:45 -07002409 if (mask == htonl(0xFFFFFFFF))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002410 flags |= RTF_HOST;
2411 flags |= RTF_UP;
2412 return flags;
2413}
2414
2415/*
2416 * This outputs /proc/net/route.
2417 * The format of the file is not supposed to be changed
Eric Dumazeta034ee32010-09-09 23:32:28 +00002418 * and needs to be same as fib_hash output to avoid breaking
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002419 * legacy utilities
2420 */
2421static int fib_route_seq_show(struct seq_file *seq, void *v)
2422{
Alexander Duyckadaf9812014-12-31 10:55:47 -08002423 struct tnode *l = v;
Stephen Hemminger13280422008-01-22 21:54:37 -08002424 struct leaf_info *li;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002425
2426 if (v == SEQ_START_TOKEN) {
2427 seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
2428 "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
2429 "\tWindow\tIRTT");
2430 return 0;
2431 }
2432
Sasha Levinb67bfe02013-02-27 17:06:00 -08002433 hlist_for_each_entry_rcu(li, &l->list, hlist) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002434 struct fib_alias *fa;
Al Viro32ab5f82006-09-26 22:21:45 -07002435 __be32 mask, prefix;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002436
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002437 mask = inet_make_mask(li->plen);
2438 prefix = htonl(l->key);
2439
Alexander Duyck56315f92015-02-25 15:31:31 -08002440 hlist_for_each_entry_rcu(fa, &li->falh, fa_list) {
Herbert Xu1371e372005-10-15 09:42:39 +10002441 const struct fib_info *fi = fa->fa_info;
Eric Dumazeta034ee32010-09-09 23:32:28 +00002442 unsigned int flags = fib_flag_trans(fa->fa_type, mask, fi);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002443
2444 if (fa->fa_type == RTN_BROADCAST
2445 || fa->fa_type == RTN_MULTICAST)
2446 continue;
2447
Tetsuo Handa652586d2013-11-14 14:31:57 -08002448 seq_setwidth(seq, 127);
2449
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002450 if (fi)
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002451 seq_printf(seq,
2452 "%s\t%08X\t%08X\t%04X\t%d\t%u\t"
Tetsuo Handa652586d2013-11-14 14:31:57 -08002453 "%d\t%08X\t%d\t%u\t%u",
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002454 fi->fib_dev ? fi->fib_dev->name : "*",
2455 prefix,
2456 fi->fib_nh->nh_gw, flags, 0, 0,
2457 fi->fib_priority,
2458 mask,
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002459 (fi->fib_advmss ?
2460 fi->fib_advmss + 40 : 0),
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002461 fi->fib_window,
Tetsuo Handa652586d2013-11-14 14:31:57 -08002462 fi->fib_rtt >> 3);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002463 else
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002464 seq_printf(seq,
2465 "*\t%08X\t%08X\t%04X\t%d\t%u\t"
Tetsuo Handa652586d2013-11-14 14:31:57 -08002466 "%d\t%08X\t%d\t%u\t%u",
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002467 prefix, 0, flags, 0, 0, 0,
Tetsuo Handa652586d2013-11-14 14:31:57 -08002468 mask, 0, 0, 0);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002469
Tetsuo Handa652586d2013-11-14 14:31:57 -08002470 seq_pad(seq, '\n');
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002471 }
2472 }
2473
2474 return 0;
2475}
2476
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002477static const struct seq_operations fib_route_seq_ops = {
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002478 .start = fib_route_seq_start,
2479 .next = fib_route_seq_next,
2480 .stop = fib_route_seq_stop,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002481 .show = fib_route_seq_show,
2482};
2483
2484static int fib_route_seq_open(struct inode *inode, struct file *file)
2485{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002486 return seq_open_net(inode, file, &fib_route_seq_ops,
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002487 sizeof(struct fib_route_iter));
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002488}
2489
Arjan van de Ven9a321442007-02-12 00:55:35 -08002490static const struct file_operations fib_route_fops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002491 .owner = THIS_MODULE,
2492 .open = fib_route_seq_open,
2493 .read = seq_read,
2494 .llseek = seq_lseek,
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002495 .release = seq_release_net,
Robert Olsson19baf832005-06-21 12:43:18 -07002496};
2497
Denis V. Lunev61a02652008-01-10 03:21:09 -08002498int __net_init fib_proc_init(struct net *net)
Robert Olsson19baf832005-06-21 12:43:18 -07002499{
Gao fengd4beaa62013-02-18 01:34:54 +00002500 if (!proc_create("fib_trie", S_IRUGO, net->proc_net, &fib_trie_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002501 goto out1;
2502
Gao fengd4beaa62013-02-18 01:34:54 +00002503 if (!proc_create("fib_triestat", S_IRUGO, net->proc_net,
2504 &fib_triestat_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002505 goto out2;
2506
Gao fengd4beaa62013-02-18 01:34:54 +00002507 if (!proc_create("route", S_IRUGO, net->proc_net, &fib_route_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002508 goto out3;
2509
Robert Olsson19baf832005-06-21 12:43:18 -07002510 return 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002511
2512out3:
Gao fengece31ff2013-02-18 01:34:56 +00002513 remove_proc_entry("fib_triestat", net->proc_net);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002514out2:
Gao fengece31ff2013-02-18 01:34:56 +00002515 remove_proc_entry("fib_trie", net->proc_net);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002516out1:
2517 return -ENOMEM;
Robert Olsson19baf832005-06-21 12:43:18 -07002518}
2519
Denis V. Lunev61a02652008-01-10 03:21:09 -08002520void __net_exit fib_proc_exit(struct net *net)
Robert Olsson19baf832005-06-21 12:43:18 -07002521{
Gao fengece31ff2013-02-18 01:34:56 +00002522 remove_proc_entry("fib_trie", net->proc_net);
2523 remove_proc_entry("fib_triestat", net->proc_net);
2524 remove_proc_entry("route", net->proc_net);
Robert Olsson19baf832005-06-21 12:43:18 -07002525}
2526
2527#endif /* CONFIG_PROC_FS */