blob: 5ef4596d7afcd28fc1c7b0d4f1e820cd65190586 [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
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080053#include <linux/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>
David S. Millerffa915d2015-05-27 00:19:03 -040075#include <linux/vmalloc.h>
Jiri Pirkob90eb752016-09-26 12:52:29 +020076#include <linux/notifier.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020077#include <net/net_namespace.h>
Robert Olsson19baf832005-06-21 12:43:18 -070078#include <net/ip.h>
79#include <net/protocol.h>
80#include <net/route.h>
81#include <net/tcp.h>
82#include <net/sock.h>
83#include <net/ip_fib.h>
David Ahernf6d3c192015-08-28 08:42:09 -070084#include <trace/events/fib.h>
Robert Olsson19baf832005-06-21 12:43:18 -070085#include "fib_lookup.h"
86
Ido Schimmelc3852ef2016-12-03 16:45:07 +010087static unsigned int fib_seq_sum(void)
88{
89 unsigned int fib_seq = 0;
90 struct net *net;
91
92 rtnl_lock();
93 for_each_net(net)
94 fib_seq += net->ipv4.fib_seq;
95 rtnl_unlock();
96
97 return fib_seq;
98}
99
Ido Schimmeld3f706f2016-12-03 16:45:05 +0100100static ATOMIC_NOTIFIER_HEAD(fib_chain);
Jiri Pirkob90eb752016-09-26 12:52:29 +0200101
Ido Schimmelc3852ef2016-12-03 16:45:07 +0100102static int call_fib_notifier(struct notifier_block *nb, struct net *net,
103 enum fib_event_type event_type,
104 struct fib_notifier_info *info)
Jiri Pirkob90eb752016-09-26 12:52:29 +0200105{
Ido Schimmelc3852ef2016-12-03 16:45:07 +0100106 info->net = net;
107 return nb->notifier_call(nb, event_type, info);
108}
109
110static void fib_rules_notify(struct net *net, struct notifier_block *nb,
111 enum fib_event_type event_type)
112{
113#ifdef CONFIG_IP_MULTIPLE_TABLES
114 struct fib_notifier_info info;
115
116 if (net->ipv4.fib_has_custom_rules)
117 call_fib_notifier(nb, net, event_type, &info);
118#endif
119}
120
121static void fib_notify(struct net *net, struct notifier_block *nb,
122 enum fib_event_type event_type);
123
124static int call_fib_entry_notifier(struct notifier_block *nb, struct net *net,
125 enum fib_event_type event_type, u32 dst,
126 int dst_len, struct fib_info *fi,
127 u8 tos, u8 type, u32 tb_id, u32 nlflags)
128{
129 struct fib_entry_notifier_info info = {
130 .dst = dst,
131 .dst_len = dst_len,
132 .fi = fi,
133 .tos = tos,
134 .type = type,
135 .tb_id = tb_id,
136 .nlflags = nlflags,
137 };
138 return call_fib_notifier(nb, net, event_type, &info.info);
139}
140
141static bool fib_dump_is_consistent(struct notifier_block *nb,
142 void (*cb)(struct notifier_block *nb),
143 unsigned int fib_seq)
144{
145 atomic_notifier_chain_register(&fib_chain, nb);
146 if (fib_seq == fib_seq_sum())
147 return true;
148 atomic_notifier_chain_unregister(&fib_chain, nb);
149 if (cb)
150 cb(nb);
151 return false;
152}
153
154#define FIB_DUMP_MAX_RETRIES 5
155int register_fib_notifier(struct notifier_block *nb,
156 void (*cb)(struct notifier_block *nb))
157{
158 int retries = 0;
159
160 do {
161 unsigned int fib_seq = fib_seq_sum();
162 struct net *net;
163
164 /* Mutex semantics guarantee that every change done to
165 * FIB tries before we read the change sequence counter
166 * is now visible to us.
167 */
168 rcu_read_lock();
169 for_each_net_rcu(net) {
170 fib_rules_notify(net, nb, FIB_EVENT_RULE_ADD);
171 fib_notify(net, nb, FIB_EVENT_ENTRY_ADD);
172 }
173 rcu_read_unlock();
174
175 if (fib_dump_is_consistent(nb, cb, fib_seq))
176 return 0;
177 } while (++retries < FIB_DUMP_MAX_RETRIES);
178
179 return -EBUSY;
Jiri Pirkob90eb752016-09-26 12:52:29 +0200180}
181EXPORT_SYMBOL(register_fib_notifier);
182
183int unregister_fib_notifier(struct notifier_block *nb)
184{
Ido Schimmeld3f706f2016-12-03 16:45:05 +0100185 return atomic_notifier_chain_unregister(&fib_chain, nb);
Jiri Pirkob90eb752016-09-26 12:52:29 +0200186}
187EXPORT_SYMBOL(unregister_fib_notifier);
188
189int call_fib_notifiers(struct net *net, enum fib_event_type event_type,
190 struct fib_notifier_info *info)
191{
Ido Schimmelcacaad12016-12-03 16:45:06 +0100192 net->ipv4.fib_seq++;
Jiri Pirkob90eb752016-09-26 12:52:29 +0200193 info->net = net;
Ido Schimmeld3f706f2016-12-03 16:45:05 +0100194 return atomic_notifier_call_chain(&fib_chain, event_type, info);
Jiri Pirkob90eb752016-09-26 12:52:29 +0200195}
196
197static int call_fib_entry_notifiers(struct net *net,
198 enum fib_event_type event_type, u32 dst,
199 int dst_len, struct fib_info *fi,
200 u8 tos, u8 type, u32 tb_id, u32 nlflags)
201{
202 struct fib_entry_notifier_info info = {
203 .dst = dst,
204 .dst_len = dst_len,
205 .fi = fi,
206 .tos = tos,
207 .type = type,
208 .tb_id = tb_id,
209 .nlflags = nlflags,
210 };
211 return call_fib_notifiers(net, event_type, &info.info);
212}
213
Robert Olsson06ef9212006-03-20 21:35:01 -0800214#define MAX_STAT_DEPTH 32
Robert Olsson19baf832005-06-21 12:43:18 -0700215
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800216#define KEYLENGTH (8*sizeof(t_key))
217#define KEY_MAX ((t_key)~0)
Robert Olsson19baf832005-06-21 12:43:18 -0700218
Robert Olsson19baf832005-06-21 12:43:18 -0700219typedef unsigned int t_key;
220
Alexander Duyck88bae712015-03-06 09:54:52 -0800221#define IS_TRIE(n) ((n)->pos >= KEYLENGTH)
222#define IS_TNODE(n) ((n)->bits)
223#define IS_LEAF(n) (!(n)->bits)
Robert Olsson2373ce12005-08-25 13:01:29 -0700224
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800225struct key_vector {
Alexander Duyck41b489f2015-03-04 15:02:33 -0800226 t_key key;
227 unsigned char pos; /* 2log(KEYLENGTH) bits needed */
228 unsigned char bits; /* 2log(KEYLENGTH) bits needed */
229 unsigned char slen;
Alexander Duyckadaf9812014-12-31 10:55:47 -0800230 union {
Alexander Duyck41b489f2015-03-04 15:02:33 -0800231 /* This list pointer if valid if (pos | bits) == 0 (LEAF) */
Alexander Duyck79e5ad22015-02-25 15:31:51 -0800232 struct hlist_head leaf;
Alexander Duyck41b489f2015-03-04 15:02:33 -0800233 /* This array is valid if (pos | bits) > 0 (TNODE) */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800234 struct key_vector __rcu *tnode[0];
Alexander Duyckadaf9812014-12-31 10:55:47 -0800235 };
Robert Olsson19baf832005-06-21 12:43:18 -0700236};
237
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800238struct tnode {
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800239 struct rcu_head rcu;
Alexander Duyck6e22d172015-03-06 09:54:39 -0800240 t_key empty_children; /* KEYLENGTH bits needed */
241 t_key full_children; /* KEYLENGTH bits needed */
Alexander Duyckf23e59f2015-03-06 09:54:46 -0800242 struct key_vector __rcu *parent;
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800243 struct key_vector kv[1];
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800244#define tn_bits kv[0].bits
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800245};
246
247#define TNODE_SIZE(n) offsetof(struct tnode, kv[0].tnode[n])
Alexander Duyck41b489f2015-03-04 15:02:33 -0800248#define LEAF_SIZE TNODE_SIZE(1)
249
Robert Olsson19baf832005-06-21 12:43:18 -0700250#ifdef CONFIG_IP_FIB_TRIE_STATS
251struct trie_use_stats {
252 unsigned int gets;
253 unsigned int backtrack;
254 unsigned int semantic_match_passed;
255 unsigned int semantic_match_miss;
256 unsigned int null_node_hit;
Robert Olsson2f368952005-07-05 15:02:40 -0700257 unsigned int resize_node_skipped;
Robert Olsson19baf832005-06-21 12:43:18 -0700258};
259#endif
260
261struct trie_stat {
262 unsigned int totdepth;
263 unsigned int maxdepth;
264 unsigned int tnodes;
265 unsigned int leaves;
266 unsigned int nullpointers;
Stephen Hemminger93672292008-01-22 21:54:05 -0800267 unsigned int prefixes;
Robert Olsson06ef9212006-03-20 21:35:01 -0800268 unsigned int nodesizes[MAX_STAT_DEPTH];
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700269};
Robert Olsson19baf832005-06-21 12:43:18 -0700270
271struct trie {
Alexander Duyck88bae712015-03-06 09:54:52 -0800272 struct key_vector kv[1];
Robert Olsson19baf832005-06-21 12:43:18 -0700273#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -0800274 struct trie_use_stats __percpu *stats;
Robert Olsson19baf832005-06-21 12:43:18 -0700275#endif
Robert Olsson19baf832005-06-21 12:43:18 -0700276};
277
Alexander Duyck88bae712015-03-06 09:54:52 -0800278static struct key_vector *resize(struct trie *t, struct key_vector *tn);
Jarek Poplawskic3059472009-07-14 08:33:08 +0000279static size_t tnode_free_size;
280
281/*
282 * synchronize_rcu after call_rcu for that many pages; it should be especially
283 * useful before resizing the root node with PREEMPT_NONE configs; the value was
284 * obtained experimentally, aiming to avoid visible slowdown.
285 */
286static const int sync_pages = 128;
Robert Olsson19baf832005-06-21 12:43:18 -0700287
Christoph Lametere18b8902006-12-06 20:33:20 -0800288static struct kmem_cache *fn_alias_kmem __read_mostly;
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -0800289static struct kmem_cache *trie_leaf_kmem __read_mostly;
Robert Olsson19baf832005-06-21 12:43:18 -0700290
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800291static inline struct tnode *tn_info(struct key_vector *kv)
292{
293 return container_of(kv, struct tnode, kv[0]);
294}
295
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800296/* caller must hold RTNL */
Alexander Duyckf23e59f2015-03-06 09:54:46 -0800297#define node_parent(tn) rtnl_dereference(tn_info(tn)->parent)
Alexander Duyck754baf82015-03-06 09:54:14 -0800298#define get_child(tn, i) rtnl_dereference((tn)->tnode[i])
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700299
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800300/* caller must hold RCU read lock or RTNL */
Alexander Duyckf23e59f2015-03-06 09:54:46 -0800301#define node_parent_rcu(tn) rcu_dereference_rtnl(tn_info(tn)->parent)
Alexander Duyck754baf82015-03-06 09:54:14 -0800302#define get_child_rcu(tn, i) rcu_dereference_rtnl((tn)->tnode[i])
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700303
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800304/* wrapper for rcu_assign_pointer */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800305static inline void node_set_parent(struct key_vector *n, struct key_vector *tp)
Stephen Hemminger06801912007-08-10 15:22:13 -0700306{
Alexander Duyckadaf9812014-12-31 10:55:47 -0800307 if (n)
Alexander Duyckf23e59f2015-03-06 09:54:46 -0800308 rcu_assign_pointer(tn_info(n)->parent, tp);
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800309}
310
Alexander Duyckf23e59f2015-03-06 09:54:46 -0800311#define NODE_INIT_PARENT(n, p) RCU_INIT_POINTER(tn_info(n)->parent, p)
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800312
313/* This provides us with the number of children in this node, in the case of a
314 * leaf this will return 0 meaning none of the children are accessible.
315 */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800316static inline unsigned long child_length(const struct key_vector *tn)
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800317{
318 return (1ul << tn->bits) & ~(1ul);
Stephen Hemminger06801912007-08-10 15:22:13 -0700319}
Robert Olsson2373ce12005-08-25 13:01:29 -0700320
Alexander Duyck88bae712015-03-06 09:54:52 -0800321#define get_cindex(key, kv) (((key) ^ (kv)->key) >> (kv)->pos)
322
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800323static inline unsigned long get_index(t_key key, struct key_vector *kv)
324{
325 unsigned long index = key ^ kv->key;
326
Alexander Duyck88bae712015-03-06 09:54:52 -0800327 if ((BITS_PER_LONG <= KEYLENGTH) && (KEYLENGTH == kv->pos))
328 return 0;
329
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800330 return index >> kv->pos;
331}
332
Alexander Duycke9b44012014-12-31 10:56:12 -0800333/* To understand this stuff, an understanding of keys and all their bits is
334 * necessary. Every node in the trie has a key associated with it, but not
335 * all of the bits in that key are significant.
336 *
337 * Consider a node 'n' and its parent 'tp'.
338 *
339 * If n is a leaf, every bit in its key is significant. Its presence is
340 * necessitated by path compression, since during a tree traversal (when
341 * searching for a leaf - unless we are doing an insertion) we will completely
342 * ignore all skipped bits we encounter. Thus we need to verify, at the end of
343 * a potentially successful search, that we have indeed been walking the
344 * correct key path.
345 *
346 * Note that we can never "miss" the correct key in the tree if present by
347 * following the wrong path. Path compression ensures that segments of the key
348 * that are the same for all keys with a given prefix are skipped, but the
349 * skipped part *is* identical for each node in the subtrie below the skipped
350 * bit! trie_insert() in this implementation takes care of that.
351 *
352 * if n is an internal node - a 'tnode' here, the various parts of its key
353 * have many different meanings.
354 *
355 * Example:
356 * _________________________________________________________________
357 * | i | i | i | i | i | i | i | N | N | N | S | S | S | S | S | C |
358 * -----------------------------------------------------------------
359 * 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
360 *
361 * _________________________________________________________________
362 * | C | C | C | u | u | u | u | u | u | u | u | u | u | u | u | u |
363 * -----------------------------------------------------------------
364 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
365 *
366 * tp->pos = 22
367 * tp->bits = 3
368 * n->pos = 13
369 * n->bits = 4
370 *
371 * First, let's just ignore the bits that come before the parent tp, that is
372 * the bits from (tp->pos + tp->bits) to 31. They are *known* but at this
373 * point we do not use them for anything.
374 *
375 * The bits from (tp->pos) to (tp->pos + tp->bits - 1) - "N", above - are the
376 * index into the parent's child array. That is, they will be used to find
377 * 'n' among tp's children.
378 *
Xunlei Pang98a384e2016-08-18 12:33:28 +0800379 * The bits from (n->pos + n->bits) to (tp->pos - 1) - "S" - are skipped bits
Alexander Duycke9b44012014-12-31 10:56:12 -0800380 * for the node n.
381 *
382 * All the bits we have seen so far are significant to the node n. The rest
383 * of the bits are really not needed or indeed known in n->key.
384 *
385 * The bits from (n->pos) to (n->pos + n->bits - 1) - "C" - are the index into
386 * n's child array, and will of course be different for each child.
387 *
Xunlei Pang98a384e2016-08-18 12:33:28 +0800388 * The rest of the bits, from 0 to (n->pos -1) - "u" - are completely unknown
Alexander Duycke9b44012014-12-31 10:56:12 -0800389 * at this point.
390 */
Robert Olsson19baf832005-06-21 12:43:18 -0700391
Denis V. Lunevf5026fa2007-12-13 09:47:57 -0800392static const int halve_threshold = 25;
393static const int inflate_threshold = 50;
Jarek Poplawski345aa032009-07-07 19:39:16 -0700394static const int halve_threshold_root = 15;
Jens Låås80b71b82009-08-28 23:57:15 -0700395static const int inflate_threshold_root = 30;
Robert Olsson2373ce12005-08-25 13:01:29 -0700396
397static void __alias_free_mem(struct rcu_head *head)
398{
399 struct fib_alias *fa = container_of(head, struct fib_alias, rcu);
400 kmem_cache_free(fn_alias_kmem, fa);
401}
402
403static inline void alias_free_mem_rcu(struct fib_alias *fa)
404{
405 call_rcu(&fa->rcu, __alias_free_mem);
406}
407
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800408#define TNODE_KMALLOC_MAX \
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800409 ilog2((PAGE_SIZE - TNODE_SIZE(0)) / sizeof(struct key_vector *))
Alexander Duyck1de3d872015-03-04 15:04:46 -0800410#define TNODE_VMALLOC_MAX \
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800411 ilog2((SIZE_MAX - TNODE_SIZE(0)) / sizeof(struct key_vector *))
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800412
413static void __node_free_rcu(struct rcu_head *head)
Robert Olsson2373ce12005-08-25 13:01:29 -0700414{
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800415 struct tnode *n = container_of(head, struct tnode, rcu);
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800416
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800417 if (!n->tn_bits)
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800418 kmem_cache_free(trie_leaf_kmem, n);
Alexander Duyck37fd30f2014-12-31 10:55:41 -0800419 else
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -0800420 kvfree(n);
Robert Olsson2373ce12005-08-25 13:01:29 -0700421}
422
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800423#define node_free(n) call_rcu(&tn_info(n)->rcu, __node_free_rcu)
Stephen Hemminger387a5482008-04-10 03:47:34 -0700424
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800425static struct tnode *tnode_alloc(int bits)
Robert Olsson2373ce12005-08-25 13:01:29 -0700426{
Alexander Duyck1de3d872015-03-04 15:04:46 -0800427 size_t size;
428
429 /* verify bits is within bounds */
430 if (bits > TNODE_VMALLOC_MAX)
431 return NULL;
432
433 /* determine size and verify it is non-zero and didn't overflow */
434 size = TNODE_SIZE(1ul << bits);
435
Robert Olsson2373ce12005-08-25 13:01:29 -0700436 if (size <= PAGE_SIZE)
Eric Dumazet8d965442008-01-13 22:31:44 -0800437 return kzalloc(size, GFP_KERNEL);
Stephen Hemminger15be75c2008-04-10 02:56:38 -0700438 else
Eric Dumazet7a1c8e52010-11-20 07:46:35 +0000439 return vzalloc(size);
Stephen Hemminger15be75c2008-04-10 02:56:38 -0700440}
Robert Olsson2373ce12005-08-25 13:01:29 -0700441
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800442static inline void empty_child_inc(struct key_vector *n)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800443{
Alexander Duyck6e22d172015-03-06 09:54:39 -0800444 ++tn_info(n)->empty_children ? : ++tn_info(n)->full_children;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800445}
446
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800447static inline void empty_child_dec(struct key_vector *n)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800448{
Alexander Duyck6e22d172015-03-06 09:54:39 -0800449 tn_info(n)->empty_children-- ? : tn_info(n)->full_children--;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800450}
451
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800452static struct key_vector *leaf_new(t_key key, struct fib_alias *fa)
Robert Olsson19baf832005-06-21 12:43:18 -0700453{
Firo Yangf38b24c2015-06-08 11:54:51 +0800454 struct key_vector *l;
455 struct tnode *kv;
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800456
Firo Yangf38b24c2015-06-08 11:54:51 +0800457 kv = kmem_cache_alloc(trie_leaf_kmem, GFP_KERNEL);
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800458 if (!kv)
459 return NULL;
460
461 /* initialize key vector */
Firo Yangf38b24c2015-06-08 11:54:51 +0800462 l = kv->kv;
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800463 l->key = key;
464 l->pos = 0;
465 l->bits = 0;
466 l->slen = fa->fa_slen;
467
468 /* link leaf to fib alias */
469 INIT_HLIST_HEAD(&l->leaf);
470 hlist_add_head(&fa->fa_list, &l->leaf);
471
Robert Olsson19baf832005-06-21 12:43:18 -0700472 return l;
473}
474
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800475static struct key_vector *tnode_new(t_key key, int pos, int bits)
Robert Olsson19baf832005-06-21 12:43:18 -0700476{
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800477 unsigned int shift = pos + bits;
Firo Yangf38b24c2015-06-08 11:54:51 +0800478 struct key_vector *tn;
479 struct tnode *tnode;
Alexander Duyck64c9b6f2014-12-31 10:55:35 -0800480
481 /* verify bits and pos their msb bits clear and values are valid */
482 BUG_ON(!bits || (shift > KEYLENGTH));
Robert Olsson19baf832005-06-21 12:43:18 -0700483
Firo Yangf38b24c2015-06-08 11:54:51 +0800484 tnode = tnode_alloc(bits);
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800485 if (!tnode)
486 return NULL;
487
Firo Yangf38b24c2015-06-08 11:54:51 +0800488 pr_debug("AT %p s=%zu %zu\n", tnode, TNODE_SIZE(0),
489 sizeof(struct key_vector *) << bits);
490
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800491 if (bits == KEYLENGTH)
Alexander Duyck6e22d172015-03-06 09:54:39 -0800492 tnode->full_children = 1;
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800493 else
Alexander Duyck6e22d172015-03-06 09:54:39 -0800494 tnode->empty_children = 1ul << bits;
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800495
Firo Yangf38b24c2015-06-08 11:54:51 +0800496 tn = tnode->kv;
Alexander Duyckdc35dbe2015-03-06 09:54:27 -0800497 tn->key = (shift < KEYLENGTH) ? (key >> shift) << shift : 0;
498 tn->pos = pos;
499 tn->bits = bits;
500 tn->slen = pos;
501
Robert Olsson19baf832005-06-21 12:43:18 -0700502 return tn;
503}
504
Alexander Duycke9b44012014-12-31 10:56:12 -0800505/* Check whether a tnode 'n' is "full", i.e. it is an internal node
Robert Olsson19baf832005-06-21 12:43:18 -0700506 * and no bits are skipped. See discussion in dyntree paper p. 6
507 */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800508static inline int tnode_full(struct key_vector *tn, struct key_vector *n)
Robert Olsson19baf832005-06-21 12:43:18 -0700509{
Alexander Duycke9b44012014-12-31 10:56:12 -0800510 return n && ((n->pos + n->bits) == tn->pos) && IS_TNODE(n);
Robert Olsson19baf832005-06-21 12:43:18 -0700511}
512
Alexander Duyckff181ed2014-12-31 10:56:43 -0800513/* Add a child at position i overwriting the old value.
514 * Update the value of full_children and empty_children.
515 */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800516static void put_child(struct key_vector *tn, unsigned long i,
517 struct key_vector *n)
Robert Olsson19baf832005-06-21 12:43:18 -0700518{
Alexander Duyck754baf82015-03-06 09:54:14 -0800519 struct key_vector *chi = get_child(tn, i);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800520 int isfull, wasfull;
Robert Olsson19baf832005-06-21 12:43:18 -0700521
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800522 BUG_ON(i >= child_length(tn));
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700523
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800524 /* update emptyChildren, overflow into fullChildren */
Ian Morris00db4122015-04-03 09:17:27 +0100525 if (!n && chi)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800526 empty_child_inc(tn);
Ian Morris00db4122015-04-03 09:17:27 +0100527 if (n && !chi)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800528 empty_child_dec(tn);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700529
Robert Olsson19baf832005-06-21 12:43:18 -0700530 /* update fullChildren */
Alexander Duyckff181ed2014-12-31 10:56:43 -0800531 wasfull = tnode_full(tn, chi);
Robert Olsson19baf832005-06-21 12:43:18 -0700532 isfull = tnode_full(tn, n);
Alexander Duyckff181ed2014-12-31 10:56:43 -0800533
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700534 if (wasfull && !isfull)
Alexander Duyck6e22d172015-03-06 09:54:39 -0800535 tn_info(tn)->full_children--;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700536 else if (!wasfull && isfull)
Alexander Duyck6e22d172015-03-06 09:54:39 -0800537 tn_info(tn)->full_children++;
Olof Johansson91b9a272005-08-09 20:24:39 -0700538
Alexander Duyck5405afd2014-12-31 10:57:08 -0800539 if (n && (tn->slen < n->slen))
540 tn->slen = n->slen;
541
Alexander Duyck41b489f2015-03-04 15:02:33 -0800542 rcu_assign_pointer(tn->tnode[i], n);
Robert Olsson19baf832005-06-21 12:43:18 -0700543}
544
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800545static void update_children(struct key_vector *tn)
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800546{
547 unsigned long i;
548
549 /* update all of the child parent pointers */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800550 for (i = child_length(tn); i;) {
Alexander Duyck754baf82015-03-06 09:54:14 -0800551 struct key_vector *inode = get_child(tn, --i);
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800552
553 if (!inode)
554 continue;
555
556 /* Either update the children of a tnode that
557 * already belongs to us or update the child
558 * to point to ourselves.
559 */
560 if (node_parent(inode) == tn)
561 update_children(inode);
562 else
563 node_set_parent(inode, tn);
564 }
565}
566
Alexander Duyck88bae712015-03-06 09:54:52 -0800567static inline void put_child_root(struct key_vector *tp, t_key key,
568 struct key_vector *n)
Alexander Duyck836a0122014-12-31 10:56:06 -0800569{
Alexander Duyck88bae712015-03-06 09:54:52 -0800570 if (IS_TRIE(tp))
571 rcu_assign_pointer(tp->tnode[0], n);
Alexander Duyck836a0122014-12-31 10:56:06 -0800572 else
Alexander Duyck88bae712015-03-06 09:54:52 -0800573 put_child(tp, get_index(key, tp), n);
Alexander Duyck836a0122014-12-31 10:56:06 -0800574}
575
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800576static inline void tnode_free_init(struct key_vector *tn)
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700577{
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800578 tn_info(tn)->rcu.next = NULL;
Alexander Duyckfc86a932014-12-31 10:56:49 -0800579}
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700580
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800581static inline void tnode_free_append(struct key_vector *tn,
582 struct key_vector *n)
Alexander Duyckfc86a932014-12-31 10:56:49 -0800583{
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800584 tn_info(n)->rcu.next = tn_info(tn)->rcu.next;
585 tn_info(tn)->rcu.next = &tn_info(n)->rcu;
Alexander Duyckfc86a932014-12-31 10:56:49 -0800586}
587
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800588static void tnode_free(struct key_vector *tn)
Alexander Duyckfc86a932014-12-31 10:56:49 -0800589{
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800590 struct callback_head *head = &tn_info(tn)->rcu;
Alexander Duyckfc86a932014-12-31 10:56:49 -0800591
592 while (head) {
593 head = head->next;
Alexander Duyck41b489f2015-03-04 15:02:33 -0800594 tnode_free_size += TNODE_SIZE(1ul << tn->bits);
Alexander Duyckfc86a932014-12-31 10:56:49 -0800595 node_free(tn);
596
Alexander Duyck56ca2ad2015-03-06 09:54:33 -0800597 tn = container_of(head, struct tnode, rcu)->kv;
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700598 }
Alexander Duyckfc86a932014-12-31 10:56:49 -0800599
600 if (tnode_free_size >= PAGE_SIZE * sync_pages) {
601 tnode_free_size = 0;
602 synchronize_rcu();
603 }
Eric Dumazet0a5c0472011-03-31 01:51:35 -0700604}
605
Alexander Duyck88bae712015-03-06 09:54:52 -0800606static struct key_vector *replace(struct trie *t,
607 struct key_vector *oldtnode,
608 struct key_vector *tn)
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800609{
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800610 struct key_vector *tp = node_parent(oldtnode);
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800611 unsigned long i;
612
613 /* setup the parent pointer out of and back into this node */
614 NODE_INIT_PARENT(tn, tp);
Alexander Duyck88bae712015-03-06 09:54:52 -0800615 put_child_root(tp, tn->key, tn);
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800616
617 /* update all of the child parent pointers */
618 update_children(tn);
619
620 /* all pointers should be clean so we are done */
621 tnode_free(oldtnode);
622
623 /* resize children now that oldtnode is freed */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800624 for (i = child_length(tn); i;) {
Alexander Duyck754baf82015-03-06 09:54:14 -0800625 struct key_vector *inode = get_child(tn, --i);
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800626
627 /* resize child node */
628 if (tnode_full(tn, inode))
Alexander Duyck88bae712015-03-06 09:54:52 -0800629 tn = resize(t, inode);
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800630 }
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800631
Alexander Duyck88bae712015-03-06 09:54:52 -0800632 return tp;
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800633}
634
Alexander Duyck88bae712015-03-06 09:54:52 -0800635static struct key_vector *inflate(struct trie *t,
636 struct key_vector *oldtnode)
Robert Olsson19baf832005-06-21 12:43:18 -0700637{
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800638 struct key_vector *tn;
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800639 unsigned long i;
Alexander Duycke9b44012014-12-31 10:56:12 -0800640 t_key m;
Robert Olsson19baf832005-06-21 12:43:18 -0700641
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700642 pr_debug("In inflate\n");
Robert Olsson19baf832005-06-21 12:43:18 -0700643
Alexander Duycke9b44012014-12-31 10:56:12 -0800644 tn = tnode_new(oldtnode->key, oldtnode->pos - 1, oldtnode->bits + 1);
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700645 if (!tn)
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800646 goto notnode;
Robert Olsson2f368952005-07-05 15:02:40 -0700647
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800648 /* prepare oldtnode to be freed */
649 tnode_free_init(oldtnode);
650
Alexander Duyck12c081a2014-12-31 10:56:55 -0800651 /* Assemble all of the pointers in our cluster, in this case that
652 * represents all of the pointers out of our allocated nodes that
653 * point to existing tnodes and the links between our allocated
654 * nodes.
Robert Olsson2f368952005-07-05 15:02:40 -0700655 */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800656 for (i = child_length(oldtnode), m = 1u << tn->pos; i;) {
Alexander Duyck754baf82015-03-06 09:54:14 -0800657 struct key_vector *inode = get_child(oldtnode, --i);
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800658 struct key_vector *node0, *node1;
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800659 unsigned long j, k;
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700660
Robert Olsson19baf832005-06-21 12:43:18 -0700661 /* An empty child */
Ian Morris51456b22015-04-03 09:17:26 +0100662 if (!inode)
Robert Olsson19baf832005-06-21 12:43:18 -0700663 continue;
664
665 /* A leaf or an internal node with skipped bits */
Alexander Duyckadaf9812014-12-31 10:55:47 -0800666 if (!tnode_full(oldtnode, inode)) {
Alexander Duycke9b44012014-12-31 10:56:12 -0800667 put_child(tn, get_index(inode->key, tn), inode);
Robert Olsson19baf832005-06-21 12:43:18 -0700668 continue;
669 }
670
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800671 /* drop the node in the old tnode free list */
672 tnode_free_append(oldtnode, inode);
673
Robert Olsson19baf832005-06-21 12:43:18 -0700674 /* An internal node with two children */
Robert Olsson19baf832005-06-21 12:43:18 -0700675 if (inode->bits == 1) {
Alexander Duyck754baf82015-03-06 09:54:14 -0800676 put_child(tn, 2 * i + 1, get_child(inode, 1));
677 put_child(tn, 2 * i, get_child(inode, 0));
Olof Johansson91b9a272005-08-09 20:24:39 -0700678 continue;
Robert Olsson19baf832005-06-21 12:43:18 -0700679 }
680
Olof Johansson91b9a272005-08-09 20:24:39 -0700681 /* We will replace this node 'inode' with two new
Alexander Duyck12c081a2014-12-31 10:56:55 -0800682 * ones, 'node0' and 'node1', each with half of the
Olof Johansson91b9a272005-08-09 20:24:39 -0700683 * original children. The two new nodes will have
684 * a position one bit further down the key and this
685 * means that the "significant" part of their keys
686 * (see the discussion near the top of this file)
687 * will differ by one bit, which will be "0" in
Alexander Duyck12c081a2014-12-31 10:56:55 -0800688 * node0's key and "1" in node1's key. Since we are
Olof Johansson91b9a272005-08-09 20:24:39 -0700689 * moving the key position by one step, the bit that
690 * we are moving away from - the bit at position
Alexander Duyck12c081a2014-12-31 10:56:55 -0800691 * (tn->pos) - is the one that will differ between
692 * node0 and node1. So... we synthesize that bit in the
693 * two new keys.
Olof Johansson91b9a272005-08-09 20:24:39 -0700694 */
Alexander Duyck12c081a2014-12-31 10:56:55 -0800695 node1 = tnode_new(inode->key | m, inode->pos, inode->bits - 1);
696 if (!node1)
697 goto nomem;
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800698 node0 = tnode_new(inode->key, inode->pos, inode->bits - 1);
Robert Olsson19baf832005-06-21 12:43:18 -0700699
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800700 tnode_free_append(tn, node1);
Alexander Duyck12c081a2014-12-31 10:56:55 -0800701 if (!node0)
702 goto nomem;
703 tnode_free_append(tn, node0);
Robert Olsson19baf832005-06-21 12:43:18 -0700704
Alexander Duyck12c081a2014-12-31 10:56:55 -0800705 /* populate child pointers in new nodes */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800706 for (k = child_length(inode), j = k / 2; j;) {
Alexander Duyck754baf82015-03-06 09:54:14 -0800707 put_child(node1, --j, get_child(inode, --k));
708 put_child(node0, j, get_child(inode, j));
709 put_child(node1, --j, get_child(inode, --k));
710 put_child(node0, j, get_child(inode, j));
Robert Olsson19baf832005-06-21 12:43:18 -0700711 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800712
Alexander Duyck12c081a2014-12-31 10:56:55 -0800713 /* link new nodes to parent */
714 NODE_INIT_PARENT(node1, tn);
715 NODE_INIT_PARENT(node0, tn);
Olof Johansson91b9a272005-08-09 20:24:39 -0700716
Alexander Duyck12c081a2014-12-31 10:56:55 -0800717 /* link parent to nodes */
718 put_child(tn, 2 * i + 1, node1);
719 put_child(tn, 2 * i, node0);
Robert Olsson19baf832005-06-21 12:43:18 -0700720 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800721
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800722 /* setup the parent pointers into and out of this node */
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800723 return replace(t, oldtnode, tn);
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700724nomem:
Alexander Duyckfc86a932014-12-31 10:56:49 -0800725 /* all pointers should be clean so we are done */
726 tnode_free(tn);
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800727notnode:
728 return NULL;
Robert Olsson19baf832005-06-21 12:43:18 -0700729}
730
Alexander Duyck88bae712015-03-06 09:54:52 -0800731static struct key_vector *halve(struct trie *t,
732 struct key_vector *oldtnode)
Robert Olsson19baf832005-06-21 12:43:18 -0700733{
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800734 struct key_vector *tn;
Alexander Duyck12c081a2014-12-31 10:56:55 -0800735 unsigned long i;
Robert Olsson19baf832005-06-21 12:43:18 -0700736
Stephen Hemminger0c7770c2005-08-23 21:59:41 -0700737 pr_debug("In halve\n");
Stephen Hemmingerc877efb2005-07-19 14:01:51 -0700738
Alexander Duycke9b44012014-12-31 10:56:12 -0800739 tn = tnode_new(oldtnode->key, oldtnode->pos + 1, oldtnode->bits - 1);
Robert Olsson2f80b3c2005-08-09 20:25:06 -0700740 if (!tn)
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800741 goto notnode;
Robert Olsson2f368952005-07-05 15:02:40 -0700742
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800743 /* prepare oldtnode to be freed */
744 tnode_free_init(oldtnode);
745
Alexander Duyck12c081a2014-12-31 10:56:55 -0800746 /* Assemble all of the pointers in our cluster, in this case that
747 * represents all of the pointers out of our allocated nodes that
748 * point to existing tnodes and the links between our allocated
749 * nodes.
Robert Olsson2f368952005-07-05 15:02:40 -0700750 */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800751 for (i = child_length(oldtnode); i;) {
Alexander Duyck754baf82015-03-06 09:54:14 -0800752 struct key_vector *node1 = get_child(oldtnode, --i);
753 struct key_vector *node0 = get_child(oldtnode, --i);
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800754 struct key_vector *inode;
Robert Olsson2f368952005-07-05 15:02:40 -0700755
Alexander Duyck12c081a2014-12-31 10:56:55 -0800756 /* At least one of the children is empty */
757 if (!node1 || !node0) {
758 put_child(tn, i / 2, node1 ? : node0);
759 continue;
Robert Olsson2f368952005-07-05 15:02:40 -0700760 }
Robert Olsson2f368952005-07-05 15:02:40 -0700761
Alexander Duyck12c081a2014-12-31 10:56:55 -0800762 /* Two nonempty children */
763 inode = tnode_new(node0->key, oldtnode->pos, 1);
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800764 if (!inode)
765 goto nomem;
Alexander Duyck12c081a2014-12-31 10:56:55 -0800766 tnode_free_append(tn, inode);
767
768 /* initialize pointers out of node */
769 put_child(inode, 1, node1);
770 put_child(inode, 0, node0);
771 NODE_INIT_PARENT(inode, tn);
772
773 /* link parent to node */
774 put_child(tn, i / 2, inode);
Robert Olsson2f368952005-07-05 15:02:40 -0700775 }
Robert Olsson19baf832005-06-21 12:43:18 -0700776
Alexander Duyck69fa57b2015-01-22 15:51:14 -0800777 /* setup the parent pointers into and out of this node */
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800778 return replace(t, oldtnode, tn);
779nomem:
780 /* all pointers should be clean so we are done */
781 tnode_free(tn);
782notnode:
783 return NULL;
Robert Olsson19baf832005-06-21 12:43:18 -0700784}
785
Alexander Duyck88bae712015-03-06 09:54:52 -0800786static struct key_vector *collapse(struct trie *t,
787 struct key_vector *oldtnode)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800788{
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800789 struct key_vector *n, *tp;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800790 unsigned long i;
791
792 /* scan the tnode looking for that one child that might still exist */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800793 for (n = NULL, i = child_length(oldtnode); !n && i;)
Alexander Duyck754baf82015-03-06 09:54:14 -0800794 n = get_child(oldtnode, --i);
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800795
796 /* compress one level */
797 tp = node_parent(oldtnode);
Alexander Duyck88bae712015-03-06 09:54:52 -0800798 put_child_root(tp, oldtnode->key, n);
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800799 node_set_parent(n, tp);
800
801 /* drop dead node */
802 node_free(oldtnode);
Alexander Duyck88bae712015-03-06 09:54:52 -0800803
804 return tp;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800805}
806
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800807static unsigned char update_suffix(struct key_vector *tn)
Alexander Duyck5405afd2014-12-31 10:57:08 -0800808{
809 unsigned char slen = tn->pos;
810 unsigned long stride, i;
Alexander Duycka52ca622016-12-01 07:27:57 -0500811 unsigned char slen_max;
812
813 /* only vector 0 can have a suffix length greater than or equal to
814 * tn->pos + tn->bits, the second highest node will have a suffix
815 * length at most of tn->pos + tn->bits - 1
816 */
817 slen_max = min_t(unsigned char, tn->pos + tn->bits - 1, tn->slen);
Alexander Duyck5405afd2014-12-31 10:57:08 -0800818
819 /* search though the list of children looking for nodes that might
820 * have a suffix greater than the one we currently have. This is
821 * why we start with a stride of 2 since a stride of 1 would
822 * represent the nodes with suffix length equal to tn->pos
823 */
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800824 for (i = 0, stride = 0x2ul ; i < child_length(tn); i += stride) {
Alexander Duyck754baf82015-03-06 09:54:14 -0800825 struct key_vector *n = get_child(tn, i);
Alexander Duyck5405afd2014-12-31 10:57:08 -0800826
827 if (!n || (n->slen <= slen))
828 continue;
829
830 /* update stride and slen based on new value */
831 stride <<= (n->slen - slen);
832 slen = n->slen;
833 i &= ~(stride - 1);
834
Alexander Duycka52ca622016-12-01 07:27:57 -0500835 /* stop searching if we have hit the maximum possible value */
836 if (slen >= slen_max)
Alexander Duyck5405afd2014-12-31 10:57:08 -0800837 break;
838 }
839
840 tn->slen = slen;
841
842 return slen;
843}
844
Alexander Duyckf05a4812014-12-31 10:56:37 -0800845/* From "Implementing a dynamic compressed trie" by Stefan Nilsson of
846 * the Helsinki University of Technology and Matti Tikkanen of Nokia
847 * Telecommunications, page 6:
848 * "A node is doubled if the ratio of non-empty children to all
849 * children in the *doubled* node is at least 'high'."
850 *
851 * 'high' in this instance is the variable 'inflate_threshold'. It
852 * is expressed as a percentage, so we multiply it with
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800853 * child_length() and instead of multiplying by 2 (since the
Alexander Duyckf05a4812014-12-31 10:56:37 -0800854 * child array will be doubled by inflate()) and multiplying
855 * the left-hand side by 100 (to handle the percentage thing) we
856 * multiply the left-hand side by 50.
857 *
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800858 * The left-hand side may look a bit weird: child_length(tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800859 * - tn->empty_children is of course the number of non-null children
860 * in the current node. tn->full_children is the number of "full"
861 * children, that is non-null tnodes with a skip value of 0.
862 * All of those will be doubled in the resulting inflated tnode, so
863 * we just count them one extra time here.
864 *
865 * A clearer way to write this would be:
866 *
867 * to_be_doubled = tn->full_children;
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800868 * not_to_be_doubled = child_length(tn) - tn->empty_children -
Alexander Duyckf05a4812014-12-31 10:56:37 -0800869 * tn->full_children;
870 *
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800871 * new_child_length = child_length(tn) * 2;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800872 *
873 * new_fill_factor = 100 * (not_to_be_doubled + 2*to_be_doubled) /
874 * new_child_length;
875 * if (new_fill_factor >= inflate_threshold)
876 *
877 * ...and so on, tho it would mess up the while () loop.
878 *
879 * anyway,
880 * 100 * (not_to_be_doubled + 2*to_be_doubled) / new_child_length >=
881 * inflate_threshold
882 *
883 * avoid a division:
884 * 100 * (not_to_be_doubled + 2*to_be_doubled) >=
885 * inflate_threshold * new_child_length
886 *
887 * expand not_to_be_doubled and to_be_doubled, and shorten:
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800888 * 100 * (child_length(tn) - tn->empty_children +
Alexander Duyckf05a4812014-12-31 10:56:37 -0800889 * tn->full_children) >= inflate_threshold * new_child_length
890 *
891 * expand new_child_length:
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800892 * 100 * (child_length(tn) - tn->empty_children +
Alexander Duyckf05a4812014-12-31 10:56:37 -0800893 * tn->full_children) >=
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800894 * inflate_threshold * child_length(tn) * 2
Alexander Duyckf05a4812014-12-31 10:56:37 -0800895 *
896 * shorten again:
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800897 * 50 * (tn->full_children + child_length(tn) -
Alexander Duyckf05a4812014-12-31 10:56:37 -0800898 * tn->empty_children) >= inflate_threshold *
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800899 * child_length(tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800900 *
901 */
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800902static inline bool should_inflate(struct key_vector *tp, struct key_vector *tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800903{
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800904 unsigned long used = child_length(tn);
Alexander Duyckf05a4812014-12-31 10:56:37 -0800905 unsigned long threshold = used;
906
907 /* Keep root node larger */
Alexander Duyck88bae712015-03-06 09:54:52 -0800908 threshold *= IS_TRIE(tp) ? inflate_threshold_root : inflate_threshold;
Alexander Duyck6e22d172015-03-06 09:54:39 -0800909 used -= tn_info(tn)->empty_children;
910 used += tn_info(tn)->full_children;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800911
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800912 /* if bits == KEYLENGTH then pos = 0, and will fail below */
913
914 return (used > 1) && tn->pos && ((50 * used) >= threshold);
Alexander Duyckf05a4812014-12-31 10:56:37 -0800915}
916
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800917static inline bool should_halve(struct key_vector *tp, struct key_vector *tn)
Alexander Duyckf05a4812014-12-31 10:56:37 -0800918{
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800919 unsigned long used = child_length(tn);
Alexander Duyckf05a4812014-12-31 10:56:37 -0800920 unsigned long threshold = used;
921
922 /* Keep root node larger */
Alexander Duyck88bae712015-03-06 09:54:52 -0800923 threshold *= IS_TRIE(tp) ? halve_threshold_root : halve_threshold;
Alexander Duyck6e22d172015-03-06 09:54:39 -0800924 used -= tn_info(tn)->empty_children;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800925
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800926 /* if bits == KEYLENGTH then used = 100% on wrap, and will fail below */
927
928 return (used > 1) && (tn->bits > 1) && ((100 * used) < threshold);
929}
930
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800931static inline bool should_collapse(struct key_vector *tn)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800932{
Alexander Duyck2e1ac882015-03-06 09:54:21 -0800933 unsigned long used = child_length(tn);
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800934
Alexander Duyck6e22d172015-03-06 09:54:39 -0800935 used -= tn_info(tn)->empty_children;
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800936
937 /* account for bits == KEYLENGTH case */
Alexander Duyck6e22d172015-03-06 09:54:39 -0800938 if ((tn->bits == KEYLENGTH) && tn_info(tn)->full_children)
Alexander Duyck95f60ea2015-01-22 15:51:26 -0800939 used -= KEY_MAX;
940
941 /* One child or none, time to drop us from the trie */
942 return used < 2;
Alexander Duyckf05a4812014-12-31 10:56:37 -0800943}
944
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800945#define MAX_WORK 10
Alexander Duyck88bae712015-03-06 09:54:52 -0800946static struct key_vector *resize(struct trie *t, struct key_vector *tn)
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800947{
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800948#ifdef CONFIG_IP_FIB_TRIE_STATS
949 struct trie_use_stats __percpu *stats = t->stats;
950#endif
Alexander Duyck35c6eda2015-03-06 09:54:08 -0800951 struct key_vector *tp = node_parent(tn);
Alexander Duyck88bae712015-03-06 09:54:52 -0800952 unsigned long cindex = get_index(tn->key, tp);
Alexander Duycka80e89d2015-01-22 15:51:20 -0800953 int max_work = MAX_WORK;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800954
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800955 pr_debug("In tnode_resize %p inflate_threshold=%d threshold=%d\n",
956 tn, inflate_threshold, halve_threshold);
957
Alexander Duyckff181ed2014-12-31 10:56:43 -0800958 /* track the tnode via the pointer from the parent instead of
959 * doing it ourselves. This way we can let RCU fully do its
960 * thing without us interfering
961 */
Alexander Duyck88bae712015-03-06 09:54:52 -0800962 BUG_ON(tn != get_child(tp, cindex));
Alexander Duyckff181ed2014-12-31 10:56:43 -0800963
Alexander Duyckf05a4812014-12-31 10:56:37 -0800964 /* Double as long as the resulting node has a number of
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800965 * nonempty nodes that are above the threshold.
966 */
Alexander Duyckb6f15f82015-03-23 11:51:53 -0700967 while (should_inflate(tp, tn) && max_work) {
Alexander Duyck88bae712015-03-06 09:54:52 -0800968 tp = inflate(t, tn);
969 if (!tp) {
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800970#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800971 this_cpu_inc(stats->resize_node_skipped);
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800972#endif
973 break;
974 }
Alexander Duyckff181ed2014-12-31 10:56:43 -0800975
Alexander Duyckb6f15f82015-03-23 11:51:53 -0700976 max_work--;
Alexander Duyck88bae712015-03-06 09:54:52 -0800977 tn = get_child(tp, cindex);
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800978 }
979
Alexander Duyckb6f15f82015-03-23 11:51:53 -0700980 /* update parent in case inflate failed */
981 tp = node_parent(tn);
982
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800983 /* Return if at least one inflate is run */
984 if (max_work != MAX_WORK)
Alexander Duyckb6f15f82015-03-23 11:51:53 -0700985 return tp;
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800986
Alexander Duyckf05a4812014-12-31 10:56:37 -0800987 /* Halve as long as the number of empty children in this
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800988 * node is above threshold.
989 */
Alexander Duyckb6f15f82015-03-23 11:51:53 -0700990 while (should_halve(tp, tn) && max_work) {
Alexander Duyck88bae712015-03-06 09:54:52 -0800991 tp = halve(t, tn);
992 if (!tp) {
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800993#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8d8e8102015-03-06 09:54:02 -0800994 this_cpu_inc(stats->resize_node_skipped);
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800995#endif
996 break;
997 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -0800998
Alexander Duyckb6f15f82015-03-23 11:51:53 -0700999 max_work--;
Alexander Duyck88bae712015-03-06 09:54:52 -08001000 tn = get_child(tp, cindex);
Alexander Duyckff181ed2014-12-31 10:56:43 -08001001 }
Alexander Duyckcf3637b2014-12-31 10:56:31 -08001002
1003 /* Only one child remains */
Alexander Duyck88bae712015-03-06 09:54:52 -08001004 if (should_collapse(tn))
1005 return collapse(t, tn);
1006
Alexander Duyckb6f15f82015-03-23 11:51:53 -07001007 /* update parent in case halve failed */
Alexander Duycka52ca622016-12-01 07:27:57 -05001008 return node_parent(tn);
Alexander Duyckcf3637b2014-12-31 10:56:31 -08001009}
1010
Alexander Duyck1a239172016-12-01 07:27:52 -05001011static void node_pull_suffix(struct key_vector *tn, unsigned char slen)
Robert Olsson19baf832005-06-21 12:43:18 -07001012{
Alexander Duyck1a239172016-12-01 07:27:52 -05001013 unsigned char node_slen = tn->slen;
1014
1015 while ((node_slen > tn->pos) && (node_slen > slen)) {
1016 slen = update_suffix(tn);
1017 if (node_slen == slen)
Alexander Duyck5405afd2014-12-31 10:57:08 -08001018 break;
Alexander Duyck1a239172016-12-01 07:27:52 -05001019
1020 tn = node_parent(tn);
1021 node_slen = tn->slen;
Alexander Duyck5405afd2014-12-31 10:57:08 -08001022 }
1023}
1024
Alexander Duyck1a239172016-12-01 07:27:52 -05001025static void node_push_suffix(struct key_vector *tn, unsigned char slen)
Alexander Duyck5405afd2014-12-31 10:57:08 -08001026{
Alexander Duyck1a239172016-12-01 07:27:52 -05001027 while (tn->slen < slen) {
1028 tn->slen = slen;
Alexander Duyck5405afd2014-12-31 10:57:08 -08001029 tn = node_parent(tn);
1030 }
1031}
1032
Robert Olsson2373ce12005-08-25 13:01:29 -07001033/* rcu_read_lock needs to be hold by caller from readside */
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001034static struct key_vector *fib_find_node(struct trie *t,
1035 struct key_vector **tp, u32 key)
Robert Olsson19baf832005-06-21 12:43:18 -07001036{
Alexander Duyck88bae712015-03-06 09:54:52 -08001037 struct key_vector *pn, *n = t->kv;
1038 unsigned long index = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001039
Alexander Duyck88bae712015-03-06 09:54:52 -08001040 do {
1041 pn = n;
1042 n = get_child_rcu(n, index);
1043
1044 if (!n)
1045 break;
1046
1047 index = get_cindex(key, n);
Alexander Duyck939afb02014-12-31 10:56:00 -08001048
1049 /* This bit of code is a bit tricky but it combines multiple
1050 * checks into a single check. The prefix consists of the
1051 * prefix plus zeros for the bits in the cindex. The index
1052 * is the difference between the key and this value. From
1053 * this we can actually derive several pieces of data.
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001054 * if (index >= (1ul << bits))
Alexander Duyck939afb02014-12-31 10:56:00 -08001055 * we have a mismatch in skip bits and failed
Alexander Duyckb3832112015-01-22 15:51:08 -08001056 * else
1057 * we know the value is cindex
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001058 *
1059 * This check is safe even if bits == KEYLENGTH due to the
1060 * fact that we can only allocate a node with 32 bits if a
1061 * long is greater than 32 bits.
Alexander Duyck939afb02014-12-31 10:56:00 -08001062 */
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001063 if (index >= (1ul << n->bits)) {
1064 n = NULL;
1065 break;
1066 }
Alexander Duyck939afb02014-12-31 10:56:00 -08001067
Alexander Duyck88bae712015-03-06 09:54:52 -08001068 /* keep searching until we find a perfect match leaf or NULL */
1069 } while (IS_TNODE(n));
Robert Olsson19baf832005-06-21 12:43:18 -07001070
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001071 *tp = pn;
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001072
Alexander Duyck939afb02014-12-31 10:56:00 -08001073 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07001074}
1075
Alexander Duyck02525362015-01-22 15:51:39 -08001076/* Return the first fib alias matching TOS with
1077 * priority less than or equal to PRIO.
1078 */
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001079static struct fib_alias *fib_find_alias(struct hlist_head *fah, u8 slen,
Alexander Duyck0b65bd92015-03-12 14:46:29 -07001080 u8 tos, u32 prio, u32 tb_id)
Alexander Duyck02525362015-01-22 15:51:39 -08001081{
1082 struct fib_alias *fa;
1083
1084 if (!fah)
1085 return NULL;
1086
Alexander Duyck56315f92015-02-25 15:31:31 -08001087 hlist_for_each_entry(fa, fah, fa_list) {
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001088 if (fa->fa_slen < slen)
1089 continue;
1090 if (fa->fa_slen != slen)
1091 break;
Alexander Duyck0b65bd92015-03-12 14:46:29 -07001092 if (fa->tb_id > tb_id)
1093 continue;
1094 if (fa->tb_id != tb_id)
1095 break;
Alexander Duyck02525362015-01-22 15:51:39 -08001096 if (fa->fa_tos > tos)
1097 continue;
1098 if (fa->fa_info->fib_priority >= prio || fa->fa_tos < tos)
1099 return fa;
1100 }
1101
1102 return NULL;
1103}
1104
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001105static void trie_rebalance(struct trie *t, struct key_vector *tn)
Robert Olsson19baf832005-06-21 12:43:18 -07001106{
Alexander Duyck88bae712015-03-06 09:54:52 -08001107 while (!IS_TRIE(tn))
1108 tn = resize(t, tn);
Robert Olsson19baf832005-06-21 12:43:18 -07001109}
1110
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001111static int fib_insert_node(struct trie *t, struct key_vector *tp,
Alexander Duyckd5d64872015-03-04 15:02:18 -08001112 struct fib_alias *new, t_key key)
Robert Olsson19baf832005-06-21 12:43:18 -07001113{
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001114 struct key_vector *n, *l;
Alexander Duyck836a0122014-12-31 10:56:06 -08001115
Alexander Duyckd5d64872015-03-04 15:02:18 -08001116 l = leaf_new(key, new);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001117 if (!l)
Alexander Duyck8d8e8102015-03-06 09:54:02 -08001118 goto noleaf;
Alexander Duyckd5d64872015-03-04 15:02:18 -08001119
1120 /* retrieve child from parent node */
Alexander Duyck88bae712015-03-06 09:54:52 -08001121 n = get_child(tp, get_index(key, tp));
Alexander Duyck836a0122014-12-31 10:56:06 -08001122
1123 /* Case 2: n is a LEAF or a TNODE and the key doesn't match.
1124 *
1125 * Add a new tnode here
1126 * first tnode need some special handling
1127 * leaves us in position for handling as case 3
1128 */
1129 if (n) {
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001130 struct key_vector *tn;
Alexander Duyck836a0122014-12-31 10:56:06 -08001131
Alexander Duycke9b44012014-12-31 10:56:12 -08001132 tn = tnode_new(key, __fls(key ^ n->key), 1);
Alexander Duyck8d8e8102015-03-06 09:54:02 -08001133 if (!tn)
1134 goto notnode;
Olof Johansson91b9a272005-08-09 20:24:39 -07001135
Alexander Duyck836a0122014-12-31 10:56:06 -08001136 /* initialize routes out of node */
1137 NODE_INIT_PARENT(tn, tp);
1138 put_child(tn, get_index(key, tn) ^ 1, n);
Robert Olsson19baf832005-06-21 12:43:18 -07001139
Alexander Duyck836a0122014-12-31 10:56:06 -08001140 /* start adding routes into the node */
Alexander Duyck88bae712015-03-06 09:54:52 -08001141 put_child_root(tp, key, tn);
Alexander Duyck836a0122014-12-31 10:56:06 -08001142 node_set_parent(n, tn);
Robert Olsson19baf832005-06-21 12:43:18 -07001143
Alexander Duyck836a0122014-12-31 10:56:06 -08001144 /* parent now has a NULL spot where the leaf can go */
Alexander Duycke962f302014-12-10 21:49:22 -08001145 tp = tn;
Robert Olsson19baf832005-06-21 12:43:18 -07001146 }
Olof Johansson91b9a272005-08-09 20:24:39 -07001147
Alexander Duyck836a0122014-12-31 10:56:06 -08001148 /* Case 3: n is NULL, and will just insert a new leaf */
Alexander Duycka52ca622016-12-01 07:27:57 -05001149 node_push_suffix(tp, new->fa_slen);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001150 NODE_INIT_PARENT(l, tp);
Alexander Duyck88bae712015-03-06 09:54:52 -08001151 put_child_root(tp, key, l);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001152 trie_rebalance(t, tp);
Olof Johansson91b9a272005-08-09 20:24:39 -07001153
Alexander Duyckd5d64872015-03-04 15:02:18 -08001154 return 0;
Alexander Duyck8d8e8102015-03-06 09:54:02 -08001155notnode:
1156 node_free(l);
1157noleaf:
1158 return -ENOMEM;
Robert Olsson19baf832005-06-21 12:43:18 -07001159}
1160
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001161static int fib_insert_alias(struct trie *t, struct key_vector *tp,
1162 struct key_vector *l, struct fib_alias *new,
Alexander Duyckd5d64872015-03-04 15:02:18 -08001163 struct fib_alias *fa, t_key key)
1164{
1165 if (!l)
1166 return fib_insert_node(t, tp, new, key);
1167
1168 if (fa) {
1169 hlist_add_before_rcu(&new->fa_list, &fa->fa_list);
1170 } else {
1171 struct fib_alias *last;
1172
1173 hlist_for_each_entry(last, &l->leaf, fa_list) {
1174 if (new->fa_slen < last->fa_slen)
1175 break;
Alexander Duyck0b65bd92015-03-12 14:46:29 -07001176 if ((new->fa_slen == last->fa_slen) &&
1177 (new->tb_id > last->tb_id))
1178 break;
Alexander Duyckd5d64872015-03-04 15:02:18 -08001179 fa = last;
1180 }
1181
1182 if (fa)
1183 hlist_add_behind_rcu(&new->fa_list, &fa->fa_list);
1184 else
1185 hlist_add_head_rcu(&new->fa_list, &l->leaf);
1186 }
1187
1188 /* if we added to the tail node then we need to update slen */
1189 if (l->slen < new->fa_slen) {
1190 l->slen = new->fa_slen;
Alexander Duyck1a239172016-12-01 07:27:52 -05001191 node_push_suffix(tp, new->fa_slen);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001192 }
1193
1194 return 0;
1195}
1196
1197/* Caller must hold RTNL. */
Jiri Pirkob90eb752016-09-26 12:52:29 +02001198int fib_table_insert(struct net *net, struct fib_table *tb,
1199 struct fib_config *cfg)
Robert Olsson19baf832005-06-21 12:43:18 -07001200{
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001201 struct trie *t = (struct trie *)tb->tb_data;
Robert Olsson19baf832005-06-21 12:43:18 -07001202 struct fib_alias *fa, *new_fa;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001203 struct key_vector *l, *tp;
Guillaume Naultb93e1fa2016-09-07 17:20:46 +02001204 u16 nlflags = NLM_F_EXCL;
Robert Olsson19baf832005-06-21 12:43:18 -07001205 struct fib_info *fi;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001206 u8 plen = cfg->fc_dst_len;
1207 u8 slen = KEYLENGTH - plen;
Thomas Graf4e902c52006-08-17 18:14:52 -07001208 u8 tos = cfg->fc_tos;
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001209 u32 key;
Robert Olsson19baf832005-06-21 12:43:18 -07001210 int err;
Robert Olsson19baf832005-06-21 12:43:18 -07001211
Alexander Duyck5786ec62015-02-25 15:31:37 -08001212 if (plen > KEYLENGTH)
Robert Olsson19baf832005-06-21 12:43:18 -07001213 return -EINVAL;
1214
Thomas Graf4e902c52006-08-17 18:14:52 -07001215 key = ntohl(cfg->fc_dst);
Robert Olsson19baf832005-06-21 12:43:18 -07001216
Patrick McHardy2dfe55b2006-08-10 23:08:33 -07001217 pr_debug("Insert table=%u %08x/%d\n", tb->tb_id, key, plen);
Robert Olsson19baf832005-06-21 12:43:18 -07001218
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001219 if ((plen < KEYLENGTH) && (key << plen))
Robert Olsson19baf832005-06-21 12:43:18 -07001220 return -EINVAL;
1221
Thomas Graf4e902c52006-08-17 18:14:52 -07001222 fi = fib_create_info(cfg);
1223 if (IS_ERR(fi)) {
1224 err = PTR_ERR(fi);
Robert Olsson19baf832005-06-21 12:43:18 -07001225 goto err;
Thomas Graf4e902c52006-08-17 18:14:52 -07001226 }
Robert Olsson19baf832005-06-21 12:43:18 -07001227
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001228 l = fib_find_node(t, &tp, key);
Alexander Duyck0b65bd92015-03-12 14:46:29 -07001229 fa = l ? fib_find_alias(&l->leaf, slen, tos, fi->fib_priority,
1230 tb->tb_id) : NULL;
Robert Olsson19baf832005-06-21 12:43:18 -07001231
1232 /* Now fa, if non-NULL, points to the first fib alias
1233 * with the same keys [prefix,tos,priority], if such key already
1234 * exists or to the node before which we will insert new one.
1235 *
1236 * If fa is NULL, we will need to allocate a new one and
Alexander Duyck56315f92015-02-25 15:31:31 -08001237 * insert to the tail of the section matching the suffix length
1238 * of the new alias.
Robert Olsson19baf832005-06-21 12:43:18 -07001239 */
1240
Julian Anastasov936f6f82008-01-28 21:18:06 -08001241 if (fa && fa->fa_tos == tos &&
1242 fa->fa_info->fib_priority == fi->fib_priority) {
1243 struct fib_alias *fa_first, *fa_match;
Robert Olsson19baf832005-06-21 12:43:18 -07001244
1245 err = -EEXIST;
Thomas Graf4e902c52006-08-17 18:14:52 -07001246 if (cfg->fc_nlflags & NLM_F_EXCL)
Robert Olsson19baf832005-06-21 12:43:18 -07001247 goto out;
1248
Guillaume Naultb93e1fa2016-09-07 17:20:46 +02001249 nlflags &= ~NLM_F_EXCL;
1250
Julian Anastasov936f6f82008-01-28 21:18:06 -08001251 /* We have 2 goals:
1252 * 1. Find exact match for type, scope, fib_info to avoid
1253 * duplicate routes
1254 * 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it
1255 */
1256 fa_match = NULL;
1257 fa_first = fa;
Alexander Duyck56315f92015-02-25 15:31:31 -08001258 hlist_for_each_entry_from(fa, fa_list) {
Alexander Duyck0b65bd92015-03-12 14:46:29 -07001259 if ((fa->fa_slen != slen) ||
1260 (fa->tb_id != tb->tb_id) ||
1261 (fa->fa_tos != tos))
Julian Anastasov936f6f82008-01-28 21:18:06 -08001262 break;
1263 if (fa->fa_info->fib_priority != fi->fib_priority)
1264 break;
1265 if (fa->fa_type == cfg->fc_type &&
Julian Anastasov936f6f82008-01-28 21:18:06 -08001266 fa->fa_info == fi) {
1267 fa_match = fa;
1268 break;
1269 }
1270 }
1271
Thomas Graf4e902c52006-08-17 18:14:52 -07001272 if (cfg->fc_nlflags & NLM_F_REPLACE) {
Robert Olsson19baf832005-06-21 12:43:18 -07001273 struct fib_info *fi_drop;
1274 u8 state;
1275
Guillaume Naultb93e1fa2016-09-07 17:20:46 +02001276 nlflags |= NLM_F_REPLACE;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001277 fa = fa_first;
1278 if (fa_match) {
1279 if (fa == fa_match)
1280 err = 0;
Joonwoo Park67250332008-01-18 03:45:18 -08001281 goto out;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001282 }
Robert Olsson2373ce12005-08-25 13:01:29 -07001283 err = -ENOBUFS;
Christoph Lametere94b1762006-12-06 20:33:17 -08001284 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001285 if (!new_fa)
Robert Olsson2373ce12005-08-25 13:01:29 -07001286 goto out;
Robert Olsson19baf832005-06-21 12:43:18 -07001287
1288 fi_drop = fa->fa_info;
Robert Olsson2373ce12005-08-25 13:01:29 -07001289 new_fa->fa_tos = fa->fa_tos;
1290 new_fa->fa_info = fi;
Thomas Graf4e902c52006-08-17 18:14:52 -07001291 new_fa->fa_type = cfg->fc_type;
Robert Olsson19baf832005-06-21 12:43:18 -07001292 state = fa->fa_state;
Julian Anastasov936f6f82008-01-28 21:18:06 -08001293 new_fa->fa_state = state & ~FA_S_ACCESSED;
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08001294 new_fa->fa_slen = fa->fa_slen;
Michal Kubečekd4e64c22015-05-22 13:40:09 +02001295 new_fa->tb_id = tb->tb_id;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001296 new_fa->fa_default = -1;
Robert Olsson19baf832005-06-21 12:43:18 -07001297
Alexander Duyck56315f92015-02-25 15:31:31 -08001298 hlist_replace_rcu(&fa->fa_list, &new_fa->fa_list);
Scott Feldman8e05fd72015-03-05 21:21:19 -08001299
Robert Olsson2373ce12005-08-25 13:01:29 -07001300 alias_free_mem_rcu(fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001301
1302 fib_release_info(fi_drop);
1303 if (state & FA_S_ACCESSED)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001304 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Jiri Pirkob90eb752016-09-26 12:52:29 +02001305
1306 call_fib_entry_notifiers(net, FIB_EVENT_ENTRY_ADD,
1307 key, plen, fi,
1308 new_fa->fa_tos, cfg->fc_type,
1309 tb->tb_id, cfg->fc_nlflags);
Milan Kocianb8f55832007-05-23 14:55:06 -07001310 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen,
Guillaume Naultb93e1fa2016-09-07 17:20:46 +02001311 tb->tb_id, &cfg->fc_nlinfo, nlflags);
Robert Olsson19baf832005-06-21 12:43:18 -07001312
Olof Johansson91b9a272005-08-09 20:24:39 -07001313 goto succeeded;
Robert Olsson19baf832005-06-21 12:43:18 -07001314 }
1315 /* Error if we find a perfect match which
1316 * uses the same scope, type, and nexthop
1317 * information.
1318 */
Julian Anastasov936f6f82008-01-28 21:18:06 -08001319 if (fa_match)
1320 goto out;
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001321
Roopa Prabhua2bb6d72015-06-17 11:07:01 -07001322 if (cfg->fc_nlflags & NLM_F_APPEND)
Guillaume Naultb93e1fa2016-09-07 17:20:46 +02001323 nlflags |= NLM_F_APPEND;
Roopa Prabhua2bb6d72015-06-17 11:07:01 -07001324 else
Julian Anastasov936f6f82008-01-28 21:18:06 -08001325 fa = fa_first;
Robert Olsson19baf832005-06-21 12:43:18 -07001326 }
1327 err = -ENOENT;
Thomas Graf4e902c52006-08-17 18:14:52 -07001328 if (!(cfg->fc_nlflags & NLM_F_CREATE))
Robert Olsson19baf832005-06-21 12:43:18 -07001329 goto out;
1330
Guillaume Naultb93e1fa2016-09-07 17:20:46 +02001331 nlflags |= NLM_F_CREATE;
Robert Olsson19baf832005-06-21 12:43:18 -07001332 err = -ENOBUFS;
Christoph Lametere94b1762006-12-06 20:33:17 -08001333 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001334 if (!new_fa)
Robert Olsson19baf832005-06-21 12:43:18 -07001335 goto out;
1336
1337 new_fa->fa_info = fi;
1338 new_fa->fa_tos = tos;
Thomas Graf4e902c52006-08-17 18:14:52 -07001339 new_fa->fa_type = cfg->fc_type;
Robert Olsson19baf832005-06-21 12:43:18 -07001340 new_fa->fa_state = 0;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001341 new_fa->fa_slen = slen;
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001342 new_fa->tb_id = tb->tb_id;
Julian Anastasov2392deb2015-07-22 10:43:23 +03001343 new_fa->fa_default = -1;
Robert Olsson19baf832005-06-21 12:43:18 -07001344
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08001345 /* Insert new entry to the list. */
Alexander Duyckd5d64872015-03-04 15:02:18 -08001346 err = fib_insert_alias(t, tp, l, new_fa, fa, key);
1347 if (err)
Jiri Pirko347e3b22016-09-26 12:52:33 +02001348 goto out_free_new_fa;
Robert Olsson19baf832005-06-21 12:43:18 -07001349
David S. Miller21d8c492011-04-14 14:49:37 -07001350 if (!plen)
1351 tb->tb_num_default++;
1352
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001353 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Jiri Pirkob90eb752016-09-26 12:52:29 +02001354 call_fib_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, key, plen, fi, tos,
1355 cfg->fc_type, tb->tb_id, cfg->fc_nlflags);
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001356 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen, new_fa->tb_id,
Roopa Prabhua2bb6d72015-06-17 11:07:01 -07001357 &cfg->fc_nlinfo, nlflags);
Robert Olsson19baf832005-06-21 12:43:18 -07001358succeeded:
1359 return 0;
Robert Olssonf835e472005-06-28 15:00:39 -07001360
1361out_free_new_fa:
1362 kmem_cache_free(fn_alias_kmem, new_fa);
Robert Olsson19baf832005-06-21 12:43:18 -07001363out:
1364 fib_release_info(fi);
Olof Johansson91b9a272005-08-09 20:24:39 -07001365err:
Robert Olsson19baf832005-06-21 12:43:18 -07001366 return err;
1367}
1368
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001369static inline t_key prefix_mismatch(t_key key, struct key_vector *n)
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001370{
1371 t_key prefix = n->key;
1372
1373 return (key ^ prefix) & (prefix | -prefix);
1374}
1375
Alexander Duyck345e9b52014-12-31 10:56:24 -08001376/* should be called with rcu_read_lock */
David S. Miller22bd5b92011-03-11 19:54:08 -05001377int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00001378 struct fib_result *res, int fib_flags)
Robert Olsson19baf832005-06-21 12:43:18 -07001379{
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001380 struct trie *t = (struct trie *) tb->tb_data;
Alexander Duyck8274a972014-12-31 10:55:29 -08001381#ifdef CONFIG_IP_FIB_TRIE_STATS
1382 struct trie_use_stats __percpu *stats = t->stats;
1383#endif
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001384 const t_key key = ntohl(flp->daddr);
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001385 struct key_vector *n, *pn;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001386 struct fib_alias *fa;
Alexander Duyck71e8b672015-03-04 15:04:03 -08001387 unsigned long index;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001388 t_key cindex;
Robert Olsson19baf832005-06-21 12:43:18 -07001389
David Ahernf6d3c192015-08-28 08:42:09 -07001390 trace_fib_table_lookup(tb->tb_id, flp);
1391
Alexander Duyck88bae712015-03-06 09:54:52 -08001392 pn = t->kv;
1393 cindex = 0;
1394
1395 n = get_child_rcu(pn, cindex);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001396 if (!n)
Alexander Duyck345e9b52014-12-31 10:56:24 -08001397 return -EAGAIN;
Robert Olsson19baf832005-06-21 12:43:18 -07001398
1399#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -08001400 this_cpu_inc(stats->gets);
Robert Olsson19baf832005-06-21 12:43:18 -07001401#endif
1402
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001403 /* Step 1: Travel to the longest prefix match in the trie */
1404 for (;;) {
Alexander Duyck88bae712015-03-06 09:54:52 -08001405 index = get_cindex(key, n);
Robert Olsson19baf832005-06-21 12:43:18 -07001406
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001407 /* This bit of code is a bit tricky but it combines multiple
1408 * checks into a single check. The prefix consists of the
1409 * prefix plus zeros for the "bits" in the prefix. The index
1410 * is the difference between the key and this value. From
1411 * this we can actually derive several pieces of data.
Alexander Duyck71e8b672015-03-04 15:04:03 -08001412 * if (index >= (1ul << bits))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001413 * we have a mismatch in skip bits and failed
Alexander Duyckb3832112015-01-22 15:51:08 -08001414 * else
1415 * we know the value is cindex
Alexander Duyck71e8b672015-03-04 15:04:03 -08001416 *
1417 * This check is safe even if bits == KEYLENGTH due to the
1418 * fact that we can only allocate a node with 32 bits if a
1419 * long is greater than 32 bits.
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001420 */
Alexander Duyck71e8b672015-03-04 15:04:03 -08001421 if (index >= (1ul << n->bits))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001422 break;
Robert Olsson19baf832005-06-21 12:43:18 -07001423
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001424 /* we have found a leaf. Prefixes have already been compared */
1425 if (IS_LEAF(n))
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08001426 goto found;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001427
1428 /* only record pn and cindex if we are going to be chopping
1429 * bits later. Otherwise we are just wasting cycles.
1430 */
Alexander Duyck5405afd2014-12-31 10:57:08 -08001431 if (n->slen > n->pos) {
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001432 pn = n;
1433 cindex = index;
Olof Johansson91b9a272005-08-09 20:24:39 -07001434 }
1435
Alexander Duyck754baf82015-03-06 09:54:14 -08001436 n = get_child_rcu(n, index);
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001437 if (unlikely(!n))
Robert Olsson19baf832005-06-21 12:43:18 -07001438 goto backtrace;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001439 }
1440
1441 /* Step 2: Sort out leaves and begin backtracing for longest prefix */
1442 for (;;) {
1443 /* record the pointer where our next node pointer is stored */
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001444 struct key_vector __rcu **cptr = n->tnode;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001445
1446 /* This test verifies that none of the bits that differ
1447 * between the key and the prefix exist in the region of
1448 * the lsb and higher in the prefix.
1449 */
Alexander Duyck5405afd2014-12-31 10:57:08 -08001450 if (unlikely(prefix_mismatch(key, n)) || (n->slen == n->pos))
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001451 goto backtrace;
1452
1453 /* exit out and process leaf */
1454 if (unlikely(IS_LEAF(n)))
1455 break;
1456
1457 /* Don't bother recording parent info. Since we are in
1458 * prefix match mode we will have to come back to wherever
1459 * we started this traversal anyway
1460 */
1461
1462 while ((n = rcu_dereference(*cptr)) == NULL) {
1463backtrace:
1464#ifdef CONFIG_IP_FIB_TRIE_STATS
1465 if (!n)
1466 this_cpu_inc(stats->null_node_hit);
1467#endif
1468 /* If we are at cindex 0 there are no more bits for
1469 * us to strip at this level so we must ascend back
1470 * up one level to see if there are any more bits to
1471 * be stripped there.
1472 */
1473 while (!cindex) {
1474 t_key pkey = pn->key;
1475
Alexander Duyck88bae712015-03-06 09:54:52 -08001476 /* If we don't have a parent then there is
1477 * nothing for us to do as we do not have any
1478 * further nodes to parse.
1479 */
1480 if (IS_TRIE(pn))
Alexander Duyck345e9b52014-12-31 10:56:24 -08001481 return -EAGAIN;
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001482#ifdef CONFIG_IP_FIB_TRIE_STATS
1483 this_cpu_inc(stats->backtrack);
1484#endif
1485 /* Get Child's index */
Alexander Duyck88bae712015-03-06 09:54:52 -08001486 pn = node_parent_rcu(pn);
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001487 cindex = get_index(pkey, pn);
1488 }
1489
1490 /* strip the least significant bit from the cindex */
1491 cindex &= cindex - 1;
1492
1493 /* grab pointer for next child node */
Alexander Duyck41b489f2015-03-04 15:02:33 -08001494 cptr = &pn->tnode[cindex];
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001495 }
Robert Olsson19baf832005-06-21 12:43:18 -07001496 }
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001497
Robert Olsson19baf832005-06-21 12:43:18 -07001498found:
Alexander Duyck71e8b672015-03-04 15:04:03 -08001499 /* this line carries forward the xor from earlier in the function */
1500 index = key ^ n->key;
1501
Alexander Duyck9f9e6362014-12-31 10:55:54 -08001502 /* Step 3: Process the leaf, if that fails fall back to backtracing */
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001503 hlist_for_each_entry_rcu(fa, &n->leaf, fa_list) {
1504 struct fib_info *fi = fa->fa_info;
1505 int nhsel, err;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001506
Alexander Duycka5829f52016-01-28 13:42:24 -08001507 if ((BITS_PER_LONG > KEYLENGTH) || (fa->fa_slen < KEYLENGTH)) {
1508 if (index >= (1ul << fa->fa_slen))
1509 continue;
1510 }
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001511 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
1512 continue;
1513 if (fi->fib_dead)
1514 continue;
1515 if (fa->fa_info->fib_scope < flp->flowi4_scope)
1516 continue;
1517 fib_alias_accessed(fa);
1518 err = fib_props[fa->fa_type].error;
1519 if (unlikely(err < 0)) {
Alexander Duyck345e9b52014-12-31 10:56:24 -08001520#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001521 this_cpu_inc(stats->semantic_match_passed);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001522#endif
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001523 return err;
1524 }
1525 if (fi->fib_flags & RTNH_F_DEAD)
1526 continue;
1527 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
1528 const struct fib_nh *nh = &fi->fib_nh[nhsel];
Andy Gospodarek0eeb0752015-06-23 13:45:37 -04001529 struct in_device *in_dev = __in_dev_get_rcu(nh->nh_dev);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001530
1531 if (nh->nh_flags & RTNH_F_DEAD)
Alexander Duyck345e9b52014-12-31 10:56:24 -08001532 continue;
Andy Gospodarek0eeb0752015-06-23 13:45:37 -04001533 if (in_dev &&
1534 IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
1535 nh->nh_flags & RTNH_F_LINKDOWN &&
1536 !(fib_flags & FIB_LOOKUP_IGNORE_LINKSTATE))
1537 continue;
David Ahern58189ca2015-09-15 15:10:50 -07001538 if (!(flp->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF)) {
David Ahern613d09b2015-08-13 14:59:02 -06001539 if (flp->flowi4_oif &&
1540 flp->flowi4_oif != nh->nh_oif)
1541 continue;
1542 }
Alexander Duyck345e9b52014-12-31 10:56:24 -08001543
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001544 if (!(fib_flags & FIB_LOOKUP_NOREF))
1545 atomic_inc(&fi->fib_clntref);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001546
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001547 res->prefixlen = KEYLENGTH - fa->fa_slen;
1548 res->nh_sel = nhsel;
1549 res->type = fa->fa_type;
1550 res->scope = fi->fib_scope;
1551 res->fi = fi;
1552 res->table = tb;
1553 res->fa_head = &n->leaf;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001554#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001555 this_cpu_inc(stats->semantic_match_passed);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001556#endif
David Ahernf6d3c192015-08-28 08:42:09 -07001557 trace_fib_table_lookup_nh(nh);
1558
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001559 return err;
Alexander Duyck345e9b52014-12-31 10:56:24 -08001560 }
Alexander Duyck345e9b52014-12-31 10:56:24 -08001561 }
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08001562#ifdef CONFIG_IP_FIB_TRIE_STATS
1563 this_cpu_inc(stats->semantic_match_miss);
1564#endif
Alexander Duyck345e9b52014-12-31 10:56:24 -08001565 goto backtrace;
Robert Olsson19baf832005-06-21 12:43:18 -07001566}
Florian Westphal6fc01432011-08-25 13:46:12 +02001567EXPORT_SYMBOL_GPL(fib_table_lookup);
Robert Olsson19baf832005-06-21 12:43:18 -07001568
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001569static void fib_remove_alias(struct trie *t, struct key_vector *tp,
1570 struct key_vector *l, struct fib_alias *old)
Alexander Duyckd5d64872015-03-04 15:02:18 -08001571{
1572 /* record the location of the previous list_info entry */
1573 struct hlist_node **pprev = old->fa_list.pprev;
1574 struct fib_alias *fa = hlist_entry(pprev, typeof(*fa), fa_list.next);
1575
1576 /* remove the fib_alias from the list */
1577 hlist_del_rcu(&old->fa_list);
1578
1579 /* if we emptied the list this leaf will be freed and we can sort
1580 * out parent suffix lengths as a part of trie_rebalance
1581 */
1582 if (hlist_empty(&l->leaf)) {
Alexander Duycka52ca622016-12-01 07:27:57 -05001583 if (tp->slen == l->slen)
1584 node_pull_suffix(tp, tp->pos);
Alexander Duyck88bae712015-03-06 09:54:52 -08001585 put_child_root(tp, l->key, NULL);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001586 node_free(l);
1587 trie_rebalance(t, tp);
1588 return;
1589 }
1590
1591 /* only access fa if it is pointing at the last valid hlist_node */
1592 if (*pprev)
1593 return;
1594
1595 /* update the trie with the latest suffix length */
1596 l->slen = fa->fa_slen;
Alexander Duyck1a239172016-12-01 07:27:52 -05001597 node_pull_suffix(tp, fa->fa_slen);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001598}
1599
1600/* Caller must hold RTNL. */
Jiri Pirkob90eb752016-09-26 12:52:29 +02001601int fib_table_delete(struct net *net, struct fib_table *tb,
1602 struct fib_config *cfg)
Robert Olsson19baf832005-06-21 12:43:18 -07001603{
1604 struct trie *t = (struct trie *) tb->tb_data;
Robert Olsson19baf832005-06-21 12:43:18 -07001605 struct fib_alias *fa, *fa_to_delete;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001606 struct key_vector *l, *tp;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001607 u8 plen = cfg->fc_dst_len;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001608 u8 slen = KEYLENGTH - plen;
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001609 u8 tos = cfg->fc_tos;
1610 u32 key;
Olof Johansson91b9a272005-08-09 20:24:39 -07001611
Alexander Duyck79e5ad22015-02-25 15:31:51 -08001612 if (plen > KEYLENGTH)
Robert Olsson19baf832005-06-21 12:43:18 -07001613 return -EINVAL;
1614
Thomas Graf4e902c52006-08-17 18:14:52 -07001615 key = ntohl(cfg->fc_dst);
Robert Olsson19baf832005-06-21 12:43:18 -07001616
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001617 if ((plen < KEYLENGTH) && (key << plen))
Robert Olsson19baf832005-06-21 12:43:18 -07001618 return -EINVAL;
1619
Alexander Duyckd4a975e2015-03-04 15:01:59 -08001620 l = fib_find_node(t, &tp, key);
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07001621 if (!l)
Robert Olsson19baf832005-06-21 12:43:18 -07001622 return -ESRCH;
1623
Alexander Duyck0b65bd92015-03-12 14:46:29 -07001624 fa = fib_find_alias(&l->leaf, slen, tos, 0, tb->tb_id);
Robert Olsson19baf832005-06-21 12:43:18 -07001625 if (!fa)
1626 return -ESRCH;
1627
Stephen Hemminger0c7770c2005-08-23 21:59:41 -07001628 pr_debug("Deleting %08x/%d tos=%d t=%p\n", key, plen, tos, t);
Robert Olsson19baf832005-06-21 12:43:18 -07001629
1630 fa_to_delete = NULL;
Alexander Duyck56315f92015-02-25 15:31:31 -08001631 hlist_for_each_entry_from(fa, fa_list) {
Robert Olsson19baf832005-06-21 12:43:18 -07001632 struct fib_info *fi = fa->fa_info;
1633
Alexander Duyck0b65bd92015-03-12 14:46:29 -07001634 if ((fa->fa_slen != slen) ||
1635 (fa->tb_id != tb->tb_id) ||
1636 (fa->fa_tos != tos))
Robert Olsson19baf832005-06-21 12:43:18 -07001637 break;
1638
Thomas Graf4e902c52006-08-17 18:14:52 -07001639 if ((!cfg->fc_type || fa->fa_type == cfg->fc_type) &&
1640 (cfg->fc_scope == RT_SCOPE_NOWHERE ||
David S. Miller37e826c2011-03-24 18:06:47 -07001641 fa->fa_info->fib_scope == cfg->fc_scope) &&
Julian Anastasov74cb3c12011-03-19 12:13:46 +00001642 (!cfg->fc_prefsrc ||
1643 fi->fib_prefsrc == cfg->fc_prefsrc) &&
Thomas Graf4e902c52006-08-17 18:14:52 -07001644 (!cfg->fc_protocol ||
1645 fi->fib_protocol == cfg->fc_protocol) &&
1646 fib_nh_match(cfg, fi) == 0) {
Robert Olsson19baf832005-06-21 12:43:18 -07001647 fa_to_delete = fa;
1648 break;
1649 }
1650 }
1651
Olof Johansson91b9a272005-08-09 20:24:39 -07001652 if (!fa_to_delete)
1653 return -ESRCH;
Robert Olsson19baf832005-06-21 12:43:18 -07001654
Jiri Pirkob90eb752016-09-26 12:52:29 +02001655 call_fib_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, key, plen,
1656 fa_to_delete->fa_info, tos, cfg->fc_type,
1657 tb->tb_id, 0);
Alexander Duyckd5d64872015-03-04 15:02:18 -08001658 rtmsg_fib(RTM_DELROUTE, htonl(key), fa_to_delete, plen, tb->tb_id,
Milan Kocianb8f55832007-05-23 14:55:06 -07001659 &cfg->fc_nlinfo, 0);
Robert Olsson19baf832005-06-21 12:43:18 -07001660
David S. Miller21d8c492011-04-14 14:49:37 -07001661 if (!plen)
1662 tb->tb_num_default--;
1663
Alexander Duyckd5d64872015-03-04 15:02:18 -08001664 fib_remove_alias(t, tp, l, fa_to_delete);
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001665
Alexander Duyckd5d64872015-03-04 15:02:18 -08001666 if (fa_to_delete->fa_state & FA_S_ACCESSED)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001667 rt_cache_flush(cfg->fc_nlinfo.nl_net);
Olof Johansson91b9a272005-08-09 20:24:39 -07001668
Alexander Duyckd5d64872015-03-04 15:02:18 -08001669 fib_release_info(fa_to_delete->fa_info);
1670 alias_free_mem_rcu(fa_to_delete);
Olof Johansson91b9a272005-08-09 20:24:39 -07001671 return 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001672}
1673
Alexander Duyck8be33e92015-03-04 14:59:19 -08001674/* Scan for the next leaf starting at the provided key value */
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001675static struct key_vector *leaf_walk_rcu(struct key_vector **tn, t_key key)
Robert Olsson19baf832005-06-21 12:43:18 -07001676{
Alexander Duyck35c6eda2015-03-06 09:54:08 -08001677 struct key_vector *pn, *n = *tn;
Alexander Duyck8be33e92015-03-04 14:59:19 -08001678 unsigned long cindex;
Robert Olsson19baf832005-06-21 12:43:18 -07001679
Alexander Duyck8be33e92015-03-04 14:59:19 -08001680 /* this loop is meant to try and find the key in the trie */
Alexander Duyck88bae712015-03-06 09:54:52 -08001681 do {
1682 /* record parent and next child index */
1683 pn = n;
Alexander Duyckc2229fe2015-10-27 15:06:45 -07001684 cindex = (key > pn->key) ? get_index(key, pn) : 0;
Alexander Duyck88bae712015-03-06 09:54:52 -08001685
1686 if (cindex >> pn->bits)
1687 break;
1688
1689 /* descend into the next child */
1690 n = get_child_rcu(pn, cindex++);
1691 if (!n)
1692 break;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001693
Alexander Duyck8be33e92015-03-04 14:59:19 -08001694 /* guarantee forward progress on the keys */
1695 if (IS_LEAF(n) && (n->key >= key))
1696 goto found;
Alexander Duyck88bae712015-03-06 09:54:52 -08001697 } while (IS_TNODE(n));
Alexander Duyck8be33e92015-03-04 14:59:19 -08001698
1699 /* this loop will search for the next leaf with a greater key */
Alexander Duyck88bae712015-03-06 09:54:52 -08001700 while (!IS_TRIE(pn)) {
Alexander Duyck8be33e92015-03-04 14:59:19 -08001701 /* if we exhausted the parent node we will need to climb */
1702 if (cindex >= (1ul << pn->bits)) {
1703 t_key pkey = pn->key;
1704
1705 pn = node_parent_rcu(pn);
Alexander Duyck8be33e92015-03-04 14:59:19 -08001706 cindex = get_index(pkey, pn) + 1;
1707 continue;
Robert Olsson19baf832005-06-21 12:43:18 -07001708 }
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001709
Alexander Duyck8be33e92015-03-04 14:59:19 -08001710 /* grab the next available node */
Alexander Duyck754baf82015-03-06 09:54:14 -08001711 n = get_child_rcu(pn, cindex++);
Alexander Duyck8be33e92015-03-04 14:59:19 -08001712 if (!n)
1713 continue;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001714
Alexander Duyck8be33e92015-03-04 14:59:19 -08001715 /* no need to compare keys since we bumped the index */
1716 if (IS_LEAF(n))
1717 goto found;
1718
1719 /* Rescan start scanning in new node */
1720 pn = n;
1721 cindex = 0;
1722 }
1723
1724 *tn = pn;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001725 return NULL; /* Root of trie */
Alexander Duyck8be33e92015-03-04 14:59:19 -08001726found:
1727 /* if we are at the limit for keys just return NULL for the tnode */
Alexander Duyck88bae712015-03-06 09:54:52 -08001728 *tn = pn;
Alexander Duyck8be33e92015-03-04 14:59:19 -08001729 return n;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001730}
1731
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001732static void fib_trie_free(struct fib_table *tb)
1733{
1734 struct trie *t = (struct trie *)tb->tb_data;
1735 struct key_vector *pn = t->kv;
1736 unsigned long cindex = 1;
1737 struct hlist_node *tmp;
1738 struct fib_alias *fa;
1739
1740 /* walk trie in reverse order and free everything */
1741 for (;;) {
1742 struct key_vector *n;
1743
1744 if (!(cindex--)) {
1745 t_key pkey = pn->key;
1746
1747 if (IS_TRIE(pn))
1748 break;
1749
1750 n = pn;
1751 pn = node_parent(pn);
1752
1753 /* drop emptied tnode */
1754 put_child_root(pn, n->key, NULL);
1755 node_free(n);
1756
1757 cindex = get_index(pkey, pn);
1758
1759 continue;
1760 }
1761
1762 /* grab the next available node */
1763 n = get_child(pn, cindex);
1764 if (!n)
1765 continue;
1766
1767 if (IS_TNODE(n)) {
1768 /* record pn and cindex for leaf walking */
1769 pn = n;
1770 cindex = 1ul << n->bits;
1771
1772 continue;
1773 }
1774
1775 hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
1776 hlist_del_rcu(&fa->fa_list);
1777 alias_free_mem_rcu(fa);
1778 }
1779
1780 put_child_root(pn, n->key, NULL);
1781 node_free(n);
1782 }
1783
1784#ifdef CONFIG_IP_FIB_TRIE_STATS
1785 free_percpu(t->stats);
1786#endif
1787 kfree(tb);
1788}
1789
1790struct fib_table *fib_trie_unmerge(struct fib_table *oldtb)
1791{
1792 struct trie *ot = (struct trie *)oldtb->tb_data;
1793 struct key_vector *l, *tp = ot->kv;
1794 struct fib_table *local_tb;
1795 struct fib_alias *fa;
1796 struct trie *lt;
1797 t_key key = 0;
1798
1799 if (oldtb->tb_data == oldtb->__data)
1800 return oldtb;
1801
1802 local_tb = fib_trie_table(RT_TABLE_LOCAL, NULL);
1803 if (!local_tb)
1804 return NULL;
1805
1806 lt = (struct trie *)local_tb->tb_data;
1807
1808 while ((l = leaf_walk_rcu(&tp, key)) != NULL) {
1809 struct key_vector *local_l = NULL, *local_tp;
1810
1811 hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
1812 struct fib_alias *new_fa;
1813
1814 if (local_tb->tb_id != fa->tb_id)
1815 continue;
1816
1817 /* clone fa for new local table */
1818 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
1819 if (!new_fa)
1820 goto out;
1821
1822 memcpy(new_fa, fa, sizeof(*fa));
1823
1824 /* insert clone into table */
1825 if (!local_l)
1826 local_l = fib_find_node(lt, &local_tp, l->key);
1827
1828 if (fib_insert_alias(lt, local_tp, local_l, new_fa,
Alexander Duyck3114cdf2016-11-15 05:46:12 -05001829 NULL, l->key)) {
1830 kmem_cache_free(fn_alias_kmem, new_fa);
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001831 goto out;
Alexander Duyck3114cdf2016-11-15 05:46:12 -05001832 }
Alexander Duyck0ddcf432015-03-06 13:47:00 -08001833 }
1834
1835 /* stop loop if key wrapped back to 0 */
1836 key = l->key + 1;
1837 if (key < l->key)
1838 break;
1839 }
1840
1841 return local_tb;
1842out:
1843 fib_trie_free(local_tb);
1844
1845 return NULL;
1846}
1847
Alexander Duyck3b709332016-11-15 05:46:06 -05001848/* Caller must hold RTNL */
1849void fib_table_flush_external(struct fib_table *tb)
1850{
1851 struct trie *t = (struct trie *)tb->tb_data;
1852 struct key_vector *pn = t->kv;
1853 unsigned long cindex = 1;
1854 struct hlist_node *tmp;
1855 struct fib_alias *fa;
1856
1857 /* walk trie in reverse order */
1858 for (;;) {
1859 unsigned char slen = 0;
1860 struct key_vector *n;
1861
1862 if (!(cindex--)) {
1863 t_key pkey = pn->key;
1864
1865 /* cannot resize the trie vector */
1866 if (IS_TRIE(pn))
1867 break;
1868
Alexander Duycka52ca622016-12-01 07:27:57 -05001869 /* update the suffix to address pulled leaves */
1870 if (pn->slen > pn->pos)
1871 update_suffix(pn);
1872
Alexander Duyck3b709332016-11-15 05:46:06 -05001873 /* resize completed node */
1874 pn = resize(t, pn);
1875 cindex = get_index(pkey, pn);
1876
1877 continue;
1878 }
1879
1880 /* grab the next available node */
1881 n = get_child(pn, cindex);
1882 if (!n)
1883 continue;
1884
1885 if (IS_TNODE(n)) {
1886 /* record pn and cindex for leaf walking */
1887 pn = n;
1888 cindex = 1ul << n->bits;
1889
1890 continue;
1891 }
1892
1893 hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
1894 /* if alias was cloned to local then we just
1895 * need to remove the local copy from main
1896 */
1897 if (tb->tb_id != fa->tb_id) {
1898 hlist_del_rcu(&fa->fa_list);
1899 alias_free_mem_rcu(fa);
1900 continue;
1901 }
1902
1903 /* record local slen */
1904 slen = fa->fa_slen;
1905 }
1906
1907 /* update leaf slen */
1908 n->slen = slen;
1909
1910 if (hlist_empty(&n->leaf)) {
1911 put_child_root(pn, n->key, NULL);
1912 node_free(n);
1913 }
1914 }
1915}
1916
Alexander Duyck8be33e92015-03-04 14:59:19 -08001917/* Caller must hold RTNL. */
Jiri Pirkob90eb752016-09-26 12:52:29 +02001918int fib_table_flush(struct net *net, struct fib_table *tb)
Robert Olsson19baf832005-06-21 12:43:18 -07001919{
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001920 struct trie *t = (struct trie *)tb->tb_data;
Alexander Duyck88bae712015-03-06 09:54:52 -08001921 struct key_vector *pn = t->kv;
1922 unsigned long cindex = 1;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001923 struct hlist_node *tmp;
1924 struct fib_alias *fa;
Stephen Hemminger82cfbb02008-01-22 21:55:32 -08001925 int found = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07001926
Alexander Duyck88bae712015-03-06 09:54:52 -08001927 /* walk trie in reverse order */
1928 for (;;) {
1929 unsigned char slen = 0;
1930 struct key_vector *n;
Robert Olsson19baf832005-06-21 12:43:18 -07001931
Alexander Duyck88bae712015-03-06 09:54:52 -08001932 if (!(cindex--)) {
1933 t_key pkey = pn->key;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001934
Alexander Duyck88bae712015-03-06 09:54:52 -08001935 /* cannot resize the trie vector */
1936 if (IS_TRIE(pn))
1937 break;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001938
Alexander Duycka52ca622016-12-01 07:27:57 -05001939 /* update the suffix to address pulled leaves */
1940 if (pn->slen > pn->pos)
1941 update_suffix(pn);
1942
Alexander Duyck88bae712015-03-06 09:54:52 -08001943 /* resize completed node */
1944 pn = resize(t, pn);
1945 cindex = get_index(pkey, pn);
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001946
Alexander Duyck88bae712015-03-06 09:54:52 -08001947 continue;
1948 }
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001949
Alexander Duyck88bae712015-03-06 09:54:52 -08001950 /* grab the next available node */
1951 n = get_child(pn, cindex);
1952 if (!n)
1953 continue;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001954
Alexander Duyck88bae712015-03-06 09:54:52 -08001955 if (IS_TNODE(n)) {
1956 /* record pn and cindex for leaf walking */
1957 pn = n;
1958 cindex = 1ul << n->bits;
1959
1960 continue;
1961 }
1962
1963 hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
1964 struct fib_info *fi = fa->fa_info;
1965
Ido Schimmel58e3bdd2017-02-09 10:28:38 +01001966 if (!fi || !(fi->fib_flags & RTNH_F_DEAD) ||
1967 tb->tb_id != fa->tb_id) {
Alexander Duyck88bae712015-03-06 09:54:52 -08001968 slen = fa->fa_slen;
1969 continue;
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001970 }
1971
Jiri Pirkob90eb752016-09-26 12:52:29 +02001972 call_fib_entry_notifiers(net, FIB_EVENT_ENTRY_DEL,
1973 n->key,
1974 KEYLENGTH - fa->fa_slen,
1975 fi, fa->fa_tos, fa->fa_type,
1976 tb->tb_id, 0);
Alexander Duyck7289e6d2015-03-04 14:58:19 -08001977 hlist_del_rcu(&fa->fa_list);
1978 fib_release_info(fa->fa_info);
1979 alias_free_mem_rcu(fa);
1980 found++;
Alexander Duyck64c62722015-01-22 15:51:45 -08001981 }
1982
Alexander Duyck88bae712015-03-06 09:54:52 -08001983 /* update leaf slen */
1984 n->slen = slen;
1985
1986 if (hlist_empty(&n->leaf)) {
1987 put_child_root(pn, n->key, NULL);
1988 node_free(n);
Alexander Duyck88bae712015-03-06 09:54:52 -08001989 }
Robert Olsson19baf832005-06-21 12:43:18 -07001990 }
1991
Stephen Hemminger0c7770c2005-08-23 21:59:41 -07001992 pr_debug("trie_flush found=%d\n", found);
Robert Olsson19baf832005-06-21 12:43:18 -07001993 return found;
1994}
1995
Ido Schimmelc3852ef2016-12-03 16:45:07 +01001996static void fib_leaf_notify(struct net *net, struct key_vector *l,
1997 struct fib_table *tb, struct notifier_block *nb,
1998 enum fib_event_type event_type)
1999{
2000 struct fib_alias *fa;
2001
2002 hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
2003 struct fib_info *fi = fa->fa_info;
2004
2005 if (!fi)
2006 continue;
2007
2008 /* local and main table can share the same trie,
2009 * so don't notify twice for the same entry.
2010 */
2011 if (tb->tb_id != fa->tb_id)
2012 continue;
2013
2014 call_fib_entry_notifier(nb, net, event_type, l->key,
2015 KEYLENGTH - fa->fa_slen, fi, fa->fa_tos,
2016 fa->fa_type, fa->tb_id, 0);
2017 }
2018}
2019
2020static void fib_table_notify(struct net *net, struct fib_table *tb,
2021 struct notifier_block *nb,
2022 enum fib_event_type event_type)
2023{
2024 struct trie *t = (struct trie *)tb->tb_data;
2025 struct key_vector *l, *tp = t->kv;
2026 t_key key = 0;
2027
2028 while ((l = leaf_walk_rcu(&tp, key)) != NULL) {
2029 fib_leaf_notify(net, l, tb, nb, event_type);
2030
2031 key = l->key + 1;
2032 /* stop in case of wrap around */
2033 if (key < l->key)
2034 break;
2035 }
2036}
2037
2038static void fib_notify(struct net *net, struct notifier_block *nb,
2039 enum fib_event_type event_type)
2040{
2041 unsigned int h;
2042
2043 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2044 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
2045 struct fib_table *tb;
2046
2047 hlist_for_each_entry_rcu(tb, head, tb_hlist)
2048 fib_table_notify(net, tb, nb, event_type);
2049 }
2050}
2051
Alexander Duycka7e53532015-03-04 15:02:44 -08002052static void __trie_free_rcu(struct rcu_head *head)
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00002053{
Alexander Duycka7e53532015-03-04 15:02:44 -08002054 struct fib_table *tb = container_of(head, struct fib_table, rcu);
Alexander Duyck8274a972014-12-31 10:55:29 -08002055#ifdef CONFIG_IP_FIB_TRIE_STATS
2056 struct trie *t = (struct trie *)tb->tb_data;
2057
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002058 if (tb->tb_data == tb->__data)
2059 free_percpu(t->stats);
Alexander Duyck8274a972014-12-31 10:55:29 -08002060#endif /* CONFIG_IP_FIB_TRIE_STATS */
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00002061 kfree(tb);
2062}
2063
Alexander Duycka7e53532015-03-04 15:02:44 -08002064void fib_free_table(struct fib_table *tb)
2065{
2066 call_rcu(&tb->rcu, __trie_free_rcu);
2067}
2068
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002069static int fn_trie_dump_leaf(struct key_vector *l, struct fib_table *tb,
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002070 struct sk_buff *skb, struct netlink_callback *cb)
Robert Olsson19baf832005-06-21 12:43:18 -07002071{
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002072 __be32 xkey = htonl(l->key);
Robert Olsson19baf832005-06-21 12:43:18 -07002073 struct fib_alias *fa;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002074 int i, s_i;
Robert Olsson19baf832005-06-21 12:43:18 -07002075
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002076 s_i = cb->args[4];
Robert Olsson19baf832005-06-21 12:43:18 -07002077 i = 0;
2078
Robert Olsson2373ce12005-08-25 13:01:29 -07002079 /* rcu_read_lock is hold by caller */
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002080 hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
Robert Olsson19baf832005-06-21 12:43:18 -07002081 if (i < s_i) {
2082 i++;
2083 continue;
2084 }
Robert Olsson19baf832005-06-21 12:43:18 -07002085
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002086 if (tb->tb_id != fa->tb_id) {
2087 i++;
2088 continue;
2089 }
2090
Eric W. Biederman15e47302012-09-07 20:12:54 +00002091 if (fib_dump_info(skb, NETLINK_CB(cb->skb).portid,
Robert Olsson19baf832005-06-21 12:43:18 -07002092 cb->nlh->nlmsg_seq,
2093 RTM_NEWROUTE,
2094 tb->tb_id,
2095 fa->fa_type,
Thomas Grafbe403ea2006-08-17 18:15:17 -07002096 xkey,
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08002097 KEYLENGTH - fa->fa_slen,
Robert Olsson19baf832005-06-21 12:43:18 -07002098 fa->fa_tos,
Stephen Hemminger64347f72008-01-22 21:55:01 -08002099 fa->fa_info, NLM_F_MULTI) < 0) {
Stephen Hemminger71d67e62008-01-31 16:45:47 -08002100 cb->args[4] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07002101 return -1;
2102 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08002103 i++;
Robert Olsson19baf832005-06-21 12:43:18 -07002104 }
Stephen Hemmingera88ee222008-01-22 21:56:11 -08002105
Stephen Hemminger71d67e62008-01-31 16:45:47 -08002106 cb->args[4] = i;
Robert Olsson19baf832005-06-21 12:43:18 -07002107 return skb->len;
2108}
2109
Alexander Duycka7e53532015-03-04 15:02:44 -08002110/* rcu_read_lock needs to be hold by caller from readside */
Stephen Hemminger16c6cf82009-09-20 10:35:36 +00002111int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
2112 struct netlink_callback *cb)
Robert Olsson19baf832005-06-21 12:43:18 -07002113{
Alexander Duyck8be33e92015-03-04 14:59:19 -08002114 struct trie *t = (struct trie *)tb->tb_data;
Alexander Duyck88bae712015-03-06 09:54:52 -08002115 struct key_vector *l, *tp = t->kv;
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08002116 /* Dump starting at last key.
2117 * Note: 0.0.0.0/0 (ie default) is first key.
2118 */
Alexander Duyck8be33e92015-03-04 14:59:19 -08002119 int count = cb->args[2];
2120 t_key key = cb->args[3];
Stephen Hemmingera88ee222008-01-22 21:56:11 -08002121
Alexander Duyck8be33e92015-03-04 14:59:19 -08002122 while ((l = leaf_walk_rcu(&tp, key)) != NULL) {
Stephen Hemmingera88ee222008-01-22 21:56:11 -08002123 if (fn_trie_dump_leaf(l, tb, skb, cb) < 0) {
Alexander Duyck8be33e92015-03-04 14:59:19 -08002124 cb->args[3] = key;
2125 cb->args[2] = count;
Stephen Hemmingera88ee222008-01-22 21:56:11 -08002126 return -1;
Robert Olsson19baf832005-06-21 12:43:18 -07002127 }
Stephen Hemmingerd5ce8a02008-01-22 21:57:22 -08002128
Stephen Hemminger71d67e62008-01-31 16:45:47 -08002129 ++count;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002130 key = l->key + 1;
2131
Stephen Hemminger71d67e62008-01-31 16:45:47 -08002132 memset(&cb->args[4], 0,
2133 sizeof(cb->args) - 4*sizeof(cb->args[0]));
Alexander Duyck8be33e92015-03-04 14:59:19 -08002134
2135 /* stop loop if key wrapped back to 0 */
2136 if (key < l->key)
2137 break;
Robert Olsson19baf832005-06-21 12:43:18 -07002138 }
Alexander Duyck8be33e92015-03-04 14:59:19 -08002139
Alexander Duyck8be33e92015-03-04 14:59:19 -08002140 cb->args[3] = key;
2141 cb->args[2] = count;
2142
Robert Olsson19baf832005-06-21 12:43:18 -07002143 return skb->len;
Robert Olsson19baf832005-06-21 12:43:18 -07002144}
2145
David S. Miller5348ba82011-02-01 15:30:56 -08002146void __init fib_trie_init(void)
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08002147{
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002148 fn_alias_kmem = kmem_cache_create("ip_fib_alias",
2149 sizeof(struct fib_alias),
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -08002150 0, SLAB_PANIC, NULL);
2151
2152 trie_leaf_kmem = kmem_cache_create("ip_fib_trie",
Alexander Duyck41b489f2015-03-04 15:02:33 -08002153 LEAF_SIZE,
Stephen Hemmingerbc3c8c12008-01-22 21:51:50 -08002154 0, SLAB_PANIC, NULL);
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08002155}
Robert Olsson19baf832005-06-21 12:43:18 -07002156
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002157struct fib_table *fib_trie_table(u32 id, struct fib_table *alias)
Robert Olsson19baf832005-06-21 12:43:18 -07002158{
2159 struct fib_table *tb;
2160 struct trie *t;
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002161 size_t sz = sizeof(*tb);
Robert Olsson19baf832005-06-21 12:43:18 -07002162
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002163 if (!alias)
2164 sz += sizeof(struct trie);
2165
2166 tb = kzalloc(sz, GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01002167 if (!tb)
Robert Olsson19baf832005-06-21 12:43:18 -07002168 return NULL;
2169
2170 tb->tb_id = id;
David S. Miller21d8c492011-04-14 14:49:37 -07002171 tb->tb_num_default = 0;
Alexander Duyck0ddcf432015-03-06 13:47:00 -08002172 tb->tb_data = (alias ? alias->__data : tb->__data);
2173
2174 if (alias)
2175 return tb;
Robert Olsson19baf832005-06-21 12:43:18 -07002176
2177 t = (struct trie *) tb->tb_data;
Alexander Duyck88bae712015-03-06 09:54:52 -08002178 t->kv[0].pos = KEYLENGTH;
2179 t->kv[0].slen = KEYLENGTH;
Alexander Duyck8274a972014-12-31 10:55:29 -08002180#ifdef CONFIG_IP_FIB_TRIE_STATS
2181 t->stats = alloc_percpu(struct trie_use_stats);
2182 if (!t->stats) {
2183 kfree(tb);
2184 tb = NULL;
2185 }
2186#endif
Robert Olsson19baf832005-06-21 12:43:18 -07002187
Robert Olsson19baf832005-06-21 12:43:18 -07002188 return tb;
2189}
2190
Robert Olsson19baf832005-06-21 12:43:18 -07002191#ifdef CONFIG_PROC_FS
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002192/* Depth first Trie walk iterator */
2193struct fib_trie_iter {
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002194 struct seq_net_private p;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002195 struct fib_table *tb;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002196 struct key_vector *tnode;
Eric Dumazeta034ee32010-09-09 23:32:28 +00002197 unsigned int index;
2198 unsigned int depth;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002199};
Robert Olsson19baf832005-06-21 12:43:18 -07002200
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002201static struct key_vector *fib_trie_get_next(struct fib_trie_iter *iter)
Robert Olsson19baf832005-06-21 12:43:18 -07002202{
Alexander Duyck98293e82014-12-31 10:56:18 -08002203 unsigned long cindex = iter->index;
Alexander Duyck88bae712015-03-06 09:54:52 -08002204 struct key_vector *pn = iter->tnode;
2205 t_key pkey;
Eric W. Biederman6640e692007-01-24 14:42:04 -08002206
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002207 pr_debug("get_next iter={node=%p index=%d depth=%d}\n",
2208 iter->tnode, iter->index, iter->depth);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002209
Alexander Duyck88bae712015-03-06 09:54:52 -08002210 while (!IS_TRIE(pn)) {
2211 while (cindex < child_length(pn)) {
2212 struct key_vector *n = get_child_rcu(pn, cindex++);
2213
2214 if (!n)
2215 continue;
2216
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002217 if (IS_LEAF(n)) {
Alexander Duyck88bae712015-03-06 09:54:52 -08002218 iter->tnode = pn;
2219 iter->index = cindex;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002220 } else {
2221 /* push down one level */
Alexander Duyckadaf9812014-12-31 10:55:47 -08002222 iter->tnode = n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002223 iter->index = 0;
2224 ++iter->depth;
2225 }
Alexander Duyck88bae712015-03-06 09:54:52 -08002226
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002227 return n;
2228 }
2229
Alexander Duyck88bae712015-03-06 09:54:52 -08002230 /* Current node exhausted, pop back up */
2231 pkey = pn->key;
2232 pn = node_parent_rcu(pn);
2233 cindex = get_index(pkey, pn) + 1;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002234 --iter->depth;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002235 }
2236
Alexander Duyck88bae712015-03-06 09:54:52 -08002237 /* record root node so further searches know we are done */
2238 iter->tnode = pn;
2239 iter->index = 0;
2240
Robert Olsson19baf832005-06-21 12:43:18 -07002241 return NULL;
2242}
2243
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002244static struct key_vector *fib_trie_get_first(struct fib_trie_iter *iter,
2245 struct trie *t)
Robert Olsson19baf832005-06-21 12:43:18 -07002246{
Firo Yangf38b24c2015-06-08 11:54:51 +08002247 struct key_vector *n, *pn;
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08002248
Stephen Hemminger132adf52007-03-08 20:44:43 -08002249 if (!t)
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08002250 return NULL;
2251
Firo Yangf38b24c2015-06-08 11:54:51 +08002252 pn = t->kv;
Alexander Duyck88bae712015-03-06 09:54:52 -08002253 n = rcu_dereference(pn->tnode[0]);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002254 if (!n)
Robert Olsson5ddf0eb2006-03-20 21:34:12 -08002255 return NULL;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002256
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002257 if (IS_TNODE(n)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002258 iter->tnode = n;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002259 iter->index = 0;
2260 iter->depth = 1;
2261 } else {
Alexander Duyck88bae712015-03-06 09:54:52 -08002262 iter->tnode = pn;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002263 iter->index = 0;
2264 iter->depth = 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002265 }
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002266
2267 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07002268}
2269
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002270static void trie_collect_stats(struct trie *t, struct trie_stat *s)
Robert Olsson19baf832005-06-21 12:43:18 -07002271{
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002272 struct key_vector *n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002273 struct fib_trie_iter iter;
Robert Olsson19baf832005-06-21 12:43:18 -07002274
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002275 memset(s, 0, sizeof(*s));
Robert Olsson19baf832005-06-21 12:43:18 -07002276
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002277 rcu_read_lock();
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002278 for (n = fib_trie_get_first(&iter, t); n; n = fib_trie_get_next(&iter)) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002279 if (IS_LEAF(n)) {
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002280 struct fib_alias *fa;
Stephen Hemminger93672292008-01-22 21:54:05 -08002281
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002282 s->leaves++;
2283 s->totdepth += iter.depth;
2284 if (iter.depth > s->maxdepth)
2285 s->maxdepth = iter.depth;
Stephen Hemminger93672292008-01-22 21:54:05 -08002286
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002287 hlist_for_each_entry_rcu(fa, &n->leaf, fa_list)
Stephen Hemminger93672292008-01-22 21:54:05 -08002288 ++s->prefixes;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002289 } else {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002290 s->tnodes++;
Alexander Duyckadaf9812014-12-31 10:55:47 -08002291 if (n->bits < MAX_STAT_DEPTH)
2292 s->nodesizes[n->bits]++;
Alexander Duyck6e22d172015-03-06 09:54:39 -08002293 s->nullpointers += tn_info(n)->empty_children;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002294 }
2295 }
2296 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07002297}
2298
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07002299/*
Robert Olsson19baf832005-06-21 12:43:18 -07002300 * This outputs /proc/net/fib_triestats
Robert Olsson19baf832005-06-21 12:43:18 -07002301 */
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002302static void trie_show_stats(struct seq_file *seq, struct trie_stat *stat)
Robert Olsson19baf832005-06-21 12:43:18 -07002303{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002304 unsigned int i, max, pointers, bytes, avdepth;
Robert Olsson19baf832005-06-21 12:43:18 -07002305
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002306 if (stat->leaves)
2307 avdepth = stat->totdepth*100 / stat->leaves;
2308 else
2309 avdepth = 0;
Robert Olsson19baf832005-06-21 12:43:18 -07002310
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002311 seq_printf(seq, "\tAver depth: %u.%02d\n",
2312 avdepth / 100, avdepth % 100);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002313 seq_printf(seq, "\tMax depth: %u\n", stat->maxdepth);
Robert Olsson19baf832005-06-21 12:43:18 -07002314
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002315 seq_printf(seq, "\tLeaves: %u\n", stat->leaves);
Alexander Duyck41b489f2015-03-04 15:02:33 -08002316 bytes = LEAF_SIZE * stat->leaves;
Stephen Hemminger93672292008-01-22 21:54:05 -08002317
2318 seq_printf(seq, "\tPrefixes: %u\n", stat->prefixes);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002319 bytes += sizeof(struct fib_alias) * stat->prefixes;
Stephen Hemminger93672292008-01-22 21:54:05 -08002320
Stephen Hemminger187b5182008-01-12 20:55:55 -08002321 seq_printf(seq, "\tInternal nodes: %u\n\t", stat->tnodes);
Alexander Duyck41b489f2015-03-04 15:02:33 -08002322 bytes += TNODE_SIZE(0) * stat->tnodes;
Robert Olsson19baf832005-06-21 12:43:18 -07002323
Robert Olsson06ef9212006-03-20 21:35:01 -08002324 max = MAX_STAT_DEPTH;
2325 while (max > 0 && stat->nodesizes[max-1] == 0)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002326 max--;
Robert Olsson19baf832005-06-21 12:43:18 -07002327
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002328 pointers = 0;
Jerry Snitselaarf585a992013-07-22 12:01:58 -07002329 for (i = 1; i < max; i++)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002330 if (stat->nodesizes[i] != 0) {
Stephen Hemminger187b5182008-01-12 20:55:55 -08002331 seq_printf(seq, " %u: %u", i, stat->nodesizes[i]);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002332 pointers += (1<<i) * stat->nodesizes[i];
2333 }
2334 seq_putc(seq, '\n');
Stephen Hemminger187b5182008-01-12 20:55:55 -08002335 seq_printf(seq, "\tPointers: %u\n", pointers);
Robert Olsson19baf832005-06-21 12:43:18 -07002336
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002337 bytes += sizeof(struct key_vector *) * pointers;
Stephen Hemminger187b5182008-01-12 20:55:55 -08002338 seq_printf(seq, "Null ptrs: %u\n", stat->nullpointers);
2339 seq_printf(seq, "Total size: %u kB\n", (bytes + 1023) / 1024);
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002340}
Robert Olsson19baf832005-06-21 12:43:18 -07002341
2342#ifdef CONFIG_IP_FIB_TRIE_STATS
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002343static void trie_show_usage(struct seq_file *seq,
Alexander Duyck8274a972014-12-31 10:55:29 -08002344 const struct trie_use_stats __percpu *stats)
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002345{
Alexander Duyck8274a972014-12-31 10:55:29 -08002346 struct trie_use_stats s = { 0 };
2347 int cpu;
2348
2349 /* loop through all of the CPUs and gather up the stats */
2350 for_each_possible_cpu(cpu) {
2351 const struct trie_use_stats *pcpu = per_cpu_ptr(stats, cpu);
2352
2353 s.gets += pcpu->gets;
2354 s.backtrack += pcpu->backtrack;
2355 s.semantic_match_passed += pcpu->semantic_match_passed;
2356 s.semantic_match_miss += pcpu->semantic_match_miss;
2357 s.null_node_hit += pcpu->null_node_hit;
2358 s.resize_node_skipped += pcpu->resize_node_skipped;
2359 }
2360
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002361 seq_printf(seq, "\nCounters:\n---------\n");
Alexander Duyck8274a972014-12-31 10:55:29 -08002362 seq_printf(seq, "gets = %u\n", s.gets);
2363 seq_printf(seq, "backtracks = %u\n", s.backtrack);
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002364 seq_printf(seq, "semantic match passed = %u\n",
Alexander Duyck8274a972014-12-31 10:55:29 -08002365 s.semantic_match_passed);
2366 seq_printf(seq, "semantic match miss = %u\n", s.semantic_match_miss);
2367 seq_printf(seq, "null node hit= %u\n", s.null_node_hit);
2368 seq_printf(seq, "skipped node resize = %u\n\n", s.resize_node_skipped);
Robert Olsson19baf832005-06-21 12:43:18 -07002369}
Stephen Hemminger66a2f7f2008-01-12 21:23:17 -08002370#endif /* CONFIG_IP_FIB_TRIE_STATS */
2371
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002372static void fib_table_print(struct seq_file *seq, struct fib_table *tb)
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002373{
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002374 if (tb->tb_id == RT_TABLE_LOCAL)
2375 seq_puts(seq, "Local:\n");
2376 else if (tb->tb_id == RT_TABLE_MAIN)
2377 seq_puts(seq, "Main:\n");
2378 else
2379 seq_printf(seq, "Id %d:\n", tb->tb_id);
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002380}
Robert Olsson19baf832005-06-21 12:43:18 -07002381
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002382
Robert Olsson19baf832005-06-21 12:43:18 -07002383static int fib_triestat_seq_show(struct seq_file *seq, void *v)
2384{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002385 struct net *net = (struct net *)seq->private;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002386 unsigned int h;
Eric W. Biederman877a9bf2007-12-07 00:47:47 -08002387
Stephen Hemmingerd717a9a2008-01-14 23:11:54 -08002388 seq_printf(seq,
Stephen Hemmingera07f5f52008-01-22 21:53:36 -08002389 "Basic info: size of leaf:"
2390 " %Zd bytes, size of tnode: %Zd bytes.\n",
Alexander Duyck41b489f2015-03-04 15:02:33 -08002391 LEAF_SIZE, TNODE_SIZE(0));
Olof Johansson91b9a272005-08-09 20:24:39 -07002392
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002393 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2394 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002395 struct fib_table *tb;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002396
Sasha Levinb67bfe02013-02-27 17:06:00 -08002397 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002398 struct trie *t = (struct trie *) tb->tb_data;
2399 struct trie_stat stat;
2400
2401 if (!t)
2402 continue;
2403
2404 fib_table_print(seq, tb);
2405
2406 trie_collect_stats(t, &stat);
2407 trie_show_stats(seq, &stat);
2408#ifdef CONFIG_IP_FIB_TRIE_STATS
Alexander Duyck8274a972014-12-31 10:55:29 -08002409 trie_show_usage(seq, t->stats);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002410#endif
2411 }
2412 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002413
Robert Olsson19baf832005-06-21 12:43:18 -07002414 return 0;
2415}
2416
Robert Olsson19baf832005-06-21 12:43:18 -07002417static int fib_triestat_seq_open(struct inode *inode, struct file *file)
2418{
Pavel Emelyanovde05c552008-07-18 04:07:21 -07002419 return single_open_net(inode, file, fib_triestat_seq_show);
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002420}
2421
Arjan van de Ven9a321442007-02-12 00:55:35 -08002422static const struct file_operations fib_triestat_fops = {
Stephen Hemmingerc877efb2005-07-19 14:01:51 -07002423 .owner = THIS_MODULE,
2424 .open = fib_triestat_seq_open,
2425 .read = seq_read,
2426 .llseek = seq_lseek,
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -07002427 .release = single_release_net,
Robert Olsson19baf832005-06-21 12:43:18 -07002428};
2429
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002430static struct key_vector *fib_trie_get_idx(struct seq_file *seq, loff_t pos)
Robert Olsson19baf832005-06-21 12:43:18 -07002431{
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002432 struct fib_trie_iter *iter = seq->private;
2433 struct net *net = seq_file_net(seq);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002434 loff_t idx = 0;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002435 unsigned int h;
Robert Olsson19baf832005-06-21 12:43:18 -07002436
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002437 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2438 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002439 struct fib_table *tb;
2440
Sasha Levinb67bfe02013-02-27 17:06:00 -08002441 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002442 struct key_vector *n;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002443
2444 for (n = fib_trie_get_first(iter,
2445 (struct trie *) tb->tb_data);
2446 n; n = fib_trie_get_next(iter))
2447 if (pos == idx++) {
2448 iter->tb = tb;
2449 return n;
2450 }
2451 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002452 }
Robert Olsson19baf832005-06-21 12:43:18 -07002453
Robert Olsson19baf832005-06-21 12:43:18 -07002454 return NULL;
2455}
2456
2457static void *fib_trie_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08002458 __acquires(RCU)
Robert Olsson19baf832005-06-21 12:43:18 -07002459{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002460 rcu_read_lock();
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002461 return fib_trie_get_idx(seq, *pos);
Robert Olsson19baf832005-06-21 12:43:18 -07002462}
2463
2464static void *fib_trie_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2465{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002466 struct fib_trie_iter *iter = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002467 struct net *net = seq_file_net(seq);
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002468 struct fib_table *tb = iter->tb;
2469 struct hlist_node *tb_node;
2470 unsigned int h;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002471 struct key_vector *n;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002472
Robert Olsson19baf832005-06-21 12:43:18 -07002473 ++*pos;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002474 /* next node in same table */
2475 n = fib_trie_get_next(iter);
2476 if (n)
2477 return n;
Olof Johansson91b9a272005-08-09 20:24:39 -07002478
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002479 /* walk rest of this hash chain */
2480 h = tb->tb_id & (FIB_TABLE_HASHSZ - 1);
Eric Dumazet0a5c0472011-03-31 01:51:35 -07002481 while ((tb_node = rcu_dereference(hlist_next_rcu(&tb->tb_hlist)))) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002482 tb = hlist_entry(tb_node, struct fib_table, tb_hlist);
2483 n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
2484 if (n)
2485 goto found;
2486 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002487
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002488 /* new hash chain */
2489 while (++h < FIB_TABLE_HASHSZ) {
2490 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002491 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002492 n = fib_trie_get_first(iter, (struct trie *) tb->tb_data);
2493 if (n)
2494 goto found;
2495 }
2496 }
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002497 return NULL;
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002498
2499found:
2500 iter->tb = tb;
2501 return n;
Robert Olsson19baf832005-06-21 12:43:18 -07002502}
2503
2504static void fib_trie_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerc95aaf92008-01-12 21:25:02 -08002505 __releases(RCU)
Robert Olsson19baf832005-06-21 12:43:18 -07002506{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002507 rcu_read_unlock();
Robert Olsson19baf832005-06-21 12:43:18 -07002508}
2509
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002510static void seq_indent(struct seq_file *seq, int n)
2511{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002512 while (n-- > 0)
2513 seq_puts(seq, " ");
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002514}
Robert Olsson19baf832005-06-21 12:43:18 -07002515
Eric Dumazet28d36e32008-01-14 23:09:56 -08002516static inline const char *rtn_scope(char *buf, size_t len, enum rt_scope_t s)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002517{
Stephen Hemminger132adf52007-03-08 20:44:43 -08002518 switch (s) {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002519 case RT_SCOPE_UNIVERSE: return "universe";
2520 case RT_SCOPE_SITE: return "site";
2521 case RT_SCOPE_LINK: return "link";
2522 case RT_SCOPE_HOST: return "host";
2523 case RT_SCOPE_NOWHERE: return "nowhere";
2524 default:
Eric Dumazet28d36e32008-01-14 23:09:56 -08002525 snprintf(buf, len, "scope=%d", s);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002526 return buf;
2527 }
2528}
2529
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -07002530static const char *const rtn_type_names[__RTN_MAX] = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002531 [RTN_UNSPEC] = "UNSPEC",
2532 [RTN_UNICAST] = "UNICAST",
2533 [RTN_LOCAL] = "LOCAL",
2534 [RTN_BROADCAST] = "BROADCAST",
2535 [RTN_ANYCAST] = "ANYCAST",
2536 [RTN_MULTICAST] = "MULTICAST",
2537 [RTN_BLACKHOLE] = "BLACKHOLE",
2538 [RTN_UNREACHABLE] = "UNREACHABLE",
2539 [RTN_PROHIBIT] = "PROHIBIT",
2540 [RTN_THROW] = "THROW",
2541 [RTN_NAT] = "NAT",
2542 [RTN_XRESOLVE] = "XRESOLVE",
2543};
2544
Eric Dumazeta034ee32010-09-09 23:32:28 +00002545static inline const char *rtn_type(char *buf, size_t len, unsigned int t)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002546{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002547 if (t < __RTN_MAX && rtn_type_names[t])
2548 return rtn_type_names[t];
Eric Dumazet28d36e32008-01-14 23:09:56 -08002549 snprintf(buf, len, "type %u", t);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002550 return buf;
2551}
2552
2553/* Pretty print the trie */
Robert Olsson19baf832005-06-21 12:43:18 -07002554static int fib_trie_seq_show(struct seq_file *seq, void *v)
2555{
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002556 const struct fib_trie_iter *iter = seq->private;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002557 struct key_vector *n = v;
Robert Olsson19baf832005-06-21 12:43:18 -07002558
Alexander Duyck88bae712015-03-06 09:54:52 -08002559 if (IS_TRIE(node_parent_rcu(n)))
Stephen Hemminger3d3b2d22008-03-23 22:43:56 -07002560 fib_table_print(seq, iter->tb);
Robert Olsson095b8502007-01-26 19:06:01 -08002561
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002562 if (IS_TNODE(n)) {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002563 __be32 prf = htonl(n->key);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002564
Alexander Duycke9b44012014-12-31 10:56:12 -08002565 seq_indent(seq, iter->depth-1);
2566 seq_printf(seq, " +-- %pI4/%zu %u %u %u\n",
2567 &prf, KEYLENGTH - n->pos - n->bits, n->bits,
Alexander Duyck6e22d172015-03-06 09:54:39 -08002568 tn_info(n)->full_children,
2569 tn_info(n)->empty_children);
Olof Johansson91b9a272005-08-09 20:24:39 -07002570 } else {
Alexander Duyckadaf9812014-12-31 10:55:47 -08002571 __be32 val = htonl(n->key);
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002572 struct fib_alias *fa;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002573
2574 seq_indent(seq, iter->depth);
Harvey Harrison673d57e2008-10-31 00:53:57 -07002575 seq_printf(seq, " |-- %pI4\n", &val);
Eric Dumazet28d36e32008-01-14 23:09:56 -08002576
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002577 hlist_for_each_entry_rcu(fa, &n->leaf, fa_list) {
2578 char buf1[32], buf2[32];
Eric Dumazet28d36e32008-01-14 23:09:56 -08002579
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002580 seq_indent(seq, iter->depth + 1);
2581 seq_printf(seq, " /%zu %s %s",
2582 KEYLENGTH - fa->fa_slen,
2583 rtn_scope(buf1, sizeof(buf1),
2584 fa->fa_info->fib_scope),
2585 rtn_type(buf2, sizeof(buf2),
2586 fa->fa_type));
2587 if (fa->fa_tos)
2588 seq_printf(seq, " tos=%d", fa->fa_tos);
2589 seq_putc(seq, '\n');
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002590 }
Robert Olsson19baf832005-06-21 12:43:18 -07002591 }
2592
2593 return 0;
2594}
2595
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002596static const struct seq_operations fib_trie_seq_ops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002597 .start = fib_trie_seq_start,
2598 .next = fib_trie_seq_next,
2599 .stop = fib_trie_seq_stop,
2600 .show = fib_trie_seq_show,
Robert Olsson19baf832005-06-21 12:43:18 -07002601};
2602
2603static int fib_trie_seq_open(struct inode *inode, struct file *file)
2604{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002605 return seq_open_net(inode, file, &fib_trie_seq_ops,
2606 sizeof(struct fib_trie_iter));
Robert Olsson19baf832005-06-21 12:43:18 -07002607}
2608
Arjan van de Ven9a321442007-02-12 00:55:35 -08002609static const struct file_operations fib_trie_fops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002610 .owner = THIS_MODULE,
2611 .open = fib_trie_seq_open,
2612 .read = seq_read,
2613 .llseek = seq_lseek,
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002614 .release = seq_release_net,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002615};
2616
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002617struct fib_route_iter {
2618 struct seq_net_private p;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002619 struct fib_table *main_tb;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002620 struct key_vector *tnode;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002621 loff_t pos;
2622 t_key key;
2623};
2624
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002625static struct key_vector *fib_route_get_idx(struct fib_route_iter *iter,
2626 loff_t pos)
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002627{
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002628 struct key_vector *l, **tp = &iter->tnode;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002629 t_key key;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002630
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002631 /* use cached location of previously found key */
Alexander Duyck8be33e92015-03-04 14:59:19 -08002632 if (iter->pos > 0 && pos >= iter->pos) {
Alexander Duyck8be33e92015-03-04 14:59:19 -08002633 key = iter->key;
2634 } else {
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002635 iter->pos = 1;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002636 key = 0;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002637 }
2638
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002639 pos -= iter->pos;
2640
2641 while ((l = leaf_walk_rcu(tp, key)) && (pos-- > 0)) {
Alexander Duyck8be33e92015-03-04 14:59:19 -08002642 key = l->key + 1;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002643 iter->pos++;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002644 l = NULL;
2645
2646 /* handle unlikely case of a key wrap */
2647 if (!key)
2648 break;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002649 }
2650
2651 if (l)
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002652 iter->key = l->key; /* remember it */
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002653 else
2654 iter->pos = 0; /* forget it */
2655
2656 return l;
2657}
2658
2659static void *fib_route_seq_start(struct seq_file *seq, loff_t *pos)
2660 __acquires(RCU)
2661{
2662 struct fib_route_iter *iter = seq->private;
2663 struct fib_table *tb;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002664 struct trie *t;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002665
2666 rcu_read_lock();
Alexander Duyck8be33e92015-03-04 14:59:19 -08002667
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002668 tb = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN);
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002669 if (!tb)
2670 return NULL;
2671
Alexander Duyck8be33e92015-03-04 14:59:19 -08002672 iter->main_tb = tb;
David Forster94d9f1c2016-08-03 15:13:01 +01002673 t = (struct trie *)tb->tb_data;
2674 iter->tnode = t->kv;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002675
2676 if (*pos != 0)
2677 return fib_route_get_idx(iter, *pos);
2678
Alexander Duyck8be33e92015-03-04 14:59:19 -08002679 iter->pos = 0;
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002680 iter->key = KEY_MAX;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002681
2682 return SEQ_START_TOKEN;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002683}
2684
2685static void *fib_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2686{
2687 struct fib_route_iter *iter = seq->private;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002688 struct key_vector *l = NULL;
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002689 t_key key = iter->key + 1;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002690
2691 ++*pos;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002692
2693 /* only allow key of 0 for start of sequence */
2694 if ((v == SEQ_START_TOKEN) || key)
2695 l = leaf_walk_rcu(&iter->tnode, key);
2696
2697 if (l) {
Alexander Duyckfd0285a2016-11-04 15:11:57 -04002698 iter->key = l->key;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002699 iter->pos++;
Alexander Duyck8be33e92015-03-04 14:59:19 -08002700 } else {
2701 iter->pos = 0;
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002702 }
2703
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002704 return l;
2705}
2706
2707static void fib_route_seq_stop(struct seq_file *seq, void *v)
2708 __releases(RCU)
2709{
2710 rcu_read_unlock();
2711}
2712
Eric Dumazeta034ee32010-09-09 23:32:28 +00002713static unsigned int fib_flag_trans(int type, __be32 mask, const struct fib_info *fi)
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002714{
Eric Dumazeta034ee32010-09-09 23:32:28 +00002715 unsigned int flags = 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002716
Eric Dumazeta034ee32010-09-09 23:32:28 +00002717 if (type == RTN_UNREACHABLE || type == RTN_PROHIBIT)
2718 flags = RTF_REJECT;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002719 if (fi && fi->fib_nh->nh_gw)
2720 flags |= RTF_GATEWAY;
Al Viro32ab5f82006-09-26 22:21:45 -07002721 if (mask == htonl(0xFFFFFFFF))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002722 flags |= RTF_HOST;
2723 flags |= RTF_UP;
2724 return flags;
2725}
2726
2727/*
2728 * This outputs /proc/net/route.
2729 * The format of the file is not supposed to be changed
Eric Dumazeta034ee32010-09-09 23:32:28 +00002730 * and needs to be same as fib_hash output to avoid breaking
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002731 * legacy utilities
2732 */
2733static int fib_route_seq_show(struct seq_file *seq, void *v)
2734{
Alexander Duyck654eff42015-03-11 16:36:08 -07002735 struct fib_route_iter *iter = seq->private;
2736 struct fib_table *tb = iter->main_tb;
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002737 struct fib_alias *fa;
Alexander Duyck35c6eda2015-03-06 09:54:08 -08002738 struct key_vector *l = v;
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08002739 __be32 prefix;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002740
2741 if (v == SEQ_START_TOKEN) {
2742 seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
2743 "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
2744 "\tWindow\tIRTT");
2745 return 0;
2746 }
2747
Alexander Duyck9b6ebad2015-02-25 15:31:44 -08002748 prefix = htonl(l->key);
2749
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002750 hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
2751 const struct fib_info *fi = fa->fa_info;
2752 __be32 mask = inet_make_mask(KEYLENGTH - fa->fa_slen);
2753 unsigned int flags = fib_flag_trans(fa->fa_type, mask, fi);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002754
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002755 if ((fa->fa_type == RTN_BROADCAST) ||
2756 (fa->fa_type == RTN_MULTICAST))
2757 continue;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002758
Alexander Duyck654eff42015-03-11 16:36:08 -07002759 if (fa->tb_id != tb->tb_id)
2760 continue;
2761
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002762 seq_setwidth(seq, 127);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002763
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002764 if (fi)
2765 seq_printf(seq,
2766 "%s\t%08X\t%08X\t%04X\t%d\t%u\t"
2767 "%d\t%08X\t%d\t%u\t%u",
2768 fi->fib_dev ? fi->fib_dev->name : "*",
2769 prefix,
2770 fi->fib_nh->nh_gw, flags, 0, 0,
2771 fi->fib_priority,
2772 mask,
2773 (fi->fib_advmss ?
2774 fi->fib_advmss + 40 : 0),
2775 fi->fib_window,
2776 fi->fib_rtt >> 3);
2777 else
2778 seq_printf(seq,
2779 "*\t%08X\t%08X\t%04X\t%d\t%u\t"
2780 "%d\t%08X\t%d\t%u\t%u",
2781 prefix, 0, flags, 0, 0, 0,
2782 mask, 0, 0, 0);
Tetsuo Handa652586d2013-11-14 14:31:57 -08002783
Alexander Duyck79e5ad22015-02-25 15:31:51 -08002784 seq_pad(seq, '\n');
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002785 }
2786
2787 return 0;
2788}
2789
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002790static const struct seq_operations fib_route_seq_ops = {
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002791 .start = fib_route_seq_start,
2792 .next = fib_route_seq_next,
2793 .stop = fib_route_seq_stop,
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002794 .show = fib_route_seq_show,
2795};
2796
2797static int fib_route_seq_open(struct inode *inode, struct file *file)
2798{
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002799 return seq_open_net(inode, file, &fib_route_seq_ops,
Stephen Hemminger8315f5d2008-02-11 21:14:39 -08002800 sizeof(struct fib_route_iter));
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002801}
2802
Arjan van de Ven9a321442007-02-12 00:55:35 -08002803static const struct file_operations fib_route_fops = {
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002804 .owner = THIS_MODULE,
2805 .open = fib_route_seq_open,
2806 .read = seq_read,
2807 .llseek = seq_lseek,
Denis V. Lunev1c340b22008-01-10 03:27:17 -08002808 .release = seq_release_net,
Robert Olsson19baf832005-06-21 12:43:18 -07002809};
2810
Denis V. Lunev61a02652008-01-10 03:21:09 -08002811int __net_init fib_proc_init(struct net *net)
Robert Olsson19baf832005-06-21 12:43:18 -07002812{
Gao fengd4beaa62013-02-18 01:34:54 +00002813 if (!proc_create("fib_trie", S_IRUGO, net->proc_net, &fib_trie_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002814 goto out1;
2815
Gao fengd4beaa62013-02-18 01:34:54 +00002816 if (!proc_create("fib_triestat", S_IRUGO, net->proc_net,
2817 &fib_triestat_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002818 goto out2;
2819
Gao fengd4beaa62013-02-18 01:34:54 +00002820 if (!proc_create("route", S_IRUGO, net->proc_net, &fib_route_fops))
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002821 goto out3;
2822
Robert Olsson19baf832005-06-21 12:43:18 -07002823 return 0;
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002824
2825out3:
Gao fengece31ff2013-02-18 01:34:56 +00002826 remove_proc_entry("fib_triestat", net->proc_net);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002827out2:
Gao fengece31ff2013-02-18 01:34:56 +00002828 remove_proc_entry("fib_trie", net->proc_net);
Stephen Hemmingercb7b5932005-09-09 13:35:42 -07002829out1:
2830 return -ENOMEM;
Robert Olsson19baf832005-06-21 12:43:18 -07002831}
2832
Denis V. Lunev61a02652008-01-10 03:21:09 -08002833void __net_exit fib_proc_exit(struct net *net)
Robert Olsson19baf832005-06-21 12:43:18 -07002834{
Gao fengece31ff2013-02-18 01:34:56 +00002835 remove_proc_entry("fib_trie", net->proc_net);
2836 remove_proc_entry("fib_triestat", net->proc_net);
2837 remove_proc_entry("route", net->proc_net);
Robert Olsson19baf832005-06-21 12:43:18 -07002838}
2839
2840#endif /* CONFIG_PROC_FS */