blob: b5fed2f5ef9e68a0a29c79b4c0b5b0c5615c8c6b [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>
Scott Feldman8e05fd72015-03-05 21:21:19 -080082#include <net/switchdev.h>
Robert Olsson19baf832005-06-21 12:43:18 -070083#include "fib_lookup.h"
84
Robert Olsson06ef9212006-03-20 21:35:01 -080085#define MAX_STAT_DEPTH 32
Robert Olsson19baf832005-06-21 12:43:18 -070086
Alexander Duyck95f60ea2015-01-22 15:51:26 -080087#define KEYLENGTH (8*sizeof(t_key))
88#define KEY_MAX ((t_key)~0)
Robert Olsson19baf832005-06-21 12:43:18 -070089
Robert Olsson19baf832005-06-21 12:43:18 -070090typedef unsigned int t_key;
91
Alexander Duyck64c9b6f2014-12-31 10:55:35 -080092#define IS_TNODE(n) ((n)->bits)
93#define IS_LEAF(n) (!(n)->bits)
Robert Olsson2373ce12005-08-25 13:01:29 -070094
Alexander Duyck35c6eda2015-03-06 09:54:08 -080095struct key_vector {
Alexander Duyck41b489f2015-03-04 15:02:33 -080096 t_key key;
97 unsigned char pos; /* 2log(KEYLENGTH) bits needed */
98 unsigned char bits; /* 2log(KEYLENGTH) bits needed */
99 unsigned char slen;
Alexander Duyckadaf9812014-12-31 10:55:47 -0800100 union {
Alexander Duyck41b489f2015-03-04 15:02:33 -0800101 /* This list pointer if valid if (pos | bits) == 0 (LEAF) */
Alexander Duyck79e5ad22015-02-25 15:31:51 -0800102 struct hlist_head leaf;
Alexander Duyck41b489f2015-03-04 15:02:33 -0800103 /* This array is valid if (pos | bits) > 0 (TNODE) */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800104 struct key_vector __rcu *tnode[0];
Alexander Duyckadaf9812014-12-31 10:55:47 -0800105 };
Robert Olsson19baf832005-06-21 12:43:18 -0700106};
107
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800108struct tnode {
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800109 struct rcu_head rcu;
Alexander Duyck6e22d172015-03-06 09:54:39 -0800110 t_key empty_children; /* KEYLENGTH bits needed */
111 t_key full_children; /* KEYLENGTH bits needed */
Alexander Duyckf23e59f2015-03-06 09:54:46 -0800112 struct key_vector __rcu *parent;
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800113 struct key_vector kv[1];
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800114#define tn_bits kv[0].bits
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800115};
116
117#define TNODE_SIZE(n) offsetof(struct tnode, kv[0].tnode[n])
Alexander Duyck41b489f2015-03-04 15:02:33 -0800118#define LEAF_SIZE TNODE_SIZE(1)
119
Robert Olsson19baf832005-06-21 12:43:18 -0700120#ifdef CONFIG_IP_FIB_TRIE_STATS
121struct trie_use_stats {
122 unsigned int gets;
123 unsigned int backtrack;
124 unsigned int semantic_match_passed;
125 unsigned int semantic_match_miss;
126 unsigned int null_node_hit;
Robert Olsson2f368952005-07-05 15:02:40 -0700127 unsigned int resize_node_skipped;
Robert Olsson19baf832005-06-21 12:43:18 -0700128};
129#endif
130
131struct trie_stat {
132 unsigned int totdepth;
133 unsigned int maxdepth;
134 unsigned int tnodes;
135 unsigned int leaves;
136 unsigned int nullpointers;
Stephen Hemminger93672292008-01-22 21:54:05 -0800137 unsigned int prefixes;
Robert Olsson06ef9212006-03-20 21:35:01 -0800138 unsigned int nodesizes[MAX_STAT_DEPTH];
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700139};
Robert Olsson19baf832005-06-21 12:43:18 -0700140
141struct trie {
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800142 struct key_vector __rcu *tnode[1];
Robert Olsson19baf832005-06-21 12:43:18 -0700143#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -0800144 struct trie_use_stats __percpu *stats;
Robert Olsson19baf832005-06-21 12:43:18 -0700145#endif
Robert Olsson19baf832005-06-21 12:43:18 -0700146};
147
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800148static struct key_vector **resize(struct trie *t, struct key_vector *tn);
Jarek Poplawskic3059472009-07-14 08:33:08 +0000149static size_t tnode_free_size;
150
151/*
152 * synchronize_rcu after call_rcu for that many pages; it should be especially
153 * useful before resizing the root node with PREEMPT_NONE configs; the value was
154 * obtained experimentally, aiming to avoid visible slowdown.
155 */
156static const int sync_pages = 128;
Robert Olsson19baf832005-06-21 12:43:18 -0700157
Christoph Lametere18b8902006-12-06 20:33:20 -0800158static struct kmem_cache *fn_alias_kmem __read_mostly;
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -0800159static struct kmem_cache *trie_leaf_kmem __read_mostly;
Robert Olsson19baf832005-06-21 12:43:18 -0700160
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800161static inline struct tnode *tn_info(struct key_vector *kv)
162{
163 return container_of(kv, struct tnode, kv[0]);
164}
165
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800166/* caller must hold RTNL */
Alexander Duyckf23e59f2015-03-06 09:54:46 -0800167#define node_parent(tn) rtnl_dereference(tn_info(tn)->parent)
Alexander Duyck754baf82015-03-06 09:54:14 -0800168#define get_child(tn, i) rtnl_dereference((tn)->tnode[i])
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700169
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800170/* caller must hold RCU read lock or RTNL */
Alexander Duyckf23e59f2015-03-06 09:54:46 -0800171#define node_parent_rcu(tn) rcu_dereference_rtnl(tn_info(tn)->parent)
Alexander Duyck754baf82015-03-06 09:54:14 -0800172#define get_child_rcu(tn, i) rcu_dereference_rtnl((tn)->tnode[i])
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700173
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800174/* wrapper for rcu_assign_pointer */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800175static inline void node_set_parent(struct key_vector *n, struct key_vector *tp)
Stephen Hemminger06801912007-08-10 15:22:13 -0700176{
Alexander Duyckadaf9812014-12-31 10:55:47 -0800177 if (n)
Alexander Duyckf23e59f2015-03-06 09:54:46 -0800178 rcu_assign_pointer(tn_info(n)->parent, tp);
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800179}
180
Alexander Duyckf23e59f2015-03-06 09:54:46 -0800181#define NODE_INIT_PARENT(n, p) RCU_INIT_POINTER(tn_info(n)->parent, p)
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800182
183/* This provides us with the number of children in this node, in the case of a
184 * leaf this will return 0 meaning none of the children are accessible.
185 */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800186static inline unsigned long child_length(const struct key_vector *tn)
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800187{
188 return (1ul << tn->bits) & ~(1ul);
Stephen Hemminger06801912007-08-10 15:22:13 -0700189}
Robert Olsson2373ce12005-08-25 13:01:29 -0700190
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800191static inline unsigned long get_index(t_key key, struct key_vector *kv)
192{
193 unsigned long index = key ^ kv->key;
194
195 return index >> kv->pos;
196}
197
Alexander Duycke9b44012014-12-31 10:56:12 -0800198/* To understand this stuff, an understanding of keys and all their bits is
199 * necessary. Every node in the trie has a key associated with it, but not
200 * all of the bits in that key are significant.
201 *
202 * Consider a node 'n' and its parent 'tp'.
203 *
204 * If n is a leaf, every bit in its key is significant. Its presence is
205 * necessitated by path compression, since during a tree traversal (when
206 * searching for a leaf - unless we are doing an insertion) we will completely
207 * ignore all skipped bits we encounter. Thus we need to verify, at the end of
208 * a potentially successful search, that we have indeed been walking the
209 * correct key path.
210 *
211 * Note that we can never "miss" the correct key in the tree if present by
212 * following the wrong path. Path compression ensures that segments of the key
213 * that are the same for all keys with a given prefix are skipped, but the
214 * skipped part *is* identical for each node in the subtrie below the skipped
215 * bit! trie_insert() in this implementation takes care of that.
216 *
217 * if n is an internal node - a 'tnode' here, the various parts of its key
218 * have many different meanings.
219 *
220 * Example:
221 * _________________________________________________________________
222 * | i | i | i | i | i | i | i | N | N | N | S | S | S | S | S | C |
223 * -----------------------------------------------------------------
224 * 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
225 *
226 * _________________________________________________________________
227 * | C | C | C | u | u | u | u | u | u | u | u | u | u | u | u | u |
228 * -----------------------------------------------------------------
229 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
230 *
231 * tp->pos = 22
232 * tp->bits = 3
233 * n->pos = 13
234 * n->bits = 4
235 *
236 * First, let's just ignore the bits that come before the parent tp, that is
237 * the bits from (tp->pos + tp->bits) to 31. They are *known* but at this
238 * point we do not use them for anything.
239 *
240 * The bits from (tp->pos) to (tp->pos + tp->bits - 1) - "N", above - are the
241 * index into the parent's child array. That is, they will be used to find
242 * 'n' among tp's children.
243 *
244 * The bits from (n->pos + n->bits) to (tn->pos - 1) - "S" - are skipped bits
245 * for the node n.
246 *
247 * All the bits we have seen so far are significant to the node n. The rest
248 * of the bits are really not needed or indeed known in n->key.
249 *
250 * The bits from (n->pos) to (n->pos + n->bits - 1) - "C" - are the index into
251 * n's child array, and will of course be different for each child.
252 *
253 * The rest of the bits, from 0 to (n->pos + n->bits), are completely unknown
254 * at this point.
255 */
Robert Olsson19baf832005-06-21 12:43:18 -0700256
Denis V. Lunevf5026fa2007-12-13 09:47:57 -0800257static const int halve_threshold = 25;
258static const int inflate_threshold = 50;
Jarek Poplawski345aa032009-07-07 19:39:16 -0700259static const int halve_threshold_root = 15;
Jens Låås80b71b82009-08-28 23:57:15 -0700260static const int inflate_threshold_root = 30;
Robert Olsson2373ce12005-08-25 13:01:29 -0700261
262static void __alias_free_mem(struct rcu_head *head)
263{
264 struct fib_alias *fa = container_of(head, struct fib_alias, rcu);
265 kmem_cache_free(fn_alias_kmem, fa);
266}
267
268static inline void alias_free_mem_rcu(struct fib_alias *fa)
269{
270 call_rcu(&fa->rcu, __alias_free_mem);
271}
272
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800273#define TNODE_KMALLOC_MAX \
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800274 ilog2((PAGE_SIZE - TNODE_SIZE(0)) / sizeof(struct key_vector *))
Alexander Duyck1de3d872015-03-04 15:04:46 -0800275#define TNODE_VMALLOC_MAX \
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800276 ilog2((SIZE_MAX - TNODE_SIZE(0)) / sizeof(struct key_vector *))
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800277
278static void __node_free_rcu(struct rcu_head *head)
Robert Olsson2373ce12005-08-25 13:01:29 -0700279{
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800280 struct tnode *n = container_of(head, struct tnode, rcu);
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800281
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800282 if (!n->tn_bits)
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800283 kmem_cache_free(trie_leaf_kmem, n);
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800284 else if (n->tn_bits <= TNODE_KMALLOC_MAX)
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800285 kfree(n);
286 else
287 vfree(n);
Robert Olsson2373ce12005-08-25 13:01:29 -0700288}
289
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800290#define node_free(n) call_rcu(&tn_info(n)->rcu, __node_free_rcu)
Stephen Hemminger387a5482008-04-10 03:47:34 -0700291
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800292static struct tnode *tnode_alloc(int bits)
Robert Olsson2373ce12005-08-25 13:01:29 -0700293{
Alexander Duyck1de3d872015-03-04 15:04:46 -0800294 size_t size;
295
296 /* verify bits is within bounds */
297 if (bits > TNODE_VMALLOC_MAX)
298 return NULL;
299
300 /* determine size and verify it is non-zero and didn't overflow */
301 size = TNODE_SIZE(1ul << bits);
302
Robert Olsson2373ce12005-08-25 13:01:29 -0700303 if (size <= PAGE_SIZE)
Eric Dumazet8d965442008-01-13 22:31:44 -0800304 return kzalloc(size, GFP_KERNEL);
Stephen Hemminger15be75c2008-04-10 02:56:38 -0700305 else
Eric Dumazet7a1c8e52010-11-20 07:46:35 +0000306 return vzalloc(size);
Stephen Hemminger15be75c2008-04-10 02:56:38 -0700307}
Robert Olsson2373ce12005-08-25 13:01:29 -0700308
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800309static inline void empty_child_inc(struct key_vector *n)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800310{
Alexander Duyck6e22d172015-03-06 09:54:39 -0800311 ++tn_info(n)->empty_children ? : ++tn_info(n)->full_children;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800312}
313
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800314static inline void empty_child_dec(struct key_vector *n)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800315{
Alexander Duyck6e22d172015-03-06 09:54:39 -0800316 tn_info(n)->empty_children-- ? : tn_info(n)->full_children--;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800317}
318
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800319static struct key_vector *leaf_new(t_key key, struct fib_alias *fa)
Robert Olsson19baf832005-06-21 12:43:18 -0700320{
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800321 struct tnode *kv = kmem_cache_alloc(trie_leaf_kmem, GFP_KERNEL);
322 struct key_vector *l = kv->kv;
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800323
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800324 if (!kv)
325 return NULL;
326
327 /* initialize key vector */
328 l->key = key;
329 l->pos = 0;
330 l->bits = 0;
331 l->slen = fa->fa_slen;
332
333 /* link leaf to fib alias */
334 INIT_HLIST_HEAD(&l->leaf);
335 hlist_add_head(&fa->fa_list, &l->leaf);
336
Robert Olsson19baf832005-06-21 12:43:18 -0700337 return l;
338}
339
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800340static struct key_vector *tnode_new(t_key key, int pos, int bits)
Robert Olsson19baf832005-06-21 12:43:18 -0700341{
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800342 struct tnode *tnode = tnode_alloc(bits);
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800343 unsigned int shift = pos + bits;
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800344 struct key_vector *tn = tnode->kv;
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800345
346 /* verify bits and pos their msb bits clear and values are valid */
347 BUG_ON(!bits || (shift > KEYLENGTH));
Robert Olsson19baf832005-06-21 12:43:18 -0700348
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800349 pr_debug("AT %p s=%zu %zu\n", tnode, TNODE_SIZE(0),
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800350 sizeof(struct key_vector *) << bits);
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800351
352 if (!tnode)
353 return NULL;
354
355 if (bits == KEYLENGTH)
Alexander Duyck6e22d172015-03-06 09:54:39 -0800356 tnode->full_children = 1;
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800357 else
Alexander Duyck6e22d172015-03-06 09:54:39 -0800358 tnode->empty_children = 1ul << bits;
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800359
360 tn->key = (shift < KEYLENGTH) ? (key >> shift) << shift : 0;
361 tn->pos = pos;
362 tn->bits = bits;
363 tn->slen = pos;
364
Robert Olsson19baf832005-06-21 12:43:18 -0700365 return tn;
366}
367
Alexander Duycke9b44012014-12-31 10:56:12 -0800368/* Check whether a tnode 'n' is "full", i.e. it is an internal node
Robert Olsson19baf832005-06-21 12:43:18 -0700369 * and no bits are skipped. See discussion in dyntree paper p. 6
370 */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800371static inline int tnode_full(struct key_vector *tn, struct key_vector *n)
Robert Olsson19baf832005-06-21 12:43:18 -0700372{
Alexander Duycke9b44012014-12-31 10:56:12 -0800373 return n && ((n->pos + n->bits) == tn->pos) && IS_TNODE(n);
Robert Olsson19baf832005-06-21 12:43:18 -0700374}
375
Alexander Duyckff181ed2014-12-31 10:56:43 -0800376/* Add a child at position i overwriting the old value.
377 * Update the value of full_children and empty_children.
378 */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800379static void put_child(struct key_vector *tn, unsigned long i,
380 struct key_vector *n)
Robert Olsson19baf832005-06-21 12:43:18 -0700381{
Alexander Duyck754baf82015-03-06 09:54:14 -0800382 struct key_vector *chi = get_child(tn, i);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800383 int isfull, wasfull;
Robert Olsson19baf832005-06-21 12:43:18 -0700384
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800385 BUG_ON(i >= child_length(tn));
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700386
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800387 /* update emptyChildren, overflow into fullChildren */
Robert Olsson19baf832005-06-21 12:43:18 -0700388 if (n == NULL && chi != NULL)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800389 empty_child_inc(tn);
390 if (n != NULL && chi == NULL)
391 empty_child_dec(tn);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700392
Robert Olsson19baf832005-06-21 12:43:18 -0700393 /* update fullChildren */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800394 wasfull = tnode_full(tn, chi);
Robert Olsson19baf832005-06-21 12:43:18 -0700395 isfull = tnode_full(tn, n);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800396
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700397 if (wasfull && !isfull)
Alexander Duyck6e22d172015-03-06 09:54:39 -0800398 tn_info(tn)->full_children--;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700399 else if (!wasfull && isfull)
Alexander Duyck6e22d172015-03-06 09:54:39 -0800400 tn_info(tn)->full_children++;
Olof Johansson91b9a272005-08-09 20:24:39 -0700401
Alexander Duyck5405afd2014-12-31 10:57:08 -0800402 if (n && (tn->slen < n->slen))
403 tn->slen = n->slen;
404
Alexander Duyck41b489f2015-03-04 15:02:33 -0800405 rcu_assign_pointer(tn->tnode[i], n);
Robert Olsson19baf832005-06-21 12:43:18 -0700406}
407
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800408static void update_children(struct key_vector *tn)
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800409{
410 unsigned long i;
411
412 /* update all of the child parent pointers */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800413 for (i = child_length(tn); i;) {
Alexander Duyck754baf82015-03-06 09:54:14 -0800414 struct key_vector *inode = get_child(tn, --i);
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800415
416 if (!inode)
417 continue;
418
419 /* Either update the children of a tnode that
420 * already belongs to us or update the child
421 * to point to ourselves.
422 */
423 if (node_parent(inode) == tn)
424 update_children(inode);
425 else
426 node_set_parent(inode, tn);
427 }
428}
429
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800430static inline void put_child_root(struct key_vector *tp, struct trie *t,
431 t_key key, struct key_vector *n)
Alexander Duyck836a0122014-12-31 10:56:06 -0800432{
433 if (tp)
434 put_child(tp, get_index(key, tp), n);
435 else
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800436 rcu_assign_pointer(t->tnode[0], n);
Alexander Duyck836a0122014-12-31 10:56:06 -0800437}
438
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800439static inline void tnode_free_init(struct key_vector *tn)
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700440{
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800441 tn_info(tn)->rcu.next = NULL;
Alexander Duyckfc86a932014-12-31 10:56:49 -0800442}
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700443
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800444static inline void tnode_free_append(struct key_vector *tn,
445 struct key_vector *n)
Alexander Duyckfc86a932014-12-31 10:56:49 -0800446{
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800447 tn_info(n)->rcu.next = tn_info(tn)->rcu.next;
448 tn_info(tn)->rcu.next = &tn_info(n)->rcu;
Alexander Duyckfc86a932014-12-31 10:56:49 -0800449}
450
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800451static void tnode_free(struct key_vector *tn)
Alexander Duyckfc86a932014-12-31 10:56:49 -0800452{
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800453 struct callback_head *head = &tn_info(tn)->rcu;
Alexander Duyckfc86a932014-12-31 10:56:49 -0800454
455 while (head) {
456 head = head->next;
Alexander Duyck41b489f2015-03-04 15:02:33 -0800457 tnode_free_size += TNODE_SIZE(1ul << tn->bits);
Alexander Duyckfc86a932014-12-31 10:56:49 -0800458 node_free(tn);
459
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800460 tn = container_of(head, struct tnode, rcu)->kv;
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700461 }
Alexander Duyckfc86a932014-12-31 10:56:49 -0800462
463 if (tnode_free_size >= PAGE_SIZE * sync_pages) {
464 tnode_free_size = 0;
465 synchronize_rcu();
466 }
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700467}
468
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800469static struct key_vector __rcu **replace(struct trie *t,
470 struct key_vector *oldtnode,
471 struct key_vector *tn)
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800472{
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800473 struct key_vector *tp = node_parent(oldtnode);
474 struct key_vector **cptr;
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800475 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
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800487 /* record the pointer that is pointing to this node */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800488 cptr = tp ? tp->tnode : t->tnode;
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800489
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800490 /* resize children now that oldtnode is freed */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800491 for (i = child_length(tn); i;) {
Alexander Duyck754baf82015-03-06 09:54:14 -0800492 struct key_vector *inode = get_child(tn, --i);
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800493
494 /* resize child node */
495 if (tnode_full(tn, inode))
496 resize(t, inode);
497 }
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800498
499 return cptr;
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800500}
501
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800502static struct key_vector __rcu **inflate(struct trie *t,
503 struct key_vector *oldtnode)
Robert Olsson19baf832005-06-21 12:43:18 -0700504{
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800505 struct key_vector *tn;
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800506 unsigned long i;
Alexander Duycke9b44012014-12-31 10:56:12 -0800507 t_key m;
Robert Olsson19baf832005-06-21 12:43:18 -0700508
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700509 pr_debug("In inflate\n");
Robert Olsson19baf832005-06-21 12:43:18 -0700510
Alexander Duycke9b44012014-12-31 10:56:12 -0800511 tn = tnode_new(oldtnode->key, oldtnode->pos - 1, oldtnode->bits + 1);
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700512 if (!tn)
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800513 goto notnode;
Robert Olsson2f368952005-07-05 15:02:40 -0700514
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800515 /* prepare oldtnode to be freed */
516 tnode_free_init(oldtnode);
517
Alexander Duyck12c081a2014-12-31 10:56:55 -0800518 /* Assemble all of the pointers in our cluster, in this case that
519 * represents all of the pointers out of our allocated nodes that
520 * point to existing tnodes and the links between our allocated
521 * nodes.
Robert Olsson2f368952005-07-05 15:02:40 -0700522 */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800523 for (i = child_length(oldtnode), m = 1u << tn->pos; i;) {
Alexander Duyck754baf82015-03-06 09:54:14 -0800524 struct key_vector *inode = get_child(oldtnode, --i);
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800525 struct key_vector *node0, *node1;
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800526 unsigned long j, k;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700527
Robert Olsson19baf832005-06-21 12:43:18 -0700528 /* An empty child */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800529 if (inode == NULL)
Robert Olsson19baf832005-06-21 12:43:18 -0700530 continue;
531
532 /* A leaf or an internal node with skipped bits */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800533 if (!tnode_full(oldtnode, inode)) {
Alexander Duycke9b44012014-12-31 10:56:12 -0800534 put_child(tn, get_index(inode->key, tn), inode);
Robert Olsson19baf832005-06-21 12:43:18 -0700535 continue;
536 }
537
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800538 /* drop the node in the old tnode free list */
539 tnode_free_append(oldtnode, inode);
540
Robert Olsson19baf832005-06-21 12:43:18 -0700541 /* An internal node with two children */
Robert Olsson19baf832005-06-21 12:43:18 -0700542 if (inode->bits == 1) {
Alexander Duyck754baf82015-03-06 09:54:14 -0800543 put_child(tn, 2 * i + 1, get_child(inode, 1));
544 put_child(tn, 2 * i, get_child(inode, 0));
Olof Johansson91b9a272005-08-09 20:24:39 -0700545 continue;
Robert Olsson19baf832005-06-21 12:43:18 -0700546 }
547
Olof Johansson91b9a272005-08-09 20:24:39 -0700548 /* We will replace this node 'inode' with two new
Alexander Duyck12c081a2014-12-31 10:56:55 -0800549 * ones, 'node0' and 'node1', each with half of the
Olof Johansson91b9a272005-08-09 20:24:39 -0700550 * original children. The two new nodes will have
551 * a position one bit further down the key and this
552 * means that the "significant" part of their keys
553 * (see the discussion near the top of this file)
554 * will differ by one bit, which will be "0" in
Alexander Duyck12c081a2014-12-31 10:56:55 -0800555 * node0's key and "1" in node1's key. Since we are
Olof Johansson91b9a272005-08-09 20:24:39 -0700556 * moving the key position by one step, the bit that
557 * we are moving away from - the bit at position
Alexander Duyck12c081a2014-12-31 10:56:55 -0800558 * (tn->pos) - is the one that will differ between
559 * node0 and node1. So... we synthesize that bit in the
560 * two new keys.
Olof Johansson91b9a272005-08-09 20:24:39 -0700561 */
Alexander Duyck12c081a2014-12-31 10:56:55 -0800562 node1 = tnode_new(inode->key | m, inode->pos, inode->bits - 1);
563 if (!node1)
564 goto nomem;
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800565 node0 = tnode_new(inode->key, inode->pos, inode->bits - 1);
Robert Olsson19baf832005-06-21 12:43:18 -0700566
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800567 tnode_free_append(tn, node1);
Alexander Duyck12c081a2014-12-31 10:56:55 -0800568 if (!node0)
569 goto nomem;
570 tnode_free_append(tn, node0);
Robert Olsson19baf832005-06-21 12:43:18 -0700571
Alexander Duyck12c081a2014-12-31 10:56:55 -0800572 /* populate child pointers in new nodes */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800573 for (k = child_length(inode), j = k / 2; j;) {
Alexander Duyck754baf82015-03-06 09:54:14 -0800574 put_child(node1, --j, get_child(inode, --k));
575 put_child(node0, j, get_child(inode, j));
576 put_child(node1, --j, get_child(inode, --k));
577 put_child(node0, j, get_child(inode, j));
Robert Olsson19baf832005-06-21 12:43:18 -0700578 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800579
Alexander Duyck12c081a2014-12-31 10:56:55 -0800580 /* link new nodes to parent */
581 NODE_INIT_PARENT(node1, tn);
582 NODE_INIT_PARENT(node0, tn);
Olof Johansson91b9a272005-08-09 20:24:39 -0700583
Alexander Duyck12c081a2014-12-31 10:56:55 -0800584 /* link parent to nodes */
585 put_child(tn, 2 * i + 1, node1);
586 put_child(tn, 2 * i, node0);
Robert Olsson19baf832005-06-21 12:43:18 -0700587 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800588
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800589 /* setup the parent pointers into and out of this node */
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800590 return replace(t, oldtnode, tn);
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700591nomem:
Alexander Duyckfc86a932014-12-31 10:56:49 -0800592 /* all pointers should be clean so we are done */
593 tnode_free(tn);
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800594notnode:
595 return NULL;
Robert Olsson19baf832005-06-21 12:43:18 -0700596}
597
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800598static struct key_vector __rcu **halve(struct trie *t,
599 struct key_vector *oldtnode)
Robert Olsson19baf832005-06-21 12:43:18 -0700600{
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800601 struct key_vector *tn;
Alexander Duyck12c081a2014-12-31 10:56:55 -0800602 unsigned long i;
Robert Olsson19baf832005-06-21 12:43:18 -0700603
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700604 pr_debug("In halve\n");
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700605
Alexander Duycke9b44012014-12-31 10:56:12 -0800606 tn = tnode_new(oldtnode->key, oldtnode->pos + 1, oldtnode->bits - 1);
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700607 if (!tn)
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800608 goto notnode;
Robert Olsson2f368952005-07-05 15:02:40 -0700609
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800610 /* prepare oldtnode to be freed */
611 tnode_free_init(oldtnode);
612
Alexander Duyck12c081a2014-12-31 10:56:55 -0800613 /* Assemble all of the pointers in our cluster, in this case that
614 * represents all of the pointers out of our allocated nodes that
615 * point to existing tnodes and the links between our allocated
616 * nodes.
Robert Olsson2f368952005-07-05 15:02:40 -0700617 */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800618 for (i = child_length(oldtnode); i;) {
Alexander Duyck754baf82015-03-06 09:54:14 -0800619 struct key_vector *node1 = get_child(oldtnode, --i);
620 struct key_vector *node0 = get_child(oldtnode, --i);
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800621 struct key_vector *inode;
Robert Olsson2f368952005-07-05 15:02:40 -0700622
Alexander Duyck12c081a2014-12-31 10:56:55 -0800623 /* At least one of the children is empty */
624 if (!node1 || !node0) {
625 put_child(tn, i / 2, node1 ? : node0);
626 continue;
Robert Olsson2f368952005-07-05 15:02:40 -0700627 }
Robert Olsson2f368952005-07-05 15:02:40 -0700628
Alexander Duyck12c081a2014-12-31 10:56:55 -0800629 /* Two nonempty children */
630 inode = tnode_new(node0->key, oldtnode->pos, 1);
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800631 if (!inode)
632 goto nomem;
Alexander Duyck12c081a2014-12-31 10:56:55 -0800633 tnode_free_append(tn, inode);
634
635 /* initialize pointers out of node */
636 put_child(inode, 1, node1);
637 put_child(inode, 0, node0);
638 NODE_INIT_PARENT(inode, tn);
639
640 /* link parent to node */
641 put_child(tn, i / 2, inode);
Robert Olsson2f368952005-07-05 15:02:40 -0700642 }
Robert Olsson19baf832005-06-21 12:43:18 -0700643
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800644 /* setup the parent pointers into and out of this node */
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800645 return replace(t, oldtnode, tn);
646nomem:
647 /* all pointers should be clean so we are done */
648 tnode_free(tn);
649notnode:
650 return NULL;
Robert Olsson19baf832005-06-21 12:43:18 -0700651}
652
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800653static void collapse(struct trie *t, struct key_vector *oldtnode)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800654{
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800655 struct key_vector *n, *tp;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800656 unsigned long i;
657
658 /* scan the tnode looking for that one child that might still exist */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800659 for (n = NULL, i = child_length(oldtnode); !n && i;)
Alexander Duyck754baf82015-03-06 09:54:14 -0800660 n = get_child(oldtnode, --i);
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800661
662 /* compress one level */
663 tp = node_parent(oldtnode);
664 put_child_root(tp, t, oldtnode->key, n);
665 node_set_parent(n, tp);
666
667 /* drop dead node */
668 node_free(oldtnode);
669}
670
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800671static unsigned char update_suffix(struct key_vector *tn)
Alexander Duyck5405afd2014-12-31 10:57:08 -0800672{
673 unsigned char slen = tn->pos;
674 unsigned long stride, i;
675
676 /* search though the list of children looking for nodes that might
677 * have a suffix greater than the one we currently have. This is
678 * why we start with a stride of 2 since a stride of 1 would
679 * represent the nodes with suffix length equal to tn->pos
680 */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800681 for (i = 0, stride = 0x2ul ; i < child_length(tn); i += stride) {
Alexander Duyck754baf82015-03-06 09:54:14 -0800682 struct key_vector *n = get_child(tn, i);
Alexander Duyck5405afd2014-12-31 10:57:08 -0800683
684 if (!n || (n->slen <= slen))
685 continue;
686
687 /* update stride and slen based on new value */
688 stride <<= (n->slen - slen);
689 slen = n->slen;
690 i &= ~(stride - 1);
691
692 /* if slen covers all but the last bit we can stop here
693 * there will be nothing longer than that since only node
694 * 0 and 1 << (bits - 1) could have that as their suffix
695 * length.
696 */
697 if ((slen + 1) >= (tn->pos + tn->bits))
698 break;
699 }
700
701 tn->slen = slen;
702
703 return slen;
704}
705
Alexander Duyckf05a4812014-12-31 10:56:37 -0800706/* From "Implementing a dynamic compressed trie" by Stefan Nilsson of
707 * the Helsinki University of Technology and Matti Tikkanen of Nokia
708 * Telecommunications, page 6:
709 * "A node is doubled if the ratio of non-empty children to all
710 * children in the *doubled* node is at least 'high'."
711 *
712 * 'high' in this instance is the variable 'inflate_threshold'. It
713 * is expressed as a percentage, so we multiply it with
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800714 * child_length() and instead of multiplying by 2 (since the
Alexander Duyckf05a4812014-12-31 10:56:37 -0800715 * child array will be doubled by inflate()) and multiplying
716 * the left-hand side by 100 (to handle the percentage thing) we
717 * multiply the left-hand side by 50.
718 *
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800719 * The left-hand side may look a bit weird: child_length(tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800720 * - tn->empty_children is of course the number of non-null children
721 * in the current node. tn->full_children is the number of "full"
722 * children, that is non-null tnodes with a skip value of 0.
723 * All of those will be doubled in the resulting inflated tnode, so
724 * we just count them one extra time here.
725 *
726 * A clearer way to write this would be:
727 *
728 * to_be_doubled = tn->full_children;
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800729 * not_to_be_doubled = child_length(tn) - tn->empty_children -
Alexander Duyckf05a4812014-12-31 10:56:37 -0800730 * tn->full_children;
731 *
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800732 * new_child_length = child_length(tn) * 2;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800733 *
734 * new_fill_factor = 100 * (not_to_be_doubled + 2*to_be_doubled) /
735 * new_child_length;
736 * if (new_fill_factor >= inflate_threshold)
737 *
738 * ...and so on, tho it would mess up the while () loop.
739 *
740 * anyway,
741 * 100 * (not_to_be_doubled + 2*to_be_doubled) / new_child_length >=
742 * inflate_threshold
743 *
744 * avoid a division:
745 * 100 * (not_to_be_doubled + 2*to_be_doubled) >=
746 * inflate_threshold * new_child_length
747 *
748 * expand not_to_be_doubled and to_be_doubled, and shorten:
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800749 * 100 * (child_length(tn) - tn->empty_children +
Alexander Duyckf05a4812014-12-31 10:56:37 -0800750 * tn->full_children) >= inflate_threshold * new_child_length
751 *
752 * expand new_child_length:
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800753 * 100 * (child_length(tn) - tn->empty_children +
Alexander Duyckf05a4812014-12-31 10:56:37 -0800754 * tn->full_children) >=
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800755 * inflate_threshold * child_length(tn) * 2
Alexander Duyckf05a4812014-12-31 10:56:37 -0800756 *
757 * shorten again:
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800758 * 50 * (tn->full_children + child_length(tn) -
Alexander Duyckf05a4812014-12-31 10:56:37 -0800759 * tn->empty_children) >= inflate_threshold *
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800760 * child_length(tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800761 *
762 */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800763static inline bool should_inflate(struct key_vector *tp, struct key_vector *tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800764{
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800765 unsigned long used = child_length(tn);
Alexander Duyckf05a4812014-12-31 10:56:37 -0800766 unsigned long threshold = used;
767
768 /* Keep root node larger */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800769 threshold *= tp ? inflate_threshold : inflate_threshold_root;
Alexander Duyck6e22d172015-03-06 09:54:39 -0800770 used -= tn_info(tn)->empty_children;
771 used += tn_info(tn)->full_children;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800772
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800773 /* if bits == KEYLENGTH then pos = 0, and will fail below */
774
775 return (used > 1) && tn->pos && ((50 * used) >= threshold);
Alexander Duyckf05a4812014-12-31 10:56:37 -0800776}
777
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800778static inline bool should_halve(struct key_vector *tp, struct key_vector *tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800779{
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800780 unsigned long used = child_length(tn);
Alexander Duyckf05a4812014-12-31 10:56:37 -0800781 unsigned long threshold = used;
782
783 /* Keep root node larger */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800784 threshold *= tp ? halve_threshold : halve_threshold_root;
Alexander Duyck6e22d172015-03-06 09:54:39 -0800785 used -= tn_info(tn)->empty_children;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800786
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800787 /* if bits == KEYLENGTH then used = 100% on wrap, and will fail below */
788
789 return (used > 1) && (tn->bits > 1) && ((100 * used) < threshold);
790}
791
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800792static inline bool should_collapse(struct key_vector *tn)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800793{
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800794 unsigned long used = child_length(tn);
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800795
Alexander Duyck6e22d172015-03-06 09:54:39 -0800796 used -= tn_info(tn)->empty_children;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800797
798 /* account for bits == KEYLENGTH case */
Alexander Duyck6e22d172015-03-06 09:54:39 -0800799 if ((tn->bits == KEYLENGTH) && tn_info(tn)->full_children)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800800 used -= KEY_MAX;
801
802 /* One child or none, time to drop us from the trie */
803 return used < 2;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800804}
805
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800806#define MAX_WORK 10
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800807static struct key_vector __rcu **resize(struct trie *t,
808 struct key_vector *tn)
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800809{
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800810#ifdef CONFIG_IP_FIB_TRIE_STATS
811 struct trie_use_stats __percpu *stats = t->stats;
812#endif
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800813 struct key_vector *tp = node_parent(tn);
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800814 unsigned long cindex = tp ? get_index(tn->key, tp) : 0;
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800815 struct key_vector __rcu **cptr = tp ? tp->tnode : t->tnode;
Alexander Duycka80e89d2015-01-22 15:51:20 -0800816 int max_work = MAX_WORK;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800817
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800818 pr_debug("In tnode_resize %p inflate_threshold=%d threshold=%d\n",
819 tn, inflate_threshold, halve_threshold);
820
Alexander Duyckff181ed2014-12-31 10:56:43 -0800821 /* track the tnode via the pointer from the parent instead of
822 * doing it ourselves. This way we can let RCU fully do its
823 * thing without us interfering
824 */
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800825 BUG_ON(tn != rtnl_dereference(cptr[cindex]));
Alexander Duyckff181ed2014-12-31 10:56:43 -0800826
Alexander Duyckf05a4812014-12-31 10:56:37 -0800827 /* Double as long as the resulting node has a number of
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800828 * nonempty nodes that are above the threshold.
829 */
Alexander Duycka80e89d2015-01-22 15:51:20 -0800830 while (should_inflate(tp, tn) && max_work) {
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800831 struct key_vector __rcu **tcptr = inflate(t, tn);
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800832
833 if (!tcptr) {
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800834#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800835 this_cpu_inc(stats->resize_node_skipped);
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800836#endif
837 break;
838 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800839
Alexander Duycka80e89d2015-01-22 15:51:20 -0800840 max_work--;
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800841 cptr = tcptr;
842 tn = rtnl_dereference(cptr[cindex]);
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800843 }
844
845 /* Return if at least one inflate is run */
846 if (max_work != MAX_WORK)
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800847 return cptr;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800848
Alexander Duyckf05a4812014-12-31 10:56:37 -0800849 /* Halve as long as the number of empty children in this
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800850 * node is above threshold.
851 */
Alexander Duycka80e89d2015-01-22 15:51:20 -0800852 while (should_halve(tp, tn) && max_work) {
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800853 struct key_vector __rcu **tcptr = halve(t, tn);
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800854
855 if (!tcptr) {
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800856#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800857 this_cpu_inc(stats->resize_node_skipped);
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800858#endif
859 break;
860 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800861
Alexander Duycka80e89d2015-01-22 15:51:20 -0800862 max_work--;
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800863 cptr = tcptr;
864 tn = rtnl_dereference(cptr[cindex]);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800865 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800866
867 /* Only one child remains */
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800868 if (should_collapse(tn)) {
869 collapse(t, tn);
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800870 return cptr;
Alexander Duyck5405afd2014-12-31 10:57:08 -0800871 }
872
873 /* Return if at least one deflate was run */
874 if (max_work != MAX_WORK)
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800875 return cptr;
Alexander Duyck5405afd2014-12-31 10:57:08 -0800876
877 /* push the suffix length to the parent node */
878 if (tn->slen > tn->pos) {
879 unsigned char slen = update_suffix(tn);
880
881 if (tp && (slen > tp->slen))
882 tp->slen = slen;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800883 }
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800884
885 return cptr;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800886}
887
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800888static void leaf_pull_suffix(struct key_vector *tp, struct key_vector *l)
Robert Olsson19baf832005-06-21 12:43:18 -0700889{
Alexander Duyck5405afd2014-12-31 10:57:08 -0800890 while (tp && (tp->slen > tp->pos) && (tp->slen > l->slen)) {
891 if (update_suffix(tp) > l->slen)
892 break;
893 tp = node_parent(tp);
894 }
895}
896
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800897static void leaf_push_suffix(struct key_vector *tn, struct key_vector *l)
Alexander Duyck5405afd2014-12-31 10:57:08 -0800898{
Alexander Duyck5405afd2014-12-31 10:57:08 -0800899 /* if this is a new leaf then tn will be NULL and we can sort
900 * out parent suffix lengths as a part of trie_rebalance
901 */
902 while (tn && (tn->slen < l->slen)) {
903 tn->slen = l->slen;
904 tn = node_parent(tn);
905 }
906}
907
Robert Olsson2373ce12005-08-25 13:01:29 -0700908/* rcu_read_lock needs to be hold by caller from readside */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800909static struct key_vector *fib_find_node(struct trie *t,
910 struct key_vector **tp, u32 key)
Robert Olsson19baf832005-06-21 12:43:18 -0700911{
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800912 struct key_vector *pn = NULL, *n = rcu_dereference_rtnl(t->tnode[0]);
Robert Olsson19baf832005-06-21 12:43:18 -0700913
Alexander Duyck939afb02014-12-31 10:56:00 -0800914 while (n) {
915 unsigned long index = get_index(key, n);
916
917 /* This bit of code is a bit tricky but it combines multiple
918 * checks into a single check. The prefix consists of the
919 * prefix plus zeros for the bits in the cindex. The index
920 * is the difference between the key and this value. From
921 * this we can actually derive several pieces of data.
Alexander Duyckd4a975e2015-03-04 15:01:59 -0800922 * if (index >= (1ul << bits))
Alexander Duyck939afb02014-12-31 10:56:00 -0800923 * we have a mismatch in skip bits and failed
Alexander Duyckb3832112015-01-22 15:51:08 -0800924 * else
925 * we know the value is cindex
Alexander Duyckd4a975e2015-03-04 15:01:59 -0800926 *
927 * This check is safe even if bits == KEYLENGTH due to the
928 * fact that we can only allocate a node with 32 bits if a
929 * long is greater than 32 bits.
Alexander Duyck939afb02014-12-31 10:56:00 -0800930 */
Alexander Duyckd4a975e2015-03-04 15:01:59 -0800931 if (index >= (1ul << n->bits)) {
932 n = NULL;
933 break;
934 }
Alexander Duyck939afb02014-12-31 10:56:00 -0800935
936 /* we have found a leaf. Prefixes have already been compared */
937 if (IS_LEAF(n))
Robert Olsson19baf832005-06-21 12:43:18 -0700938 break;
Alexander Duyck939afb02014-12-31 10:56:00 -0800939
Alexander Duyckd4a975e2015-03-04 15:01:59 -0800940 pn = n;
Alexander Duyck754baf82015-03-06 09:54:14 -0800941 n = get_child_rcu(n, index);
Robert Olsson19baf832005-06-21 12:43:18 -0700942 }
Robert Olsson19baf832005-06-21 12:43:18 -0700943
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800944 *tp = pn;
Alexander Duyckd4a975e2015-03-04 15:01:59 -0800945
Alexander Duyck939afb02014-12-31 10:56:00 -0800946 return n;
Robert Olsson19baf832005-06-21 12:43:18 -0700947}
948
Alexander Duyck02525362015-01-22 15:51:39 -0800949/* Return the first fib alias matching TOS with
950 * priority less than or equal to PRIO.
951 */
Alexander Duyck79e5ad22015-02-25 15:31:51 -0800952static struct fib_alias *fib_find_alias(struct hlist_head *fah, u8 slen,
953 u8 tos, u32 prio)
Alexander Duyck02525362015-01-22 15:51:39 -0800954{
955 struct fib_alias *fa;
956
957 if (!fah)
958 return NULL;
959
Alexander Duyck56315f92015-02-25 15:31:31 -0800960 hlist_for_each_entry(fa, fah, fa_list) {
Alexander Duyck79e5ad22015-02-25 15:31:51 -0800961 if (fa->fa_slen < slen)
962 continue;
963 if (fa->fa_slen != slen)
964 break;
Alexander Duyck02525362015-01-22 15:51:39 -0800965 if (fa->fa_tos > tos)
966 continue;
967 if (fa->fa_info->fib_priority >= prio || fa->fa_tos < tos)
968 return fa;
969 }
970
971 return NULL;
972}
973
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800974static void trie_rebalance(struct trie *t, struct key_vector *tn)
Robert Olsson19baf832005-06-21 12:43:18 -0700975{
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800976 struct key_vector __rcu **cptr = t->tnode;
Robert Olsson19baf832005-06-21 12:43:18 -0700977
Alexander Duyckd5d64872015-03-04 15:02:18 -0800978 while (tn) {
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800979 struct key_vector *tp = node_parent(tn);
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800980
981 cptr = resize(t, tn);
982 if (!tp)
983 break;
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800984 tn = container_of(cptr, struct key_vector, tnode[0]);
Robert Olsson19baf832005-06-21 12:43:18 -0700985 }
Robert Olsson19baf832005-06-21 12:43:18 -0700986}
987
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800988static int fib_insert_node(struct trie *t, struct key_vector *tp,
Alexander Duyckd5d64872015-03-04 15:02:18 -0800989 struct fib_alias *new, t_key key)
Robert Olsson19baf832005-06-21 12:43:18 -0700990{
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800991 struct key_vector *n, *l;
Alexander Duyck836a0122014-12-31 10:56:06 -0800992
Alexander Duyckd5d64872015-03-04 15:02:18 -0800993 l = leaf_new(key, new);
Alexander Duyck79e5ad22015-02-25 15:31:51 -0800994 if (!l)
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800995 goto noleaf;
Alexander Duyckd5d64872015-03-04 15:02:18 -0800996
997 /* retrieve child from parent node */
998 if (tp)
Alexander Duyck754baf82015-03-06 09:54:14 -0800999 n = get_child(tp, get_index(key, tp));
Alexander Duyckd5d64872015-03-04 15:02:18 -08001000 else
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001001 n = rcu_dereference_rtnl(t->tnode[0]);
Alexander Duyck836a0122014-12-31 10:56:06 -08001002
1003 /* Case 2: n is a LEAF or a TNODE and the key doesn't match.
1004 *
1005 * Add a new tnode here
1006 * first tnode need some special handling
1007 * leaves us in position for handling as case 3
1008 */
1009 if (n) {
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001010 struct key_vector *tn;
Alexander Duyck836a0122014-12-31 10:56:06 -08001011
Alexander Duycke9b44012014-12-31 10:56:12 -08001012 tn = tnode_new(key, __fls(key ^ n->key), 1);
Alexander Duyck8d8e8102015-03-06 09:54:02 -08001013 if (!tn)
1014 goto notnode;
Olof Johansson91b9a272005-08-09 20:24:39 -07001015
Alexander Duyck836a0122014-12-31 10:56:06 -08001016 /* initialize routes out of node */
1017 NODE_INIT_PARENT(tn, tp);
1018 put_child(tn, get_index(key, tn) ^ 1, n);
Robert Olsson19baf832005-06-21 12:43:18 -07001019
Alexander Duyck836a0122014-12-31 10:56:06 -08001020 /* start adding routes into the node */
1021 put_child_root(tp, t, key, tn);
1022 node_set_parent(n, tn);
Robert Olsson19baf832005-06-21 12:43:18 -07001023
Alexander Duyck836a0122014-12-31 10:56:06 -08001024 /* parent now has a NULL spot where the leaf can go */
Alexander Duycke962f302014-12-10 21:49:22 -08001025 tp = tn;
Robert Olsson19baf832005-06-21 12:43:18 -07001026 }
Olof Johansson91b9a272005-08-09 20:24:39 -07001027
Alexander Duyck836a0122014-12-31 10:56:06 -08001028 /* Case 3: n is NULL, and will just insert a new leaf */
Alexander Duyckd5d64872015-03-04 15:02:18 -08001029 NODE_INIT_PARENT(l, tp);
1030 put_child_root(tp, t, key, l);
1031 trie_rebalance(t, tp);
Olof Johansson91b9a272005-08-09 20:24:39 -07001032
Alexander Duyckd5d64872015-03-04 15:02:18 -08001033 return 0;
Alexander Duyck8d8e8102015-03-06 09:54:02 -08001034notnode:
1035 node_free(l);
1036noleaf:
1037 return -ENOMEM;
Robert Olsson19baf832005-06-21 12:43:18 -07001038}
1039
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001040static int fib_insert_alias(struct trie *t, struct key_vector *tp,
1041 struct key_vector *l, struct fib_alias *new,
Alexander Duyckd5d64872015-03-04 15:02:18 -08001042 struct fib_alias *fa, t_key key)
1043{
1044 if (!l)
1045 return fib_insert_node(t, tp, new, key);
1046
1047 if (fa) {
1048 hlist_add_before_rcu(&new->fa_list, &fa->fa_list);
1049 } else {
1050 struct fib_alias *last;
1051
1052 hlist_for_each_entry(last, &l->leaf, fa_list) {
1053 if (new->fa_slen < last->fa_slen)
1054 break;
1055 fa = last;
1056 }
1057
1058 if (fa)
1059 hlist_add_behind_rcu(&new->fa_list, &fa->fa_list);
1060 else
1061 hlist_add_head_rcu(&new->fa_list, &l->leaf);
1062 }
1063
1064 /* if we added to the tail node then we need to update slen */
1065 if (l->slen < new->fa_slen) {
1066 l->slen = new->fa_slen;
1067 leaf_push_suffix(tp, l);
1068 }
1069
1070 return 0;
1071}
1072
1073/* Caller must hold RTNL. */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001074int fib_table_insert(struct fib_table *tb, struct fib_config *cfg)
Robert Olsson19baf832005-06-21 12:43:18 -07001075{
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001076 struct trie *t = (struct trie *)tb->tb_data;
Robert Olsson19baf832005-06-21 12:43:18 -07001077 struct fib_alias *fa, *new_fa;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001078 struct key_vector *l, *tp;
Robert Olsson19baf832005-06-21 12:43:18 -07001079 struct fib_info *fi;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001080 u8 plen = cfg->fc_dst_len;
1081 u8 slen = KEYLENGTH - plen;
Thomas Graf4e902c52006-08-17 18:14:52 -07001082 u8 tos = cfg->fc_tos;
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001083 u32 key;
Robert Olsson19baf832005-06-21 12:43:18 -07001084 int err;
Robert Olsson19baf832005-06-21 12:43:18 -07001085
Alexander Duyck5786ec62015-02-25 15:31:37 -08001086 if (plen > KEYLENGTH)
Robert Olsson19baf832005-06-21 12:43:18 -07001087 return -EINVAL;
1088
Thomas Graf4e902c52006-08-17 18:14:52 -07001089 key = ntohl(cfg->fc_dst);
Robert Olsson19baf832005-06-21 12:43:18 -07001090
Patrick McHardy2dfe55b2006-08-10 23:08:33 -07001091 pr_debug("Insert table=%u %08x/%d\n", tb->tb_id, key, plen);
Robert Olsson19baf832005-06-21 12:43:18 -07001092
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001093 if ((plen < KEYLENGTH) && (key << plen))
Robert Olsson19baf832005-06-21 12:43:18 -07001094 return -EINVAL;
1095
Thomas Graf4e902c52006-08-17 18:14:52 -07001096 fi = fib_create_info(cfg);
1097 if (IS_ERR(fi)) {
1098 err = PTR_ERR(fi);
Robert Olsson19baf832005-06-21 12:43:18 -07001099 goto err;
Thomas Graf4e902c52006-08-17 18:14:52 -07001100 }
Robert Olsson19baf832005-06-21 12:43:18 -07001101
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001102 l = fib_find_node(t, &tp, key);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001103 fa = l ? fib_find_alias(&l->leaf, slen, tos, fi->fib_priority) : NULL;
Robert Olsson19baf832005-06-21 12:43:18 -07001104
1105 /* Now fa, if non-NULL, points to the first fib alias
1106 * with the same keys [prefix,tos,priority], if such key already
1107 * exists or to the node before which we will insert new one.
1108 *
1109 * If fa is NULL, we will need to allocate a new one and
Alexander Duyck56315f92015-02-25 15:31:31 -08001110 * insert to the tail of the section matching the suffix length
1111 * of the new alias.
Robert Olsson19baf832005-06-21 12:43:18 -07001112 */
1113
Julian Anastasov936f6f82008-01-28 21:18:06 -08001114 if (fa && fa->fa_tos == tos &&
1115 fa->fa_info->fib_priority == fi->fib_priority) {
1116 struct fib_alias *fa_first, *fa_match;
Robert Olsson19baf832005-06-21 12:43:18 -07001117
1118 err = -EEXIST;
Thomas Graf4e902c52006-08-17 18:14:52 -07001119 if (cfg->fc_nlflags & NLM_F_EXCL)
Robert Olsson19baf832005-06-21 12:43:18 -07001120 goto out;
1121
Julian Anastasov936f6f82008-01-28 21:18:06 -08001122 /* We have 2 goals:
1123 * 1. Find exact match for type, scope, fib_info to avoid
1124 * duplicate routes
1125 * 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it
1126 */
1127 fa_match = NULL;
1128 fa_first = fa;
Alexander Duyck56315f92015-02-25 15:31:31 -08001129 hlist_for_each_entry_from(fa, fa_list) {
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001130 if ((fa->fa_slen != slen) || (fa->fa_tos != tos))
Julian Anastasov936f6f82008-01-28 21:18:06 -08001131 break;
1132 if (fa->fa_info->fib_priority != fi->fib_priority)
1133 break;
1134 if (fa->fa_type == cfg->fc_type &&
Julian Anastasov936f6f82008-01-28 21:18:06 -08001135 fa->fa_info == fi) {
1136 fa_match = fa;
1137 break;
1138 }
1139 }
1140
Thomas Graf4e902c52006-08-17 18:14:52 -07001141 if (cfg->fc_nlflags & NLM_F_REPLACE) {
Robert Olsson19baf832005-06-21 12:43:18 -07001142 struct fib_info *fi_drop;
1143 u8 state;
1144
Julian Anastasov936f6f82008-01-28 21:18:06 -08001145 fa = fa_first;
1146 if (fa_match) {
1147 if (fa == fa_match)
1148 err = 0;
Joonwoo Park67250332008-01-18 03:45:18 -08001149 goto out;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001150 }
Robert Olsson2373ce12005-08-25 13:01:29 -07001151 err = -ENOBUFS;
Christoph Lametere94b1762006-12-06 20:33:17 -08001152 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
Robert Olsson2373ce12005-08-25 13:01:29 -07001153 if (new_fa == NULL)
1154 goto out;
Robert Olsson19baf832005-06-21 12:43:18 -07001155
1156 fi_drop = fa->fa_info;
Robert Olsson2373ce12005-08-25 13:01:29 -07001157 new_fa->fa_tos = fa->fa_tos;
1158 new_fa->fa_info = fi;
Thomas Graf4e902c52006-08-17 18:14:52 -07001159 new_fa->fa_type = cfg->fc_type;
Robert Olsson19baf832005-06-21 12:43:18 -07001160 state = fa->fa_state;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001161 new_fa->fa_state = state & ~FA_S_ACCESSED;
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08001162 new_fa->fa_slen = fa->fa_slen;
Robert Olsson19baf832005-06-21 12:43:18 -07001163
Scott Feldman8e05fd72015-03-05 21:21:19 -08001164 err = netdev_switch_fib_ipv4_add(key, plen, fi,
1165 new_fa->fa_tos,
1166 cfg->fc_type,
1167 tb->tb_id);
1168 if (err) {
1169 netdev_switch_fib_ipv4_abort(fi);
1170 kmem_cache_free(fn_alias_kmem, new_fa);
1171 goto out;
1172 }
1173
Alexander Duyck56315f92015-02-25 15:31:31 -08001174 hlist_replace_rcu(&fa->fa_list, &new_fa->fa_list);
Scott Feldman8e05fd72015-03-05 21:21:19 -08001175
Robert Olsson2373ce12005-08-25 13:01:29 -07001176 alias_free_mem_rcu(fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001177
1178 fib_release_info(fi_drop);
1179 if (state & FA_S_ACCESSED)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001180 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Milan Kocianb8f55832007-05-23 14:55:06 -07001181 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen,
1182 tb->tb_id, &cfg->fc_nlinfo, NLM_F_REPLACE);
Robert Olsson19baf832005-06-21 12:43:18 -07001183
Olof Johansson91b9a272005-08-09 20:24:39 -07001184 goto succeeded;
Robert Olsson19baf832005-06-21 12:43:18 -07001185 }
1186 /* Error if we find a perfect match which
1187 * uses the same scope, type, and nexthop
1188 * information.
1189 */
Julian Anastasov936f6f82008-01-28 21:18:06 -08001190 if (fa_match)
1191 goto out;
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001192
Thomas Graf4e902c52006-08-17 18:14:52 -07001193 if (!(cfg->fc_nlflags & NLM_F_APPEND))
Julian Anastasov936f6f82008-01-28 21:18:06 -08001194 fa = fa_first;
Robert Olsson19baf832005-06-21 12:43:18 -07001195 }
1196 err = -ENOENT;
Thomas Graf4e902c52006-08-17 18:14:52 -07001197 if (!(cfg->fc_nlflags & NLM_F_CREATE))
Robert Olsson19baf832005-06-21 12:43:18 -07001198 goto out;
1199
1200 err = -ENOBUFS;
Christoph Lametere94b1762006-12-06 20:33:17 -08001201 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
Robert Olsson19baf832005-06-21 12:43:18 -07001202 if (new_fa == NULL)
1203 goto out;
1204
1205 new_fa->fa_info = fi;
1206 new_fa->fa_tos = tos;
Thomas Graf4e902c52006-08-17 18:14:52 -07001207 new_fa->fa_type = cfg->fc_type;
Robert Olsson19baf832005-06-21 12:43:18 -07001208 new_fa->fa_state = 0;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001209 new_fa->fa_slen = slen;
Robert Olsson19baf832005-06-21 12:43:18 -07001210
Scott Feldman8e05fd72015-03-05 21:21:19 -08001211 /* (Optionally) offload fib entry to switch hardware. */
1212 err = netdev_switch_fib_ipv4_add(key, plen, fi, tos,
1213 cfg->fc_type, tb->tb_id);
1214 if (err) {
1215 netdev_switch_fib_ipv4_abort(fi);
1216 goto out_free_new_fa;
1217 }
1218
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08001219 /* Insert new entry to the list. */
Alexander Duyckd5d64872015-03-04 15:02:18 -08001220 err = fib_insert_alias(t, tp, l, new_fa, fa, key);
1221 if (err)
Scott Feldman8e05fd72015-03-05 21:21:19 -08001222 goto out_sw_fib_del;
Robert Olsson19baf832005-06-21 12:43:18 -07001223
David S. Miller21d8c492011-04-14 14:49:37 -07001224 if (!plen)
1225 tb->tb_num_default++;
1226
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001227 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Thomas Graf4e902c52006-08-17 18:14:52 -07001228 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen, tb->tb_id,
Milan Kocianb8f55832007-05-23 14:55:06 -07001229 &cfg->fc_nlinfo, 0);
Robert Olsson19baf832005-06-21 12:43:18 -07001230succeeded:
1231 return 0;
Robert Olssonf835e472005-06-28 15:00:39 -07001232
Scott Feldman8e05fd72015-03-05 21:21:19 -08001233out_sw_fib_del:
1234 netdev_switch_fib_ipv4_del(key, plen, fi, tos, cfg->fc_type, tb->tb_id);
Robert Olssonf835e472005-06-28 15:00:39 -07001235out_free_new_fa:
1236 kmem_cache_free(fn_alias_kmem, new_fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001237out:
1238 fib_release_info(fi);
Olof Johansson91b9a272005-08-09 20:24:39 -07001239err:
Robert Olsson19baf832005-06-21 12:43:18 -07001240 return err;
1241}
1242
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001243static inline t_key prefix_mismatch(t_key key, struct key_vector *n)
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001244{
1245 t_key prefix = n->key;
1246
1247 return (key ^ prefix) & (prefix | -prefix);
1248}
1249
Alexander Duyck345e9b52014-12-31 10:56:24 -08001250/* should be called with rcu_read_lock */
David S. Miller22bd5b92011-03-11 19:54:08 -05001251int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00001252 struct fib_result *res, int fib_flags)
Robert Olsson19baf832005-06-21 12:43:18 -07001253{
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001254 struct trie *t = (struct trie *)tb->tb_data;
Alexander Duyck8274a972014-12-31 10:55:29 -08001255#ifdef CONFIG_IP_FIB_TRIE_STATS
1256 struct trie_use_stats __percpu *stats = t->stats;
1257#endif
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001258 const t_key key = ntohl(flp->daddr);
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001259 struct key_vector *n, *pn;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001260 struct fib_alias *fa;
Alexander Duyck71e8b672015-03-04 15:04:03 -08001261 unsigned long index;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001262 t_key cindex;
Robert Olsson19baf832005-06-21 12:43:18 -07001263
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001264 n = rcu_dereference(t->tnode[0]);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001265 if (!n)
Alexander Duyck345e9b52014-12-31 10:56:24 -08001266 return -EAGAIN;
Robert Olsson19baf832005-06-21 12:43:18 -07001267
1268#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -08001269 this_cpu_inc(stats->gets);
Robert Olsson19baf832005-06-21 12:43:18 -07001270#endif
1271
Alexander Duyckadaf9812014-12-31 10:55:47 -08001272 pn = n;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001273 cindex = 0;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001274
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001275 /* Step 1: Travel to the longest prefix match in the trie */
1276 for (;;) {
Alexander Duyck71e8b672015-03-04 15:04:03 -08001277 index = get_index(key, n);
Robert Olsson19baf832005-06-21 12:43:18 -07001278
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001279 /* This bit of code is a bit tricky but it combines multiple
1280 * checks into a single check. The prefix consists of the
1281 * prefix plus zeros for the "bits" in the prefix. The index
1282 * is the difference between the key and this value. From
1283 * this we can actually derive several pieces of data.
Alexander Duyck71e8b672015-03-04 15:04:03 -08001284 * if (index >= (1ul << bits))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001285 * we have a mismatch in skip bits and failed
Alexander Duyckb3832112015-01-22 15:51:08 -08001286 * else
1287 * we know the value is cindex
Alexander Duyck71e8b672015-03-04 15:04:03 -08001288 *
1289 * This check is safe even if bits == KEYLENGTH due to the
1290 * fact that we can only allocate a node with 32 bits if a
1291 * long is greater than 32 bits.
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001292 */
Alexander Duyck71e8b672015-03-04 15:04:03 -08001293 if (index >= (1ul << n->bits))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001294 break;
Robert Olsson19baf832005-06-21 12:43:18 -07001295
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001296 /* we have found a leaf. Prefixes have already been compared */
1297 if (IS_LEAF(n))
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001298 goto found;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001299
1300 /* only record pn and cindex if we are going to be chopping
1301 * bits later. Otherwise we are just wasting cycles.
1302 */
Alexander Duyck5405afd2014-12-31 10:57:08 -08001303 if (n->slen > n->pos) {
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001304 pn = n;
1305 cindex = index;
Olof Johansson91b9a272005-08-09 20:24:39 -07001306 }
1307
Alexander Duyck754baf82015-03-06 09:54:14 -08001308 n = get_child_rcu(n, index);
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001309 if (unlikely(!n))
Robert Olsson19baf832005-06-21 12:43:18 -07001310 goto backtrace;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001311 }
1312
1313 /* Step 2: Sort out leaves and begin backtracing for longest prefix */
1314 for (;;) {
1315 /* record the pointer where our next node pointer is stored */
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001316 struct key_vector __rcu **cptr = n->tnode;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001317
1318 /* This test verifies that none of the bits that differ
1319 * between the key and the prefix exist in the region of
1320 * the lsb and higher in the prefix.
1321 */
Alexander Duyck5405afd2014-12-31 10:57:08 -08001322 if (unlikely(prefix_mismatch(key, n)) || (n->slen == n->pos))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001323 goto backtrace;
1324
1325 /* exit out and process leaf */
1326 if (unlikely(IS_LEAF(n)))
1327 break;
1328
1329 /* Don't bother recording parent info. Since we are in
1330 * prefix match mode we will have to come back to wherever
1331 * we started this traversal anyway
1332 */
1333
1334 while ((n = rcu_dereference(*cptr)) == NULL) {
1335backtrace:
1336#ifdef CONFIG_IP_FIB_TRIE_STATS
1337 if (!n)
1338 this_cpu_inc(stats->null_node_hit);
1339#endif
1340 /* If we are at cindex 0 there are no more bits for
1341 * us to strip at this level so we must ascend back
1342 * up one level to see if there are any more bits to
1343 * be stripped there.
1344 */
1345 while (!cindex) {
1346 t_key pkey = pn->key;
1347
1348 pn = node_parent_rcu(pn);
1349 if (unlikely(!pn))
Alexander Duyck345e9b52014-12-31 10:56:24 -08001350 return -EAGAIN;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001351#ifdef CONFIG_IP_FIB_TRIE_STATS
1352 this_cpu_inc(stats->backtrack);
1353#endif
1354 /* Get Child's index */
1355 cindex = get_index(pkey, pn);
1356 }
1357
1358 /* strip the least significant bit from the cindex */
1359 cindex &= cindex - 1;
1360
1361 /* grab pointer for next child node */
Alexander Duyck41b489f2015-03-04 15:02:33 -08001362 cptr = &pn->tnode[cindex];
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001363 }
Robert Olsson19baf832005-06-21 12:43:18 -07001364 }
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001365
Robert Olsson19baf832005-06-21 12:43:18 -07001366found:
Alexander Duyck71e8b672015-03-04 15:04:03 -08001367 /* this line carries forward the xor from earlier in the function */
1368 index = key ^ n->key;
1369
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001370 /* Step 3: Process the leaf, if that fails fall back to backtracing */
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001371 hlist_for_each_entry_rcu(fa, &n->leaf, fa_list) {
1372 struct fib_info *fi = fa->fa_info;
1373 int nhsel, err;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001374
Alexander Duyck71e8b672015-03-04 15:04:03 -08001375 if ((index >= (1ul << fa->fa_slen)) &&
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001376 ((BITS_PER_LONG > KEYLENGTH) || (fa->fa_slen != KEYLENGTH)))
Alexander Duyck71e8b672015-03-04 15:04:03 -08001377 continue;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001378 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
1379 continue;
1380 if (fi->fib_dead)
1381 continue;
1382 if (fa->fa_info->fib_scope < flp->flowi4_scope)
1383 continue;
1384 fib_alias_accessed(fa);
1385 err = fib_props[fa->fa_type].error;
1386 if (unlikely(err < 0)) {
Alexander Duyck345e9b52014-12-31 10:56:24 -08001387#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001388 this_cpu_inc(stats->semantic_match_passed);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001389#endif
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001390 return err;
1391 }
1392 if (fi->fib_flags & RTNH_F_DEAD)
1393 continue;
1394 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
1395 const struct fib_nh *nh = &fi->fib_nh[nhsel];
1396
1397 if (nh->nh_flags & RTNH_F_DEAD)
Alexander Duyck345e9b52014-12-31 10:56:24 -08001398 continue;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001399 if (flp->flowi4_oif && flp->flowi4_oif != nh->nh_oif)
1400 continue;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001401
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001402 if (!(fib_flags & FIB_LOOKUP_NOREF))
1403 atomic_inc(&fi->fib_clntref);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001404
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001405 res->prefixlen = KEYLENGTH - fa->fa_slen;
1406 res->nh_sel = nhsel;
1407 res->type = fa->fa_type;
1408 res->scope = fi->fib_scope;
1409 res->fi = fi;
1410 res->table = tb;
1411 res->fa_head = &n->leaf;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001412#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001413 this_cpu_inc(stats->semantic_match_passed);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001414#endif
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001415 return err;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001416 }
Alexander Duyck345e9b52014-12-31 10:56:24 -08001417 }
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08001418#ifdef CONFIG_IP_FIB_TRIE_STATS
1419 this_cpu_inc(stats->semantic_match_miss);
1420#endif
Alexander Duyck345e9b52014-12-31 10:56:24 -08001421 goto backtrace;
Robert Olsson19baf832005-06-21 12:43:18 -07001422}
Florian Westphal6fc01432011-08-25 13:46:12 +02001423EXPORT_SYMBOL_GPL(fib_table_lookup);
Robert Olsson19baf832005-06-21 12:43:18 -07001424
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001425static void fib_remove_alias(struct trie *t, struct key_vector *tp,
1426 struct key_vector *l, struct fib_alias *old)
Alexander Duyckd5d64872015-03-04 15:02:18 -08001427{
1428 /* record the location of the previous list_info entry */
1429 struct hlist_node **pprev = old->fa_list.pprev;
1430 struct fib_alias *fa = hlist_entry(pprev, typeof(*fa), fa_list.next);
1431
1432 /* remove the fib_alias from the list */
1433 hlist_del_rcu(&old->fa_list);
1434
1435 /* if we emptied the list this leaf will be freed and we can sort
1436 * out parent suffix lengths as a part of trie_rebalance
1437 */
1438 if (hlist_empty(&l->leaf)) {
1439 put_child_root(tp, t, l->key, NULL);
1440 node_free(l);
1441 trie_rebalance(t, tp);
1442 return;
1443 }
1444
1445 /* only access fa if it is pointing at the last valid hlist_node */
1446 if (*pprev)
1447 return;
1448
1449 /* update the trie with the latest suffix length */
1450 l->slen = fa->fa_slen;
1451 leaf_pull_suffix(tp, l);
1452}
1453
1454/* Caller must hold RTNL. */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001455int fib_table_delete(struct fib_table *tb, struct fib_config *cfg)
Robert Olsson19baf832005-06-21 12:43:18 -07001456{
1457 struct trie *t = (struct trie *) tb->tb_data;
Robert Olsson19baf832005-06-21 12:43:18 -07001458 struct fib_alias *fa, *fa_to_delete;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001459 struct key_vector *l, *tp;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001460 u8 plen = cfg->fc_dst_len;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001461 u8 slen = KEYLENGTH - plen;
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001462 u8 tos = cfg->fc_tos;
1463 u32 key;
Olof Johansson91b9a272005-08-09 20:24:39 -07001464
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001465 if (plen > KEYLENGTH)
Robert Olsson19baf832005-06-21 12:43:18 -07001466 return -EINVAL;
1467
Thomas Graf4e902c52006-08-17 18:14:52 -07001468 key = ntohl(cfg->fc_dst);
Robert Olsson19baf832005-06-21 12:43:18 -07001469
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001470 if ((plen < KEYLENGTH) && (key << plen))
Robert Olsson19baf832005-06-21 12:43:18 -07001471 return -EINVAL;
1472
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001473 l = fib_find_node(t, &tp, key);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001474 if (!l)
Robert Olsson19baf832005-06-21 12:43:18 -07001475 return -ESRCH;
1476
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001477 fa = fib_find_alias(&l->leaf, slen, tos, 0);
Robert Olsson19baf832005-06-21 12:43:18 -07001478 if (!fa)
1479 return -ESRCH;
1480
Stephen Hemminger0c7770c2005-08-23 21:59:41 -07001481 pr_debug("Deleting %08x/%d tos=%d t=%p\n", key, plen, tos, t);
Robert Olsson19baf832005-06-21 12:43:18 -07001482
1483 fa_to_delete = NULL;
Alexander Duyck56315f92015-02-25 15:31:31 -08001484 hlist_for_each_entry_from(fa, fa_list) {
Robert Olsson19baf832005-06-21 12:43:18 -07001485 struct fib_info *fi = fa->fa_info;
1486
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001487 if ((fa->fa_slen != slen) || (fa->fa_tos != tos))
Robert Olsson19baf832005-06-21 12:43:18 -07001488 break;
1489
Thomas Graf4e902c52006-08-17 18:14:52 -07001490 if ((!cfg->fc_type || fa->fa_type == cfg->fc_type) &&
1491 (cfg->fc_scope == RT_SCOPE_NOWHERE ||
David S. Miller37e826c2011-03-24 18:06:47 -07001492 fa->fa_info->fib_scope == cfg->fc_scope) &&
Julian Anastasov74cb3c12011-03-19 12:13:46 +00001493 (!cfg->fc_prefsrc ||
1494 fi->fib_prefsrc == cfg->fc_prefsrc) &&
Thomas Graf4e902c52006-08-17 18:14:52 -07001495 (!cfg->fc_protocol ||
1496 fi->fib_protocol == cfg->fc_protocol) &&
1497 fib_nh_match(cfg, fi) == 0) {
Robert Olsson19baf832005-06-21 12:43:18 -07001498 fa_to_delete = fa;
1499 break;
1500 }
1501 }
1502
Olof Johansson91b9a272005-08-09 20:24:39 -07001503 if (!fa_to_delete)
1504 return -ESRCH;
Robert Olsson19baf832005-06-21 12:43:18 -07001505
Scott Feldman8e05fd72015-03-05 21:21:19 -08001506 netdev_switch_fib_ipv4_del(key, plen, fa_to_delete->fa_info, tos,
1507 cfg->fc_type, tb->tb_id);
1508
Alexander Duyckd5d64872015-03-04 15:02:18 -08001509 rtmsg_fib(RTM_DELROUTE, htonl(key), fa_to_delete, plen, tb->tb_id,
Milan Kocianb8f55832007-05-23 14:55:06 -07001510 &cfg->fc_nlinfo, 0);
Robert Olsson19baf832005-06-21 12:43:18 -07001511
David S. Miller21d8c492011-04-14 14:49:37 -07001512 if (!plen)
1513 tb->tb_num_default--;
1514
Alexander Duyckd5d64872015-03-04 15:02:18 -08001515 fib_remove_alias(t, tp, l, fa_to_delete);
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001516
Alexander Duyckd5d64872015-03-04 15:02:18 -08001517 if (fa_to_delete->fa_state & FA_S_ACCESSED)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001518 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Olof Johansson91b9a272005-08-09 20:24:39 -07001519
Alexander Duyckd5d64872015-03-04 15:02:18 -08001520 fib_release_info(fa_to_delete->fa_info);
1521 alias_free_mem_rcu(fa_to_delete);
Olof Johansson91b9a272005-08-09 20:24:39 -07001522 return 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001523}
1524
Alexander Duyck8be33e92015-03-04 14:59:19 -08001525/* Scan for the next leaf starting at the provided key value */
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001526static struct key_vector *leaf_walk_rcu(struct key_vector **tn, t_key key)
Robert Olsson19baf832005-06-21 12:43:18 -07001527{
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001528 struct key_vector *pn, *n = *tn;
Alexander Duyck8be33e92015-03-04 14:59:19 -08001529 unsigned long cindex;
Robert Olsson19baf832005-06-21 12:43:18 -07001530
Alexander Duyck8be33e92015-03-04 14:59:19 -08001531 /* record parent node for backtracing */
1532 pn = n;
1533 cindex = n ? get_index(key, n) : 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001534
Alexander Duyck8be33e92015-03-04 14:59:19 -08001535 /* this loop is meant to try and find the key in the trie */
1536 while (n) {
1537 unsigned long idx = get_index(key, n);
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001538
Alexander Duyck8be33e92015-03-04 14:59:19 -08001539 /* guarantee forward progress on the keys */
1540 if (IS_LEAF(n) && (n->key >= key))
1541 goto found;
1542 if (idx >= (1ul << n->bits))
1543 break;
1544
1545 /* record parent and next child index */
1546 pn = n;
1547 cindex = idx;
1548
1549 /* descend into the next child */
Alexander Duyck754baf82015-03-06 09:54:14 -08001550 n = get_child_rcu(pn, cindex++);
Alexander Duyck8be33e92015-03-04 14:59:19 -08001551 }
1552
1553 /* this loop will search for the next leaf with a greater key */
1554 while (pn) {
1555 /* if we exhausted the parent node we will need to climb */
1556 if (cindex >= (1ul << pn->bits)) {
1557 t_key pkey = pn->key;
1558
1559 pn = node_parent_rcu(pn);
1560 if (!pn)
1561 break;
1562
1563 cindex = get_index(pkey, pn) + 1;
1564 continue;
Robert Olsson19baf832005-06-21 12:43:18 -07001565 }
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001566
Alexander Duyck8be33e92015-03-04 14:59:19 -08001567 /* grab the next available node */
Alexander Duyck754baf82015-03-06 09:54:14 -08001568 n = get_child_rcu(pn, cindex++);
Alexander Duyck8be33e92015-03-04 14:59:19 -08001569 if (!n)
1570 continue;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001571
Alexander Duyck8be33e92015-03-04 14:59:19 -08001572 /* no need to compare keys since we bumped the index */
1573 if (IS_LEAF(n))
1574 goto found;
1575
1576 /* Rescan start scanning in new node */
1577 pn = n;
1578 cindex = 0;
1579 }
1580
1581 *tn = pn;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001582 return NULL; /* Root of trie */
Alexander Duyck8be33e92015-03-04 14:59:19 -08001583found:
1584 /* if we are at the limit for keys just return NULL for the tnode */
1585 *tn = (n->key == KEY_MAX) ? NULL : pn;
1586 return n;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001587}
1588
Scott Feldman104616e2015-03-05 21:21:16 -08001589/* Caller must hold RTNL */
1590void fib_table_flush_external(struct fib_table *tb)
1591{
1592 struct trie *t = (struct trie *)tb->tb_data;
1593 struct fib_alias *fa;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001594 struct key_vector *n, *pn;
Scott Feldman104616e2015-03-05 21:21:16 -08001595 unsigned long cindex;
Scott Feldman104616e2015-03-05 21:21:16 -08001596
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001597 n = rcu_dereference(t->tnode[0]);
Scott Feldman104616e2015-03-05 21:21:16 -08001598 if (!n)
1599 return;
1600
1601 pn = NULL;
1602 cindex = 0;
1603
1604 while (IS_TNODE(n)) {
1605 /* record pn and cindex for leaf walking */
1606 pn = n;
1607 cindex = 1ul << n->bits;
1608backtrace:
1609 /* walk trie in reverse order */
1610 do {
1611 while (!(cindex--)) {
1612 t_key pkey = pn->key;
1613
Scott Feldman104616e2015-03-05 21:21:16 -08001614 /* if we got the root we are done */
Alexander Duyck72be7262015-03-06 09:53:56 -08001615 pn = node_parent(pn);
Scott Feldman104616e2015-03-05 21:21:16 -08001616 if (!pn)
1617 return;
1618
1619 cindex = get_index(pkey, pn);
1620 }
1621
1622 /* grab the next available node */
Alexander Duyck754baf82015-03-06 09:54:14 -08001623 n = get_child(pn, cindex);
Scott Feldman104616e2015-03-05 21:21:16 -08001624 } while (!n);
1625 }
1626
1627 hlist_for_each_entry(fa, &n->leaf, fa_list) {
1628 struct fib_info *fi = fa->fa_info;
1629
Alexander Duyck72be7262015-03-06 09:53:56 -08001630 if (!fi || !(fi->fib_flags & RTNH_F_EXTERNAL))
1631 continue;
1632
1633 netdev_switch_fib_ipv4_del(n->key,
1634 KEYLENGTH - fa->fa_slen,
1635 fi, fa->fa_tos,
1636 fa->fa_type, tb->tb_id);
Scott Feldman104616e2015-03-05 21:21:16 -08001637 }
1638
1639 /* if trie is leaf only loop is completed */
1640 if (pn)
1641 goto backtrace;
1642}
1643
Alexander Duyck8be33e92015-03-04 14:59:19 -08001644/* Caller must hold RTNL. */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001645int fib_table_flush(struct fib_table *tb)
Robert Olsson19baf832005-06-21 12:43:18 -07001646{
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001647 struct trie *t = (struct trie *)tb->tb_data;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001648 struct key_vector *n, *pn;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001649 struct hlist_node *tmp;
1650 struct fib_alias *fa;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001651 unsigned long cindex;
1652 unsigned char slen;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001653 int found = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001654
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001655 n = rcu_dereference(t->tnode[0]);
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001656 if (!n)
1657 goto flush_complete;
Robert Olsson19baf832005-06-21 12:43:18 -07001658
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001659 pn = NULL;
1660 cindex = 0;
1661
1662 while (IS_TNODE(n)) {
1663 /* record pn and cindex for leaf walking */
1664 pn = n;
1665 cindex = 1ul << n->bits;
1666backtrace:
1667 /* walk trie in reverse order */
1668 do {
1669 while (!(cindex--)) {
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001670 struct key_vector __rcu **cptr;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001671 t_key pkey = pn->key;
1672
1673 n = pn;
1674 pn = node_parent(n);
1675
1676 /* resize completed node */
Alexander Duyck8d8e8102015-03-06 09:54:02 -08001677 cptr = resize(t, n);
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001678
1679 /* if we got the root we are done */
1680 if (!pn)
1681 goto flush_complete;
1682
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001683 pn = container_of(cptr, struct key_vector,
1684 tnode[0]);
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001685 cindex = get_index(pkey, pn);
1686 }
1687
1688 /* grab the next available node */
Alexander Duyck754baf82015-03-06 09:54:14 -08001689 n = get_child(pn, cindex);
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001690 } while (!n);
1691 }
1692
1693 /* track slen in case any prefixes survive */
1694 slen = 0;
1695
1696 hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
1697 struct fib_info *fi = fa->fa_info;
1698
1699 if (fi && (fi->fib_flags & RTNH_F_DEAD)) {
Scott Feldman8e05fd72015-03-05 21:21:19 -08001700 netdev_switch_fib_ipv4_del(n->key,
1701 KEYLENGTH - fa->fa_slen,
1702 fi, fa->fa_tos,
1703 fa->fa_type, tb->tb_id);
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001704 hlist_del_rcu(&fa->fa_list);
1705 fib_release_info(fa->fa_info);
1706 alias_free_mem_rcu(fa);
1707 found++;
1708
1709 continue;
Alexander Duyck64c62722015-01-22 15:51:45 -08001710 }
1711
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001712 slen = fa->fa_slen;
Robert Olsson19baf832005-06-21 12:43:18 -07001713 }
1714
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001715 /* update leaf slen */
1716 n->slen = slen;
1717
1718 if (hlist_empty(&n->leaf)) {
1719 put_child_root(pn, t, n->key, NULL);
1720 node_free(n);
1721 } else {
Alexander Duyckd5d64872015-03-04 15:02:18 -08001722 leaf_pull_suffix(pn, n);
Alexander Duyck64c62722015-01-22 15:51:45 -08001723 }
Robert Olsson19baf832005-06-21 12:43:18 -07001724
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001725 /* if trie is leaf only loop is completed */
1726 if (pn)
1727 goto backtrace;
1728flush_complete:
Stephen Hemminger0c7770c2005-08-23 21:59:41 -07001729 pr_debug("trie_flush found=%d\n", found);
Robert Olsson19baf832005-06-21 12:43:18 -07001730 return found;
1731}
1732
Alexander Duycka7e53532015-03-04 15:02:44 -08001733static void __trie_free_rcu(struct rcu_head *head)
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00001734{
Alexander Duycka7e53532015-03-04 15:02:44 -08001735 struct fib_table *tb = container_of(head, struct fib_table, rcu);
Alexander Duyck8274a972014-12-31 10:55:29 -08001736#ifdef CONFIG_IP_FIB_TRIE_STATS
1737 struct trie *t = (struct trie *)tb->tb_data;
1738
1739 free_percpu(t->stats);
1740#endif /* CONFIG_IP_FIB_TRIE_STATS */
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00001741 kfree(tb);
1742}
1743
Alexander Duycka7e53532015-03-04 15:02:44 -08001744void fib_free_table(struct fib_table *tb)
1745{
1746 call_rcu(&tb->rcu, __trie_free_rcu);
1747}
1748
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001749static int fn_trie_dump_leaf(struct key_vector *l, struct fib_table *tb,
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001750 struct sk_buff *skb, struct netlink_callback *cb)
Robert Olsson19baf832005-06-21 12:43:18 -07001751{
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001752 __be32 xkey = htonl(l->key);
Robert Olsson19baf832005-06-21 12:43:18 -07001753 struct fib_alias *fa;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001754 int i, s_i;
Robert Olsson19baf832005-06-21 12:43:18 -07001755
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001756 s_i = cb->args[4];
Robert Olsson19baf832005-06-21 12:43:18 -07001757 i = 0;
1758
Robert Olsson2373ce12005-08-25 13:01:29 -07001759 /* rcu_read_lock is hold by caller */
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001760 hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
Robert Olsson19baf832005-06-21 12:43:18 -07001761 if (i < s_i) {
1762 i++;
1763 continue;
1764 }
Robert Olsson19baf832005-06-21 12:43:18 -07001765
Eric W. Biederman15e47302012-09-07 20:12:54 +00001766 if (fib_dump_info(skb, NETLINK_CB(cb->skb).portid,
Robert Olsson19baf832005-06-21 12:43:18 -07001767 cb->nlh->nlmsg_seq,
1768 RTM_NEWROUTE,
1769 tb->tb_id,
1770 fa->fa_type,
Thomas Grafbe403ea2006-08-17 18:15:17 -07001771 xkey,
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08001772 KEYLENGTH - fa->fa_slen,
Robert Olsson19baf832005-06-21 12:43:18 -07001773 fa->fa_tos,
Stephen Hemminger64347f72008-01-22 21:55:01 -08001774 fa->fa_info, NLM_F_MULTI) < 0) {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001775 cb->args[4] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001776 return -1;
1777 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001778 i++;
Robert Olsson19baf832005-06-21 12:43:18 -07001779 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001780
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001781 cb->args[4] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001782 return skb->len;
1783}
1784
Alexander Duycka7e53532015-03-04 15:02:44 -08001785/* rcu_read_lock needs to be hold by caller from readside */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001786int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
1787 struct netlink_callback *cb)
Robert Olsson19baf832005-06-21 12:43:18 -07001788{
Alexander Duyck8be33e92015-03-04 14:59:19 -08001789 struct trie *t = (struct trie *)tb->tb_data;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001790 struct key_vector *l, *tp;
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001791 /* Dump starting at last key.
1792 * Note: 0.0.0.0/0 (ie default) is first key.
1793 */
Alexander Duyck8be33e92015-03-04 14:59:19 -08001794 int count = cb->args[2];
1795 t_key key = cb->args[3];
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001796
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001797 tp = rcu_dereference_rtnl(t->tnode[0]);
Alexander Duyck8be33e92015-03-04 14:59:19 -08001798
1799 while ((l = leaf_walk_rcu(&tp, key)) != NULL) {
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001800 if (fn_trie_dump_leaf(l, tb, skb, cb) < 0) {
Alexander Duyck8be33e92015-03-04 14:59:19 -08001801 cb->args[3] = key;
1802 cb->args[2] = count;
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001803 return -1;
Robert Olsson19baf832005-06-21 12:43:18 -07001804 }
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001805
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001806 ++count;
Alexander Duyck8be33e92015-03-04 14:59:19 -08001807 key = l->key + 1;
1808
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001809 memset(&cb->args[4], 0,
1810 sizeof(cb->args) - 4*sizeof(cb->args[0]));
Alexander Duyck8be33e92015-03-04 14:59:19 -08001811
1812 /* stop loop if key wrapped back to 0 */
1813 if (key < l->key)
1814 break;
Robert Olsson19baf832005-06-21 12:43:18 -07001815 }
Alexander Duyck8be33e92015-03-04 14:59:19 -08001816
Alexander Duyck8be33e92015-03-04 14:59:19 -08001817 cb->args[3] = key;
1818 cb->args[2] = count;
1819
Robert Olsson19baf832005-06-21 12:43:18 -07001820 return skb->len;
Robert Olsson19baf832005-06-21 12:43:18 -07001821}
1822
David S. Miller5348ba82011-02-01 15:30:56 -08001823void __init fib_trie_init(void)
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001824{
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001825 fn_alias_kmem = kmem_cache_create("ip_fib_alias",
1826 sizeof(struct fib_alias),
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -08001827 0, SLAB_PANIC, NULL);
1828
1829 trie_leaf_kmem = kmem_cache_create("ip_fib_trie",
Alexander Duyck41b489f2015-03-04 15:02:33 -08001830 LEAF_SIZE,
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -08001831 0, SLAB_PANIC, NULL);
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001832}
Robert Olsson19baf832005-06-21 12:43:18 -07001833
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001834
David S. Miller5348ba82011-02-01 15:30:56 -08001835struct fib_table *fib_trie_table(u32 id)
Robert Olsson19baf832005-06-21 12:43:18 -07001836{
1837 struct fib_table *tb;
1838 struct trie *t;
1839
Robert Olsson19baf832005-06-21 12:43:18 -07001840 tb = kmalloc(sizeof(struct fib_table) + sizeof(struct trie),
1841 GFP_KERNEL);
1842 if (tb == NULL)
1843 return NULL;
1844
1845 tb->tb_id = id;
Denis V. Lunev971b8932007-12-08 00:32:23 -08001846 tb->tb_default = -1;
David S. Miller21d8c492011-04-14 14:49:37 -07001847 tb->tb_num_default = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001848
1849 t = (struct trie *) tb->tb_data;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001850 RCU_INIT_POINTER(t->tnode[0], NULL);
Alexander Duyck8274a972014-12-31 10:55:29 -08001851#ifdef CONFIG_IP_FIB_TRIE_STATS
1852 t->stats = alloc_percpu(struct trie_use_stats);
1853 if (!t->stats) {
1854 kfree(tb);
1855 tb = NULL;
1856 }
1857#endif
Robert Olsson19baf832005-06-21 12:43:18 -07001858
Robert Olsson19baf832005-06-21 12:43:18 -07001859 return tb;
1860}
1861
Robert Olsson19baf832005-06-21 12:43:18 -07001862#ifdef CONFIG_PROC_FS
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001863/* Depth first Trie walk iterator */
1864struct fib_trie_iter {
Denis V. Lunev1c340b22008-01-10 03:27:17 -08001865 struct seq_net_private p;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001866 struct fib_table *tb;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001867 struct key_vector *tnode;
Eric Dumazeta034ee32010-09-09 23:32:28 +00001868 unsigned int index;
1869 unsigned int depth;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001870};
Robert Olsson19baf832005-06-21 12:43:18 -07001871
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001872static struct key_vector *fib_trie_get_next(struct fib_trie_iter *iter)
Robert Olsson19baf832005-06-21 12:43:18 -07001873{
Alexander Duyck98293e82014-12-31 10:56:18 -08001874 unsigned long cindex = iter->index;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001875 struct key_vector *tn = iter->tnode;
1876 struct key_vector *p;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001877
Eric W. Biederman6640e692007-01-24 14:42:04 -08001878 /* A single entry routing table */
1879 if (!tn)
1880 return NULL;
1881
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001882 pr_debug("get_next iter={node=%p index=%d depth=%d}\n",
1883 iter->tnode, iter->index, iter->depth);
1884rescan:
Alexander Duyck2e1ac882015-03-06 09:54:21 -08001885 while (cindex < child_length(tn)) {
Alexander Duyck754baf82015-03-06 09:54:14 -08001886 struct key_vector *n = get_child_rcu(tn, cindex);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001887
1888 if (n) {
1889 if (IS_LEAF(n)) {
1890 iter->tnode = tn;
1891 iter->index = cindex + 1;
1892 } else {
1893 /* push down one level */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001894 iter->tnode = n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001895 iter->index = 0;
1896 ++iter->depth;
1897 }
1898 return n;
1899 }
1900
1901 ++cindex;
1902 }
1903
1904 /* Current node exhausted, pop back up */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001905 p = node_parent_rcu(tn);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001906 if (p) {
Alexander Duycke9b44012014-12-31 10:56:12 -08001907 cindex = get_index(tn->key, p) + 1;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001908 tn = p;
1909 --iter->depth;
1910 goto rescan;
1911 }
1912
1913 /* got root? */
Robert Olsson19baf832005-06-21 12:43:18 -07001914 return NULL;
1915}
1916
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001917static struct key_vector *fib_trie_get_first(struct fib_trie_iter *iter,
1918 struct trie *t)
Robert Olsson19baf832005-06-21 12:43:18 -07001919{
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001920 struct key_vector *n;
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08001921
Stephen Hemminger132adf52007-03-08 20:44:43 -08001922 if (!t)
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08001923 return NULL;
1924
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001925 n = rcu_dereference(t->tnode[0]);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001926 if (!n)
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08001927 return NULL;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001928
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001929 if (IS_TNODE(n)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08001930 iter->tnode = n;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001931 iter->index = 0;
1932 iter->depth = 1;
1933 } else {
1934 iter->tnode = NULL;
1935 iter->index = 0;
1936 iter->depth = 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001937 }
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001938
1939 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07001940}
1941
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001942static void trie_collect_stats(struct trie *t, struct trie_stat *s)
Robert Olsson19baf832005-06-21 12:43:18 -07001943{
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001944 struct key_vector *n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001945 struct fib_trie_iter iter;
Robert Olsson19baf832005-06-21 12:43:18 -07001946
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001947 memset(s, 0, sizeof(*s));
Robert Olsson19baf832005-06-21 12:43:18 -07001948
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001949 rcu_read_lock();
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001950 for (n = fib_trie_get_first(&iter, t); n; n = fib_trie_get_next(&iter)) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001951 if (IS_LEAF(n)) {
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001952 struct fib_alias *fa;
Stephen Hemminger93672292008-01-22 21:54:05 -08001953
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001954 s->leaves++;
1955 s->totdepth += iter.depth;
1956 if (iter.depth > s->maxdepth)
1957 s->maxdepth = iter.depth;
Stephen Hemminger93672292008-01-22 21:54:05 -08001958
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001959 hlist_for_each_entry_rcu(fa, &n->leaf, fa_list)
Stephen Hemminger93672292008-01-22 21:54:05 -08001960 ++s->prefixes;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001961 } else {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001962 s->tnodes++;
Alexander Duyckadaf9812014-12-31 10:55:47 -08001963 if (n->bits < MAX_STAT_DEPTH)
1964 s->nodesizes[n->bits]++;
Alexander Duyck6e22d172015-03-06 09:54:39 -08001965 s->nullpointers += tn_info(n)->empty_children;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001966 }
1967 }
1968 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07001969}
1970
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001971/*
Robert Olsson19baf832005-06-21 12:43:18 -07001972 * This outputs /proc/net/fib_triestats
Robert Olsson19baf832005-06-21 12:43:18 -07001973 */
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001974static void trie_show_stats(struct seq_file *seq, struct trie_stat *stat)
Robert Olsson19baf832005-06-21 12:43:18 -07001975{
Eric Dumazeta034ee32010-09-09 23:32:28 +00001976 unsigned int i, max, pointers, bytes, avdepth;
Robert Olsson19baf832005-06-21 12:43:18 -07001977
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001978 if (stat->leaves)
1979 avdepth = stat->totdepth*100 / stat->leaves;
1980 else
1981 avdepth = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001982
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001983 seq_printf(seq, "\tAver depth: %u.%02d\n",
1984 avdepth / 100, avdepth % 100);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001985 seq_printf(seq, "\tMax depth: %u\n", stat->maxdepth);
Robert Olsson19baf832005-06-21 12:43:18 -07001986
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001987 seq_printf(seq, "\tLeaves: %u\n", stat->leaves);
Alexander Duyck41b489f2015-03-04 15:02:33 -08001988 bytes = LEAF_SIZE * stat->leaves;
Stephen Hemminger93672292008-01-22 21:54:05 -08001989
1990 seq_printf(seq, "\tPrefixes: %u\n", stat->prefixes);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001991 bytes += sizeof(struct fib_alias) * stat->prefixes;
Stephen Hemminger93672292008-01-22 21:54:05 -08001992
Stephen Hemminger187b5182008-01-12 20:55:55 -08001993 seq_printf(seq, "\tInternal nodes: %u\n\t", stat->tnodes);
Alexander Duyck41b489f2015-03-04 15:02:33 -08001994 bytes += TNODE_SIZE(0) * stat->tnodes;
Robert Olsson19baf832005-06-21 12:43:18 -07001995
Robert Olsson06ef9212006-03-20 21:35:01 -08001996 max = MAX_STAT_DEPTH;
1997 while (max > 0 && stat->nodesizes[max-1] == 0)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001998 max--;
Robert Olsson19baf832005-06-21 12:43:18 -07001999
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002000 pointers = 0;
Jerry Snitselaarf585a992013-07-22 12:01:58 -07002001 for (i = 1; i < max; i++)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002002 if (stat->nodesizes[i] != 0) {
Stephen Hemminger187b5182008-01-12 20:55:55 -08002003 seq_printf(seq, " %u: %u", i, stat->nodesizes[i]);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002004 pointers += (1<<i) * stat->nodesizes[i];
2005 }
2006 seq_putc(seq, '\n');
Stephen Hemminger187b5182008-01-12 20:55:55 -08002007 seq_printf(seq, "\tPointers: %u\n", pointers);
Robert Olsson19baf832005-06-21 12:43:18 -07002008
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002009 bytes += sizeof(struct key_vector *) * pointers;
Stephen Hemminger187b5182008-01-12 20:55:55 -08002010 seq_printf(seq, "Null ptrs: %u\n", stat->nullpointers);
2011 seq_printf(seq, "Total size: %u kB\n", (bytes + 1023) / 1024);
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002012}
Robert Olsson19baf832005-06-21 12:43:18 -07002013
2014#ifdef CONFIG_IP_FIB_TRIE_STATS
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002015static void trie_show_usage(struct seq_file *seq,
Alexander Duyck8274a972014-12-31 10:55:29 -08002016 const struct trie_use_stats __percpu *stats)
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002017{
Alexander Duyck8274a972014-12-31 10:55:29 -08002018 struct trie_use_stats s = { 0 };
2019 int cpu;
2020
2021 /* loop through all of the CPUs and gather up the stats */
2022 for_each_possible_cpu(cpu) {
2023 const struct trie_use_stats *pcpu = per_cpu_ptr(stats, cpu);
2024
2025 s.gets += pcpu->gets;
2026 s.backtrack += pcpu->backtrack;
2027 s.semantic_match_passed += pcpu->semantic_match_passed;
2028 s.semantic_match_miss += pcpu->semantic_match_miss;
2029 s.null_node_hit += pcpu->null_node_hit;
2030 s.resize_node_skipped += pcpu->resize_node_skipped;
2031 }
2032
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002033 seq_printf(seq, "\nCounters:\n---------\n");
Alexander Duyck8274a972014-12-31 10:55:29 -08002034 seq_printf(seq, "gets = %u\n", s.gets);
2035 seq_printf(seq, "backtracks = %u\n", s.backtrack);
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002036 seq_printf(seq, "semantic match passed = %u\n",
Alexander Duyck8274a972014-12-31 10:55:29 -08002037 s.semantic_match_passed);
2038 seq_printf(seq, "semantic match miss = %u\n", s.semantic_match_miss);
2039 seq_printf(seq, "null node hit= %u\n", s.null_node_hit);
2040 seq_printf(seq, "skipped node resize = %u\n\n", s.resize_node_skipped);
Robert Olsson19baf832005-06-21 12:43:18 -07002041}
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002042#endif /* CONFIG_IP_FIB_TRIE_STATS */
2043
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002044static void fib_table_print(struct seq_file *seq, struct fib_table *tb)
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002045{
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002046 if (tb->tb_id == RT_TABLE_LOCAL)
2047 seq_puts(seq, "Local:\n");
2048 else if (tb->tb_id == RT_TABLE_MAIN)
2049 seq_puts(seq, "Main:\n");
2050 else
2051 seq_printf(seq, "Id %d:\n", tb->tb_id);
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002052}
Robert Olsson19baf832005-06-21 12:43:18 -07002053
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002054
Robert Olsson19baf832005-06-21 12:43:18 -07002055static int fib_triestat_seq_show(struct seq_file *seq, void *v)
2056{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002057 struct net *net = (struct net *)seq->private;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002058 unsigned int h;
Eric W. Biederman877a9bf2007-12-07 00:47:47 -08002059
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002060 seq_printf(seq,
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002061 "Basic info: size of leaf:"
2062 " %Zd bytes, size of tnode: %Zd bytes.\n",
Alexander Duyck41b489f2015-03-04 15:02:33 -08002063 LEAF_SIZE, TNODE_SIZE(0));
Olof Johansson91b9a272005-08-09 20:24:39 -07002064
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002065 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2066 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002067 struct fib_table *tb;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002068
Sasha Levinb67bfe02013-02-27 17:06:00 -08002069 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002070 struct trie *t = (struct trie *) tb->tb_data;
2071 struct trie_stat stat;
2072
2073 if (!t)
2074 continue;
2075
2076 fib_table_print(seq, tb);
2077
2078 trie_collect_stats(t, &stat);
2079 trie_show_stats(seq, &stat);
2080#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -08002081 trie_show_usage(seq, t->stats);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002082#endif
2083 }
2084 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002085
Robert Olsson19baf832005-06-21 12:43:18 -07002086 return 0;
2087}
2088
Robert Olsson19baf832005-06-21 12:43:18 -07002089static int fib_triestat_seq_open(struct inode *inode, struct file *file)
2090{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002091 return single_open_net(inode, file, fib_triestat_seq_show);
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002092}
2093
Arjan van de Ven9a321442007-02-12 00:55:35 -08002094static const struct file_operations fib_triestat_fops = {
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07002095 .owner = THIS_MODULE,
2096 .open = fib_triestat_seq_open,
2097 .read = seq_read,
2098 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002099 .release = single_release_net,
Robert Olsson19baf832005-06-21 12:43:18 -07002100};
2101
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002102static struct key_vector *fib_trie_get_idx(struct seq_file *seq, loff_t pos)
Robert Olsson19baf832005-06-21 12:43:18 -07002103{
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002104 struct fib_trie_iter *iter = seq->private;
2105 struct net *net = seq_file_net(seq);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002106 loff_t idx = 0;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002107 unsigned int h;
Robert Olsson19baf832005-06-21 12:43:18 -07002108
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002109 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2110 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002111 struct fib_table *tb;
2112
Sasha Levinb67bfe02013-02-27 17:06:00 -08002113 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002114 struct key_vector *n;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002115
2116 for (n = fib_trie_get_first(iter,
2117 (struct trie *) tb->tb_data);
2118 n; n = fib_trie_get_next(iter))
2119 if (pos == idx++) {
2120 iter->tb = tb;
2121 return n;
2122 }
2123 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002124 }
Robert Olsson19baf832005-06-21 12:43:18 -07002125
Robert Olsson19baf832005-06-21 12:43:18 -07002126 return NULL;
2127}
2128
2129static void *fib_trie_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08002130 __acquires(RCU)
Robert Olsson19baf832005-06-21 12:43:18 -07002131{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002132 rcu_read_lock();
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002133 return fib_trie_get_idx(seq, *pos);
Robert Olsson19baf832005-06-21 12:43:18 -07002134}
2135
2136static void *fib_trie_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2137{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002138 struct fib_trie_iter *iter = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002139 struct net *net = seq_file_net(seq);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002140 struct fib_table *tb = iter->tb;
2141 struct hlist_node *tb_node;
2142 unsigned int h;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002143 struct key_vector *n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002144
Robert Olsson19baf832005-06-21 12:43:18 -07002145 ++*pos;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002146 /* next node in same table */
2147 n = fib_trie_get_next(iter);
2148 if (n)
2149 return n;
Olof Johansson91b9a272005-08-09 20:24:39 -07002150
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002151 /* walk rest of this hash chain */
2152 h = tb->tb_id & (FIB_TABLE_HASHSZ - 1);
Eric Dumazet0a5c0472011-03-31 01:51:35 -07002153 while ((tb_node = rcu_dereference(hlist_next_rcu(&tb->tb_hlist)))) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002154 tb = hlist_entry(tb_node, struct fib_table, tb_hlist);
2155 n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
2156 if (n)
2157 goto found;
2158 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002159
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002160 /* new hash chain */
2161 while (++h < FIB_TABLE_HASHSZ) {
2162 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002163 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002164 n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
2165 if (n)
2166 goto found;
2167 }
2168 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002169 return NULL;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002170
2171found:
2172 iter->tb = tb;
2173 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07002174}
2175
2176static void fib_trie_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08002177 __releases(RCU)
Robert Olsson19baf832005-06-21 12:43:18 -07002178{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002179 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07002180}
2181
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002182static void seq_indent(struct seq_file *seq, int n)
2183{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002184 while (n-- > 0)
2185 seq_puts(seq, " ");
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002186}
Robert Olsson19baf832005-06-21 12:43:18 -07002187
Eric Dumazet28d36e32008-01-14 23:09:56 -08002188static inline const char *rtn_scope(char *buf, size_t len, enum rt_scope_t s)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002189{
Stephen Hemminger132adf52007-03-08 20:44:43 -08002190 switch (s) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002191 case RT_SCOPE_UNIVERSE: return "universe";
2192 case RT_SCOPE_SITE: return "site";
2193 case RT_SCOPE_LINK: return "link";
2194 case RT_SCOPE_HOST: return "host";
2195 case RT_SCOPE_NOWHERE: return "nowhere";
2196 default:
Eric Dumazet28d36e32008-01-14 23:09:56 -08002197 snprintf(buf, len, "scope=%d", s);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002198 return buf;
2199 }
2200}
2201
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -07002202static const char *const rtn_type_names[__RTN_MAX] = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002203 [RTN_UNSPEC] = "UNSPEC",
2204 [RTN_UNICAST] = "UNICAST",
2205 [RTN_LOCAL] = "LOCAL",
2206 [RTN_BROADCAST] = "BROADCAST",
2207 [RTN_ANYCAST] = "ANYCAST",
2208 [RTN_MULTICAST] = "MULTICAST",
2209 [RTN_BLACKHOLE] = "BLACKHOLE",
2210 [RTN_UNREACHABLE] = "UNREACHABLE",
2211 [RTN_PROHIBIT] = "PROHIBIT",
2212 [RTN_THROW] = "THROW",
2213 [RTN_NAT] = "NAT",
2214 [RTN_XRESOLVE] = "XRESOLVE",
2215};
2216
Eric Dumazeta034ee32010-09-09 23:32:28 +00002217static inline const char *rtn_type(char *buf, size_t len, unsigned int t)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002218{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002219 if (t < __RTN_MAX && rtn_type_names[t])
2220 return rtn_type_names[t];
Eric Dumazet28d36e32008-01-14 23:09:56 -08002221 snprintf(buf, len, "type %u", t);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002222 return buf;
2223}
2224
2225/* Pretty print the trie */
Robert Olsson19baf832005-06-21 12:43:18 -07002226static int fib_trie_seq_show(struct seq_file *seq, void *v)
2227{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002228 const struct fib_trie_iter *iter = seq->private;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002229 struct key_vector *n = v;
Robert Olsson19baf832005-06-21 12:43:18 -07002230
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002231 if (!node_parent_rcu(n))
2232 fib_table_print(seq, iter->tb);
Robert Olsson095b8502007-01-26 19:06:01 -08002233
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002234 if (IS_TNODE(n)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002235 __be32 prf = htonl(n->key);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002236
Alexander Duycke9b44012014-12-31 10:56:12 -08002237 seq_indent(seq, iter->depth-1);
2238 seq_printf(seq, " +-- %pI4/%zu %u %u %u\n",
2239 &prf, KEYLENGTH - n->pos - n->bits, n->bits,
Alexander Duyck6e22d172015-03-06 09:54:39 -08002240 tn_info(n)->full_children,
2241 tn_info(n)->empty_children);
Olof Johansson91b9a272005-08-09 20:24:39 -07002242 } else {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002243 __be32 val = htonl(n->key);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002244 struct fib_alias *fa;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002245
2246 seq_indent(seq, iter->depth);
Harvey Harrison673d57e2008-10-31 00:53:57 -07002247 seq_printf(seq, " |-- %pI4\n", &val);
Eric Dumazet28d36e32008-01-14 23:09:56 -08002248
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002249 hlist_for_each_entry_rcu(fa, &n->leaf, fa_list) {
2250 char buf1[32], buf2[32];
Eric Dumazet28d36e32008-01-14 23:09:56 -08002251
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002252 seq_indent(seq, iter->depth + 1);
2253 seq_printf(seq, " /%zu %s %s",
2254 KEYLENGTH - fa->fa_slen,
2255 rtn_scope(buf1, sizeof(buf1),
2256 fa->fa_info->fib_scope),
2257 rtn_type(buf2, sizeof(buf2),
2258 fa->fa_type));
2259 if (fa->fa_tos)
2260 seq_printf(seq, " tos=%d", fa->fa_tos);
2261 seq_putc(seq, '\n');
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002262 }
Robert Olsson19baf832005-06-21 12:43:18 -07002263 }
2264
2265 return 0;
2266}
2267
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002268static const struct seq_operations fib_trie_seq_ops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002269 .start = fib_trie_seq_start,
2270 .next = fib_trie_seq_next,
2271 .stop = fib_trie_seq_stop,
2272 .show = fib_trie_seq_show,
Robert Olsson19baf832005-06-21 12:43:18 -07002273};
2274
2275static int fib_trie_seq_open(struct inode *inode, struct file *file)
2276{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002277 return seq_open_net(inode, file, &fib_trie_seq_ops,
2278 sizeof(struct fib_trie_iter));
Robert Olsson19baf832005-06-21 12:43:18 -07002279}
2280
Arjan van de Ven9a321442007-02-12 00:55:35 -08002281static const struct file_operations fib_trie_fops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002282 .owner = THIS_MODULE,
2283 .open = fib_trie_seq_open,
2284 .read = seq_read,
2285 .llseek = seq_lseek,
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002286 .release = seq_release_net,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002287};
2288
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002289struct fib_route_iter {
2290 struct seq_net_private p;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002291 struct fib_table *main_tb;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002292 struct key_vector *tnode;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002293 loff_t pos;
2294 t_key key;
2295};
2296
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002297static struct key_vector *fib_route_get_idx(struct fib_route_iter *iter,
2298 loff_t pos)
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002299{
Alexander Duyck8be33e92015-03-04 14:59:19 -08002300 struct fib_table *tb = iter->main_tb;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002301 struct key_vector *l, **tp = &iter->tnode;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002302 struct trie *t;
2303 t_key key;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002304
Alexander Duyck8be33e92015-03-04 14:59:19 -08002305 /* use cache location of next-to-find key */
2306 if (iter->pos > 0 && pos >= iter->pos) {
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002307 pos -= iter->pos;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002308 key = iter->key;
2309 } else {
2310 t = (struct trie *)tb->tb_data;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002311 iter->tnode = rcu_dereference_rtnl(t->tnode[0]);
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002312 iter->pos = 0;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002313 key = 0;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002314 }
2315
Alexander Duyck8be33e92015-03-04 14:59:19 -08002316 while ((l = leaf_walk_rcu(tp, key)) != NULL) {
2317 key = l->key + 1;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002318 iter->pos++;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002319
2320 if (pos-- <= 0)
2321 break;
2322
2323 l = NULL;
2324
2325 /* handle unlikely case of a key wrap */
2326 if (!key)
2327 break;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002328 }
2329
2330 if (l)
Alexander Duyck8be33e92015-03-04 14:59:19 -08002331 iter->key = key; /* remember it */
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002332 else
2333 iter->pos = 0; /* forget it */
2334
2335 return l;
2336}
2337
2338static void *fib_route_seq_start(struct seq_file *seq, loff_t *pos)
2339 __acquires(RCU)
2340{
2341 struct fib_route_iter *iter = seq->private;
2342 struct fib_table *tb;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002343 struct trie *t;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002344
2345 rcu_read_lock();
Alexander Duyck8be33e92015-03-04 14:59:19 -08002346
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002347 tb = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN);
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002348 if (!tb)
2349 return NULL;
2350
Alexander Duyck8be33e92015-03-04 14:59:19 -08002351 iter->main_tb = tb;
2352
2353 if (*pos != 0)
2354 return fib_route_get_idx(iter, *pos);
2355
2356 t = (struct trie *)tb->tb_data;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002357 iter->tnode = rcu_dereference_rtnl(t->tnode[0]);
Alexander Duyck8be33e92015-03-04 14:59:19 -08002358 iter->pos = 0;
2359 iter->key = 0;
2360
2361 return SEQ_START_TOKEN;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002362}
2363
2364static void *fib_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2365{
2366 struct fib_route_iter *iter = seq->private;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002367 struct key_vector *l = NULL;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002368 t_key key = iter->key;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002369
2370 ++*pos;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002371
2372 /* only allow key of 0 for start of sequence */
2373 if ((v == SEQ_START_TOKEN) || key)
2374 l = leaf_walk_rcu(&iter->tnode, key);
2375
2376 if (l) {
2377 iter->key = l->key + 1;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002378 iter->pos++;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002379 } else {
2380 iter->pos = 0;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002381 }
2382
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002383 return l;
2384}
2385
2386static void fib_route_seq_stop(struct seq_file *seq, void *v)
2387 __releases(RCU)
2388{
2389 rcu_read_unlock();
2390}
2391
Eric Dumazeta034ee32010-09-09 23:32:28 +00002392static unsigned int fib_flag_trans(int type, __be32 mask, const struct fib_info *fi)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002393{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002394 unsigned int flags = 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002395
Eric Dumazeta034ee32010-09-09 23:32:28 +00002396 if (type == RTN_UNREACHABLE || type == RTN_PROHIBIT)
2397 flags = RTF_REJECT;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002398 if (fi && fi->fib_nh->nh_gw)
2399 flags |= RTF_GATEWAY;
Al Viro32ab5f82006-09-26 22:21:45 -07002400 if (mask == htonl(0xFFFFFFFF))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002401 flags |= RTF_HOST;
2402 flags |= RTF_UP;
2403 return flags;
2404}
2405
2406/*
2407 * This outputs /proc/net/route.
2408 * The format of the file is not supposed to be changed
Eric Dumazeta034ee32010-09-09 23:32:28 +00002409 * and needs to be same as fib_hash output to avoid breaking
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002410 * legacy utilities
2411 */
2412static int fib_route_seq_show(struct seq_file *seq, void *v)
2413{
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002414 struct fib_alias *fa;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002415 struct key_vector *l = v;
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08002416 __be32 prefix;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002417
2418 if (v == SEQ_START_TOKEN) {
2419 seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
2420 "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
2421 "\tWindow\tIRTT");
2422 return 0;
2423 }
2424
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08002425 prefix = htonl(l->key);
2426
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002427 hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
2428 const struct fib_info *fi = fa->fa_info;
2429 __be32 mask = inet_make_mask(KEYLENGTH - fa->fa_slen);
2430 unsigned int flags = fib_flag_trans(fa->fa_type, mask, fi);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002431
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002432 if ((fa->fa_type == RTN_BROADCAST) ||
2433 (fa->fa_type == RTN_MULTICAST))
2434 continue;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002435
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002436 seq_setwidth(seq, 127);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002437
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002438 if (fi)
2439 seq_printf(seq,
2440 "%s\t%08X\t%08X\t%04X\t%d\t%u\t"
2441 "%d\t%08X\t%d\t%u\t%u",
2442 fi->fib_dev ? fi->fib_dev->name : "*",
2443 prefix,
2444 fi->fib_nh->nh_gw, flags, 0, 0,
2445 fi->fib_priority,
2446 mask,
2447 (fi->fib_advmss ?
2448 fi->fib_advmss + 40 : 0),
2449 fi->fib_window,
2450 fi->fib_rtt >> 3);
2451 else
2452 seq_printf(seq,
2453 "*\t%08X\t%08X\t%04X\t%d\t%u\t"
2454 "%d\t%08X\t%d\t%u\t%u",
2455 prefix, 0, flags, 0, 0, 0,
2456 mask, 0, 0, 0);
Tetsuo Handa652586d2013-11-14 14:31:57 -08002457
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002458 seq_pad(seq, '\n');
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002459 }
2460
2461 return 0;
2462}
2463
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002464static const struct seq_operations fib_route_seq_ops = {
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002465 .start = fib_route_seq_start,
2466 .next = fib_route_seq_next,
2467 .stop = fib_route_seq_stop,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002468 .show = fib_route_seq_show,
2469};
2470
2471static int fib_route_seq_open(struct inode *inode, struct file *file)
2472{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002473 return seq_open_net(inode, file, &fib_route_seq_ops,
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002474 sizeof(struct fib_route_iter));
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002475}
2476
Arjan van de Ven9a321442007-02-12 00:55:35 -08002477static const struct file_operations fib_route_fops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002478 .owner = THIS_MODULE,
2479 .open = fib_route_seq_open,
2480 .read = seq_read,
2481 .llseek = seq_lseek,
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002482 .release = seq_release_net,
Robert Olsson19baf832005-06-21 12:43:18 -07002483};
2484
Denis V. Lunev61a02652008-01-10 03:21:09 -08002485int __net_init fib_proc_init(struct net *net)
Robert Olsson19baf832005-06-21 12:43:18 -07002486{
Gao fengd4beaa62013-02-18 01:34:54 +00002487 if (!proc_create("fib_trie", S_IRUGO, net->proc_net, &fib_trie_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002488 goto out1;
2489
Gao fengd4beaa62013-02-18 01:34:54 +00002490 if (!proc_create("fib_triestat", S_IRUGO, net->proc_net,
2491 &fib_triestat_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002492 goto out2;
2493
Gao fengd4beaa62013-02-18 01:34:54 +00002494 if (!proc_create("route", S_IRUGO, net->proc_net, &fib_route_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002495 goto out3;
2496
Robert Olsson19baf832005-06-21 12:43:18 -07002497 return 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002498
2499out3:
Gao fengece31ff2013-02-18 01:34:56 +00002500 remove_proc_entry("fib_triestat", net->proc_net);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002501out2:
Gao fengece31ff2013-02-18 01:34:56 +00002502 remove_proc_entry("fib_trie", net->proc_net);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002503out1:
2504 return -ENOMEM;
Robert Olsson19baf832005-06-21 12:43:18 -07002505}
2506
Denis V. Lunev61a02652008-01-10 03:21:09 -08002507void __net_exit fib_proc_exit(struct net *net)
Robert Olsson19baf832005-06-21 12:43:18 -07002508{
Gao fengece31ff2013-02-18 01:34:56 +00002509 remove_proc_entry("fib_trie", net->proc_net);
2510 remove_proc_entry("fib_triestat", net->proc_net);
2511 remove_proc_entry("route", net->proc_net);
Robert Olsson19baf832005-06-21 12:43:18 -07002512}
2513
2514#endif /* CONFIG_PROC_FS */