blob: 485983ef0444b20c33dbfc45ca098649f901eb5f [file] [log] [blame]
Robert Olsson19baf832005-06-21 12:43:18 -07001/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version
5 * 2 of the License, or (at your option) any later version.
6 *
7 * Robert Olsson <robert.olsson@its.uu.se> Uppsala Universitet
8 * & Swedish University of Agricultural Sciences.
9 *
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090010 * Jens Laas <jens.laas@data.slu.se> Swedish University of
Robert Olsson19baf832005-06-21 12:43:18 -070011 * Agricultural Sciences.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090012 *
Robert Olsson19baf832005-06-21 12:43:18 -070013 * Hans Liss <hans.liss@its.uu.se> Uppsala Universitet
14 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015 * This work is based on the LPC-trie which is originally described in:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090016 *
Robert Olsson19baf832005-06-21 12:43:18 -070017 * An experimental study of compression methods for dynamic tries
18 * Stefan Nilsson and Matti Tikkanen. Algorithmica, 33(1):19-33, 2002.
Justin P. Mattock631dd1a2010-10-18 11:03:14 +020019 * http://www.csc.kth.se/~snilsson/software/dyntrie2/
Robert Olsson19baf832005-06-21 12:43:18 -070020 *
21 *
22 * IP-address lookup using LC-tries. Stefan Nilsson and Gunnar Karlsson
23 * IEEE Journal on Selected Areas in Communications, 17(6):1083-1092, June 1999
24 *
Robert Olsson19baf832005-06-21 12:43:18 -070025 *
26 * Code from fib_hash has been reused which includes the following header:
27 *
28 *
29 * INET An implementation of the TCP/IP protocol suite for the LINUX
30 * operating system. INET is implemented using the BSD Socket
31 * interface as the means of communication with the user level.
32 *
33 * IPv4 FIB: lookup engine and maintenance routines.
34 *
35 *
36 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
37 *
38 * This program is free software; you can redistribute it and/or
39 * modify it under the terms of the GNU General Public License
40 * as published by the Free Software Foundation; either version
41 * 2 of the License, or (at your option) any later version.
Robert Olssonfd966252005-12-22 11:25:10 -080042 *
43 * Substantial contributions to this work comes from:
44 *
45 * David S. Miller, <davem@davemloft.net>
46 * Stephen Hemminger <shemminger@osdl.org>
47 * Paul E. McKenney <paulmck@us.ibm.com>
48 * Patrick McHardy <kaber@trash.net>
Robert Olsson19baf832005-06-21 12:43:18 -070049 */
50
Jens Låås80b71b82009-08-28 23:57:15 -070051#define VERSION "0.409"
Robert Olsson19baf832005-06-21 12:43:18 -070052
Robert Olsson19baf832005-06-21 12:43:18 -070053#include <asm/uaccess.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070054#include <linux/bitops.h>
Robert Olsson19baf832005-06-21 12:43:18 -070055#include <linux/types.h>
56#include <linux/kernel.h>
Robert Olsson19baf832005-06-21 12:43:18 -070057#include <linux/mm.h>
58#include <linux/string.h>
59#include <linux/socket.h>
60#include <linux/sockios.h>
61#include <linux/errno.h>
62#include <linux/in.h>
63#include <linux/inet.h>
Stephen Hemmingercd8787a2006-01-03 14:38:34 -080064#include <linux/inetdevice.h>
Robert Olsson19baf832005-06-21 12:43:18 -070065#include <linux/netdevice.h>
66#include <linux/if_arp.h>
67#include <linux/proc_fs.h>
Robert Olsson2373ce12005-08-25 13:01:29 -070068#include <linux/rcupdate.h>
Robert Olsson19baf832005-06-21 12:43:18 -070069#include <linux/skbuff.h>
70#include <linux/netlink.h>
71#include <linux/init.h>
72#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090073#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040074#include <linux/export.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020075#include <net/net_namespace.h>
Robert Olsson19baf832005-06-21 12:43:18 -070076#include <net/ip.h>
77#include <net/protocol.h>
78#include <net/route.h>
79#include <net/tcp.h>
80#include <net/sock.h>
81#include <net/ip_fib.h>
82#include "fib_lookup.h"
83
Robert Olsson06ef9212006-03-20 21:35:01 -080084#define MAX_STAT_DEPTH 32
Robert Olsson19baf832005-06-21 12:43:18 -070085
Robert Olsson19baf832005-06-21 12:43:18 -070086#define KEYLENGTH (8*sizeof(t_key))
Robert Olsson19baf832005-06-21 12:43:18 -070087
Robert Olsson19baf832005-06-21 12:43:18 -070088typedef unsigned int t_key;
89
Alexander Duyck64c9b6f2014-12-31 10:55:35 -080090#define IS_TNODE(n) ((n)->bits)
91#define IS_LEAF(n) (!(n)->bits)
Robert Olsson2373ce12005-08-25 13:01:29 -070092
Alexander Duycke9b44012014-12-31 10:56:12 -080093#define get_index(_key, _kv) (((_key) ^ (_kv)->key) >> (_kv)->pos)
Alexander Duyck9f9e6362014-12-31 10:55:54 -080094
Alexander Duyck64c9b6f2014-12-31 10:55:35 -080095struct tnode {
96 t_key key;
97 unsigned char bits; /* 2log(KEYLENGTH) bits needed */
98 unsigned char pos; /* 2log(KEYLENGTH) bits needed */
99 struct tnode __rcu *parent;
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800100 struct rcu_head rcu;
Alexander Duyckadaf9812014-12-31 10:55:47 -0800101 union {
102 /* The fields in this struct are valid if bits > 0 (TNODE) */
103 struct {
104 unsigned int full_children; /* KEYLENGTH bits needed */
105 unsigned int empty_children; /* KEYLENGTH bits needed */
106 struct tnode __rcu *child[0];
107 };
108 /* This list pointer if valid if bits == 0 (LEAF) */
109 struct hlist_head list;
110 };
Robert Olsson19baf832005-06-21 12:43:18 -0700111};
112
113struct leaf_info {
114 struct hlist_node hlist;
115 int plen;
Eric Dumazet5c745012011-07-18 03:16:33 +0000116 u32 mask_plen; /* ntohl(inet_make_mask(plen)) */
Robert Olsson19baf832005-06-21 12:43:18 -0700117 struct list_head falh;
Eric Dumazet5c745012011-07-18 03:16:33 +0000118 struct rcu_head rcu;
Robert Olsson19baf832005-06-21 12:43:18 -0700119};
120
Robert Olsson19baf832005-06-21 12:43:18 -0700121#ifdef CONFIG_IP_FIB_TRIE_STATS
122struct trie_use_stats {
123 unsigned int gets;
124 unsigned int backtrack;
125 unsigned int semantic_match_passed;
126 unsigned int semantic_match_miss;
127 unsigned int null_node_hit;
Robert Olsson2f368952005-07-05 15:02:40 -0700128 unsigned int resize_node_skipped;
Robert Olsson19baf832005-06-21 12:43:18 -0700129};
130#endif
131
132struct trie_stat {
133 unsigned int totdepth;
134 unsigned int maxdepth;
135 unsigned int tnodes;
136 unsigned int leaves;
137 unsigned int nullpointers;
Stephen Hemminger93672292008-01-22 21:54:05 -0800138 unsigned int prefixes;
Robert Olsson06ef9212006-03-20 21:35:01 -0800139 unsigned int nodesizes[MAX_STAT_DEPTH];
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700140};
Robert Olsson19baf832005-06-21 12:43:18 -0700141
142struct trie {
Alexander Duyckadaf9812014-12-31 10:55:47 -0800143 struct tnode __rcu *trie;
Robert Olsson19baf832005-06-21 12:43:18 -0700144#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -0800145 struct trie_use_stats __percpu *stats;
Robert Olsson19baf832005-06-21 12:43:18 -0700146#endif
Robert Olsson19baf832005-06-21 12:43:18 -0700147};
148
Alexander Duyckff181ed2014-12-31 10:56:43 -0800149static void resize(struct trie *t, struct tnode *tn);
Jarek Poplawskic3059472009-07-14 08:33:08 +0000150static size_t tnode_free_size;
151
152/*
153 * synchronize_rcu after call_rcu for that many pages; it should be especially
154 * useful before resizing the root node with PREEMPT_NONE configs; the value was
155 * obtained experimentally, aiming to avoid visible slowdown.
156 */
157static const int sync_pages = 128;
Robert Olsson19baf832005-06-21 12:43:18 -0700158
Christoph Lametere18b8902006-12-06 20:33:20 -0800159static struct kmem_cache *fn_alias_kmem __read_mostly;
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -0800160static struct kmem_cache *trie_leaf_kmem __read_mostly;
Robert Olsson19baf832005-06-21 12:43:18 -0700161
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800162/* caller must hold RTNL */
163#define node_parent(n) rtnl_dereference((n)->parent)
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700164
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800165/* caller must hold RCU read lock or RTNL */
166#define node_parent_rcu(n) rcu_dereference_rtnl((n)->parent)
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700167
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800168/* wrapper for rcu_assign_pointer */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800169static inline void node_set_parent(struct tnode *n, struct tnode *tp)
Stephen Hemminger06801912007-08-10 15:22:13 -0700170{
Alexander Duyckadaf9812014-12-31 10:55:47 -0800171 if (n)
172 rcu_assign_pointer(n->parent, tp);
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800173}
174
175#define NODE_INIT_PARENT(n, p) RCU_INIT_POINTER((n)->parent, p)
176
177/* This provides us with the number of children in this node, in the case of a
178 * leaf this will return 0 meaning none of the children are accessible.
179 */
Alexander Duyck98293e82014-12-31 10:56:18 -0800180static inline unsigned long tnode_child_length(const struct tnode *tn)
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800181{
182 return (1ul << tn->bits) & ~(1ul);
Stephen Hemminger06801912007-08-10 15:22:13 -0700183}
Robert Olsson2373ce12005-08-25 13:01:29 -0700184
Alexander Duyck98293e82014-12-31 10:56:18 -0800185/* caller must hold RTNL */
186static inline struct tnode *tnode_get_child(const struct tnode *tn,
187 unsigned long i)
Robert Olsson19baf832005-06-21 12:43:18 -0700188{
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800189 BUG_ON(i >= tnode_child_length(tn));
Robert Olsson19baf832005-06-21 12:43:18 -0700190
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700191 return rtnl_dereference(tn->child[i]);
Eric Dumazetb59cfbf2008-01-18 03:31:36 -0800192}
193
Alexander Duyck98293e82014-12-31 10:56:18 -0800194/* caller must hold RCU read lock or RTNL */
195static inline struct tnode *tnode_get_child_rcu(const struct tnode *tn,
196 unsigned long i)
Eric Dumazetb59cfbf2008-01-18 03:31:36 -0800197{
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800198 BUG_ON(i >= tnode_child_length(tn));
Eric Dumazetb59cfbf2008-01-18 03:31:36 -0800199
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700200 return rcu_dereference_rtnl(tn->child[i]);
Robert Olsson19baf832005-06-21 12:43:18 -0700201}
202
Alexander Duycke9b44012014-12-31 10:56:12 -0800203/* To understand this stuff, an understanding of keys and all their bits is
204 * necessary. Every node in the trie has a key associated with it, but not
205 * all of the bits in that key are significant.
206 *
207 * Consider a node 'n' and its parent 'tp'.
208 *
209 * If n is a leaf, every bit in its key is significant. Its presence is
210 * necessitated by path compression, since during a tree traversal (when
211 * searching for a leaf - unless we are doing an insertion) we will completely
212 * ignore all skipped bits we encounter. Thus we need to verify, at the end of
213 * a potentially successful search, that we have indeed been walking the
214 * correct key path.
215 *
216 * Note that we can never "miss" the correct key in the tree if present by
217 * following the wrong path. Path compression ensures that segments of the key
218 * that are the same for all keys with a given prefix are skipped, but the
219 * skipped part *is* identical for each node in the subtrie below the skipped
220 * bit! trie_insert() in this implementation takes care of that.
221 *
222 * if n is an internal node - a 'tnode' here, the various parts of its key
223 * have many different meanings.
224 *
225 * Example:
226 * _________________________________________________________________
227 * | i | i | i | i | i | i | i | N | N | N | S | S | S | S | S | C |
228 * -----------------------------------------------------------------
229 * 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
230 *
231 * _________________________________________________________________
232 * | C | C | C | u | u | u | u | u | u | u | u | u | u | u | u | u |
233 * -----------------------------------------------------------------
234 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
235 *
236 * tp->pos = 22
237 * tp->bits = 3
238 * n->pos = 13
239 * n->bits = 4
240 *
241 * First, let's just ignore the bits that come before the parent tp, that is
242 * the bits from (tp->pos + tp->bits) to 31. They are *known* but at this
243 * point we do not use them for anything.
244 *
245 * The bits from (tp->pos) to (tp->pos + tp->bits - 1) - "N", above - are the
246 * index into the parent's child array. That is, they will be used to find
247 * 'n' among tp's children.
248 *
249 * The bits from (n->pos + n->bits) to (tn->pos - 1) - "S" - are skipped bits
250 * for the node n.
251 *
252 * All the bits we have seen so far are significant to the node n. The rest
253 * of the bits are really not needed or indeed known in n->key.
254 *
255 * The bits from (n->pos) to (n->pos + n->bits - 1) - "C" - are the index into
256 * n's child array, and will of course be different for each child.
257 *
258 * The rest of the bits, from 0 to (n->pos + n->bits), are completely unknown
259 * at this point.
260 */
Robert Olsson19baf832005-06-21 12:43:18 -0700261
Denis V. Lunevf5026fa2007-12-13 09:47:57 -0800262static const int halve_threshold = 25;
263static const int inflate_threshold = 50;
Jarek Poplawski345aa032009-07-07 19:39:16 -0700264static const int halve_threshold_root = 15;
Jens Låås80b71b82009-08-28 23:57:15 -0700265static const int inflate_threshold_root = 30;
Robert Olsson2373ce12005-08-25 13:01:29 -0700266
267static void __alias_free_mem(struct rcu_head *head)
268{
269 struct fib_alias *fa = container_of(head, struct fib_alias, rcu);
270 kmem_cache_free(fn_alias_kmem, fa);
271}
272
273static inline void alias_free_mem_rcu(struct fib_alias *fa)
274{
275 call_rcu(&fa->rcu, __alias_free_mem);
276}
277
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800278#define TNODE_KMALLOC_MAX \
Alexander Duyckadaf9812014-12-31 10:55:47 -0800279 ilog2((PAGE_SIZE - sizeof(struct tnode)) / sizeof(struct tnode *))
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800280
281static void __node_free_rcu(struct rcu_head *head)
Robert Olsson2373ce12005-08-25 13:01:29 -0700282{
Alexander Duyckadaf9812014-12-31 10:55:47 -0800283 struct tnode *n = container_of(head, struct tnode, rcu);
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800284
285 if (IS_LEAF(n))
286 kmem_cache_free(trie_leaf_kmem, n);
287 else if (n->bits <= TNODE_KMALLOC_MAX)
288 kfree(n);
289 else
290 vfree(n);
Robert Olsson2373ce12005-08-25 13:01:29 -0700291}
292
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800293#define node_free(n) call_rcu(&n->rcu, __node_free_rcu)
Stephen Hemminger387a5482008-04-10 03:47:34 -0700294
Robert Olsson2373ce12005-08-25 13:01:29 -0700295static inline void free_leaf_info(struct leaf_info *leaf)
296{
Lai Jiangshanbceb0f42011-03-18 11:42:34 +0800297 kfree_rcu(leaf, rcu);
Robert Olsson2373ce12005-08-25 13:01:29 -0700298}
299
Eric Dumazet8d965442008-01-13 22:31:44 -0800300static struct tnode *tnode_alloc(size_t size)
Robert Olsson2373ce12005-08-25 13:01:29 -0700301{
Robert Olsson2373ce12005-08-25 13:01:29 -0700302 if (size <= PAGE_SIZE)
Eric Dumazet8d965442008-01-13 22:31:44 -0800303 return kzalloc(size, GFP_KERNEL);
Stephen Hemminger15be75c2008-04-10 02:56:38 -0700304 else
Eric Dumazet7a1c8e52010-11-20 07:46:35 +0000305 return vzalloc(size);
Stephen Hemminger15be75c2008-04-10 02:56:38 -0700306}
Robert Olsson2373ce12005-08-25 13:01:29 -0700307
Alexander Duyckadaf9812014-12-31 10:55:47 -0800308static struct tnode *leaf_new(t_key key)
Robert Olsson19baf832005-06-21 12:43:18 -0700309{
Alexander Duyckadaf9812014-12-31 10:55:47 -0800310 struct tnode *l = kmem_cache_alloc(trie_leaf_kmem, GFP_KERNEL);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700311 if (l) {
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800312 l->parent = NULL;
313 /* set key and pos to reflect full key value
314 * any trailing zeros in the key should be ignored
315 * as the nodes are searched
316 */
317 l->key = key;
Alexander Duycke9b44012014-12-31 10:56:12 -0800318 l->pos = 0;
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800319 /* set bits to 0 indicating we are not a tnode */
320 l->bits = 0;
321
Robert Olsson19baf832005-06-21 12:43:18 -0700322 INIT_HLIST_HEAD(&l->list);
323 }
324 return l;
325}
326
327static struct leaf_info *leaf_info_new(int plen)
328{
329 struct leaf_info *li = kmalloc(sizeof(struct leaf_info), GFP_KERNEL);
Robert Olsson2373ce12005-08-25 13:01:29 -0700330 if (li) {
331 li->plen = plen;
Eric Dumazet5c745012011-07-18 03:16:33 +0000332 li->mask_plen = ntohl(inet_make_mask(plen));
Robert Olsson2373ce12005-08-25 13:01:29 -0700333 INIT_LIST_HEAD(&li->falh);
Patrick McHardyf0e36f82005-07-05 14:44:55 -0700334 }
Robert Olsson2373ce12005-08-25 13:01:29 -0700335 return li;
Patrick McHardyf0e36f82005-07-05 14:44:55 -0700336}
337
Stephen Hemmingera07f5f52008-01-22 21:53:36 -0800338static struct tnode *tnode_new(t_key key, int pos, int bits)
Robert Olsson19baf832005-06-21 12:43:18 -0700339{
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800340 size_t sz = offsetof(struct tnode, child[1 << bits]);
Patrick McHardyf0e36f82005-07-05 14:44:55 -0700341 struct tnode *tn = tnode_alloc(sz);
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800342 unsigned int shift = pos + bits;
343
344 /* verify bits and pos their msb bits clear and values are valid */
345 BUG_ON(!bits || (shift > KEYLENGTH));
Robert Olsson19baf832005-06-21 12:43:18 -0700346
Olof Johansson91b9a272005-08-09 20:24:39 -0700347 if (tn) {
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800348 tn->parent = NULL;
Robert Olsson19baf832005-06-21 12:43:18 -0700349 tn->pos = pos;
350 tn->bits = bits;
Alexander Duycke9b44012014-12-31 10:56:12 -0800351 tn->key = (shift < KEYLENGTH) ? (key >> shift) << shift : 0;
Robert Olsson19baf832005-06-21 12:43:18 -0700352 tn->full_children = 0;
353 tn->empty_children = 1<<bits;
354 }
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700355
Eric Dumazeta034ee32010-09-09 23:32:28 +0000356 pr_debug("AT %p s=%zu %zu\n", tn, sizeof(struct tnode),
Alexander Duyckadaf9812014-12-31 10:55:47 -0800357 sizeof(struct tnode *) << bits);
Robert Olsson19baf832005-06-21 12:43:18 -0700358 return tn;
359}
360
Alexander Duycke9b44012014-12-31 10:56:12 -0800361/* Check whether a tnode 'n' is "full", i.e. it is an internal node
Robert Olsson19baf832005-06-21 12:43:18 -0700362 * and no bits are skipped. See discussion in dyntree paper p. 6
363 */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800364static inline int tnode_full(const struct tnode *tn, const struct tnode *n)
Robert Olsson19baf832005-06-21 12:43:18 -0700365{
Alexander Duycke9b44012014-12-31 10:56:12 -0800366 return n && ((n->pos + n->bits) == tn->pos) && IS_TNODE(n);
Robert Olsson19baf832005-06-21 12:43:18 -0700367}
368
Alexander Duyckff181ed2014-12-31 10:56:43 -0800369/* Add a child at position i overwriting the old value.
370 * Update the value of full_children and empty_children.
371 */
372static void put_child(struct tnode *tn, unsigned long i, struct tnode *n)
Robert Olsson19baf832005-06-21 12:43:18 -0700373{
Alexander Duyckadaf9812014-12-31 10:55:47 -0800374 struct tnode *chi = rtnl_dereference(tn->child[i]);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800375 int isfull, wasfull;
Robert Olsson19baf832005-06-21 12:43:18 -0700376
Alexander Duyck98293e82014-12-31 10:56:18 -0800377 BUG_ON(i >= tnode_child_length(tn));
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700378
Robert Olsson19baf832005-06-21 12:43:18 -0700379 /* update emptyChildren */
380 if (n == NULL && chi != NULL)
381 tn->empty_children++;
382 else if (n != NULL && chi == NULL)
383 tn->empty_children--;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700384
Robert Olsson19baf832005-06-21 12:43:18 -0700385 /* update fullChildren */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800386 wasfull = tnode_full(tn, chi);
Robert Olsson19baf832005-06-21 12:43:18 -0700387 isfull = tnode_full(tn, n);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800388
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700389 if (wasfull && !isfull)
Robert Olsson19baf832005-06-21 12:43:18 -0700390 tn->full_children--;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700391 else if (!wasfull && isfull)
Robert Olsson19baf832005-06-21 12:43:18 -0700392 tn->full_children++;
Olof Johansson91b9a272005-08-09 20:24:39 -0700393
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800394 node_set_parent(n, tn);
Robert Olsson19baf832005-06-21 12:43:18 -0700395
Eric Dumazetcf778b02012-01-12 04:41:32 +0000396 rcu_assign_pointer(tn->child[i], n);
Robert Olsson19baf832005-06-21 12:43:18 -0700397}
398
Alexander Duyck836a0122014-12-31 10:56:06 -0800399static void put_child_root(struct tnode *tp, struct trie *t,
400 t_key key, struct tnode *n)
401{
402 if (tp)
403 put_child(tp, get_index(key, tp), n);
404 else
405 rcu_assign_pointer(t->trie, n);
406}
407
Alexander Duyckfc86a932014-12-31 10:56:49 -0800408static inline void tnode_free_init(struct tnode *tn)
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700409{
Alexander Duyckfc86a932014-12-31 10:56:49 -0800410 tn->rcu.next = NULL;
411}
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700412
Alexander Duyckfc86a932014-12-31 10:56:49 -0800413static inline void tnode_free_append(struct tnode *tn, struct tnode *n)
414{
415 n->rcu.next = tn->rcu.next;
416 tn->rcu.next = &n->rcu;
417}
418
419static void tnode_free(struct tnode *tn)
420{
421 struct callback_head *head = &tn->rcu;
422
423 while (head) {
424 head = head->next;
425 tnode_free_size += offsetof(struct tnode, child[1 << tn->bits]);
426 node_free(tn);
427
428 tn = container_of(head, struct tnode, rcu);
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700429 }
Alexander Duyckfc86a932014-12-31 10:56:49 -0800430
431 if (tnode_free_size >= PAGE_SIZE * sync_pages) {
432 tnode_free_size = 0;
433 synchronize_rcu();
434 }
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700435}
436
Alexander Duyckff181ed2014-12-31 10:56:43 -0800437static int inflate(struct trie *t, struct tnode *oldtnode)
Robert Olsson19baf832005-06-21 12:43:18 -0700438{
Alexander Duyck98293e82014-12-31 10:56:18 -0800439 unsigned long olen = tnode_child_length(oldtnode);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800440 struct tnode *tp = node_parent(oldtnode);
Alexander Duyckadaf9812014-12-31 10:55:47 -0800441 struct tnode *tn;
Alexander Duyck98293e82014-12-31 10:56:18 -0800442 unsigned long i;
Alexander Duycke9b44012014-12-31 10:56:12 -0800443 t_key m;
Robert Olsson19baf832005-06-21 12:43:18 -0700444
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700445 pr_debug("In inflate\n");
Robert Olsson19baf832005-06-21 12:43:18 -0700446
Alexander Duycke9b44012014-12-31 10:56:12 -0800447 tn = tnode_new(oldtnode->key, oldtnode->pos - 1, oldtnode->bits + 1);
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700448 if (!tn)
Alexander Duyckff181ed2014-12-31 10:56:43 -0800449 return -ENOMEM;
Robert Olsson2f368952005-07-05 15:02:40 -0700450
451 /*
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700452 * Preallocate and store tnodes before the actual work so we
453 * don't get into an inconsistent state if memory allocation
454 * fails. In case of failure we return the oldnode and inflate
Robert Olsson2f368952005-07-05 15:02:40 -0700455 * of tnode is ignored.
456 */
Alexander Duycke9b44012014-12-31 10:56:12 -0800457 for (i = 0, m = 1u << tn->pos; i < olen; i++) {
458 struct tnode *inode = tnode_get_child(oldtnode, i);
Olof Johansson91b9a272005-08-09 20:24:39 -0700459
Alexander Duycke9b44012014-12-31 10:56:12 -0800460 if (tnode_full(oldtnode, inode) && (inode->bits > 1)) {
Robert Olsson2f368952005-07-05 15:02:40 -0700461 struct tnode *left, *right;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700462
Alexander Duycke9b44012014-12-31 10:56:12 -0800463 left = tnode_new(inode->key & ~m, inode->pos,
Robert Olsson2f368952005-07-05 15:02:40 -0700464 inode->bits - 1);
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700465 if (!left)
466 goto nomem;
Alexander Duyckfc86a932014-12-31 10:56:49 -0800467 tnode_free_append(tn, left);
Olof Johansson91b9a272005-08-09 20:24:39 -0700468
Alexander Duycke9b44012014-12-31 10:56:12 -0800469 right = tnode_new(inode->key | m, inode->pos,
Robert Olsson2f368952005-07-05 15:02:40 -0700470 inode->bits - 1);
471
Alexander Duyckfc86a932014-12-31 10:56:49 -0800472 if (!right)
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700473 goto nomem;
Alexander Duyckfc86a932014-12-31 10:56:49 -0800474 tnode_free_append(tn, right);
Robert Olsson2f368952005-07-05 15:02:40 -0700475
Alexander Duyckadaf9812014-12-31 10:55:47 -0800476 put_child(tn, 2*i, left);
477 put_child(tn, 2*i+1, right);
Robert Olsson2f368952005-07-05 15:02:40 -0700478 }
479 }
480
Alexander Duyckfc86a932014-12-31 10:56:49 -0800481 /* prepare oldtnode to be freed */
482 tnode_free_init(oldtnode);
483
Olof Johansson91b9a272005-08-09 20:24:39 -0700484 for (i = 0; i < olen; i++) {
Alexander Duyckadaf9812014-12-31 10:55:47 -0800485 struct tnode *inode = tnode_get_child(oldtnode, i);
Olof Johansson91b9a272005-08-09 20:24:39 -0700486 struct tnode *left, *right;
Alexander Duyck98293e82014-12-31 10:56:18 -0800487 unsigned long size, j;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700488
Robert Olsson19baf832005-06-21 12:43:18 -0700489 /* An empty child */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800490 if (inode == NULL)
Robert Olsson19baf832005-06-21 12:43:18 -0700491 continue;
492
493 /* A leaf or an internal node with skipped bits */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800494 if (!tnode_full(oldtnode, inode)) {
Alexander Duycke9b44012014-12-31 10:56:12 -0800495 put_child(tn, get_index(inode->key, tn), inode);
Robert Olsson19baf832005-06-21 12:43:18 -0700496 continue;
497 }
498
Alexander Duyckfc86a932014-12-31 10:56:49 -0800499 /* drop the node in the old tnode free list */
500 tnode_free_append(oldtnode, inode);
501
Robert Olsson19baf832005-06-21 12:43:18 -0700502 /* An internal node with two children */
Robert Olsson19baf832005-06-21 12:43:18 -0700503 if (inode->bits == 1) {
Lin Ming61648d92012-07-29 02:00:03 +0000504 put_child(tn, 2*i, rtnl_dereference(inode->child[0]));
505 put_child(tn, 2*i+1, rtnl_dereference(inode->child[1]));
Olof Johansson91b9a272005-08-09 20:24:39 -0700506 continue;
Robert Olsson19baf832005-06-21 12:43:18 -0700507 }
508
Olof Johansson91b9a272005-08-09 20:24:39 -0700509 /* An internal node with more than two children */
Robert Olsson19baf832005-06-21 12:43:18 -0700510
Olof Johansson91b9a272005-08-09 20:24:39 -0700511 /* We will replace this node 'inode' with two new
512 * ones, 'left' and 'right', each with half of the
513 * original children. The two new nodes will have
514 * a position one bit further down the key and this
515 * means that the "significant" part of their keys
516 * (see the discussion near the top of this file)
517 * will differ by one bit, which will be "0" in
518 * left's key and "1" in right's key. Since we are
519 * moving the key position by one step, the bit that
520 * we are moving away from - the bit at position
521 * (inode->pos) - is the one that will differ between
522 * left and right. So... we synthesize that bit in the
523 * two new keys.
524 * The mask 'm' below will be a single "one" bit at
525 * the position (inode->pos)
526 */
Robert Olsson19baf832005-06-21 12:43:18 -0700527
Olof Johansson91b9a272005-08-09 20:24:39 -0700528 /* Use the old key, but set the new significant
529 * bit to zero.
530 */
Robert Olsson19baf832005-06-21 12:43:18 -0700531
Alexander Duyckadaf9812014-12-31 10:55:47 -0800532 left = tnode_get_child(tn, 2*i);
Lin Ming61648d92012-07-29 02:00:03 +0000533 put_child(tn, 2*i, NULL);
Robert Olsson19baf832005-06-21 12:43:18 -0700534
Olof Johansson91b9a272005-08-09 20:24:39 -0700535 BUG_ON(!left);
Robert Olsson2f368952005-07-05 15:02:40 -0700536
Alexander Duyckadaf9812014-12-31 10:55:47 -0800537 right = tnode_get_child(tn, 2*i+1);
Lin Ming61648d92012-07-29 02:00:03 +0000538 put_child(tn, 2*i+1, NULL);
Robert Olsson2f368952005-07-05 15:02:40 -0700539
Olof Johansson91b9a272005-08-09 20:24:39 -0700540 BUG_ON(!right);
Robert Olsson2f368952005-07-05 15:02:40 -0700541
Olof Johansson91b9a272005-08-09 20:24:39 -0700542 size = tnode_child_length(left);
543 for (j = 0; j < size; j++) {
Lin Ming61648d92012-07-29 02:00:03 +0000544 put_child(left, j, rtnl_dereference(inode->child[j]));
545 put_child(right, j, rtnl_dereference(inode->child[j + size]));
Robert Olsson19baf832005-06-21 12:43:18 -0700546 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800547
548 put_child(tn, 2 * i, left);
549 put_child(tn, 2 * i + 1, right);
Olof Johansson91b9a272005-08-09 20:24:39 -0700550
Alexander Duyckfc86a932014-12-31 10:56:49 -0800551 /* resize child nodes */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800552 resize(t, left);
553 resize(t, right);
Robert Olsson19baf832005-06-21 12:43:18 -0700554 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800555
556 put_child_root(tp, t, tn->key, tn);
Alexander Duyckfc86a932014-12-31 10:56:49 -0800557
558 /* we completed without error, prepare to free old node */
559 tnode_free(oldtnode);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800560 return 0;
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700561nomem:
Alexander Duyckfc86a932014-12-31 10:56:49 -0800562 /* all pointers should be clean so we are done */
563 tnode_free(tn);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800564 return -ENOMEM;
Robert Olsson19baf832005-06-21 12:43:18 -0700565}
566
Alexander Duyckff181ed2014-12-31 10:56:43 -0800567static int halve(struct trie *t, struct tnode *oldtnode)
Robert Olsson19baf832005-06-21 12:43:18 -0700568{
Alexander Duyck98293e82014-12-31 10:56:18 -0800569 unsigned long olen = tnode_child_length(oldtnode);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800570 struct tnode *tp = node_parent(oldtnode);
Alexander Duyckadaf9812014-12-31 10:55:47 -0800571 struct tnode *tn, *left, *right;
Robert Olsson19baf832005-06-21 12:43:18 -0700572 int i;
Robert Olsson19baf832005-06-21 12:43:18 -0700573
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700574 pr_debug("In halve\n");
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700575
Alexander Duycke9b44012014-12-31 10:56:12 -0800576 tn = tnode_new(oldtnode->key, oldtnode->pos + 1, oldtnode->bits - 1);
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700577 if (!tn)
Alexander Duyckff181ed2014-12-31 10:56:43 -0800578 return -ENOMEM;
Robert Olsson2f368952005-07-05 15:02:40 -0700579
580 /*
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700581 * Preallocate and store tnodes before the actual work so we
582 * don't get into an inconsistent state if memory allocation
583 * fails. In case of failure we return the oldnode and halve
Robert Olsson2f368952005-07-05 15:02:40 -0700584 * of tnode is ignored.
585 */
586
Olof Johansson91b9a272005-08-09 20:24:39 -0700587 for (i = 0; i < olen; i += 2) {
Robert Olsson2f368952005-07-05 15:02:40 -0700588 left = tnode_get_child(oldtnode, i);
589 right = tnode_get_child(oldtnode, i+1);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700590
Robert Olsson2f368952005-07-05 15:02:40 -0700591 /* Two nonempty children */
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700592 if (left && right) {
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700593 struct tnode *newn;
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700594
Alexander Duycke9b44012014-12-31 10:56:12 -0800595 newn = tnode_new(left->key, oldtnode->pos, 1);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800596 if (!newn) {
Alexander Duyckfc86a932014-12-31 10:56:49 -0800597 tnode_free(tn);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800598 return -ENOMEM;
599 }
Alexander Duyckfc86a932014-12-31 10:56:49 -0800600 tnode_free_append(tn, newn);
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700601
Alexander Duyckadaf9812014-12-31 10:55:47 -0800602 put_child(tn, i/2, newn);
Robert Olsson2f368952005-07-05 15:02:40 -0700603 }
Robert Olsson2f368952005-07-05 15:02:40 -0700604
Robert Olsson2f368952005-07-05 15:02:40 -0700605 }
Robert Olsson19baf832005-06-21 12:43:18 -0700606
Alexander Duyckfc86a932014-12-31 10:56:49 -0800607 /* prepare oldtnode to be freed */
608 tnode_free_init(oldtnode);
609
Olof Johansson91b9a272005-08-09 20:24:39 -0700610 for (i = 0; i < olen; i += 2) {
611 struct tnode *newBinNode;
612
Robert Olsson19baf832005-06-21 12:43:18 -0700613 left = tnode_get_child(oldtnode, i);
614 right = tnode_get_child(oldtnode, i+1);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700615
Robert Olsson19baf832005-06-21 12:43:18 -0700616 /* At least one of the children is empty */
617 if (left == NULL) {
618 if (right == NULL) /* Both are empty */
619 continue;
Lin Ming61648d92012-07-29 02:00:03 +0000620 put_child(tn, i/2, right);
Olof Johansson91b9a272005-08-09 20:24:39 -0700621 continue;
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700622 }
Olof Johansson91b9a272005-08-09 20:24:39 -0700623
624 if (right == NULL) {
Lin Ming61648d92012-07-29 02:00:03 +0000625 put_child(tn, i/2, left);
Olof Johansson91b9a272005-08-09 20:24:39 -0700626 continue;
627 }
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700628
Robert Olsson19baf832005-06-21 12:43:18 -0700629 /* Two nonempty children */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800630 newBinNode = tnode_get_child(tn, i/2);
Lin Ming61648d92012-07-29 02:00:03 +0000631 put_child(newBinNode, 0, left);
632 put_child(newBinNode, 1, right);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800633
634 put_child(tn, i / 2, newBinNode);
635
Alexander Duyckfc86a932014-12-31 10:56:49 -0800636 /* resize child node */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800637 resize(t, newBinNode);
Robert Olsson19baf832005-06-21 12:43:18 -0700638 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800639
640 put_child_root(tp, t, tn->key, tn);
Alexander Duyckfc86a932014-12-31 10:56:49 -0800641
642 /* all pointers should be clean so we are done */
643 tnode_free(oldtnode);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800644
645 return 0;
Robert Olsson19baf832005-06-21 12:43:18 -0700646}
647
Alexander Duyckf05a4812014-12-31 10:56:37 -0800648/* From "Implementing a dynamic compressed trie" by Stefan Nilsson of
649 * the Helsinki University of Technology and Matti Tikkanen of Nokia
650 * Telecommunications, page 6:
651 * "A node is doubled if the ratio of non-empty children to all
652 * children in the *doubled* node is at least 'high'."
653 *
654 * 'high' in this instance is the variable 'inflate_threshold'. It
655 * is expressed as a percentage, so we multiply it with
656 * tnode_child_length() and instead of multiplying by 2 (since the
657 * child array will be doubled by inflate()) and multiplying
658 * the left-hand side by 100 (to handle the percentage thing) we
659 * multiply the left-hand side by 50.
660 *
661 * The left-hand side may look a bit weird: tnode_child_length(tn)
662 * - tn->empty_children is of course the number of non-null children
663 * in the current node. tn->full_children is the number of "full"
664 * children, that is non-null tnodes with a skip value of 0.
665 * All of those will be doubled in the resulting inflated tnode, so
666 * we just count them one extra time here.
667 *
668 * A clearer way to write this would be:
669 *
670 * to_be_doubled = tn->full_children;
671 * not_to_be_doubled = tnode_child_length(tn) - tn->empty_children -
672 * tn->full_children;
673 *
674 * new_child_length = tnode_child_length(tn) * 2;
675 *
676 * new_fill_factor = 100 * (not_to_be_doubled + 2*to_be_doubled) /
677 * new_child_length;
678 * if (new_fill_factor >= inflate_threshold)
679 *
680 * ...and so on, tho it would mess up the while () loop.
681 *
682 * anyway,
683 * 100 * (not_to_be_doubled + 2*to_be_doubled) / new_child_length >=
684 * inflate_threshold
685 *
686 * avoid a division:
687 * 100 * (not_to_be_doubled + 2*to_be_doubled) >=
688 * inflate_threshold * new_child_length
689 *
690 * expand not_to_be_doubled and to_be_doubled, and shorten:
691 * 100 * (tnode_child_length(tn) - tn->empty_children +
692 * tn->full_children) >= inflate_threshold * new_child_length
693 *
694 * expand new_child_length:
695 * 100 * (tnode_child_length(tn) - tn->empty_children +
696 * tn->full_children) >=
697 * inflate_threshold * tnode_child_length(tn) * 2
698 *
699 * shorten again:
700 * 50 * (tn->full_children + tnode_child_length(tn) -
701 * tn->empty_children) >= inflate_threshold *
702 * tnode_child_length(tn)
703 *
704 */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800705static bool should_inflate(const struct tnode *tp, const struct tnode *tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800706{
707 unsigned long used = tnode_child_length(tn);
708 unsigned long threshold = used;
709
710 /* Keep root node larger */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800711 threshold *= tp ? inflate_threshold : inflate_threshold_root;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800712 used += tn->full_children;
713 used -= tn->empty_children;
714
715 return tn->pos && ((50 * used) >= threshold);
716}
717
Alexander Duyckff181ed2014-12-31 10:56:43 -0800718static bool should_halve(const struct tnode *tp, const struct tnode *tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800719{
720 unsigned long used = tnode_child_length(tn);
721 unsigned long threshold = used;
722
723 /* Keep root node larger */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800724 threshold *= tp ? halve_threshold : halve_threshold_root;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800725 used -= tn->empty_children;
726
727 return (tn->bits > 1) && ((100 * used) < threshold);
728}
729
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800730#define MAX_WORK 10
Alexander Duyckff181ed2014-12-31 10:56:43 -0800731static void resize(struct trie *t, struct tnode *tn)
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800732{
Alexander Duyckff181ed2014-12-31 10:56:43 -0800733 struct tnode *tp = node_parent(tn), *n = NULL;
734 struct tnode __rcu **cptr;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800735 int max_work;
736
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800737 pr_debug("In tnode_resize %p inflate_threshold=%d threshold=%d\n",
738 tn, inflate_threshold, halve_threshold);
739
Alexander Duyckff181ed2014-12-31 10:56:43 -0800740 /* track the tnode via the pointer from the parent instead of
741 * doing it ourselves. This way we can let RCU fully do its
742 * thing without us interfering
743 */
744 cptr = tp ? &tp->child[get_index(tn->key, tp)] : &t->trie;
745 BUG_ON(tn != rtnl_dereference(*cptr));
746
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800747 /* No children */
748 if (tn->empty_children > (tnode_child_length(tn) - 1))
749 goto no_children;
750
751 /* One child */
752 if (tn->empty_children == (tnode_child_length(tn) - 1))
753 goto one_child;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800754
755 /* Double as long as the resulting node has a number of
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800756 * nonempty nodes that are above the threshold.
757 */
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800758 max_work = MAX_WORK;
Alexander Duyckff181ed2014-12-31 10:56:43 -0800759 while (should_inflate(tp, tn) && max_work--) {
760 if (inflate(t, tn)) {
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800761#ifdef CONFIG_IP_FIB_TRIE_STATS
762 this_cpu_inc(t->stats->resize_node_skipped);
763#endif
764 break;
765 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800766
767 tn = rtnl_dereference(*cptr);
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800768 }
769
770 /* Return if at least one inflate is run */
771 if (max_work != MAX_WORK)
Alexander Duyckff181ed2014-12-31 10:56:43 -0800772 return;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800773
Alexander Duyckf05a4812014-12-31 10:56:37 -0800774 /* Halve as long as the number of empty children in this
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800775 * node is above threshold.
776 */
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800777 max_work = MAX_WORK;
Alexander Duyckff181ed2014-12-31 10:56:43 -0800778 while (should_halve(tp, tn) && max_work--) {
779 if (halve(t, tn)) {
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800780#ifdef CONFIG_IP_FIB_TRIE_STATS
781 this_cpu_inc(t->stats->resize_node_skipped);
782#endif
783 break;
784 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800785
Alexander Duyckff181ed2014-12-31 10:56:43 -0800786 tn = rtnl_dereference(*cptr);
787 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800788
789 /* Only one child remains */
790 if (tn->empty_children == (tnode_child_length(tn) - 1)) {
791 unsigned long i;
792one_child:
793 for (i = tnode_child_length(tn); !n && i;)
794 n = tnode_get_child(tn, --i);
795no_children:
796 /* compress one level */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800797 put_child_root(tp, t, tn->key, n);
798 node_set_parent(n, tp);
799
800 /* drop dead node */
Alexander Duyckfc86a932014-12-31 10:56:49 -0800801 tnode_free_init(tn);
802 tnode_free(tn);
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800803 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800804}
805
Robert Olsson772cb712005-09-19 15:31:18 -0700806/* readside must use rcu_read_lock currently dump routines
Robert Olsson2373ce12005-08-25 13:01:29 -0700807 via get_fa_head and dump */
808
Alexander Duyckadaf9812014-12-31 10:55:47 -0800809static struct leaf_info *find_leaf_info(struct tnode *l, int plen)
Robert Olsson19baf832005-06-21 12:43:18 -0700810{
Robert Olsson772cb712005-09-19 15:31:18 -0700811 struct hlist_head *head = &l->list;
Robert Olsson19baf832005-06-21 12:43:18 -0700812 struct leaf_info *li;
813
Sasha Levinb67bfe02013-02-27 17:06:00 -0800814 hlist_for_each_entry_rcu(li, head, hlist)
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700815 if (li->plen == plen)
Robert Olsson19baf832005-06-21 12:43:18 -0700816 return li;
Olof Johansson91b9a272005-08-09 20:24:39 -0700817
Robert Olsson19baf832005-06-21 12:43:18 -0700818 return NULL;
819}
820
Alexander Duyckadaf9812014-12-31 10:55:47 -0800821static inline struct list_head *get_fa_head(struct tnode *l, int plen)
Robert Olsson19baf832005-06-21 12:43:18 -0700822{
Robert Olsson772cb712005-09-19 15:31:18 -0700823 struct leaf_info *li = find_leaf_info(l, plen);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700824
Olof Johansson91b9a272005-08-09 20:24:39 -0700825 if (!li)
826 return NULL;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700827
Olof Johansson91b9a272005-08-09 20:24:39 -0700828 return &li->falh;
Robert Olsson19baf832005-06-21 12:43:18 -0700829}
830
831static void insert_leaf_info(struct hlist_head *head, struct leaf_info *new)
832{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900833 struct leaf_info *li = NULL, *last = NULL;
Robert Olsson19baf832005-06-21 12:43:18 -0700834
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900835 if (hlist_empty(head)) {
836 hlist_add_head_rcu(&new->hlist, head);
837 } else {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800838 hlist_for_each_entry(li, head, hlist) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900839 if (new->plen > li->plen)
840 break;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700841
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900842 last = li;
843 }
844 if (last)
Ken Helias1d023282014-08-06 16:09:16 -0700845 hlist_add_behind_rcu(&new->hlist, &last->hlist);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900846 else
847 hlist_add_before_rcu(&new->hlist, &li->hlist);
848 }
Robert Olsson19baf832005-06-21 12:43:18 -0700849}
850
Robert Olsson2373ce12005-08-25 13:01:29 -0700851/* rcu_read_lock needs to be hold by caller from readside */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800852static struct tnode *fib_find_node(struct trie *t, u32 key)
Robert Olsson19baf832005-06-21 12:43:18 -0700853{
Alexander Duyckadaf9812014-12-31 10:55:47 -0800854 struct tnode *n = rcu_dereference_rtnl(t->trie);
Robert Olsson19baf832005-06-21 12:43:18 -0700855
Alexander Duyck939afb02014-12-31 10:56:00 -0800856 while (n) {
857 unsigned long index = get_index(key, n);
858
859 /* This bit of code is a bit tricky but it combines multiple
860 * checks into a single check. The prefix consists of the
861 * prefix plus zeros for the bits in the cindex. The index
862 * is the difference between the key and this value. From
863 * this we can actually derive several pieces of data.
864 * if !(index >> bits)
865 * we know the value is cindex
866 * else
867 * we have a mismatch in skip bits and failed
868 */
869 if (index >> n->bits)
870 return NULL;
871
872 /* we have found a leaf. Prefixes have already been compared */
873 if (IS_LEAF(n))
Robert Olsson19baf832005-06-21 12:43:18 -0700874 break;
Alexander Duyck939afb02014-12-31 10:56:00 -0800875
876 n = rcu_dereference_rtnl(n->child[index]);
Robert Olsson19baf832005-06-21 12:43:18 -0700877 }
Robert Olsson19baf832005-06-21 12:43:18 -0700878
Alexander Duyck939afb02014-12-31 10:56:00 -0800879 return n;
Robert Olsson19baf832005-06-21 12:43:18 -0700880}
881
Jarek Poplawski7b855762009-06-18 00:28:51 -0700882static void trie_rebalance(struct trie *t, struct tnode *tn)
Robert Olsson19baf832005-06-21 12:43:18 -0700883{
Stephen Hemminger06801912007-08-10 15:22:13 -0700884 struct tnode *tp;
Robert Olsson19baf832005-06-21 12:43:18 -0700885
Alexander Duyckff181ed2014-12-31 10:56:43 -0800886 while ((tp = node_parent(tn)) != NULL) {
887 resize(t, tn);
Stephen Hemminger06801912007-08-10 15:22:13 -0700888 tn = tp;
Robert Olsson19baf832005-06-21 12:43:18 -0700889 }
Stephen Hemminger06801912007-08-10 15:22:13 -0700890
Robert Olsson19baf832005-06-21 12:43:18 -0700891 /* Handle last (top) tnode */
Jarek Poplawski7b855762009-06-18 00:28:51 -0700892 if (IS_TNODE(tn))
Alexander Duyckff181ed2014-12-31 10:56:43 -0800893 resize(t, tn);
Robert Olsson19baf832005-06-21 12:43:18 -0700894}
895
Robert Olsson2373ce12005-08-25 13:01:29 -0700896/* only used from updater-side */
897
Stephen Hemmingerfea86ad2008-01-12 20:57:07 -0800898static struct list_head *fib_insert_node(struct trie *t, u32 key, int plen)
Robert Olsson19baf832005-06-21 12:43:18 -0700899{
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700900 struct list_head *fa_head = NULL;
Alexander Duyck836a0122014-12-31 10:56:06 -0800901 struct tnode *l, *n, *tp = NULL;
Robert Olsson19baf832005-06-21 12:43:18 -0700902 struct leaf_info *li;
Robert Olsson19baf832005-06-21 12:43:18 -0700903
Alexander Duyck836a0122014-12-31 10:56:06 -0800904 li = leaf_info_new(plen);
905 if (!li)
906 return NULL;
907 fa_head = &li->falh;
908
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700909 n = rtnl_dereference(t->trie);
Robert Olsson19baf832005-06-21 12:43:18 -0700910
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700911 /* If we point to NULL, stop. Either the tree is empty and we should
912 * just put a new leaf in if, or we have reached an empty child slot,
Robert Olsson19baf832005-06-21 12:43:18 -0700913 * and we should just put our new leaf in that.
Robert Olsson19baf832005-06-21 12:43:18 -0700914 *
Alexander Duyck836a0122014-12-31 10:56:06 -0800915 * If we hit a node with a key that does't match then we should stop
916 * and create a new tnode to replace that node and insert ourselves
917 * and the other node into the new tnode.
Robert Olsson19baf832005-06-21 12:43:18 -0700918 */
Alexander Duyck836a0122014-12-31 10:56:06 -0800919 while (n) {
920 unsigned long index = get_index(key, n);
Robert Olsson19baf832005-06-21 12:43:18 -0700921
Alexander Duyck836a0122014-12-31 10:56:06 -0800922 /* This bit of code is a bit tricky but it combines multiple
923 * checks into a single check. The prefix consists of the
924 * prefix plus zeros for the "bits" in the prefix. The index
925 * is the difference between the key and this value. From
926 * this we can actually derive several pieces of data.
927 * if !(index >> bits)
928 * we know the value is child index
929 * else
930 * we have a mismatch in skip bits and failed
Robert Olsson19baf832005-06-21 12:43:18 -0700931 */
Alexander Duyck836a0122014-12-31 10:56:06 -0800932 if (index >> n->bits)
933 break;
Robert Olsson19baf832005-06-21 12:43:18 -0700934
Alexander Duyck836a0122014-12-31 10:56:06 -0800935 /* we have found a leaf. Prefixes have already been compared */
936 if (IS_LEAF(n)) {
937 /* Case 1: n is a leaf, and prefixes match*/
938 insert_leaf_info(&n->list, li);
939 return fa_head;
Robert Olsson19baf832005-06-21 12:43:18 -0700940 }
Robert Olsson19baf832005-06-21 12:43:18 -0700941
Alexander Duyck836a0122014-12-31 10:56:06 -0800942 tp = n;
943 n = rcu_dereference_rtnl(n->child[index]);
944 }
945
946 l = leaf_new(key);
947 if (!l) {
948 free_leaf_info(li);
949 return NULL;
950 }
951
952 insert_leaf_info(&l->list, li);
953
954 /* Case 2: n is a LEAF or a TNODE and the key doesn't match.
955 *
956 * Add a new tnode here
957 * first tnode need some special handling
958 * leaves us in position for handling as case 3
959 */
960 if (n) {
961 struct tnode *tn;
Alexander Duyck836a0122014-12-31 10:56:06 -0800962
Alexander Duycke9b44012014-12-31 10:56:12 -0800963 tn = tnode_new(key, __fls(key ^ n->key), 1);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700964 if (!tn) {
Robert Olssonf835e472005-06-28 15:00:39 -0700965 free_leaf_info(li);
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800966 node_free(l);
Stephen Hemmingerfea86ad2008-01-12 20:57:07 -0800967 return NULL;
Olof Johansson91b9a272005-08-09 20:24:39 -0700968 }
969
Alexander Duyck836a0122014-12-31 10:56:06 -0800970 /* initialize routes out of node */
971 NODE_INIT_PARENT(tn, tp);
972 put_child(tn, get_index(key, tn) ^ 1, n);
Robert Olsson19baf832005-06-21 12:43:18 -0700973
Alexander Duyck836a0122014-12-31 10:56:06 -0800974 /* start adding routes into the node */
975 put_child_root(tp, t, key, tn);
976 node_set_parent(n, tn);
Robert Olsson19baf832005-06-21 12:43:18 -0700977
Alexander Duyck836a0122014-12-31 10:56:06 -0800978 /* parent now has a NULL spot where the leaf can go */
Alexander Duycke962f302014-12-10 21:49:22 -0800979 tp = tn;
Robert Olsson19baf832005-06-21 12:43:18 -0700980 }
Olof Johansson91b9a272005-08-09 20:24:39 -0700981
Alexander Duyck836a0122014-12-31 10:56:06 -0800982 /* Case 3: n is NULL, and will just insert a new leaf */
983 if (tp) {
984 NODE_INIT_PARENT(l, tp);
985 put_child(tp, get_index(key, tp), l);
986 trie_rebalance(t, tp);
987 } else {
988 rcu_assign_pointer(t->trie, l);
989 }
Olof Johansson91b9a272005-08-09 20:24:39 -0700990
Robert Olsson19baf832005-06-21 12:43:18 -0700991 return fa_head;
992}
993
Robert Olssond562f1f2007-03-26 14:22:22 -0700994/*
995 * Caller must hold RTNL.
996 */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +0000997int fib_table_insert(struct fib_table *tb, struct fib_config *cfg)
Robert Olsson19baf832005-06-21 12:43:18 -0700998{
999 struct trie *t = (struct trie *) tb->tb_data;
1000 struct fib_alias *fa, *new_fa;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001001 struct list_head *fa_head = NULL;
Robert Olsson19baf832005-06-21 12:43:18 -07001002 struct fib_info *fi;
Thomas Graf4e902c52006-08-17 18:14:52 -07001003 int plen = cfg->fc_dst_len;
1004 u8 tos = cfg->fc_tos;
Robert Olsson19baf832005-06-21 12:43:18 -07001005 u32 key, mask;
1006 int err;
Alexander Duyckadaf9812014-12-31 10:55:47 -08001007 struct tnode *l;
Robert Olsson19baf832005-06-21 12:43:18 -07001008
1009 if (plen > 32)
1010 return -EINVAL;
1011
Thomas Graf4e902c52006-08-17 18:14:52 -07001012 key = ntohl(cfg->fc_dst);
Robert Olsson19baf832005-06-21 12:43:18 -07001013
Patrick McHardy2dfe55b2006-08-10 23:08:33 -07001014 pr_debug("Insert table=%u %08x/%d\n", tb->tb_id, key, plen);
Robert Olsson19baf832005-06-21 12:43:18 -07001015
Olof Johansson91b9a272005-08-09 20:24:39 -07001016 mask = ntohl(inet_make_mask(plen));
Robert Olsson19baf832005-06-21 12:43:18 -07001017
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001018 if (key & ~mask)
Robert Olsson19baf832005-06-21 12:43:18 -07001019 return -EINVAL;
1020
1021 key = key & mask;
1022
Thomas Graf4e902c52006-08-17 18:14:52 -07001023 fi = fib_create_info(cfg);
1024 if (IS_ERR(fi)) {
1025 err = PTR_ERR(fi);
Robert Olsson19baf832005-06-21 12:43:18 -07001026 goto err;
Thomas Graf4e902c52006-08-17 18:14:52 -07001027 }
Robert Olsson19baf832005-06-21 12:43:18 -07001028
1029 l = fib_find_node(t, key);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001030 fa = NULL;
Robert Olsson19baf832005-06-21 12:43:18 -07001031
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001032 if (l) {
Robert Olsson19baf832005-06-21 12:43:18 -07001033 fa_head = get_fa_head(l, plen);
1034 fa = fib_find_alias(fa_head, tos, fi->fib_priority);
1035 }
1036
1037 /* Now fa, if non-NULL, points to the first fib alias
1038 * with the same keys [prefix,tos,priority], if such key already
1039 * exists or to the node before which we will insert new one.
1040 *
1041 * If fa is NULL, we will need to allocate a new one and
1042 * insert to the head of f.
1043 *
1044 * If f is NULL, no fib node matched the destination key
1045 * and we need to allocate a new one of those as well.
1046 */
1047
Julian Anastasov936f6f82008-01-28 21:18:06 -08001048 if (fa && fa->fa_tos == tos &&
1049 fa->fa_info->fib_priority == fi->fib_priority) {
1050 struct fib_alias *fa_first, *fa_match;
Robert Olsson19baf832005-06-21 12:43:18 -07001051
1052 err = -EEXIST;
Thomas Graf4e902c52006-08-17 18:14:52 -07001053 if (cfg->fc_nlflags & NLM_F_EXCL)
Robert Olsson19baf832005-06-21 12:43:18 -07001054 goto out;
1055
Julian Anastasov936f6f82008-01-28 21:18:06 -08001056 /* We have 2 goals:
1057 * 1. Find exact match for type, scope, fib_info to avoid
1058 * duplicate routes
1059 * 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it
1060 */
1061 fa_match = NULL;
1062 fa_first = fa;
1063 fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
1064 list_for_each_entry_continue(fa, fa_head, fa_list) {
1065 if (fa->fa_tos != tos)
1066 break;
1067 if (fa->fa_info->fib_priority != fi->fib_priority)
1068 break;
1069 if (fa->fa_type == cfg->fc_type &&
Julian Anastasov936f6f82008-01-28 21:18:06 -08001070 fa->fa_info == fi) {
1071 fa_match = fa;
1072 break;
1073 }
1074 }
1075
Thomas Graf4e902c52006-08-17 18:14:52 -07001076 if (cfg->fc_nlflags & NLM_F_REPLACE) {
Robert Olsson19baf832005-06-21 12:43:18 -07001077 struct fib_info *fi_drop;
1078 u8 state;
1079
Julian Anastasov936f6f82008-01-28 21:18:06 -08001080 fa = fa_first;
1081 if (fa_match) {
1082 if (fa == fa_match)
1083 err = 0;
Joonwoo Park67250332008-01-18 03:45:18 -08001084 goto out;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001085 }
Robert Olsson2373ce12005-08-25 13:01:29 -07001086 err = -ENOBUFS;
Christoph Lametere94b1762006-12-06 20:33:17 -08001087 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
Robert Olsson2373ce12005-08-25 13:01:29 -07001088 if (new_fa == NULL)
1089 goto out;
Robert Olsson19baf832005-06-21 12:43:18 -07001090
1091 fi_drop = fa->fa_info;
Robert Olsson2373ce12005-08-25 13:01:29 -07001092 new_fa->fa_tos = fa->fa_tos;
1093 new_fa->fa_info = fi;
Thomas Graf4e902c52006-08-17 18:14:52 -07001094 new_fa->fa_type = cfg->fc_type;
Robert Olsson19baf832005-06-21 12:43:18 -07001095 state = fa->fa_state;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001096 new_fa->fa_state = state & ~FA_S_ACCESSED;
Robert Olsson19baf832005-06-21 12:43:18 -07001097
Robert Olsson2373ce12005-08-25 13:01:29 -07001098 list_replace_rcu(&fa->fa_list, &new_fa->fa_list);
1099 alias_free_mem_rcu(fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001100
1101 fib_release_info(fi_drop);
1102 if (state & FA_S_ACCESSED)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001103 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Milan Kocianb8f55832007-05-23 14:55:06 -07001104 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen,
1105 tb->tb_id, &cfg->fc_nlinfo, NLM_F_REPLACE);
Robert Olsson19baf832005-06-21 12:43:18 -07001106
Olof Johansson91b9a272005-08-09 20:24:39 -07001107 goto succeeded;
Robert Olsson19baf832005-06-21 12:43:18 -07001108 }
1109 /* Error if we find a perfect match which
1110 * uses the same scope, type, and nexthop
1111 * information.
1112 */
Julian Anastasov936f6f82008-01-28 21:18:06 -08001113 if (fa_match)
1114 goto out;
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001115
Thomas Graf4e902c52006-08-17 18:14:52 -07001116 if (!(cfg->fc_nlflags & NLM_F_APPEND))
Julian Anastasov936f6f82008-01-28 21:18:06 -08001117 fa = fa_first;
Robert Olsson19baf832005-06-21 12:43:18 -07001118 }
1119 err = -ENOENT;
Thomas Graf4e902c52006-08-17 18:14:52 -07001120 if (!(cfg->fc_nlflags & NLM_F_CREATE))
Robert Olsson19baf832005-06-21 12:43:18 -07001121 goto out;
1122
1123 err = -ENOBUFS;
Christoph Lametere94b1762006-12-06 20:33:17 -08001124 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
Robert Olsson19baf832005-06-21 12:43:18 -07001125 if (new_fa == NULL)
1126 goto out;
1127
1128 new_fa->fa_info = fi;
1129 new_fa->fa_tos = tos;
Thomas Graf4e902c52006-08-17 18:14:52 -07001130 new_fa->fa_type = cfg->fc_type;
Robert Olsson19baf832005-06-21 12:43:18 -07001131 new_fa->fa_state = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001132 /*
1133 * Insert new entry to the list.
1134 */
1135
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001136 if (!fa_head) {
Stephen Hemmingerfea86ad2008-01-12 20:57:07 -08001137 fa_head = fib_insert_node(t, key, plen);
1138 if (unlikely(!fa_head)) {
1139 err = -ENOMEM;
Robert Olssonf835e472005-06-28 15:00:39 -07001140 goto out_free_new_fa;
Stephen Hemmingerfea86ad2008-01-12 20:57:07 -08001141 }
Robert Olssonf835e472005-06-28 15:00:39 -07001142 }
Robert Olsson19baf832005-06-21 12:43:18 -07001143
David S. Miller21d8c492011-04-14 14:49:37 -07001144 if (!plen)
1145 tb->tb_num_default++;
1146
Robert Olsson2373ce12005-08-25 13:01:29 -07001147 list_add_tail_rcu(&new_fa->fa_list,
1148 (fa ? &fa->fa_list : fa_head));
Robert Olsson19baf832005-06-21 12:43:18 -07001149
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001150 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Thomas Graf4e902c52006-08-17 18:14:52 -07001151 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen, tb->tb_id,
Milan Kocianb8f55832007-05-23 14:55:06 -07001152 &cfg->fc_nlinfo, 0);
Robert Olsson19baf832005-06-21 12:43:18 -07001153succeeded:
1154 return 0;
Robert Olssonf835e472005-06-28 15:00:39 -07001155
1156out_free_new_fa:
1157 kmem_cache_free(fn_alias_kmem, new_fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001158out:
1159 fib_release_info(fi);
Olof Johansson91b9a272005-08-09 20:24:39 -07001160err:
Robert Olsson19baf832005-06-21 12:43:18 -07001161 return err;
1162}
1163
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001164static inline t_key prefix_mismatch(t_key key, struct tnode *n)
1165{
1166 t_key prefix = n->key;
1167
1168 return (key ^ prefix) & (prefix | -prefix);
1169}
1170
Alexander Duyck345e9b52014-12-31 10:56:24 -08001171/* should be called with rcu_read_lock */
David S. Miller22bd5b92011-03-11 19:54:08 -05001172int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00001173 struct fib_result *res, int fib_flags)
Robert Olsson19baf832005-06-21 12:43:18 -07001174{
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001175 struct trie *t = (struct trie *)tb->tb_data;
Alexander Duyck8274a972014-12-31 10:55:29 -08001176#ifdef CONFIG_IP_FIB_TRIE_STATS
1177 struct trie_use_stats __percpu *stats = t->stats;
1178#endif
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001179 const t_key key = ntohl(flp->daddr);
1180 struct tnode *n, *pn;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001181 struct leaf_info *li;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001182 t_key cindex;
Robert Olsson19baf832005-06-21 12:43:18 -07001183
Robert Olsson2373ce12005-08-25 13:01:29 -07001184 n = rcu_dereference(t->trie);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001185 if (!n)
Alexander Duyck345e9b52014-12-31 10:56:24 -08001186 return -EAGAIN;
Robert Olsson19baf832005-06-21 12:43:18 -07001187
1188#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -08001189 this_cpu_inc(stats->gets);
Robert Olsson19baf832005-06-21 12:43:18 -07001190#endif
1191
Alexander Duyckadaf9812014-12-31 10:55:47 -08001192 pn = n;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001193 cindex = 0;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001194
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001195 /* Step 1: Travel to the longest prefix match in the trie */
1196 for (;;) {
1197 unsigned long index = get_index(key, n);
Robert Olsson19baf832005-06-21 12:43:18 -07001198
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001199 /* This bit of code is a bit tricky but it combines multiple
1200 * checks into a single check. The prefix consists of the
1201 * prefix plus zeros for the "bits" in the prefix. The index
1202 * is the difference between the key and this value. From
1203 * this we can actually derive several pieces of data.
1204 * if !(index >> bits)
1205 * we know the value is child index
1206 * else
1207 * we have a mismatch in skip bits and failed
1208 */
1209 if (index >> n->bits)
1210 break;
Robert Olsson19baf832005-06-21 12:43:18 -07001211
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001212 /* we have found a leaf. Prefixes have already been compared */
1213 if (IS_LEAF(n))
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001214 goto found;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001215
1216 /* only record pn and cindex if we are going to be chopping
1217 * bits later. Otherwise we are just wasting cycles.
1218 */
1219 if (index) {
1220 pn = n;
1221 cindex = index;
Olof Johansson91b9a272005-08-09 20:24:39 -07001222 }
1223
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001224 n = rcu_dereference(n->child[index]);
1225 if (unlikely(!n))
Robert Olsson19baf832005-06-21 12:43:18 -07001226 goto backtrace;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001227 }
1228
1229 /* Step 2: Sort out leaves and begin backtracing for longest prefix */
1230 for (;;) {
1231 /* record the pointer where our next node pointer is stored */
1232 struct tnode __rcu **cptr = n->child;
1233
1234 /* This test verifies that none of the bits that differ
1235 * between the key and the prefix exist in the region of
1236 * the lsb and higher in the prefix.
1237 */
1238 if (unlikely(prefix_mismatch(key, n)))
1239 goto backtrace;
1240
1241 /* exit out and process leaf */
1242 if (unlikely(IS_LEAF(n)))
1243 break;
1244
1245 /* Don't bother recording parent info. Since we are in
1246 * prefix match mode we will have to come back to wherever
1247 * we started this traversal anyway
1248 */
1249
1250 while ((n = rcu_dereference(*cptr)) == NULL) {
1251backtrace:
1252#ifdef CONFIG_IP_FIB_TRIE_STATS
1253 if (!n)
1254 this_cpu_inc(stats->null_node_hit);
1255#endif
1256 /* If we are at cindex 0 there are no more bits for
1257 * us to strip at this level so we must ascend back
1258 * up one level to see if there are any more bits to
1259 * be stripped there.
1260 */
1261 while (!cindex) {
1262 t_key pkey = pn->key;
1263
1264 pn = node_parent_rcu(pn);
1265 if (unlikely(!pn))
Alexander Duyck345e9b52014-12-31 10:56:24 -08001266 return -EAGAIN;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001267#ifdef CONFIG_IP_FIB_TRIE_STATS
1268 this_cpu_inc(stats->backtrack);
1269#endif
1270 /* Get Child's index */
1271 cindex = get_index(pkey, pn);
1272 }
1273
1274 /* strip the least significant bit from the cindex */
1275 cindex &= cindex - 1;
1276
1277 /* grab pointer for next child node */
1278 cptr = &pn->child[cindex];
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001279 }
Robert Olsson19baf832005-06-21 12:43:18 -07001280 }
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001281
Robert Olsson19baf832005-06-21 12:43:18 -07001282found:
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001283 /* Step 3: Process the leaf, if that fails fall back to backtracing */
Alexander Duyck345e9b52014-12-31 10:56:24 -08001284 hlist_for_each_entry_rcu(li, &n->list, hlist) {
1285 struct fib_alias *fa;
1286
1287 if ((key ^ n->key) & li->mask_plen)
1288 continue;
1289
1290 list_for_each_entry_rcu(fa, &li->falh, fa_list) {
1291 struct fib_info *fi = fa->fa_info;
1292 int nhsel, err;
1293
1294 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
1295 continue;
1296 if (fi->fib_dead)
1297 continue;
1298 if (fa->fa_info->fib_scope < flp->flowi4_scope)
1299 continue;
1300 fib_alias_accessed(fa);
1301 err = fib_props[fa->fa_type].error;
1302 if (unlikely(err < 0)) {
1303#ifdef CONFIG_IP_FIB_TRIE_STATS
1304 this_cpu_inc(stats->semantic_match_passed);
1305#endif
1306 return err;
1307 }
1308 if (fi->fib_flags & RTNH_F_DEAD)
1309 continue;
1310 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
1311 const struct fib_nh *nh = &fi->fib_nh[nhsel];
1312
1313 if (nh->nh_flags & RTNH_F_DEAD)
1314 continue;
1315 if (flp->flowi4_oif && flp->flowi4_oif != nh->nh_oif)
1316 continue;
1317
1318 if (!(fib_flags & FIB_LOOKUP_NOREF))
1319 atomic_inc(&fi->fib_clntref);
1320
1321 res->prefixlen = li->plen;
1322 res->nh_sel = nhsel;
1323 res->type = fa->fa_type;
1324 res->scope = fi->fib_scope;
1325 res->fi = fi;
1326 res->table = tb;
1327 res->fa_head = &li->falh;
1328#ifdef CONFIG_IP_FIB_TRIE_STATS
1329 this_cpu_inc(stats->semantic_match_passed);
1330#endif
1331 return err;
1332 }
1333 }
1334
1335#ifdef CONFIG_IP_FIB_TRIE_STATS
1336 this_cpu_inc(stats->semantic_match_miss);
1337#endif
1338 }
1339 goto backtrace;
Robert Olsson19baf832005-06-21 12:43:18 -07001340}
Florian Westphal6fc01432011-08-25 13:46:12 +02001341EXPORT_SYMBOL_GPL(fib_table_lookup);
Robert Olsson19baf832005-06-21 12:43:18 -07001342
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001343/*
1344 * Remove the leaf and return parent.
1345 */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001346static void trie_leaf_remove(struct trie *t, struct tnode *l)
Robert Olsson19baf832005-06-21 12:43:18 -07001347{
Alexander Duyck64c9b6f2014-12-31 10:55:35 -08001348 struct tnode *tp = node_parent(l);
Robert Olsson19baf832005-06-21 12:43:18 -07001349
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001350 pr_debug("entering trie_leaf_remove(%p)\n", l);
Robert Olsson19baf832005-06-21 12:43:18 -07001351
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001352 if (tp) {
Alexander Duyck836a0122014-12-31 10:56:06 -08001353 put_child(tp, get_index(l->key, tp), NULL);
Jarek Poplawski7b855762009-06-18 00:28:51 -07001354 trie_rebalance(t, tp);
Alexander Duyck836a0122014-12-31 10:56:06 -08001355 } else {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001356 RCU_INIT_POINTER(t->trie, NULL);
Alexander Duyck836a0122014-12-31 10:56:06 -08001357 }
Robert Olsson19baf832005-06-21 12:43:18 -07001358
Alexander Duyck37fd30f2014-12-31 10:55:41 -08001359 node_free(l);
Robert Olsson19baf832005-06-21 12:43:18 -07001360}
1361
Robert Olssond562f1f2007-03-26 14:22:22 -07001362/*
1363 * Caller must hold RTNL.
1364 */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001365int fib_table_delete(struct fib_table *tb, struct fib_config *cfg)
Robert Olsson19baf832005-06-21 12:43:18 -07001366{
1367 struct trie *t = (struct trie *) tb->tb_data;
1368 u32 key, mask;
Thomas Graf4e902c52006-08-17 18:14:52 -07001369 int plen = cfg->fc_dst_len;
1370 u8 tos = cfg->fc_tos;
Robert Olsson19baf832005-06-21 12:43:18 -07001371 struct fib_alias *fa, *fa_to_delete;
1372 struct list_head *fa_head;
Alexander Duyckadaf9812014-12-31 10:55:47 -08001373 struct tnode *l;
Olof Johansson91b9a272005-08-09 20:24:39 -07001374 struct leaf_info *li;
1375
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001376 if (plen > 32)
Robert Olsson19baf832005-06-21 12:43:18 -07001377 return -EINVAL;
1378
Thomas Graf4e902c52006-08-17 18:14:52 -07001379 key = ntohl(cfg->fc_dst);
Olof Johansson91b9a272005-08-09 20:24:39 -07001380 mask = ntohl(inet_make_mask(plen));
Robert Olsson19baf832005-06-21 12:43:18 -07001381
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001382 if (key & ~mask)
Robert Olsson19baf832005-06-21 12:43:18 -07001383 return -EINVAL;
1384
1385 key = key & mask;
1386 l = fib_find_node(t, key);
1387
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001388 if (!l)
Robert Olsson19baf832005-06-21 12:43:18 -07001389 return -ESRCH;
1390
Igor Maravicad5b3102012-08-13 10:26:08 +02001391 li = find_leaf_info(l, plen);
1392
1393 if (!li)
1394 return -ESRCH;
1395
1396 fa_head = &li->falh;
Robert Olsson19baf832005-06-21 12:43:18 -07001397 fa = fib_find_alias(fa_head, tos, 0);
1398
1399 if (!fa)
1400 return -ESRCH;
1401
Stephen Hemminger0c7770c2005-08-23 21:59:41 -07001402 pr_debug("Deleting %08x/%d tos=%d t=%p\n", key, plen, tos, t);
Robert Olsson19baf832005-06-21 12:43:18 -07001403
1404 fa_to_delete = NULL;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001405 fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
1406 list_for_each_entry_continue(fa, fa_head, fa_list) {
Robert Olsson19baf832005-06-21 12:43:18 -07001407 struct fib_info *fi = fa->fa_info;
1408
1409 if (fa->fa_tos != tos)
1410 break;
1411
Thomas Graf4e902c52006-08-17 18:14:52 -07001412 if ((!cfg->fc_type || fa->fa_type == cfg->fc_type) &&
1413 (cfg->fc_scope == RT_SCOPE_NOWHERE ||
David S. Miller37e826c2011-03-24 18:06:47 -07001414 fa->fa_info->fib_scope == cfg->fc_scope) &&
Julian Anastasov74cb3c12011-03-19 12:13:46 +00001415 (!cfg->fc_prefsrc ||
1416 fi->fib_prefsrc == cfg->fc_prefsrc) &&
Thomas Graf4e902c52006-08-17 18:14:52 -07001417 (!cfg->fc_protocol ||
1418 fi->fib_protocol == cfg->fc_protocol) &&
1419 fib_nh_match(cfg, fi) == 0) {
Robert Olsson19baf832005-06-21 12:43:18 -07001420 fa_to_delete = fa;
1421 break;
1422 }
1423 }
1424
Olof Johansson91b9a272005-08-09 20:24:39 -07001425 if (!fa_to_delete)
1426 return -ESRCH;
Robert Olsson19baf832005-06-21 12:43:18 -07001427
Olof Johansson91b9a272005-08-09 20:24:39 -07001428 fa = fa_to_delete;
Thomas Graf4e902c52006-08-17 18:14:52 -07001429 rtmsg_fib(RTM_DELROUTE, htonl(key), fa, plen, tb->tb_id,
Milan Kocianb8f55832007-05-23 14:55:06 -07001430 &cfg->fc_nlinfo, 0);
Robert Olsson19baf832005-06-21 12:43:18 -07001431
Robert Olsson2373ce12005-08-25 13:01:29 -07001432 list_del_rcu(&fa->fa_list);
Robert Olsson19baf832005-06-21 12:43:18 -07001433
David S. Miller21d8c492011-04-14 14:49:37 -07001434 if (!plen)
1435 tb->tb_num_default--;
1436
Olof Johansson91b9a272005-08-09 20:24:39 -07001437 if (list_empty(fa_head)) {
Robert Olsson2373ce12005-08-25 13:01:29 -07001438 hlist_del_rcu(&li->hlist);
Olof Johansson91b9a272005-08-09 20:24:39 -07001439 free_leaf_info(li);
Robert Olsson2373ce12005-08-25 13:01:29 -07001440 }
Olof Johansson91b9a272005-08-09 20:24:39 -07001441
1442 if (hlist_empty(&l->list))
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001443 trie_leaf_remove(t, l);
Olof Johansson91b9a272005-08-09 20:24:39 -07001444
1445 if (fa->fa_state & FA_S_ACCESSED)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001446 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Olof Johansson91b9a272005-08-09 20:24:39 -07001447
Robert Olsson2373ce12005-08-25 13:01:29 -07001448 fib_release_info(fa->fa_info);
1449 alias_free_mem_rcu(fa);
Olof Johansson91b9a272005-08-09 20:24:39 -07001450 return 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001451}
1452
Stephen Hemmingeref3660c2008-04-10 03:46:12 -07001453static int trie_flush_list(struct list_head *head)
Robert Olsson19baf832005-06-21 12:43:18 -07001454{
1455 struct fib_alias *fa, *fa_node;
1456 int found = 0;
1457
1458 list_for_each_entry_safe(fa, fa_node, head, fa_list) {
1459 struct fib_info *fi = fa->fa_info;
Robert Olsson19baf832005-06-21 12:43:18 -07001460
Robert Olsson2373ce12005-08-25 13:01:29 -07001461 if (fi && (fi->fib_flags & RTNH_F_DEAD)) {
1462 list_del_rcu(&fa->fa_list);
1463 fib_release_info(fa->fa_info);
1464 alias_free_mem_rcu(fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001465 found++;
1466 }
1467 }
1468 return found;
1469}
1470
Alexander Duyckadaf9812014-12-31 10:55:47 -08001471static int trie_flush_leaf(struct tnode *l)
Robert Olsson19baf832005-06-21 12:43:18 -07001472{
1473 int found = 0;
1474 struct hlist_head *lih = &l->list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001475 struct hlist_node *tmp;
Robert Olsson19baf832005-06-21 12:43:18 -07001476 struct leaf_info *li = NULL;
1477
Sasha Levinb67bfe02013-02-27 17:06:00 -08001478 hlist_for_each_entry_safe(li, tmp, lih, hlist) {
Stephen Hemmingeref3660c2008-04-10 03:46:12 -07001479 found += trie_flush_list(&li->falh);
Robert Olsson19baf832005-06-21 12:43:18 -07001480
1481 if (list_empty(&li->falh)) {
Robert Olsson2373ce12005-08-25 13:01:29 -07001482 hlist_del_rcu(&li->hlist);
Robert Olsson19baf832005-06-21 12:43:18 -07001483 free_leaf_info(li);
1484 }
1485 }
1486 return found;
1487}
1488
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001489/*
1490 * Scan for the next right leaf starting at node p->child[idx]
1491 * Since we have back pointer, no recursion necessary.
1492 */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001493static struct tnode *leaf_walk_rcu(struct tnode *p, struct tnode *c)
Robert Olsson19baf832005-06-21 12:43:18 -07001494{
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001495 do {
Alexander Duyck98293e82014-12-31 10:56:18 -08001496 unsigned long idx = c ? idx = get_index(c->key, p) + 1 : 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001497
Alexander Duyck98293e82014-12-31 10:56:18 -08001498 while (idx < tnode_child_length(p)) {
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001499 c = tnode_get_child_rcu(p, idx++);
Robert Olsson2373ce12005-08-25 13:01:29 -07001500 if (!c)
Olof Johansson91b9a272005-08-09 20:24:39 -07001501 continue;
Robert Olsson19baf832005-06-21 12:43:18 -07001502
Eric Dumazetaab515d2013-08-05 11:18:49 -07001503 if (IS_LEAF(c))
Alexander Duyckadaf9812014-12-31 10:55:47 -08001504 return c;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001505
1506 /* Rescan start scanning in new node */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001507 p = c;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001508 idx = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001509 }
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001510
1511 /* Node empty, walk back up to parent */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001512 c = p;
Eric Dumazeta034ee32010-09-09 23:32:28 +00001513 } while ((p = node_parent_rcu(c)) != NULL);
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001514
1515 return NULL; /* Root of trie */
1516}
1517
Alexander Duyckadaf9812014-12-31 10:55:47 -08001518static struct tnode *trie_firstleaf(struct trie *t)
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001519{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001520 struct tnode *n = rcu_dereference_rtnl(t->trie);
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001521
1522 if (!n)
1523 return NULL;
1524
1525 if (IS_LEAF(n)) /* trie is just a leaf */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001526 return n;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001527
1528 return leaf_walk_rcu(n, NULL);
1529}
1530
Alexander Duyckadaf9812014-12-31 10:55:47 -08001531static struct tnode *trie_nextleaf(struct tnode *l)
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001532{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001533 struct tnode *p = node_parent_rcu(l);
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001534
1535 if (!p)
1536 return NULL; /* trie with just one leaf */
1537
Alexander Duyckadaf9812014-12-31 10:55:47 -08001538 return leaf_walk_rcu(p, l);
Robert Olsson19baf832005-06-21 12:43:18 -07001539}
1540
Alexander Duyckadaf9812014-12-31 10:55:47 -08001541static struct tnode *trie_leafindex(struct trie *t, int index)
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001542{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001543 struct tnode *l = trie_firstleaf(t);
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001544
Stephen Hemmingerec28cf72008-02-11 21:12:49 -08001545 while (l && index-- > 0)
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001546 l = trie_nextleaf(l);
Stephen Hemmingerec28cf72008-02-11 21:12:49 -08001547
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001548 return l;
1549}
1550
1551
Robert Olssond562f1f2007-03-26 14:22:22 -07001552/*
1553 * Caller must hold RTNL.
1554 */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001555int fib_table_flush(struct fib_table *tb)
Robert Olsson19baf832005-06-21 12:43:18 -07001556{
1557 struct trie *t = (struct trie *) tb->tb_data;
Alexander Duyckadaf9812014-12-31 10:55:47 -08001558 struct tnode *l, *ll = NULL;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001559 int found = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001560
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001561 for (l = trie_firstleaf(t); l; l = trie_nextleaf(l)) {
Stephen Hemmingeref3660c2008-04-10 03:46:12 -07001562 found += trie_flush_leaf(l);
Robert Olsson19baf832005-06-21 12:43:18 -07001563
1564 if (ll && hlist_empty(&ll->list))
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001565 trie_leaf_remove(t, ll);
Robert Olsson19baf832005-06-21 12:43:18 -07001566 ll = l;
1567 }
1568
1569 if (ll && hlist_empty(&ll->list))
Stephen Hemminger9195bef2008-01-22 21:56:34 -08001570 trie_leaf_remove(t, ll);
Robert Olsson19baf832005-06-21 12:43:18 -07001571
Stephen Hemminger0c7770c2005-08-23 21:59:41 -07001572 pr_debug("trie_flush found=%d\n", found);
Robert Olsson19baf832005-06-21 12:43:18 -07001573 return found;
1574}
1575
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00001576void fib_free_table(struct fib_table *tb)
1577{
Alexander Duyck8274a972014-12-31 10:55:29 -08001578#ifdef CONFIG_IP_FIB_TRIE_STATS
1579 struct trie *t = (struct trie *)tb->tb_data;
1580
1581 free_percpu(t->stats);
1582#endif /* CONFIG_IP_FIB_TRIE_STATS */
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00001583 kfree(tb);
1584}
1585
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001586static int fn_trie_dump_fa(t_key key, int plen, struct list_head *fah,
1587 struct fib_table *tb,
Robert Olsson19baf832005-06-21 12:43:18 -07001588 struct sk_buff *skb, struct netlink_callback *cb)
1589{
1590 int i, s_i;
1591 struct fib_alias *fa;
Al Viro32ab5f82006-09-26 22:21:45 -07001592 __be32 xkey = htonl(key);
Robert Olsson19baf832005-06-21 12:43:18 -07001593
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001594 s_i = cb->args[5];
Robert Olsson19baf832005-06-21 12:43:18 -07001595 i = 0;
1596
Robert Olsson2373ce12005-08-25 13:01:29 -07001597 /* rcu_read_lock is hold by caller */
1598
1599 list_for_each_entry_rcu(fa, fah, fa_list) {
Robert Olsson19baf832005-06-21 12:43:18 -07001600 if (i < s_i) {
1601 i++;
1602 continue;
1603 }
Robert Olsson19baf832005-06-21 12:43:18 -07001604
Eric W. Biederman15e47302012-09-07 20:12:54 +00001605 if (fib_dump_info(skb, NETLINK_CB(cb->skb).portid,
Robert Olsson19baf832005-06-21 12:43:18 -07001606 cb->nlh->nlmsg_seq,
1607 RTM_NEWROUTE,
1608 tb->tb_id,
1609 fa->fa_type,
Thomas Grafbe403ea2006-08-17 18:15:17 -07001610 xkey,
Robert Olsson19baf832005-06-21 12:43:18 -07001611 plen,
1612 fa->fa_tos,
Stephen Hemminger64347f72008-01-22 21:55:01 -08001613 fa->fa_info, NLM_F_MULTI) < 0) {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001614 cb->args[5] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001615 return -1;
Olof Johansson91b9a272005-08-09 20:24:39 -07001616 }
Robert Olsson19baf832005-06-21 12:43:18 -07001617 i++;
1618 }
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001619 cb->args[5] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001620 return skb->len;
1621}
1622
Alexander Duyckadaf9812014-12-31 10:55:47 -08001623static int fn_trie_dump_leaf(struct tnode *l, struct fib_table *tb,
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001624 struct sk_buff *skb, struct netlink_callback *cb)
Robert Olsson19baf832005-06-21 12:43:18 -07001625{
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001626 struct leaf_info *li;
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001627 int i, s_i;
Robert Olsson19baf832005-06-21 12:43:18 -07001628
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001629 s_i = cb->args[4];
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001630 i = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001631
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001632 /* rcu_read_lock is hold by caller */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001633 hlist_for_each_entry_rcu(li, &l->list, hlist) {
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001634 if (i < s_i) {
1635 i++;
Robert Olsson19baf832005-06-21 12:43:18 -07001636 continue;
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001637 }
Robert Olsson19baf832005-06-21 12:43:18 -07001638
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001639 if (i > s_i)
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001640 cb->args[5] = 0;
Olof Johansson91b9a272005-08-09 20:24:39 -07001641
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001642 if (list_empty(&li->falh))
Robert Olsson19baf832005-06-21 12:43:18 -07001643 continue;
1644
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001645 if (fn_trie_dump_fa(l->key, li->plen, &li->falh, tb, skb, cb) < 0) {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001646 cb->args[4] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001647 return -1;
1648 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001649 i++;
Robert Olsson19baf832005-06-21 12:43:18 -07001650 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001651
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001652 cb->args[4] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07001653 return skb->len;
1654}
1655
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00001656int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
1657 struct netlink_callback *cb)
Robert Olsson19baf832005-06-21 12:43:18 -07001658{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001659 struct tnode *l;
Robert Olsson19baf832005-06-21 12:43:18 -07001660 struct trie *t = (struct trie *) tb->tb_data;
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001661 t_key key = cb->args[2];
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001662 int count = cb->args[3];
Robert Olsson19baf832005-06-21 12:43:18 -07001663
Robert Olsson2373ce12005-08-25 13:01:29 -07001664 rcu_read_lock();
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001665 /* Dump starting at last key.
1666 * Note: 0.0.0.0/0 (ie default) is first key.
1667 */
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001668 if (count == 0)
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001669 l = trie_firstleaf(t);
1670 else {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001671 /* Normally, continue from last key, but if that is missing
1672 * fallback to using slow rescan
1673 */
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001674 l = fib_find_node(t, key);
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001675 if (!l)
1676 l = trie_leafindex(t, count);
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001677 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001678
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001679 while (l) {
1680 cb->args[2] = l->key;
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001681 if (fn_trie_dump_leaf(l, tb, skb, cb) < 0) {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001682 cb->args[3] = count;
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001683 rcu_read_unlock();
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001684 return -1;
Robert Olsson19baf832005-06-21 12:43:18 -07001685 }
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001686
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001687 ++count;
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08001688 l = trie_nextleaf(l);
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001689 memset(&cb->args[4], 0,
1690 sizeof(cb->args) - 4*sizeof(cb->args[0]));
Robert Olsson19baf832005-06-21 12:43:18 -07001691 }
Stephen Hemminger71d67e62008-01-31 16:45:47 -08001692 cb->args[3] = count;
Robert Olsson2373ce12005-08-25 13:01:29 -07001693 rcu_read_unlock();
Stephen Hemmingera88ee222008-01-22 21:56:11 -08001694
Robert Olsson19baf832005-06-21 12:43:18 -07001695 return skb->len;
Robert Olsson19baf832005-06-21 12:43:18 -07001696}
1697
David S. Miller5348ba82011-02-01 15:30:56 -08001698void __init fib_trie_init(void)
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001699{
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001700 fn_alias_kmem = kmem_cache_create("ip_fib_alias",
1701 sizeof(struct fib_alias),
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -08001702 0, SLAB_PANIC, NULL);
1703
1704 trie_leaf_kmem = kmem_cache_create("ip_fib_trie",
Alexander Duyckadaf9812014-12-31 10:55:47 -08001705 max(sizeof(struct tnode),
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -08001706 sizeof(struct leaf_info)),
1707 0, SLAB_PANIC, NULL);
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001708}
Robert Olsson19baf832005-06-21 12:43:18 -07001709
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001710
David S. Miller5348ba82011-02-01 15:30:56 -08001711struct fib_table *fib_trie_table(u32 id)
Robert Olsson19baf832005-06-21 12:43:18 -07001712{
1713 struct fib_table *tb;
1714 struct trie *t;
1715
Robert Olsson19baf832005-06-21 12:43:18 -07001716 tb = kmalloc(sizeof(struct fib_table) + sizeof(struct trie),
1717 GFP_KERNEL);
1718 if (tb == NULL)
1719 return NULL;
1720
1721 tb->tb_id = id;
Denis V. Lunev971b8932007-12-08 00:32:23 -08001722 tb->tb_default = -1;
David S. Miller21d8c492011-04-14 14:49:37 -07001723 tb->tb_num_default = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001724
1725 t = (struct trie *) tb->tb_data;
Alexander Duyck8274a972014-12-31 10:55:29 -08001726 RCU_INIT_POINTER(t->trie, NULL);
1727#ifdef CONFIG_IP_FIB_TRIE_STATS
1728 t->stats = alloc_percpu(struct trie_use_stats);
1729 if (!t->stats) {
1730 kfree(tb);
1731 tb = NULL;
1732 }
1733#endif
Robert Olsson19baf832005-06-21 12:43:18 -07001734
Robert Olsson19baf832005-06-21 12:43:18 -07001735 return tb;
1736}
1737
Robert Olsson19baf832005-06-21 12:43:18 -07001738#ifdef CONFIG_PROC_FS
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001739/* Depth first Trie walk iterator */
1740struct fib_trie_iter {
Denis V. Lunev1c340b22008-01-10 03:27:17 -08001741 struct seq_net_private p;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001742 struct fib_table *tb;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001743 struct tnode *tnode;
Eric Dumazeta034ee32010-09-09 23:32:28 +00001744 unsigned int index;
1745 unsigned int depth;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001746};
Robert Olsson19baf832005-06-21 12:43:18 -07001747
Alexander Duyckadaf9812014-12-31 10:55:47 -08001748static struct tnode *fib_trie_get_next(struct fib_trie_iter *iter)
Robert Olsson19baf832005-06-21 12:43:18 -07001749{
Alexander Duyck98293e82014-12-31 10:56:18 -08001750 unsigned long cindex = iter->index;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001751 struct tnode *tn = iter->tnode;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001752 struct tnode *p;
1753
Eric W. Biederman6640e692007-01-24 14:42:04 -08001754 /* A single entry routing table */
1755 if (!tn)
1756 return NULL;
1757
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001758 pr_debug("get_next iter={node=%p index=%d depth=%d}\n",
1759 iter->tnode, iter->index, iter->depth);
1760rescan:
Alexander Duyck98293e82014-12-31 10:56:18 -08001761 while (cindex < tnode_child_length(tn)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08001762 struct tnode *n = tnode_get_child_rcu(tn, cindex);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001763
1764 if (n) {
1765 if (IS_LEAF(n)) {
1766 iter->tnode = tn;
1767 iter->index = cindex + 1;
1768 } else {
1769 /* push down one level */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001770 iter->tnode = n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001771 iter->index = 0;
1772 ++iter->depth;
1773 }
1774 return n;
1775 }
1776
1777 ++cindex;
1778 }
1779
1780 /* Current node exhausted, pop back up */
Alexander Duyckadaf9812014-12-31 10:55:47 -08001781 p = node_parent_rcu(tn);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001782 if (p) {
Alexander Duycke9b44012014-12-31 10:56:12 -08001783 cindex = get_index(tn->key, p) + 1;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001784 tn = p;
1785 --iter->depth;
1786 goto rescan;
1787 }
1788
1789 /* got root? */
Robert Olsson19baf832005-06-21 12:43:18 -07001790 return NULL;
1791}
1792
Alexander Duyckadaf9812014-12-31 10:55:47 -08001793static struct tnode *fib_trie_get_first(struct fib_trie_iter *iter,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001794 struct trie *t)
Robert Olsson19baf832005-06-21 12:43:18 -07001795{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001796 struct tnode *n;
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08001797
Stephen Hemminger132adf52007-03-08 20:44:43 -08001798 if (!t)
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08001799 return NULL;
1800
1801 n = rcu_dereference(t->trie);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001802 if (!n)
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08001803 return NULL;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001804
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001805 if (IS_TNODE(n)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08001806 iter->tnode = n;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001807 iter->index = 0;
1808 iter->depth = 1;
1809 } else {
1810 iter->tnode = NULL;
1811 iter->index = 0;
1812 iter->depth = 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001813 }
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001814
1815 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07001816}
1817
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001818static void trie_collect_stats(struct trie *t, struct trie_stat *s)
Robert Olsson19baf832005-06-21 12:43:18 -07001819{
Alexander Duyckadaf9812014-12-31 10:55:47 -08001820 struct tnode *n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001821 struct fib_trie_iter iter;
Robert Olsson19baf832005-06-21 12:43:18 -07001822
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001823 memset(s, 0, sizeof(*s));
Robert Olsson19baf832005-06-21 12:43:18 -07001824
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001825 rcu_read_lock();
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001826 for (n = fib_trie_get_first(&iter, t); n; n = fib_trie_get_next(&iter)) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001827 if (IS_LEAF(n)) {
Stephen Hemminger93672292008-01-22 21:54:05 -08001828 struct leaf_info *li;
Stephen Hemminger93672292008-01-22 21:54:05 -08001829
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001830 s->leaves++;
1831 s->totdepth += iter.depth;
1832 if (iter.depth > s->maxdepth)
1833 s->maxdepth = iter.depth;
Stephen Hemminger93672292008-01-22 21:54:05 -08001834
Alexander Duyckadaf9812014-12-31 10:55:47 -08001835 hlist_for_each_entry_rcu(li, &n->list, hlist)
Stephen Hemminger93672292008-01-22 21:54:05 -08001836 ++s->prefixes;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001837 } else {
Alexander Duyck98293e82014-12-31 10:56:18 -08001838 unsigned long i;
Robert Olsson19baf832005-06-21 12:43:18 -07001839
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001840 s->tnodes++;
Alexander Duyckadaf9812014-12-31 10:55:47 -08001841 if (n->bits < MAX_STAT_DEPTH)
1842 s->nodesizes[n->bits]++;
Robert Olsson06ef9212006-03-20 21:35:01 -08001843
Alexander Duyck98293e82014-12-31 10:56:18 -08001844 for (i = 0; i < tnode_child_length(n); i++) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08001845 if (!rcu_access_pointer(n->child[i]))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001846 s->nullpointers++;
Alexander Duyck98293e82014-12-31 10:56:18 -08001847 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001848 }
1849 }
1850 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07001851}
1852
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001853/*
Robert Olsson19baf832005-06-21 12:43:18 -07001854 * This outputs /proc/net/fib_triestats
Robert Olsson19baf832005-06-21 12:43:18 -07001855 */
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001856static void trie_show_stats(struct seq_file *seq, struct trie_stat *stat)
Robert Olsson19baf832005-06-21 12:43:18 -07001857{
Eric Dumazeta034ee32010-09-09 23:32:28 +00001858 unsigned int i, max, pointers, bytes, avdepth;
Robert Olsson19baf832005-06-21 12:43:18 -07001859
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001860 if (stat->leaves)
1861 avdepth = stat->totdepth*100 / stat->leaves;
1862 else
1863 avdepth = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001864
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001865 seq_printf(seq, "\tAver depth: %u.%02d\n",
1866 avdepth / 100, avdepth % 100);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001867 seq_printf(seq, "\tMax depth: %u\n", stat->maxdepth);
Robert Olsson19baf832005-06-21 12:43:18 -07001868
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001869 seq_printf(seq, "\tLeaves: %u\n", stat->leaves);
Alexander Duyckadaf9812014-12-31 10:55:47 -08001870 bytes = sizeof(struct tnode) * stat->leaves;
Stephen Hemminger93672292008-01-22 21:54:05 -08001871
1872 seq_printf(seq, "\tPrefixes: %u\n", stat->prefixes);
1873 bytes += sizeof(struct leaf_info) * stat->prefixes;
1874
Stephen Hemminger187b5182008-01-12 20:55:55 -08001875 seq_printf(seq, "\tInternal nodes: %u\n\t", stat->tnodes);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001876 bytes += sizeof(struct tnode) * stat->tnodes;
Robert Olsson19baf832005-06-21 12:43:18 -07001877
Robert Olsson06ef9212006-03-20 21:35:01 -08001878 max = MAX_STAT_DEPTH;
1879 while (max > 0 && stat->nodesizes[max-1] == 0)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001880 max--;
Robert Olsson19baf832005-06-21 12:43:18 -07001881
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001882 pointers = 0;
Jerry Snitselaarf585a992013-07-22 12:01:58 -07001883 for (i = 1; i < max; i++)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001884 if (stat->nodesizes[i] != 0) {
Stephen Hemminger187b5182008-01-12 20:55:55 -08001885 seq_printf(seq, " %u: %u", i, stat->nodesizes[i]);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001886 pointers += (1<<i) * stat->nodesizes[i];
1887 }
1888 seq_putc(seq, '\n');
Stephen Hemminger187b5182008-01-12 20:55:55 -08001889 seq_printf(seq, "\tPointers: %u\n", pointers);
Robert Olsson19baf832005-06-21 12:43:18 -07001890
Alexander Duyckadaf9812014-12-31 10:55:47 -08001891 bytes += sizeof(struct tnode *) * pointers;
Stephen Hemminger187b5182008-01-12 20:55:55 -08001892 seq_printf(seq, "Null ptrs: %u\n", stat->nullpointers);
1893 seq_printf(seq, "Total size: %u kB\n", (bytes + 1023) / 1024);
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08001894}
Robert Olsson19baf832005-06-21 12:43:18 -07001895
1896#ifdef CONFIG_IP_FIB_TRIE_STATS
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08001897static void trie_show_usage(struct seq_file *seq,
Alexander Duyck8274a972014-12-31 10:55:29 -08001898 const struct trie_use_stats __percpu *stats)
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08001899{
Alexander Duyck8274a972014-12-31 10:55:29 -08001900 struct trie_use_stats s = { 0 };
1901 int cpu;
1902
1903 /* loop through all of the CPUs and gather up the stats */
1904 for_each_possible_cpu(cpu) {
1905 const struct trie_use_stats *pcpu = per_cpu_ptr(stats, cpu);
1906
1907 s.gets += pcpu->gets;
1908 s.backtrack += pcpu->backtrack;
1909 s.semantic_match_passed += pcpu->semantic_match_passed;
1910 s.semantic_match_miss += pcpu->semantic_match_miss;
1911 s.null_node_hit += pcpu->null_node_hit;
1912 s.resize_node_skipped += pcpu->resize_node_skipped;
1913 }
1914
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08001915 seq_printf(seq, "\nCounters:\n---------\n");
Alexander Duyck8274a972014-12-31 10:55:29 -08001916 seq_printf(seq, "gets = %u\n", s.gets);
1917 seq_printf(seq, "backtracks = %u\n", s.backtrack);
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001918 seq_printf(seq, "semantic match passed = %u\n",
Alexander Duyck8274a972014-12-31 10:55:29 -08001919 s.semantic_match_passed);
1920 seq_printf(seq, "semantic match miss = %u\n", s.semantic_match_miss);
1921 seq_printf(seq, "null node hit= %u\n", s.null_node_hit);
1922 seq_printf(seq, "skipped node resize = %u\n\n", s.resize_node_skipped);
Robert Olsson19baf832005-06-21 12:43:18 -07001923}
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08001924#endif /* CONFIG_IP_FIB_TRIE_STATS */
1925
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001926static void fib_table_print(struct seq_file *seq, struct fib_table *tb)
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08001927{
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001928 if (tb->tb_id == RT_TABLE_LOCAL)
1929 seq_puts(seq, "Local:\n");
1930 else if (tb->tb_id == RT_TABLE_MAIN)
1931 seq_puts(seq, "Main:\n");
1932 else
1933 seq_printf(seq, "Id %d:\n", tb->tb_id);
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08001934}
Robert Olsson19baf832005-06-21 12:43:18 -07001935
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001936
Robert Olsson19baf832005-06-21 12:43:18 -07001937static int fib_triestat_seq_show(struct seq_file *seq, void *v)
1938{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08001939 struct net *net = (struct net *)seq->private;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001940 unsigned int h;
Eric W. Biederman877a9bf2007-12-07 00:47:47 -08001941
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08001942 seq_printf(seq,
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001943 "Basic info: size of leaf:"
1944 " %Zd bytes, size of tnode: %Zd bytes.\n",
Alexander Duyckadaf9812014-12-31 10:55:47 -08001945 sizeof(struct tnode), sizeof(struct tnode));
Olof Johansson91b9a272005-08-09 20:24:39 -07001946
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001947 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
1948 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001949 struct fib_table *tb;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001950
Sasha Levinb67bfe02013-02-27 17:06:00 -08001951 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001952 struct trie *t = (struct trie *) tb->tb_data;
1953 struct trie_stat stat;
1954
1955 if (!t)
1956 continue;
1957
1958 fib_table_print(seq, tb);
1959
1960 trie_collect_stats(t, &stat);
1961 trie_show_stats(seq, &stat);
1962#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -08001963 trie_show_usage(seq, t->stats);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001964#endif
1965 }
1966 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001967
Robert Olsson19baf832005-06-21 12:43:18 -07001968 return 0;
1969}
1970
Robert Olsson19baf832005-06-21 12:43:18 -07001971static int fib_triestat_seq_open(struct inode *inode, struct file *file)
1972{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07001973 return single_open_net(inode, file, fib_triestat_seq_show);
Denis V. Lunev1c340b22008-01-10 03:27:17 -08001974}
1975
Arjan van de Ven9a321442007-02-12 00:55:35 -08001976static const struct file_operations fib_triestat_fops = {
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001977 .owner = THIS_MODULE,
1978 .open = fib_triestat_seq_open,
1979 .read = seq_read,
1980 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07001981 .release = single_release_net,
Robert Olsson19baf832005-06-21 12:43:18 -07001982};
1983
Alexander Duyckadaf9812014-12-31 10:55:47 -08001984static struct tnode *fib_trie_get_idx(struct seq_file *seq, loff_t pos)
Robert Olsson19baf832005-06-21 12:43:18 -07001985{
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001986 struct fib_trie_iter *iter = seq->private;
1987 struct net *net = seq_file_net(seq);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07001988 loff_t idx = 0;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001989 unsigned int h;
Robert Olsson19baf832005-06-21 12:43:18 -07001990
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001991 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
1992 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001993 struct fib_table *tb;
1994
Sasha Levinb67bfe02013-02-27 17:06:00 -08001995 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08001996 struct tnode *n;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07001997
1998 for (n = fib_trie_get_first(iter,
1999 (struct trie *) tb->tb_data);
2000 n; n = fib_trie_get_next(iter))
2001 if (pos == idx++) {
2002 iter->tb = tb;
2003 return n;
2004 }
2005 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002006 }
Robert Olsson19baf832005-06-21 12:43:18 -07002007
Robert Olsson19baf832005-06-21 12:43:18 -07002008 return NULL;
2009}
2010
2011static void *fib_trie_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08002012 __acquires(RCU)
Robert Olsson19baf832005-06-21 12:43:18 -07002013{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002014 rcu_read_lock();
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002015 return fib_trie_get_idx(seq, *pos);
Robert Olsson19baf832005-06-21 12:43:18 -07002016}
2017
2018static void *fib_trie_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2019{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002020 struct fib_trie_iter *iter = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002021 struct net *net = seq_file_net(seq);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002022 struct fib_table *tb = iter->tb;
2023 struct hlist_node *tb_node;
2024 unsigned int h;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002025 struct tnode *n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002026
Robert Olsson19baf832005-06-21 12:43:18 -07002027 ++*pos;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002028 /* next node in same table */
2029 n = fib_trie_get_next(iter);
2030 if (n)
2031 return n;
Olof Johansson91b9a272005-08-09 20:24:39 -07002032
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002033 /* walk rest of this hash chain */
2034 h = tb->tb_id & (FIB_TABLE_HASHSZ - 1);
Eric Dumazet0a5c0472011-03-31 01:51:35 -07002035 while ((tb_node = rcu_dereference(hlist_next_rcu(&tb->tb_hlist)))) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002036 tb = hlist_entry(tb_node, struct fib_table, tb_hlist);
2037 n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
2038 if (n)
2039 goto found;
2040 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002041
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002042 /* new hash chain */
2043 while (++h < FIB_TABLE_HASHSZ) {
2044 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002045 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002046 n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
2047 if (n)
2048 goto found;
2049 }
2050 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002051 return NULL;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002052
2053found:
2054 iter->tb = tb;
2055 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07002056}
2057
2058static void fib_trie_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08002059 __releases(RCU)
Robert Olsson19baf832005-06-21 12:43:18 -07002060{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002061 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07002062}
2063
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002064static void seq_indent(struct seq_file *seq, int n)
2065{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002066 while (n-- > 0)
2067 seq_puts(seq, " ");
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002068}
Robert Olsson19baf832005-06-21 12:43:18 -07002069
Eric Dumazet28d36e32008-01-14 23:09:56 -08002070static inline const char *rtn_scope(char *buf, size_t len, enum rt_scope_t s)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002071{
Stephen Hemminger132adf52007-03-08 20:44:43 -08002072 switch (s) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002073 case RT_SCOPE_UNIVERSE: return "universe";
2074 case RT_SCOPE_SITE: return "site";
2075 case RT_SCOPE_LINK: return "link";
2076 case RT_SCOPE_HOST: return "host";
2077 case RT_SCOPE_NOWHERE: return "nowhere";
2078 default:
Eric Dumazet28d36e32008-01-14 23:09:56 -08002079 snprintf(buf, len, "scope=%d", s);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002080 return buf;
2081 }
2082}
2083
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -07002084static const char *const rtn_type_names[__RTN_MAX] = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002085 [RTN_UNSPEC] = "UNSPEC",
2086 [RTN_UNICAST] = "UNICAST",
2087 [RTN_LOCAL] = "LOCAL",
2088 [RTN_BROADCAST] = "BROADCAST",
2089 [RTN_ANYCAST] = "ANYCAST",
2090 [RTN_MULTICAST] = "MULTICAST",
2091 [RTN_BLACKHOLE] = "BLACKHOLE",
2092 [RTN_UNREACHABLE] = "UNREACHABLE",
2093 [RTN_PROHIBIT] = "PROHIBIT",
2094 [RTN_THROW] = "THROW",
2095 [RTN_NAT] = "NAT",
2096 [RTN_XRESOLVE] = "XRESOLVE",
2097};
2098
Eric Dumazeta034ee32010-09-09 23:32:28 +00002099static inline const char *rtn_type(char *buf, size_t len, unsigned int t)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002100{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002101 if (t < __RTN_MAX && rtn_type_names[t])
2102 return rtn_type_names[t];
Eric Dumazet28d36e32008-01-14 23:09:56 -08002103 snprintf(buf, len, "type %u", t);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002104 return buf;
2105}
2106
2107/* Pretty print the trie */
Robert Olsson19baf832005-06-21 12:43:18 -07002108static int fib_trie_seq_show(struct seq_file *seq, void *v)
2109{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002110 const struct fib_trie_iter *iter = seq->private;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002111 struct tnode *n = v;
Robert Olsson19baf832005-06-21 12:43:18 -07002112
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002113 if (!node_parent_rcu(n))
2114 fib_table_print(seq, iter->tb);
Robert Olsson095b8502007-01-26 19:06:01 -08002115
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002116 if (IS_TNODE(n)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002117 __be32 prf = htonl(n->key);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002118
Alexander Duycke9b44012014-12-31 10:56:12 -08002119 seq_indent(seq, iter->depth-1);
2120 seq_printf(seq, " +-- %pI4/%zu %u %u %u\n",
2121 &prf, KEYLENGTH - n->pos - n->bits, n->bits,
2122 n->full_children, n->empty_children);
Olof Johansson91b9a272005-08-09 20:24:39 -07002123 } else {
Stephen Hemminger13280422008-01-22 21:54:37 -08002124 struct leaf_info *li;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002125 __be32 val = htonl(n->key);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002126
2127 seq_indent(seq, iter->depth);
Harvey Harrison673d57e2008-10-31 00:53:57 -07002128 seq_printf(seq, " |-- %pI4\n", &val);
Eric Dumazet28d36e32008-01-14 23:09:56 -08002129
Alexander Duyckadaf9812014-12-31 10:55:47 -08002130 hlist_for_each_entry_rcu(li, &n->list, hlist) {
Stephen Hemminger13280422008-01-22 21:54:37 -08002131 struct fib_alias *fa;
Eric Dumazet28d36e32008-01-14 23:09:56 -08002132
Stephen Hemminger13280422008-01-22 21:54:37 -08002133 list_for_each_entry_rcu(fa, &li->falh, fa_list) {
2134 char buf1[32], buf2[32];
Eric Dumazet28d36e32008-01-14 23:09:56 -08002135
Stephen Hemminger13280422008-01-22 21:54:37 -08002136 seq_indent(seq, iter->depth+1);
2137 seq_printf(seq, " /%d %s %s", li->plen,
2138 rtn_scope(buf1, sizeof(buf1),
David S. Miller37e826c2011-03-24 18:06:47 -07002139 fa->fa_info->fib_scope),
Stephen Hemminger13280422008-01-22 21:54:37 -08002140 rtn_type(buf2, sizeof(buf2),
2141 fa->fa_type));
2142 if (fa->fa_tos)
Denis V. Lunevb9c4d822008-02-05 02:58:45 -08002143 seq_printf(seq, " tos=%d", fa->fa_tos);
Stephen Hemminger13280422008-01-22 21:54:37 -08002144 seq_putc(seq, '\n');
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002145 }
2146 }
Robert Olsson19baf832005-06-21 12:43:18 -07002147 }
2148
2149 return 0;
2150}
2151
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002152static const struct seq_operations fib_trie_seq_ops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002153 .start = fib_trie_seq_start,
2154 .next = fib_trie_seq_next,
2155 .stop = fib_trie_seq_stop,
2156 .show = fib_trie_seq_show,
Robert Olsson19baf832005-06-21 12:43:18 -07002157};
2158
2159static int fib_trie_seq_open(struct inode *inode, struct file *file)
2160{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002161 return seq_open_net(inode, file, &fib_trie_seq_ops,
2162 sizeof(struct fib_trie_iter));
Robert Olsson19baf832005-06-21 12:43:18 -07002163}
2164
Arjan van de Ven9a321442007-02-12 00:55:35 -08002165static const struct file_operations fib_trie_fops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002166 .owner = THIS_MODULE,
2167 .open = fib_trie_seq_open,
2168 .read = seq_read,
2169 .llseek = seq_lseek,
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002170 .release = seq_release_net,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002171};
2172
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002173struct fib_route_iter {
2174 struct seq_net_private p;
2175 struct trie *main_trie;
2176 loff_t pos;
2177 t_key key;
2178};
2179
Alexander Duyckadaf9812014-12-31 10:55:47 -08002180static struct tnode *fib_route_get_idx(struct fib_route_iter *iter, loff_t pos)
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002181{
Alexander Duyckadaf9812014-12-31 10:55:47 -08002182 struct tnode *l = NULL;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002183 struct trie *t = iter->main_trie;
2184
2185 /* use cache location of last found key */
2186 if (iter->pos > 0 && pos >= iter->pos && (l = fib_find_node(t, iter->key)))
2187 pos -= iter->pos;
2188 else {
2189 iter->pos = 0;
2190 l = trie_firstleaf(t);
2191 }
2192
2193 while (l && pos-- > 0) {
2194 iter->pos++;
2195 l = trie_nextleaf(l);
2196 }
2197
2198 if (l)
2199 iter->key = pos; /* remember it */
2200 else
2201 iter->pos = 0; /* forget it */
2202
2203 return l;
2204}
2205
2206static void *fib_route_seq_start(struct seq_file *seq, loff_t *pos)
2207 __acquires(RCU)
2208{
2209 struct fib_route_iter *iter = seq->private;
2210 struct fib_table *tb;
2211
2212 rcu_read_lock();
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002213 tb = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN);
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002214 if (!tb)
2215 return NULL;
2216
2217 iter->main_trie = (struct trie *) tb->tb_data;
2218 if (*pos == 0)
2219 return SEQ_START_TOKEN;
2220 else
2221 return fib_route_get_idx(iter, *pos - 1);
2222}
2223
2224static void *fib_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2225{
2226 struct fib_route_iter *iter = seq->private;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002227 struct tnode *l = v;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002228
2229 ++*pos;
2230 if (v == SEQ_START_TOKEN) {
2231 iter->pos = 0;
2232 l = trie_firstleaf(iter->main_trie);
2233 } else {
2234 iter->pos++;
2235 l = trie_nextleaf(l);
2236 }
2237
2238 if (l)
2239 iter->key = l->key;
2240 else
2241 iter->pos = 0;
2242 return l;
2243}
2244
2245static void fib_route_seq_stop(struct seq_file *seq, void *v)
2246 __releases(RCU)
2247{
2248 rcu_read_unlock();
2249}
2250
Eric Dumazeta034ee32010-09-09 23:32:28 +00002251static unsigned int fib_flag_trans(int type, __be32 mask, const struct fib_info *fi)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002252{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002253 unsigned int flags = 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002254
Eric Dumazeta034ee32010-09-09 23:32:28 +00002255 if (type == RTN_UNREACHABLE || type == RTN_PROHIBIT)
2256 flags = RTF_REJECT;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002257 if (fi && fi->fib_nh->nh_gw)
2258 flags |= RTF_GATEWAY;
Al Viro32ab5f82006-09-26 22:21:45 -07002259 if (mask == htonl(0xFFFFFFFF))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002260 flags |= RTF_HOST;
2261 flags |= RTF_UP;
2262 return flags;
2263}
2264
2265/*
2266 * This outputs /proc/net/route.
2267 * The format of the file is not supposed to be changed
Eric Dumazeta034ee32010-09-09 23:32:28 +00002268 * and needs to be same as fib_hash output to avoid breaking
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002269 * legacy utilities
2270 */
2271static int fib_route_seq_show(struct seq_file *seq, void *v)
2272{
Alexander Duyckadaf9812014-12-31 10:55:47 -08002273 struct tnode *l = v;
Stephen Hemminger13280422008-01-22 21:54:37 -08002274 struct leaf_info *li;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002275
2276 if (v == SEQ_START_TOKEN) {
2277 seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
2278 "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
2279 "\tWindow\tIRTT");
2280 return 0;
2281 }
2282
Sasha Levinb67bfe02013-02-27 17:06:00 -08002283 hlist_for_each_entry_rcu(li, &l->list, hlist) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002284 struct fib_alias *fa;
Al Viro32ab5f82006-09-26 22:21:45 -07002285 __be32 mask, prefix;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002286
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002287 mask = inet_make_mask(li->plen);
2288 prefix = htonl(l->key);
2289
2290 list_for_each_entry_rcu(fa, &li->falh, fa_list) {
Herbert Xu1371e372005-10-15 09:42:39 +10002291 const struct fib_info *fi = fa->fa_info;
Eric Dumazeta034ee32010-09-09 23:32:28 +00002292 unsigned int flags = fib_flag_trans(fa->fa_type, mask, fi);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002293
2294 if (fa->fa_type == RTN_BROADCAST
2295 || fa->fa_type == RTN_MULTICAST)
2296 continue;
2297
Tetsuo Handa652586d2013-11-14 14:31:57 -08002298 seq_setwidth(seq, 127);
2299
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002300 if (fi)
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002301 seq_printf(seq,
2302 "%s\t%08X\t%08X\t%04X\t%d\t%u\t"
Tetsuo Handa652586d2013-11-14 14:31:57 -08002303 "%d\t%08X\t%d\t%u\t%u",
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002304 fi->fib_dev ? fi->fib_dev->name : "*",
2305 prefix,
2306 fi->fib_nh->nh_gw, flags, 0, 0,
2307 fi->fib_priority,
2308 mask,
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002309 (fi->fib_advmss ?
2310 fi->fib_advmss + 40 : 0),
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002311 fi->fib_window,
Tetsuo Handa652586d2013-11-14 14:31:57 -08002312 fi->fib_rtt >> 3);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002313 else
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002314 seq_printf(seq,
2315 "*\t%08X\t%08X\t%04X\t%d\t%u\t"
Tetsuo Handa652586d2013-11-14 14:31:57 -08002316 "%d\t%08X\t%d\t%u\t%u",
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002317 prefix, 0, flags, 0, 0, 0,
Tetsuo Handa652586d2013-11-14 14:31:57 -08002318 mask, 0, 0, 0);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002319
Tetsuo Handa652586d2013-11-14 14:31:57 -08002320 seq_pad(seq, '\n');
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002321 }
2322 }
2323
2324 return 0;
2325}
2326
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002327static const struct seq_operations fib_route_seq_ops = {
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002328 .start = fib_route_seq_start,
2329 .next = fib_route_seq_next,
2330 .stop = fib_route_seq_stop,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002331 .show = fib_route_seq_show,
2332};
2333
2334static int fib_route_seq_open(struct inode *inode, struct file *file)
2335{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002336 return seq_open_net(inode, file, &fib_route_seq_ops,
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002337 sizeof(struct fib_route_iter));
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002338}
2339
Arjan van de Ven9a321442007-02-12 00:55:35 -08002340static const struct file_operations fib_route_fops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002341 .owner = THIS_MODULE,
2342 .open = fib_route_seq_open,
2343 .read = seq_read,
2344 .llseek = seq_lseek,
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002345 .release = seq_release_net,
Robert Olsson19baf832005-06-21 12:43:18 -07002346};
2347
Denis V. Lunev61a02652008-01-10 03:21:09 -08002348int __net_init fib_proc_init(struct net *net)
Robert Olsson19baf832005-06-21 12:43:18 -07002349{
Gao fengd4beaa62013-02-18 01:34:54 +00002350 if (!proc_create("fib_trie", S_IRUGO, net->proc_net, &fib_trie_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002351 goto out1;
2352
Gao fengd4beaa62013-02-18 01:34:54 +00002353 if (!proc_create("fib_triestat", S_IRUGO, net->proc_net,
2354 &fib_triestat_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002355 goto out2;
2356
Gao fengd4beaa62013-02-18 01:34:54 +00002357 if (!proc_create("route", S_IRUGO, net->proc_net, &fib_route_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002358 goto out3;
2359
Robert Olsson19baf832005-06-21 12:43:18 -07002360 return 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002361
2362out3:
Gao fengece31ff2013-02-18 01:34:56 +00002363 remove_proc_entry("fib_triestat", net->proc_net);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002364out2:
Gao fengece31ff2013-02-18 01:34:56 +00002365 remove_proc_entry("fib_trie", net->proc_net);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002366out1:
2367 return -ENOMEM;
Robert Olsson19baf832005-06-21 12:43:18 -07002368}
2369
Denis V. Lunev61a02652008-01-10 03:21:09 -08002370void __net_exit fib_proc_exit(struct net *net)
Robert Olsson19baf832005-06-21 12:43:18 -07002371{
Gao fengece31ff2013-02-18 01:34:56 +00002372 remove_proc_entry("fib_trie", net->proc_net);
2373 remove_proc_entry("fib_triestat", net->proc_net);
2374 remove_proc_entry("route", net->proc_net);
Robert Olsson19baf832005-06-21 12:43:18 -07002375}
2376
2377#endif /* CONFIG_PROC_FS */