Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1 | /* |
| 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 Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 10 | * Jens Laas <jens.laas@data.slu.se> Swedish University of |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 11 | * Agricultural Sciences. |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 12 | * |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 13 | * Hans Liss <hans.liss@its.uu.se> Uppsala Universitet |
| 14 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 15 | * This work is based on the LPC-trie which is originally described in: |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 16 | * |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 17 | * An experimental study of compression methods for dynamic tries |
| 18 | * Stefan Nilsson and Matti Tikkanen. Algorithmica, 33(1):19-33, 2002. |
Justin P. Mattock | 631dd1a | 2010-10-18 11:03:14 +0200 | [diff] [blame] | 19 | * http://www.csc.kth.se/~snilsson/software/dyntrie2/ |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 20 | * |
| 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 Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 25 | * |
| 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 Olsson | fd96625 | 2005-12-22 11:25:10 -0800 | [diff] [blame] | 42 | * |
| 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 Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 49 | */ |
| 50 | |
Jens Låås | 80b71b8 | 2009-08-28 23:57:15 -0700 | [diff] [blame] | 51 | #define VERSION "0.409" |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 52 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 53 | #include <asm/uaccess.h> |
Jiri Slaby | 1977f03 | 2007-10-18 23:40:25 -0700 | [diff] [blame] | 54 | #include <linux/bitops.h> |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 55 | #include <linux/types.h> |
| 56 | #include <linux/kernel.h> |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 57 | #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 Hemminger | cd8787a | 2006-01-03 14:38:34 -0800 | [diff] [blame] | 64 | #include <linux/inetdevice.h> |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 65 | #include <linux/netdevice.h> |
| 66 | #include <linux/if_arp.h> |
| 67 | #include <linux/proc_fs.h> |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 68 | #include <linux/rcupdate.h> |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 69 | #include <linux/skbuff.h> |
| 70 | #include <linux/netlink.h> |
| 71 | #include <linux/init.h> |
| 72 | #include <linux/list.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 73 | #include <linux/slab.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 74 | #include <linux/export.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 75 | #include <net/net_namespace.h> |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 76 | #include <net/ip.h> |
| 77 | #include <net/protocol.h> |
| 78 | #include <net/route.h> |
| 79 | #include <net/tcp.h> |
| 80 | #include <net/sock.h> |
| 81 | #include <net/ip_fib.h> |
| 82 | #include "fib_lookup.h" |
| 83 | |
Robert Olsson | 06ef921 | 2006-03-20 21:35:01 -0800 | [diff] [blame] | 84 | #define MAX_STAT_DEPTH 32 |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 85 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 86 | #define KEYLENGTH (8*sizeof(t_key)) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 87 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 88 | typedef unsigned int t_key; |
| 89 | |
Alexander Duyck | 64c9b6f | 2014-12-31 10:55:35 -0800 | [diff] [blame] | 90 | #define IS_TNODE(n) ((n)->bits) |
| 91 | #define IS_LEAF(n) (!(n)->bits) |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 92 | |
Alexander Duyck | e9b4401 | 2014-12-31 10:56:12 -0800 | [diff] [blame] | 93 | #define get_index(_key, _kv) (((_key) ^ (_kv)->key) >> (_kv)->pos) |
Alexander Duyck | 9f9e636 | 2014-12-31 10:55:54 -0800 | [diff] [blame] | 94 | |
Alexander Duyck | 64c9b6f | 2014-12-31 10:55:35 -0800 | [diff] [blame] | 95 | struct tnode { |
| 96 | t_key key; |
| 97 | unsigned char bits; /* 2log(KEYLENGTH) bits needed */ |
| 98 | unsigned char pos; /* 2log(KEYLENGTH) bits needed */ |
| 99 | struct tnode __rcu *parent; |
Alexander Duyck | 37fd30f | 2014-12-31 10:55:41 -0800 | [diff] [blame] | 100 | struct rcu_head rcu; |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 101 | union { |
| 102 | /* The fields in this struct are valid if bits > 0 (TNODE) */ |
| 103 | struct { |
| 104 | unsigned int full_children; /* KEYLENGTH bits needed */ |
| 105 | unsigned int empty_children; /* KEYLENGTH bits needed */ |
| 106 | struct tnode __rcu *child[0]; |
| 107 | }; |
| 108 | /* This list pointer if valid if bits == 0 (LEAF) */ |
| 109 | struct hlist_head list; |
| 110 | }; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | struct leaf_info { |
| 114 | struct hlist_node hlist; |
| 115 | int plen; |
Eric Dumazet | 5c74501 | 2011-07-18 03:16:33 +0000 | [diff] [blame] | 116 | u32 mask_plen; /* ntohl(inet_make_mask(plen)) */ |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 117 | struct list_head falh; |
Eric Dumazet | 5c74501 | 2011-07-18 03:16:33 +0000 | [diff] [blame] | 118 | struct rcu_head rcu; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 121 | #ifdef CONFIG_IP_FIB_TRIE_STATS |
| 122 | struct trie_use_stats { |
| 123 | unsigned int gets; |
| 124 | unsigned int backtrack; |
| 125 | unsigned int semantic_match_passed; |
| 126 | unsigned int semantic_match_miss; |
| 127 | unsigned int null_node_hit; |
Robert Olsson | 2f36895 | 2005-07-05 15:02:40 -0700 | [diff] [blame] | 128 | unsigned int resize_node_skipped; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 129 | }; |
| 130 | #endif |
| 131 | |
| 132 | struct trie_stat { |
| 133 | unsigned int totdepth; |
| 134 | unsigned int maxdepth; |
| 135 | unsigned int tnodes; |
| 136 | unsigned int leaves; |
| 137 | unsigned int nullpointers; |
Stephen Hemminger | 9367229 | 2008-01-22 21:54:05 -0800 | [diff] [blame] | 138 | unsigned int prefixes; |
Robert Olsson | 06ef921 | 2006-03-20 21:35:01 -0800 | [diff] [blame] | 139 | unsigned int nodesizes[MAX_STAT_DEPTH]; |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 140 | }; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 141 | |
| 142 | struct trie { |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 143 | struct tnode __rcu *trie; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 144 | #ifdef CONFIG_IP_FIB_TRIE_STATS |
Alexander Duyck | 8274a97 | 2014-12-31 10:55:29 -0800 | [diff] [blame] | 145 | struct trie_use_stats __percpu *stats; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 146 | #endif |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 149 | static void resize(struct trie *t, struct tnode *tn); |
Jarek Poplawski | c305947 | 2009-07-14 08:33:08 +0000 | [diff] [blame] | 150 | static size_t tnode_free_size; |
| 151 | |
| 152 | /* |
| 153 | * synchronize_rcu after call_rcu for that many pages; it should be especially |
| 154 | * useful before resizing the root node with PREEMPT_NONE configs; the value was |
| 155 | * obtained experimentally, aiming to avoid visible slowdown. |
| 156 | */ |
| 157 | static const int sync_pages = 128; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 158 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 159 | static struct kmem_cache *fn_alias_kmem __read_mostly; |
Stephen Hemminger | bc3c8c1 | 2008-01-22 21:51:50 -0800 | [diff] [blame] | 160 | static struct kmem_cache *trie_leaf_kmem __read_mostly; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 161 | |
Alexander Duyck | 64c9b6f | 2014-12-31 10:55:35 -0800 | [diff] [blame] | 162 | /* caller must hold RTNL */ |
| 163 | #define node_parent(n) rtnl_dereference((n)->parent) |
Eric Dumazet | 0a5c047 | 2011-03-31 01:51:35 -0700 | [diff] [blame] | 164 | |
Alexander Duyck | 64c9b6f | 2014-12-31 10:55:35 -0800 | [diff] [blame] | 165 | /* caller must hold RCU read lock or RTNL */ |
| 166 | #define node_parent_rcu(n) rcu_dereference_rtnl((n)->parent) |
Eric Dumazet | 0a5c047 | 2011-03-31 01:51:35 -0700 | [diff] [blame] | 167 | |
Alexander Duyck | 64c9b6f | 2014-12-31 10:55:35 -0800 | [diff] [blame] | 168 | /* wrapper for rcu_assign_pointer */ |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 169 | static inline void node_set_parent(struct tnode *n, struct tnode *tp) |
Stephen Hemminger | 0680191 | 2007-08-10 15:22:13 -0700 | [diff] [blame] | 170 | { |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 171 | if (n) |
| 172 | rcu_assign_pointer(n->parent, tp); |
Alexander Duyck | 64c9b6f | 2014-12-31 10:55:35 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | #define NODE_INIT_PARENT(n, p) RCU_INIT_POINTER((n)->parent, p) |
| 176 | |
| 177 | /* This provides us with the number of children in this node, in the case of a |
| 178 | * leaf this will return 0 meaning none of the children are accessible. |
| 179 | */ |
Alexander Duyck | 98293e8 | 2014-12-31 10:56:18 -0800 | [diff] [blame] | 180 | static inline unsigned long tnode_child_length(const struct tnode *tn) |
Alexander Duyck | 64c9b6f | 2014-12-31 10:55:35 -0800 | [diff] [blame] | 181 | { |
| 182 | return (1ul << tn->bits) & ~(1ul); |
Stephen Hemminger | 0680191 | 2007-08-10 15:22:13 -0700 | [diff] [blame] | 183 | } |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 184 | |
Alexander Duyck | 98293e8 | 2014-12-31 10:56:18 -0800 | [diff] [blame] | 185 | /* caller must hold RTNL */ |
| 186 | static inline struct tnode *tnode_get_child(const struct tnode *tn, |
| 187 | unsigned long i) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 188 | { |
Eric Dumazet | 0a5c047 | 2011-03-31 01:51:35 -0700 | [diff] [blame] | 189 | return rtnl_dereference(tn->child[i]); |
Eric Dumazet | b59cfbf | 2008-01-18 03:31:36 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Alexander Duyck | 98293e8 | 2014-12-31 10:56:18 -0800 | [diff] [blame] | 192 | /* caller must hold RCU read lock or RTNL */ |
| 193 | static inline struct tnode *tnode_get_child_rcu(const struct tnode *tn, |
| 194 | unsigned long i) |
Eric Dumazet | b59cfbf | 2008-01-18 03:31:36 -0800 | [diff] [blame] | 195 | { |
Eric Dumazet | 0a5c047 | 2011-03-31 01:51:35 -0700 | [diff] [blame] | 196 | return rcu_dereference_rtnl(tn->child[i]); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Alexander Duyck | e9b4401 | 2014-12-31 10:56:12 -0800 | [diff] [blame] | 199 | /* To understand this stuff, an understanding of keys and all their bits is |
| 200 | * necessary. Every node in the trie has a key associated with it, but not |
| 201 | * all of the bits in that key are significant. |
| 202 | * |
| 203 | * Consider a node 'n' and its parent 'tp'. |
| 204 | * |
| 205 | * If n is a leaf, every bit in its key is significant. Its presence is |
| 206 | * necessitated by path compression, since during a tree traversal (when |
| 207 | * searching for a leaf - unless we are doing an insertion) we will completely |
| 208 | * ignore all skipped bits we encounter. Thus we need to verify, at the end of |
| 209 | * a potentially successful search, that we have indeed been walking the |
| 210 | * correct key path. |
| 211 | * |
| 212 | * Note that we can never "miss" the correct key in the tree if present by |
| 213 | * following the wrong path. Path compression ensures that segments of the key |
| 214 | * that are the same for all keys with a given prefix are skipped, but the |
| 215 | * skipped part *is* identical for each node in the subtrie below the skipped |
| 216 | * bit! trie_insert() in this implementation takes care of that. |
| 217 | * |
| 218 | * if n is an internal node - a 'tnode' here, the various parts of its key |
| 219 | * have many different meanings. |
| 220 | * |
| 221 | * Example: |
| 222 | * _________________________________________________________________ |
| 223 | * | i | i | i | i | i | i | i | N | N | N | S | S | S | S | S | C | |
| 224 | * ----------------------------------------------------------------- |
| 225 | * 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 |
| 226 | * |
| 227 | * _________________________________________________________________ |
| 228 | * | C | C | C | u | u | u | u | u | u | u | u | u | u | u | u | u | |
| 229 | * ----------------------------------------------------------------- |
| 230 | * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
| 231 | * |
| 232 | * tp->pos = 22 |
| 233 | * tp->bits = 3 |
| 234 | * n->pos = 13 |
| 235 | * n->bits = 4 |
| 236 | * |
| 237 | * First, let's just ignore the bits that come before the parent tp, that is |
| 238 | * the bits from (tp->pos + tp->bits) to 31. They are *known* but at this |
| 239 | * point we do not use them for anything. |
| 240 | * |
| 241 | * The bits from (tp->pos) to (tp->pos + tp->bits - 1) - "N", above - are the |
| 242 | * index into the parent's child array. That is, they will be used to find |
| 243 | * 'n' among tp's children. |
| 244 | * |
| 245 | * The bits from (n->pos + n->bits) to (tn->pos - 1) - "S" - are skipped bits |
| 246 | * for the node n. |
| 247 | * |
| 248 | * All the bits we have seen so far are significant to the node n. The rest |
| 249 | * of the bits are really not needed or indeed known in n->key. |
| 250 | * |
| 251 | * The bits from (n->pos) to (n->pos + n->bits - 1) - "C" - are the index into |
| 252 | * n's child array, and will of course be different for each child. |
| 253 | * |
| 254 | * The rest of the bits, from 0 to (n->pos + n->bits), are completely unknown |
| 255 | * at this point. |
| 256 | */ |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 257 | |
Denis V. Lunev | f5026fa | 2007-12-13 09:47:57 -0800 | [diff] [blame] | 258 | static const int halve_threshold = 25; |
| 259 | static const int inflate_threshold = 50; |
Jarek Poplawski | 345aa03 | 2009-07-07 19:39:16 -0700 | [diff] [blame] | 260 | static const int halve_threshold_root = 15; |
Jens Låås | 80b71b8 | 2009-08-28 23:57:15 -0700 | [diff] [blame] | 261 | static const int inflate_threshold_root = 30; |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 262 | |
| 263 | static void __alias_free_mem(struct rcu_head *head) |
| 264 | { |
| 265 | struct fib_alias *fa = container_of(head, struct fib_alias, rcu); |
| 266 | kmem_cache_free(fn_alias_kmem, fa); |
| 267 | } |
| 268 | |
| 269 | static inline void alias_free_mem_rcu(struct fib_alias *fa) |
| 270 | { |
| 271 | call_rcu(&fa->rcu, __alias_free_mem); |
| 272 | } |
| 273 | |
Alexander Duyck | 37fd30f | 2014-12-31 10:55:41 -0800 | [diff] [blame] | 274 | #define TNODE_KMALLOC_MAX \ |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 275 | ilog2((PAGE_SIZE - sizeof(struct tnode)) / sizeof(struct tnode *)) |
Alexander Duyck | 37fd30f | 2014-12-31 10:55:41 -0800 | [diff] [blame] | 276 | |
| 277 | static void __node_free_rcu(struct rcu_head *head) |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 278 | { |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 279 | struct tnode *n = container_of(head, struct tnode, rcu); |
Alexander Duyck | 37fd30f | 2014-12-31 10:55:41 -0800 | [diff] [blame] | 280 | |
| 281 | if (IS_LEAF(n)) |
| 282 | kmem_cache_free(trie_leaf_kmem, n); |
| 283 | else if (n->bits <= TNODE_KMALLOC_MAX) |
| 284 | kfree(n); |
| 285 | else |
| 286 | vfree(n); |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Alexander Duyck | 37fd30f | 2014-12-31 10:55:41 -0800 | [diff] [blame] | 289 | #define node_free(n) call_rcu(&n->rcu, __node_free_rcu) |
Stephen Hemminger | 387a548 | 2008-04-10 03:47:34 -0700 | [diff] [blame] | 290 | |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 291 | static inline void free_leaf_info(struct leaf_info *leaf) |
| 292 | { |
Lai Jiangshan | bceb0f4 | 2011-03-18 11:42:34 +0800 | [diff] [blame] | 293 | kfree_rcu(leaf, rcu); |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Eric Dumazet | 8d96544 | 2008-01-13 22:31:44 -0800 | [diff] [blame] | 296 | static struct tnode *tnode_alloc(size_t size) |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 297 | { |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 298 | if (size <= PAGE_SIZE) |
Eric Dumazet | 8d96544 | 2008-01-13 22:31:44 -0800 | [diff] [blame] | 299 | return kzalloc(size, GFP_KERNEL); |
Stephen Hemminger | 15be75c | 2008-04-10 02:56:38 -0700 | [diff] [blame] | 300 | else |
Eric Dumazet | 7a1c8e5 | 2010-11-20 07:46:35 +0000 | [diff] [blame] | 301 | return vzalloc(size); |
Stephen Hemminger | 15be75c | 2008-04-10 02:56:38 -0700 | [diff] [blame] | 302 | } |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 303 | |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 304 | static struct tnode *leaf_new(t_key key) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 305 | { |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 306 | struct tnode *l = kmem_cache_alloc(trie_leaf_kmem, GFP_KERNEL); |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 307 | if (l) { |
Alexander Duyck | 64c9b6f | 2014-12-31 10:55:35 -0800 | [diff] [blame] | 308 | l->parent = NULL; |
| 309 | /* set key and pos to reflect full key value |
| 310 | * any trailing zeros in the key should be ignored |
| 311 | * as the nodes are searched |
| 312 | */ |
| 313 | l->key = key; |
Alexander Duyck | e9b4401 | 2014-12-31 10:56:12 -0800 | [diff] [blame] | 314 | l->pos = 0; |
Alexander Duyck | 64c9b6f | 2014-12-31 10:55:35 -0800 | [diff] [blame] | 315 | /* set bits to 0 indicating we are not a tnode */ |
| 316 | l->bits = 0; |
| 317 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 318 | INIT_HLIST_HEAD(&l->list); |
| 319 | } |
| 320 | return l; |
| 321 | } |
| 322 | |
| 323 | static struct leaf_info *leaf_info_new(int plen) |
| 324 | { |
| 325 | struct leaf_info *li = kmalloc(sizeof(struct leaf_info), GFP_KERNEL); |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 326 | if (li) { |
| 327 | li->plen = plen; |
Eric Dumazet | 5c74501 | 2011-07-18 03:16:33 +0000 | [diff] [blame] | 328 | li->mask_plen = ntohl(inet_make_mask(plen)); |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 329 | INIT_LIST_HEAD(&li->falh); |
Patrick McHardy | f0e36f8 | 2005-07-05 14:44:55 -0700 | [diff] [blame] | 330 | } |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 331 | return li; |
Patrick McHardy | f0e36f8 | 2005-07-05 14:44:55 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Stephen Hemminger | a07f5f5 | 2008-01-22 21:53:36 -0800 | [diff] [blame] | 334 | static struct tnode *tnode_new(t_key key, int pos, int bits) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 335 | { |
Alexander Duyck | 37fd30f | 2014-12-31 10:55:41 -0800 | [diff] [blame] | 336 | size_t sz = offsetof(struct tnode, child[1 << bits]); |
Patrick McHardy | f0e36f8 | 2005-07-05 14:44:55 -0700 | [diff] [blame] | 337 | struct tnode *tn = tnode_alloc(sz); |
Alexander Duyck | 64c9b6f | 2014-12-31 10:55:35 -0800 | [diff] [blame] | 338 | unsigned int shift = pos + bits; |
| 339 | |
| 340 | /* verify bits and pos their msb bits clear and values are valid */ |
| 341 | BUG_ON(!bits || (shift > KEYLENGTH)); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 342 | |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 343 | if (tn) { |
Alexander Duyck | 64c9b6f | 2014-12-31 10:55:35 -0800 | [diff] [blame] | 344 | tn->parent = NULL; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 345 | tn->pos = pos; |
| 346 | tn->bits = bits; |
Alexander Duyck | e9b4401 | 2014-12-31 10:56:12 -0800 | [diff] [blame] | 347 | tn->key = (shift < KEYLENGTH) ? (key >> shift) << shift : 0; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 348 | tn->full_children = 0; |
| 349 | tn->empty_children = 1<<bits; |
| 350 | } |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 351 | |
Eric Dumazet | a034ee3 | 2010-09-09 23:32:28 +0000 | [diff] [blame] | 352 | pr_debug("AT %p s=%zu %zu\n", tn, sizeof(struct tnode), |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 353 | sizeof(struct tnode *) << bits); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 354 | return tn; |
| 355 | } |
| 356 | |
Alexander Duyck | e9b4401 | 2014-12-31 10:56:12 -0800 | [diff] [blame] | 357 | /* Check whether a tnode 'n' is "full", i.e. it is an internal node |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 358 | * and no bits are skipped. See discussion in dyntree paper p. 6 |
| 359 | */ |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 360 | static inline int tnode_full(const struct tnode *tn, const struct tnode *n) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 361 | { |
Alexander Duyck | e9b4401 | 2014-12-31 10:56:12 -0800 | [diff] [blame] | 362 | return n && ((n->pos + n->bits) == tn->pos) && IS_TNODE(n); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 365 | /* Add a child at position i overwriting the old value. |
| 366 | * Update the value of full_children and empty_children. |
| 367 | */ |
| 368 | static void put_child(struct tnode *tn, unsigned long i, struct tnode *n) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 369 | { |
Alexander Duyck | 21d1f11 | 2014-12-31 10:57:02 -0800 | [diff] [blame^] | 370 | struct tnode *chi = tnode_get_child(tn, i); |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 371 | int isfull, wasfull; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 372 | |
Alexander Duyck | 98293e8 | 2014-12-31 10:56:18 -0800 | [diff] [blame] | 373 | BUG_ON(i >= tnode_child_length(tn)); |
Stephen Hemminger | 0c7770c | 2005-08-23 21:59:41 -0700 | [diff] [blame] | 374 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 375 | /* update emptyChildren */ |
| 376 | if (n == NULL && chi != NULL) |
| 377 | tn->empty_children++; |
| 378 | else if (n != NULL && chi == NULL) |
| 379 | tn->empty_children--; |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 380 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 381 | /* update fullChildren */ |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 382 | wasfull = tnode_full(tn, chi); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 383 | isfull = tnode_full(tn, n); |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 384 | |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 385 | if (wasfull && !isfull) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 386 | tn->full_children--; |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 387 | else if (!wasfull && isfull) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 388 | tn->full_children++; |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 389 | |
Eric Dumazet | cf778b0 | 2012-01-12 04:41:32 +0000 | [diff] [blame] | 390 | rcu_assign_pointer(tn->child[i], n); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Alexander Duyck | 836a012 | 2014-12-31 10:56:06 -0800 | [diff] [blame] | 393 | static void put_child_root(struct tnode *tp, struct trie *t, |
| 394 | t_key key, struct tnode *n) |
| 395 | { |
| 396 | if (tp) |
| 397 | put_child(tp, get_index(key, tp), n); |
| 398 | else |
| 399 | rcu_assign_pointer(t->trie, n); |
| 400 | } |
| 401 | |
Alexander Duyck | fc86a93 | 2014-12-31 10:56:49 -0800 | [diff] [blame] | 402 | static inline void tnode_free_init(struct tnode *tn) |
Eric Dumazet | 0a5c047 | 2011-03-31 01:51:35 -0700 | [diff] [blame] | 403 | { |
Alexander Duyck | fc86a93 | 2014-12-31 10:56:49 -0800 | [diff] [blame] | 404 | tn->rcu.next = NULL; |
| 405 | } |
Eric Dumazet | 0a5c047 | 2011-03-31 01:51:35 -0700 | [diff] [blame] | 406 | |
Alexander Duyck | fc86a93 | 2014-12-31 10:56:49 -0800 | [diff] [blame] | 407 | static inline void tnode_free_append(struct tnode *tn, struct tnode *n) |
| 408 | { |
| 409 | n->rcu.next = tn->rcu.next; |
| 410 | tn->rcu.next = &n->rcu; |
| 411 | } |
| 412 | |
| 413 | static void tnode_free(struct tnode *tn) |
| 414 | { |
| 415 | struct callback_head *head = &tn->rcu; |
| 416 | |
| 417 | while (head) { |
| 418 | head = head->next; |
| 419 | tnode_free_size += offsetof(struct tnode, child[1 << tn->bits]); |
| 420 | node_free(tn); |
| 421 | |
| 422 | tn = container_of(head, struct tnode, rcu); |
Eric Dumazet | 0a5c047 | 2011-03-31 01:51:35 -0700 | [diff] [blame] | 423 | } |
Alexander Duyck | fc86a93 | 2014-12-31 10:56:49 -0800 | [diff] [blame] | 424 | |
| 425 | if (tnode_free_size >= PAGE_SIZE * sync_pages) { |
| 426 | tnode_free_size = 0; |
| 427 | synchronize_rcu(); |
| 428 | } |
Eric Dumazet | 0a5c047 | 2011-03-31 01:51:35 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 431 | static int inflate(struct trie *t, struct tnode *oldtnode) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 432 | { |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 433 | struct tnode *inode, *node0, *node1, *tn, *tp; |
| 434 | unsigned long i, j, k; |
Alexander Duyck | e9b4401 | 2014-12-31 10:56:12 -0800 | [diff] [blame] | 435 | t_key m; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 436 | |
Stephen Hemminger | 0c7770c | 2005-08-23 21:59:41 -0700 | [diff] [blame] | 437 | pr_debug("In inflate\n"); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 438 | |
Alexander Duyck | e9b4401 | 2014-12-31 10:56:12 -0800 | [diff] [blame] | 439 | tn = tnode_new(oldtnode->key, oldtnode->pos - 1, oldtnode->bits + 1); |
Stephen Hemminger | 0c7770c | 2005-08-23 21:59:41 -0700 | [diff] [blame] | 440 | if (!tn) |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 441 | return -ENOMEM; |
Robert Olsson | 2f36895 | 2005-07-05 15:02:40 -0700 | [diff] [blame] | 442 | |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 443 | /* Assemble all of the pointers in our cluster, in this case that |
| 444 | * represents all of the pointers out of our allocated nodes that |
| 445 | * point to existing tnodes and the links between our allocated |
| 446 | * nodes. |
Robert Olsson | 2f36895 | 2005-07-05 15:02:40 -0700 | [diff] [blame] | 447 | */ |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 448 | for (i = tnode_child_length(oldtnode), m = 1u << tn->pos; i;) { |
| 449 | inode = tnode_get_child(oldtnode, --i); |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 450 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 451 | /* An empty child */ |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 452 | if (inode == NULL) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 453 | continue; |
| 454 | |
| 455 | /* A leaf or an internal node with skipped bits */ |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 456 | if (!tnode_full(oldtnode, inode)) { |
Alexander Duyck | e9b4401 | 2014-12-31 10:56:12 -0800 | [diff] [blame] | 457 | put_child(tn, get_index(inode->key, tn), inode); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 458 | continue; |
| 459 | } |
| 460 | |
| 461 | /* An internal node with two children */ |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 462 | if (inode->bits == 1) { |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 463 | put_child(tn, 2 * i + 1, tnode_get_child(inode, 1)); |
| 464 | put_child(tn, 2 * i, tnode_get_child(inode, 0)); |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 465 | continue; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 468 | /* We will replace this node 'inode' with two new |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 469 | * ones, 'node0' and 'node1', each with half of the |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 470 | * original children. The two new nodes will have |
| 471 | * a position one bit further down the key and this |
| 472 | * means that the "significant" part of their keys |
| 473 | * (see the discussion near the top of this file) |
| 474 | * will differ by one bit, which will be "0" in |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 475 | * node0's key and "1" in node1's key. Since we are |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 476 | * moving the key position by one step, the bit that |
| 477 | * we are moving away from - the bit at position |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 478 | * (tn->pos) - is the one that will differ between |
| 479 | * node0 and node1. So... we synthesize that bit in the |
| 480 | * two new keys. |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 481 | */ |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 482 | node1 = tnode_new(inode->key | m, inode->pos, inode->bits - 1); |
| 483 | if (!node1) |
| 484 | goto nomem; |
| 485 | tnode_free_append(tn, node1); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 486 | |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 487 | node0 = tnode_new(inode->key & ~m, inode->pos, inode->bits - 1); |
| 488 | if (!node0) |
| 489 | goto nomem; |
| 490 | tnode_free_append(tn, node0); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 491 | |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 492 | /* populate child pointers in new nodes */ |
| 493 | for (k = tnode_child_length(inode), j = k / 2; j;) { |
| 494 | put_child(node1, --j, tnode_get_child(inode, --k)); |
| 495 | put_child(node0, j, tnode_get_child(inode, j)); |
| 496 | put_child(node1, --j, tnode_get_child(inode, --k)); |
| 497 | put_child(node0, j, tnode_get_child(inode, j)); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 498 | } |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 499 | |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 500 | /* link new nodes to parent */ |
| 501 | NODE_INIT_PARENT(node1, tn); |
| 502 | NODE_INIT_PARENT(node0, tn); |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 503 | |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 504 | /* link parent to nodes */ |
| 505 | put_child(tn, 2 * i + 1, node1); |
| 506 | put_child(tn, 2 * i, node0); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 507 | } |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 508 | |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 509 | /* setup the parent pointer into and out of this node */ |
| 510 | tp = node_parent(oldtnode); |
| 511 | NODE_INIT_PARENT(tn, tp); |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 512 | put_child_root(tp, t, tn->key, tn); |
Alexander Duyck | fc86a93 | 2014-12-31 10:56:49 -0800 | [diff] [blame] | 513 | |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 514 | /* prepare oldtnode to be freed */ |
| 515 | tnode_free_init(oldtnode); |
| 516 | |
| 517 | /* update all child nodes parent pointers to route to us */ |
| 518 | for (i = tnode_child_length(oldtnode); i;) { |
| 519 | inode = tnode_get_child(oldtnode, --i); |
| 520 | |
| 521 | /* A leaf or an internal node with skipped bits */ |
| 522 | if (!tnode_full(oldtnode, inode)) { |
| 523 | node_set_parent(inode, tn); |
| 524 | continue; |
| 525 | } |
| 526 | |
| 527 | /* drop the node in the old tnode free list */ |
| 528 | tnode_free_append(oldtnode, inode); |
| 529 | |
| 530 | /* fetch new nodes */ |
| 531 | node1 = tnode_get_child(tn, 2 * i + 1); |
| 532 | node0 = tnode_get_child(tn, 2 * i); |
| 533 | |
| 534 | /* bits == 1 then node0 and node1 represent inode's children */ |
| 535 | if (inode->bits == 1) { |
| 536 | node_set_parent(node1, tn); |
| 537 | node_set_parent(node0, tn); |
| 538 | continue; |
| 539 | } |
| 540 | |
| 541 | /* update parent pointers in child node's children */ |
| 542 | for (k = tnode_child_length(inode), j = k / 2; j;) { |
| 543 | node_set_parent(tnode_get_child(inode, --k), node1); |
| 544 | node_set_parent(tnode_get_child(inode, --j), node0); |
| 545 | node_set_parent(tnode_get_child(inode, --k), node1); |
| 546 | node_set_parent(tnode_get_child(inode, --j), node0); |
| 547 | } |
| 548 | |
| 549 | /* resize child nodes */ |
| 550 | resize(t, node1); |
| 551 | resize(t, node0); |
| 552 | } |
| 553 | |
Alexander Duyck | fc86a93 | 2014-12-31 10:56:49 -0800 | [diff] [blame] | 554 | /* we completed without error, prepare to free old node */ |
| 555 | tnode_free(oldtnode); |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 556 | return 0; |
Robert Olsson | 2f80b3c | 2005-08-09 20:25:06 -0700 | [diff] [blame] | 557 | nomem: |
Alexander Duyck | fc86a93 | 2014-12-31 10:56:49 -0800 | [diff] [blame] | 558 | /* all pointers should be clean so we are done */ |
| 559 | tnode_free(tn); |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 560 | return -ENOMEM; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 563 | static int halve(struct trie *t, struct tnode *oldtnode) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 564 | { |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 565 | struct tnode *tn, *tp, *inode, *node0, *node1; |
| 566 | unsigned long i; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 567 | |
Stephen Hemminger | 0c7770c | 2005-08-23 21:59:41 -0700 | [diff] [blame] | 568 | pr_debug("In halve\n"); |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 569 | |
Alexander Duyck | e9b4401 | 2014-12-31 10:56:12 -0800 | [diff] [blame] | 570 | tn = tnode_new(oldtnode->key, oldtnode->pos + 1, oldtnode->bits - 1); |
Robert Olsson | 2f80b3c | 2005-08-09 20:25:06 -0700 | [diff] [blame] | 571 | if (!tn) |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 572 | return -ENOMEM; |
Robert Olsson | 2f36895 | 2005-07-05 15:02:40 -0700 | [diff] [blame] | 573 | |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 574 | /* Assemble all of the pointers in our cluster, in this case that |
| 575 | * represents all of the pointers out of our allocated nodes that |
| 576 | * point to existing tnodes and the links between our allocated |
| 577 | * nodes. |
Robert Olsson | 2f36895 | 2005-07-05 15:02:40 -0700 | [diff] [blame] | 578 | */ |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 579 | for (i = tnode_child_length(oldtnode); i;) { |
| 580 | node1 = tnode_get_child(oldtnode, --i); |
| 581 | node0 = tnode_get_child(oldtnode, --i); |
Robert Olsson | 2f36895 | 2005-07-05 15:02:40 -0700 | [diff] [blame] | 582 | |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 583 | /* At least one of the children is empty */ |
| 584 | if (!node1 || !node0) { |
| 585 | put_child(tn, i / 2, node1 ? : node0); |
| 586 | continue; |
Robert Olsson | 2f36895 | 2005-07-05 15:02:40 -0700 | [diff] [blame] | 587 | } |
Robert Olsson | 2f36895 | 2005-07-05 15:02:40 -0700 | [diff] [blame] | 588 | |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 589 | /* Two nonempty children */ |
| 590 | inode = tnode_new(node0->key, oldtnode->pos, 1); |
| 591 | if (!inode) { |
| 592 | tnode_free(tn); |
| 593 | return -ENOMEM; |
| 594 | } |
| 595 | tnode_free_append(tn, inode); |
| 596 | |
| 597 | /* initialize pointers out of node */ |
| 598 | put_child(inode, 1, node1); |
| 599 | put_child(inode, 0, node0); |
| 600 | NODE_INIT_PARENT(inode, tn); |
| 601 | |
| 602 | /* link parent to node */ |
| 603 | put_child(tn, i / 2, inode); |
Robert Olsson | 2f36895 | 2005-07-05 15:02:40 -0700 | [diff] [blame] | 604 | } |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 605 | |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 606 | /* setup the parent pointer out of and back into this node */ |
| 607 | tp = node_parent(oldtnode); |
| 608 | NODE_INIT_PARENT(tn, tp); |
| 609 | put_child_root(tp, t, tn->key, tn); |
| 610 | |
Alexander Duyck | fc86a93 | 2014-12-31 10:56:49 -0800 | [diff] [blame] | 611 | /* prepare oldtnode to be freed */ |
| 612 | tnode_free_init(oldtnode); |
| 613 | |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 614 | /* update all of the child parent pointers */ |
| 615 | for (i = tnode_child_length(tn); i;) { |
| 616 | inode = tnode_get_child(tn, --i); |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 617 | |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 618 | /* only new tnodes will be considered "full" nodes */ |
| 619 | if (!tnode_full(tn, inode)) { |
| 620 | node_set_parent(inode, tn); |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 621 | continue; |
| 622 | } |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 623 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 624 | /* Two nonempty children */ |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 625 | node_set_parent(tnode_get_child(inode, 1), inode); |
| 626 | node_set_parent(tnode_get_child(inode, 0), inode); |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 627 | |
Alexander Duyck | fc86a93 | 2014-12-31 10:56:49 -0800 | [diff] [blame] | 628 | /* resize child node */ |
Alexander Duyck | 12c081a | 2014-12-31 10:56:55 -0800 | [diff] [blame] | 629 | resize(t, inode); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 630 | } |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 631 | |
Alexander Duyck | fc86a93 | 2014-12-31 10:56:49 -0800 | [diff] [blame] | 632 | /* all pointers should be clean so we are done */ |
| 633 | tnode_free(oldtnode); |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 634 | |
| 635 | return 0; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Alexander Duyck | f05a481 | 2014-12-31 10:56:37 -0800 | [diff] [blame] | 638 | /* From "Implementing a dynamic compressed trie" by Stefan Nilsson of |
| 639 | * the Helsinki University of Technology and Matti Tikkanen of Nokia |
| 640 | * Telecommunications, page 6: |
| 641 | * "A node is doubled if the ratio of non-empty children to all |
| 642 | * children in the *doubled* node is at least 'high'." |
| 643 | * |
| 644 | * 'high' in this instance is the variable 'inflate_threshold'. It |
| 645 | * is expressed as a percentage, so we multiply it with |
| 646 | * tnode_child_length() and instead of multiplying by 2 (since the |
| 647 | * child array will be doubled by inflate()) and multiplying |
| 648 | * the left-hand side by 100 (to handle the percentage thing) we |
| 649 | * multiply the left-hand side by 50. |
| 650 | * |
| 651 | * The left-hand side may look a bit weird: tnode_child_length(tn) |
| 652 | * - tn->empty_children is of course the number of non-null children |
| 653 | * in the current node. tn->full_children is the number of "full" |
| 654 | * children, that is non-null tnodes with a skip value of 0. |
| 655 | * All of those will be doubled in the resulting inflated tnode, so |
| 656 | * we just count them one extra time here. |
| 657 | * |
| 658 | * A clearer way to write this would be: |
| 659 | * |
| 660 | * to_be_doubled = tn->full_children; |
| 661 | * not_to_be_doubled = tnode_child_length(tn) - tn->empty_children - |
| 662 | * tn->full_children; |
| 663 | * |
| 664 | * new_child_length = tnode_child_length(tn) * 2; |
| 665 | * |
| 666 | * new_fill_factor = 100 * (not_to_be_doubled + 2*to_be_doubled) / |
| 667 | * new_child_length; |
| 668 | * if (new_fill_factor >= inflate_threshold) |
| 669 | * |
| 670 | * ...and so on, tho it would mess up the while () loop. |
| 671 | * |
| 672 | * anyway, |
| 673 | * 100 * (not_to_be_doubled + 2*to_be_doubled) / new_child_length >= |
| 674 | * inflate_threshold |
| 675 | * |
| 676 | * avoid a division: |
| 677 | * 100 * (not_to_be_doubled + 2*to_be_doubled) >= |
| 678 | * inflate_threshold * new_child_length |
| 679 | * |
| 680 | * expand not_to_be_doubled and to_be_doubled, and shorten: |
| 681 | * 100 * (tnode_child_length(tn) - tn->empty_children + |
| 682 | * tn->full_children) >= inflate_threshold * new_child_length |
| 683 | * |
| 684 | * expand new_child_length: |
| 685 | * 100 * (tnode_child_length(tn) - tn->empty_children + |
| 686 | * tn->full_children) >= |
| 687 | * inflate_threshold * tnode_child_length(tn) * 2 |
| 688 | * |
| 689 | * shorten again: |
| 690 | * 50 * (tn->full_children + tnode_child_length(tn) - |
| 691 | * tn->empty_children) >= inflate_threshold * |
| 692 | * tnode_child_length(tn) |
| 693 | * |
| 694 | */ |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 695 | static bool should_inflate(const struct tnode *tp, const struct tnode *tn) |
Alexander Duyck | f05a481 | 2014-12-31 10:56:37 -0800 | [diff] [blame] | 696 | { |
| 697 | unsigned long used = tnode_child_length(tn); |
| 698 | unsigned long threshold = used; |
| 699 | |
| 700 | /* Keep root node larger */ |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 701 | threshold *= tp ? inflate_threshold : inflate_threshold_root; |
Alexander Duyck | f05a481 | 2014-12-31 10:56:37 -0800 | [diff] [blame] | 702 | used += tn->full_children; |
| 703 | used -= tn->empty_children; |
| 704 | |
| 705 | return tn->pos && ((50 * used) >= threshold); |
| 706 | } |
| 707 | |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 708 | static bool should_halve(const struct tnode *tp, const struct tnode *tn) |
Alexander Duyck | f05a481 | 2014-12-31 10:56:37 -0800 | [diff] [blame] | 709 | { |
| 710 | unsigned long used = tnode_child_length(tn); |
| 711 | unsigned long threshold = used; |
| 712 | |
| 713 | /* Keep root node larger */ |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 714 | threshold *= tp ? halve_threshold : halve_threshold_root; |
Alexander Duyck | f05a481 | 2014-12-31 10:56:37 -0800 | [diff] [blame] | 715 | used -= tn->empty_children; |
| 716 | |
| 717 | return (tn->bits > 1) && ((100 * used) < threshold); |
| 718 | } |
| 719 | |
Alexander Duyck | cf3637b | 2014-12-31 10:56:31 -0800 | [diff] [blame] | 720 | #define MAX_WORK 10 |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 721 | static void resize(struct trie *t, struct tnode *tn) |
Alexander Duyck | cf3637b | 2014-12-31 10:56:31 -0800 | [diff] [blame] | 722 | { |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 723 | struct tnode *tp = node_parent(tn), *n = NULL; |
| 724 | struct tnode __rcu **cptr; |
Alexander Duyck | cf3637b | 2014-12-31 10:56:31 -0800 | [diff] [blame] | 725 | int max_work; |
| 726 | |
Alexander Duyck | cf3637b | 2014-12-31 10:56:31 -0800 | [diff] [blame] | 727 | pr_debug("In tnode_resize %p inflate_threshold=%d threshold=%d\n", |
| 728 | tn, inflate_threshold, halve_threshold); |
| 729 | |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 730 | /* track the tnode via the pointer from the parent instead of |
| 731 | * doing it ourselves. This way we can let RCU fully do its |
| 732 | * thing without us interfering |
| 733 | */ |
| 734 | cptr = tp ? &tp->child[get_index(tn->key, tp)] : &t->trie; |
| 735 | BUG_ON(tn != rtnl_dereference(*cptr)); |
| 736 | |
Alexander Duyck | cf3637b | 2014-12-31 10:56:31 -0800 | [diff] [blame] | 737 | /* No children */ |
| 738 | if (tn->empty_children > (tnode_child_length(tn) - 1)) |
| 739 | goto no_children; |
| 740 | |
| 741 | /* One child */ |
| 742 | if (tn->empty_children == (tnode_child_length(tn) - 1)) |
| 743 | goto one_child; |
Alexander Duyck | f05a481 | 2014-12-31 10:56:37 -0800 | [diff] [blame] | 744 | |
| 745 | /* Double as long as the resulting node has a number of |
Alexander Duyck | cf3637b | 2014-12-31 10:56:31 -0800 | [diff] [blame] | 746 | * nonempty nodes that are above the threshold. |
| 747 | */ |
Alexander Duyck | cf3637b | 2014-12-31 10:56:31 -0800 | [diff] [blame] | 748 | max_work = MAX_WORK; |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 749 | while (should_inflate(tp, tn) && max_work--) { |
| 750 | if (inflate(t, tn)) { |
Alexander Duyck | cf3637b | 2014-12-31 10:56:31 -0800 | [diff] [blame] | 751 | #ifdef CONFIG_IP_FIB_TRIE_STATS |
| 752 | this_cpu_inc(t->stats->resize_node_skipped); |
| 753 | #endif |
| 754 | break; |
| 755 | } |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 756 | |
| 757 | tn = rtnl_dereference(*cptr); |
Alexander Duyck | cf3637b | 2014-12-31 10:56:31 -0800 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | /* Return if at least one inflate is run */ |
| 761 | if (max_work != MAX_WORK) |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 762 | return; |
Alexander Duyck | cf3637b | 2014-12-31 10:56:31 -0800 | [diff] [blame] | 763 | |
Alexander Duyck | f05a481 | 2014-12-31 10:56:37 -0800 | [diff] [blame] | 764 | /* Halve as long as the number of empty children in this |
Alexander Duyck | cf3637b | 2014-12-31 10:56:31 -0800 | [diff] [blame] | 765 | * node is above threshold. |
| 766 | */ |
Alexander Duyck | cf3637b | 2014-12-31 10:56:31 -0800 | [diff] [blame] | 767 | max_work = MAX_WORK; |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 768 | while (should_halve(tp, tn) && max_work--) { |
| 769 | if (halve(t, tn)) { |
Alexander Duyck | cf3637b | 2014-12-31 10:56:31 -0800 | [diff] [blame] | 770 | #ifdef CONFIG_IP_FIB_TRIE_STATS |
| 771 | this_cpu_inc(t->stats->resize_node_skipped); |
| 772 | #endif |
| 773 | break; |
| 774 | } |
Alexander Duyck | cf3637b | 2014-12-31 10:56:31 -0800 | [diff] [blame] | 775 | |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 776 | tn = rtnl_dereference(*cptr); |
| 777 | } |
Alexander Duyck | cf3637b | 2014-12-31 10:56:31 -0800 | [diff] [blame] | 778 | |
| 779 | /* Only one child remains */ |
| 780 | if (tn->empty_children == (tnode_child_length(tn) - 1)) { |
| 781 | unsigned long i; |
| 782 | one_child: |
| 783 | for (i = tnode_child_length(tn); !n && i;) |
| 784 | n = tnode_get_child(tn, --i); |
| 785 | no_children: |
| 786 | /* compress one level */ |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 787 | put_child_root(tp, t, tn->key, n); |
| 788 | node_set_parent(n, tp); |
| 789 | |
| 790 | /* drop dead node */ |
Alexander Duyck | fc86a93 | 2014-12-31 10:56:49 -0800 | [diff] [blame] | 791 | tnode_free_init(tn); |
| 792 | tnode_free(tn); |
Alexander Duyck | cf3637b | 2014-12-31 10:56:31 -0800 | [diff] [blame] | 793 | } |
Alexander Duyck | cf3637b | 2014-12-31 10:56:31 -0800 | [diff] [blame] | 794 | } |
| 795 | |
Robert Olsson | 772cb71 | 2005-09-19 15:31:18 -0700 | [diff] [blame] | 796 | /* readside must use rcu_read_lock currently dump routines |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 797 | via get_fa_head and dump */ |
| 798 | |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 799 | static struct leaf_info *find_leaf_info(struct tnode *l, int plen) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 800 | { |
Robert Olsson | 772cb71 | 2005-09-19 15:31:18 -0700 | [diff] [blame] | 801 | struct hlist_head *head = &l->list; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 802 | struct leaf_info *li; |
| 803 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 804 | hlist_for_each_entry_rcu(li, head, hlist) |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 805 | if (li->plen == plen) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 806 | return li; |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 807 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 808 | return NULL; |
| 809 | } |
| 810 | |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 811 | static inline struct list_head *get_fa_head(struct tnode *l, int plen) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 812 | { |
Robert Olsson | 772cb71 | 2005-09-19 15:31:18 -0700 | [diff] [blame] | 813 | struct leaf_info *li = find_leaf_info(l, plen); |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 814 | |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 815 | if (!li) |
| 816 | return NULL; |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 817 | |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 818 | return &li->falh; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | static void insert_leaf_info(struct hlist_head *head, struct leaf_info *new) |
| 822 | { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 823 | struct leaf_info *li = NULL, *last = NULL; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 824 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 825 | if (hlist_empty(head)) { |
| 826 | hlist_add_head_rcu(&new->hlist, head); |
| 827 | } else { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 828 | hlist_for_each_entry(li, head, hlist) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 829 | if (new->plen > li->plen) |
| 830 | break; |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 831 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 832 | last = li; |
| 833 | } |
| 834 | if (last) |
Ken Helias | 1d02328 | 2014-08-06 16:09:16 -0700 | [diff] [blame] | 835 | hlist_add_behind_rcu(&new->hlist, &last->hlist); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 836 | else |
| 837 | hlist_add_before_rcu(&new->hlist, &li->hlist); |
| 838 | } |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 839 | } |
| 840 | |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 841 | /* rcu_read_lock needs to be hold by caller from readside */ |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 842 | static struct tnode *fib_find_node(struct trie *t, u32 key) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 843 | { |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 844 | struct tnode *n = rcu_dereference_rtnl(t->trie); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 845 | |
Alexander Duyck | 939afb0 | 2014-12-31 10:56:00 -0800 | [diff] [blame] | 846 | while (n) { |
| 847 | unsigned long index = get_index(key, n); |
| 848 | |
| 849 | /* This bit of code is a bit tricky but it combines multiple |
| 850 | * checks into a single check. The prefix consists of the |
| 851 | * prefix plus zeros for the bits in the cindex. The index |
| 852 | * is the difference between the key and this value. From |
| 853 | * this we can actually derive several pieces of data. |
| 854 | * if !(index >> bits) |
| 855 | * we know the value is cindex |
| 856 | * else |
| 857 | * we have a mismatch in skip bits and failed |
| 858 | */ |
| 859 | if (index >> n->bits) |
| 860 | return NULL; |
| 861 | |
| 862 | /* we have found a leaf. Prefixes have already been compared */ |
| 863 | if (IS_LEAF(n)) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 864 | break; |
Alexander Duyck | 939afb0 | 2014-12-31 10:56:00 -0800 | [diff] [blame] | 865 | |
Alexander Duyck | 21d1f11 | 2014-12-31 10:57:02 -0800 | [diff] [blame^] | 866 | n = tnode_get_child_rcu(n, index); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 867 | } |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 868 | |
Alexander Duyck | 939afb0 | 2014-12-31 10:56:00 -0800 | [diff] [blame] | 869 | return n; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 870 | } |
| 871 | |
Jarek Poplawski | 7b85576 | 2009-06-18 00:28:51 -0700 | [diff] [blame] | 872 | static void trie_rebalance(struct trie *t, struct tnode *tn) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 873 | { |
Stephen Hemminger | 0680191 | 2007-08-10 15:22:13 -0700 | [diff] [blame] | 874 | struct tnode *tp; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 875 | |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 876 | while ((tp = node_parent(tn)) != NULL) { |
| 877 | resize(t, tn); |
Stephen Hemminger | 0680191 | 2007-08-10 15:22:13 -0700 | [diff] [blame] | 878 | tn = tp; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 879 | } |
Stephen Hemminger | 0680191 | 2007-08-10 15:22:13 -0700 | [diff] [blame] | 880 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 881 | /* Handle last (top) tnode */ |
Jarek Poplawski | 7b85576 | 2009-06-18 00:28:51 -0700 | [diff] [blame] | 882 | if (IS_TNODE(tn)) |
Alexander Duyck | ff181ed | 2014-12-31 10:56:43 -0800 | [diff] [blame] | 883 | resize(t, tn); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 886 | /* only used from updater-side */ |
| 887 | |
Stephen Hemminger | fea86ad | 2008-01-12 20:57:07 -0800 | [diff] [blame] | 888 | static struct list_head *fib_insert_node(struct trie *t, u32 key, int plen) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 889 | { |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 890 | struct list_head *fa_head = NULL; |
Alexander Duyck | 836a012 | 2014-12-31 10:56:06 -0800 | [diff] [blame] | 891 | struct tnode *l, *n, *tp = NULL; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 892 | struct leaf_info *li; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 893 | |
Alexander Duyck | 836a012 | 2014-12-31 10:56:06 -0800 | [diff] [blame] | 894 | li = leaf_info_new(plen); |
| 895 | if (!li) |
| 896 | return NULL; |
| 897 | fa_head = &li->falh; |
| 898 | |
Eric Dumazet | 0a5c047 | 2011-03-31 01:51:35 -0700 | [diff] [blame] | 899 | n = rtnl_dereference(t->trie); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 900 | |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 901 | /* If we point to NULL, stop. Either the tree is empty and we should |
| 902 | * just put a new leaf in if, or we have reached an empty child slot, |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 903 | * and we should just put our new leaf in that. |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 904 | * |
Alexander Duyck | 836a012 | 2014-12-31 10:56:06 -0800 | [diff] [blame] | 905 | * If we hit a node with a key that does't match then we should stop |
| 906 | * and create a new tnode to replace that node and insert ourselves |
| 907 | * and the other node into the new tnode. |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 908 | */ |
Alexander Duyck | 836a012 | 2014-12-31 10:56:06 -0800 | [diff] [blame] | 909 | while (n) { |
| 910 | unsigned long index = get_index(key, n); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 911 | |
Alexander Duyck | 836a012 | 2014-12-31 10:56:06 -0800 | [diff] [blame] | 912 | /* This bit of code is a bit tricky but it combines multiple |
| 913 | * checks into a single check. The prefix consists of the |
| 914 | * prefix plus zeros for the "bits" in the prefix. The index |
| 915 | * is the difference between the key and this value. From |
| 916 | * this we can actually derive several pieces of data. |
| 917 | * if !(index >> bits) |
| 918 | * we know the value is child index |
| 919 | * else |
| 920 | * we have a mismatch in skip bits and failed |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 921 | */ |
Alexander Duyck | 836a012 | 2014-12-31 10:56:06 -0800 | [diff] [blame] | 922 | if (index >> n->bits) |
| 923 | break; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 924 | |
Alexander Duyck | 836a012 | 2014-12-31 10:56:06 -0800 | [diff] [blame] | 925 | /* we have found a leaf. Prefixes have already been compared */ |
| 926 | if (IS_LEAF(n)) { |
| 927 | /* Case 1: n is a leaf, and prefixes match*/ |
| 928 | insert_leaf_info(&n->list, li); |
| 929 | return fa_head; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 930 | } |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 931 | |
Alexander Duyck | 836a012 | 2014-12-31 10:56:06 -0800 | [diff] [blame] | 932 | tp = n; |
Alexander Duyck | 21d1f11 | 2014-12-31 10:57:02 -0800 | [diff] [blame^] | 933 | n = tnode_get_child_rcu(n, index); |
Alexander Duyck | 836a012 | 2014-12-31 10:56:06 -0800 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | l = leaf_new(key); |
| 937 | if (!l) { |
| 938 | free_leaf_info(li); |
| 939 | return NULL; |
| 940 | } |
| 941 | |
| 942 | insert_leaf_info(&l->list, li); |
| 943 | |
| 944 | /* Case 2: n is a LEAF or a TNODE and the key doesn't match. |
| 945 | * |
| 946 | * Add a new tnode here |
| 947 | * first tnode need some special handling |
| 948 | * leaves us in position for handling as case 3 |
| 949 | */ |
| 950 | if (n) { |
| 951 | struct tnode *tn; |
Alexander Duyck | 836a012 | 2014-12-31 10:56:06 -0800 | [diff] [blame] | 952 | |
Alexander Duyck | e9b4401 | 2014-12-31 10:56:12 -0800 | [diff] [blame] | 953 | tn = tnode_new(key, __fls(key ^ n->key), 1); |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 954 | if (!tn) { |
Robert Olsson | f835e47 | 2005-06-28 15:00:39 -0700 | [diff] [blame] | 955 | free_leaf_info(li); |
Alexander Duyck | 37fd30f | 2014-12-31 10:55:41 -0800 | [diff] [blame] | 956 | node_free(l); |
Stephen Hemminger | fea86ad | 2008-01-12 20:57:07 -0800 | [diff] [blame] | 957 | return NULL; |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Alexander Duyck | 836a012 | 2014-12-31 10:56:06 -0800 | [diff] [blame] | 960 | /* initialize routes out of node */ |
| 961 | NODE_INIT_PARENT(tn, tp); |
| 962 | put_child(tn, get_index(key, tn) ^ 1, n); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 963 | |
Alexander Duyck | 836a012 | 2014-12-31 10:56:06 -0800 | [diff] [blame] | 964 | /* start adding routes into the node */ |
| 965 | put_child_root(tp, t, key, tn); |
| 966 | node_set_parent(n, tn); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 967 | |
Alexander Duyck | 836a012 | 2014-12-31 10:56:06 -0800 | [diff] [blame] | 968 | /* parent now has a NULL spot where the leaf can go */ |
Alexander Duyck | e962f30 | 2014-12-10 21:49:22 -0800 | [diff] [blame] | 969 | tp = tn; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 970 | } |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 971 | |
Alexander Duyck | 836a012 | 2014-12-31 10:56:06 -0800 | [diff] [blame] | 972 | /* Case 3: n is NULL, and will just insert a new leaf */ |
| 973 | if (tp) { |
| 974 | NODE_INIT_PARENT(l, tp); |
| 975 | put_child(tp, get_index(key, tp), l); |
| 976 | trie_rebalance(t, tp); |
| 977 | } else { |
| 978 | rcu_assign_pointer(t->trie, l); |
| 979 | } |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 980 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 981 | return fa_head; |
| 982 | } |
| 983 | |
Robert Olsson | d562f1f | 2007-03-26 14:22:22 -0700 | [diff] [blame] | 984 | /* |
| 985 | * Caller must hold RTNL. |
| 986 | */ |
Stephen Hemminger | 16c6cf8 | 2009-09-20 10:35:36 +0000 | [diff] [blame] | 987 | int fib_table_insert(struct fib_table *tb, struct fib_config *cfg) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 988 | { |
| 989 | struct trie *t = (struct trie *) tb->tb_data; |
| 990 | struct fib_alias *fa, *new_fa; |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 991 | struct list_head *fa_head = NULL; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 992 | struct fib_info *fi; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 993 | int plen = cfg->fc_dst_len; |
| 994 | u8 tos = cfg->fc_tos; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 995 | u32 key, mask; |
| 996 | int err; |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 997 | struct tnode *l; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 998 | |
| 999 | if (plen > 32) |
| 1000 | return -EINVAL; |
| 1001 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1002 | key = ntohl(cfg->fc_dst); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1003 | |
Patrick McHardy | 2dfe55b | 2006-08-10 23:08:33 -0700 | [diff] [blame] | 1004 | pr_debug("Insert table=%u %08x/%d\n", tb->tb_id, key, plen); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1005 | |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 1006 | mask = ntohl(inet_make_mask(plen)); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1007 | |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 1008 | if (key & ~mask) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1009 | return -EINVAL; |
| 1010 | |
| 1011 | key = key & mask; |
| 1012 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1013 | fi = fib_create_info(cfg); |
| 1014 | if (IS_ERR(fi)) { |
| 1015 | err = PTR_ERR(fi); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1016 | goto err; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1017 | } |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1018 | |
| 1019 | l = fib_find_node(t, key); |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 1020 | fa = NULL; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1021 | |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 1022 | if (l) { |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1023 | fa_head = get_fa_head(l, plen); |
| 1024 | fa = fib_find_alias(fa_head, tos, fi->fib_priority); |
| 1025 | } |
| 1026 | |
| 1027 | /* Now fa, if non-NULL, points to the first fib alias |
| 1028 | * with the same keys [prefix,tos,priority], if such key already |
| 1029 | * exists or to the node before which we will insert new one. |
| 1030 | * |
| 1031 | * If fa is NULL, we will need to allocate a new one and |
| 1032 | * insert to the head of f. |
| 1033 | * |
| 1034 | * If f is NULL, no fib node matched the destination key |
| 1035 | * and we need to allocate a new one of those as well. |
| 1036 | */ |
| 1037 | |
Julian Anastasov | 936f6f8 | 2008-01-28 21:18:06 -0800 | [diff] [blame] | 1038 | if (fa && fa->fa_tos == tos && |
| 1039 | fa->fa_info->fib_priority == fi->fib_priority) { |
| 1040 | struct fib_alias *fa_first, *fa_match; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1041 | |
| 1042 | err = -EEXIST; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1043 | if (cfg->fc_nlflags & NLM_F_EXCL) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1044 | goto out; |
| 1045 | |
Julian Anastasov | 936f6f8 | 2008-01-28 21:18:06 -0800 | [diff] [blame] | 1046 | /* We have 2 goals: |
| 1047 | * 1. Find exact match for type, scope, fib_info to avoid |
| 1048 | * duplicate routes |
| 1049 | * 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it |
| 1050 | */ |
| 1051 | fa_match = NULL; |
| 1052 | fa_first = fa; |
| 1053 | fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list); |
| 1054 | list_for_each_entry_continue(fa, fa_head, fa_list) { |
| 1055 | if (fa->fa_tos != tos) |
| 1056 | break; |
| 1057 | if (fa->fa_info->fib_priority != fi->fib_priority) |
| 1058 | break; |
| 1059 | if (fa->fa_type == cfg->fc_type && |
Julian Anastasov | 936f6f8 | 2008-01-28 21:18:06 -0800 | [diff] [blame] | 1060 | fa->fa_info == fi) { |
| 1061 | fa_match = fa; |
| 1062 | break; |
| 1063 | } |
| 1064 | } |
| 1065 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1066 | if (cfg->fc_nlflags & NLM_F_REPLACE) { |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1067 | struct fib_info *fi_drop; |
| 1068 | u8 state; |
| 1069 | |
Julian Anastasov | 936f6f8 | 2008-01-28 21:18:06 -0800 | [diff] [blame] | 1070 | fa = fa_first; |
| 1071 | if (fa_match) { |
| 1072 | if (fa == fa_match) |
| 1073 | err = 0; |
Joonwoo Park | 6725033 | 2008-01-18 03:45:18 -0800 | [diff] [blame] | 1074 | goto out; |
Julian Anastasov | 936f6f8 | 2008-01-28 21:18:06 -0800 | [diff] [blame] | 1075 | } |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 1076 | err = -ENOBUFS; |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 1077 | new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL); |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 1078 | if (new_fa == NULL) |
| 1079 | goto out; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1080 | |
| 1081 | fi_drop = fa->fa_info; |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 1082 | new_fa->fa_tos = fa->fa_tos; |
| 1083 | new_fa->fa_info = fi; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1084 | new_fa->fa_type = cfg->fc_type; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1085 | state = fa->fa_state; |
Julian Anastasov | 936f6f8 | 2008-01-28 21:18:06 -0800 | [diff] [blame] | 1086 | new_fa->fa_state = state & ~FA_S_ACCESSED; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1087 | |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 1088 | list_replace_rcu(&fa->fa_list, &new_fa->fa_list); |
| 1089 | alias_free_mem_rcu(fa); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1090 | |
| 1091 | fib_release_info(fi_drop); |
| 1092 | if (state & FA_S_ACCESSED) |
Nicolas Dichtel | 4ccfe6d | 2012-09-07 00:45:29 +0000 | [diff] [blame] | 1093 | rt_cache_flush(cfg->fc_nlinfo.nl_net); |
Milan Kocian | b8f5583 | 2007-05-23 14:55:06 -0700 | [diff] [blame] | 1094 | rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen, |
| 1095 | tb->tb_id, &cfg->fc_nlinfo, NLM_F_REPLACE); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1096 | |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 1097 | goto succeeded; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1098 | } |
| 1099 | /* Error if we find a perfect match which |
| 1100 | * uses the same scope, type, and nexthop |
| 1101 | * information. |
| 1102 | */ |
Julian Anastasov | 936f6f8 | 2008-01-28 21:18:06 -0800 | [diff] [blame] | 1103 | if (fa_match) |
| 1104 | goto out; |
Stephen Hemminger | a07f5f5 | 2008-01-22 21:53:36 -0800 | [diff] [blame] | 1105 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1106 | if (!(cfg->fc_nlflags & NLM_F_APPEND)) |
Julian Anastasov | 936f6f8 | 2008-01-28 21:18:06 -0800 | [diff] [blame] | 1107 | fa = fa_first; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1108 | } |
| 1109 | err = -ENOENT; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1110 | if (!(cfg->fc_nlflags & NLM_F_CREATE)) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1111 | goto out; |
| 1112 | |
| 1113 | err = -ENOBUFS; |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 1114 | new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1115 | if (new_fa == NULL) |
| 1116 | goto out; |
| 1117 | |
| 1118 | new_fa->fa_info = fi; |
| 1119 | new_fa->fa_tos = tos; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1120 | new_fa->fa_type = cfg->fc_type; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1121 | new_fa->fa_state = 0; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1122 | /* |
| 1123 | * Insert new entry to the list. |
| 1124 | */ |
| 1125 | |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 1126 | if (!fa_head) { |
Stephen Hemminger | fea86ad | 2008-01-12 20:57:07 -0800 | [diff] [blame] | 1127 | fa_head = fib_insert_node(t, key, plen); |
| 1128 | if (unlikely(!fa_head)) { |
| 1129 | err = -ENOMEM; |
Robert Olsson | f835e47 | 2005-06-28 15:00:39 -0700 | [diff] [blame] | 1130 | goto out_free_new_fa; |
Stephen Hemminger | fea86ad | 2008-01-12 20:57:07 -0800 | [diff] [blame] | 1131 | } |
Robert Olsson | f835e47 | 2005-06-28 15:00:39 -0700 | [diff] [blame] | 1132 | } |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1133 | |
David S. Miller | 21d8c49 | 2011-04-14 14:49:37 -0700 | [diff] [blame] | 1134 | if (!plen) |
| 1135 | tb->tb_num_default++; |
| 1136 | |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 1137 | list_add_tail_rcu(&new_fa->fa_list, |
| 1138 | (fa ? &fa->fa_list : fa_head)); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1139 | |
Nicolas Dichtel | 4ccfe6d | 2012-09-07 00:45:29 +0000 | [diff] [blame] | 1140 | rt_cache_flush(cfg->fc_nlinfo.nl_net); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1141 | rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen, tb->tb_id, |
Milan Kocian | b8f5583 | 2007-05-23 14:55:06 -0700 | [diff] [blame] | 1142 | &cfg->fc_nlinfo, 0); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1143 | succeeded: |
| 1144 | return 0; |
Robert Olsson | f835e47 | 2005-06-28 15:00:39 -0700 | [diff] [blame] | 1145 | |
| 1146 | out_free_new_fa: |
| 1147 | kmem_cache_free(fn_alias_kmem, new_fa); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1148 | out: |
| 1149 | fib_release_info(fi); |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 1150 | err: |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1151 | return err; |
| 1152 | } |
| 1153 | |
Alexander Duyck | 9f9e636 | 2014-12-31 10:55:54 -0800 | [diff] [blame] | 1154 | static inline t_key prefix_mismatch(t_key key, struct tnode *n) |
| 1155 | { |
| 1156 | t_key prefix = n->key; |
| 1157 | |
| 1158 | return (key ^ prefix) & (prefix | -prefix); |
| 1159 | } |
| 1160 | |
Alexander Duyck | 345e9b5 | 2014-12-31 10:56:24 -0800 | [diff] [blame] | 1161 | /* should be called with rcu_read_lock */ |
David S. Miller | 22bd5b9 | 2011-03-11 19:54:08 -0500 | [diff] [blame] | 1162 | int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp, |
Eric Dumazet | ebc0ffa | 2010-10-05 10:41:36 +0000 | [diff] [blame] | 1163 | struct fib_result *res, int fib_flags) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1164 | { |
Alexander Duyck | 9f9e636 | 2014-12-31 10:55:54 -0800 | [diff] [blame] | 1165 | struct trie *t = (struct trie *)tb->tb_data; |
Alexander Duyck | 8274a97 | 2014-12-31 10:55:29 -0800 | [diff] [blame] | 1166 | #ifdef CONFIG_IP_FIB_TRIE_STATS |
| 1167 | struct trie_use_stats __percpu *stats = t->stats; |
| 1168 | #endif |
Alexander Duyck | 9f9e636 | 2014-12-31 10:55:54 -0800 | [diff] [blame] | 1169 | const t_key key = ntohl(flp->daddr); |
| 1170 | struct tnode *n, *pn; |
Alexander Duyck | 345e9b5 | 2014-12-31 10:56:24 -0800 | [diff] [blame] | 1171 | struct leaf_info *li; |
Alexander Duyck | 9f9e636 | 2014-12-31 10:55:54 -0800 | [diff] [blame] | 1172 | t_key cindex; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1173 | |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 1174 | n = rcu_dereference(t->trie); |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 1175 | if (!n) |
Alexander Duyck | 345e9b5 | 2014-12-31 10:56:24 -0800 | [diff] [blame] | 1176 | return -EAGAIN; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1177 | |
| 1178 | #ifdef CONFIG_IP_FIB_TRIE_STATS |
Alexander Duyck | 8274a97 | 2014-12-31 10:55:29 -0800 | [diff] [blame] | 1179 | this_cpu_inc(stats->gets); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1180 | #endif |
| 1181 | |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1182 | pn = n; |
Alexander Duyck | 9f9e636 | 2014-12-31 10:55:54 -0800 | [diff] [blame] | 1183 | cindex = 0; |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 1184 | |
Alexander Duyck | 9f9e636 | 2014-12-31 10:55:54 -0800 | [diff] [blame] | 1185 | /* Step 1: Travel to the longest prefix match in the trie */ |
| 1186 | for (;;) { |
| 1187 | unsigned long index = get_index(key, n); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1188 | |
Alexander Duyck | 9f9e636 | 2014-12-31 10:55:54 -0800 | [diff] [blame] | 1189 | /* This bit of code is a bit tricky but it combines multiple |
| 1190 | * checks into a single check. The prefix consists of the |
| 1191 | * prefix plus zeros for the "bits" in the prefix. The index |
| 1192 | * is the difference between the key and this value. From |
| 1193 | * this we can actually derive several pieces of data. |
| 1194 | * if !(index >> bits) |
| 1195 | * we know the value is child index |
| 1196 | * else |
| 1197 | * we have a mismatch in skip bits and failed |
| 1198 | */ |
| 1199 | if (index >> n->bits) |
| 1200 | break; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1201 | |
Alexander Duyck | 9f9e636 | 2014-12-31 10:55:54 -0800 | [diff] [blame] | 1202 | /* we have found a leaf. Prefixes have already been compared */ |
| 1203 | if (IS_LEAF(n)) |
Stephen Hemminger | a07f5f5 | 2008-01-22 21:53:36 -0800 | [diff] [blame] | 1204 | goto found; |
Alexander Duyck | 9f9e636 | 2014-12-31 10:55:54 -0800 | [diff] [blame] | 1205 | |
| 1206 | /* only record pn and cindex if we are going to be chopping |
| 1207 | * bits later. Otherwise we are just wasting cycles. |
| 1208 | */ |
| 1209 | if (index) { |
| 1210 | pn = n; |
| 1211 | cindex = index; |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
Alexander Duyck | 21d1f11 | 2014-12-31 10:57:02 -0800 | [diff] [blame^] | 1214 | n = tnode_get_child_rcu(n, index); |
Alexander Duyck | 9f9e636 | 2014-12-31 10:55:54 -0800 | [diff] [blame] | 1215 | if (unlikely(!n)) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1216 | goto backtrace; |
Alexander Duyck | 9f9e636 | 2014-12-31 10:55:54 -0800 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | /* Step 2: Sort out leaves and begin backtracing for longest prefix */ |
| 1220 | for (;;) { |
| 1221 | /* record the pointer where our next node pointer is stored */ |
| 1222 | struct tnode __rcu **cptr = n->child; |
| 1223 | |
| 1224 | /* This test verifies that none of the bits that differ |
| 1225 | * between the key and the prefix exist in the region of |
| 1226 | * the lsb and higher in the prefix. |
| 1227 | */ |
| 1228 | if (unlikely(prefix_mismatch(key, n))) |
| 1229 | goto backtrace; |
| 1230 | |
| 1231 | /* exit out and process leaf */ |
| 1232 | if (unlikely(IS_LEAF(n))) |
| 1233 | break; |
| 1234 | |
| 1235 | /* Don't bother recording parent info. Since we are in |
| 1236 | * prefix match mode we will have to come back to wherever |
| 1237 | * we started this traversal anyway |
| 1238 | */ |
| 1239 | |
| 1240 | while ((n = rcu_dereference(*cptr)) == NULL) { |
| 1241 | backtrace: |
| 1242 | #ifdef CONFIG_IP_FIB_TRIE_STATS |
| 1243 | if (!n) |
| 1244 | this_cpu_inc(stats->null_node_hit); |
| 1245 | #endif |
| 1246 | /* If we are at cindex 0 there are no more bits for |
| 1247 | * us to strip at this level so we must ascend back |
| 1248 | * up one level to see if there are any more bits to |
| 1249 | * be stripped there. |
| 1250 | */ |
| 1251 | while (!cindex) { |
| 1252 | t_key pkey = pn->key; |
| 1253 | |
| 1254 | pn = node_parent_rcu(pn); |
| 1255 | if (unlikely(!pn)) |
Alexander Duyck | 345e9b5 | 2014-12-31 10:56:24 -0800 | [diff] [blame] | 1256 | return -EAGAIN; |
Alexander Duyck | 9f9e636 | 2014-12-31 10:55:54 -0800 | [diff] [blame] | 1257 | #ifdef CONFIG_IP_FIB_TRIE_STATS |
| 1258 | this_cpu_inc(stats->backtrack); |
| 1259 | #endif |
| 1260 | /* Get Child's index */ |
| 1261 | cindex = get_index(pkey, pn); |
| 1262 | } |
| 1263 | |
| 1264 | /* strip the least significant bit from the cindex */ |
| 1265 | cindex &= cindex - 1; |
| 1266 | |
| 1267 | /* grab pointer for next child node */ |
| 1268 | cptr = &pn->child[cindex]; |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 1269 | } |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1270 | } |
Alexander Duyck | 9f9e636 | 2014-12-31 10:55:54 -0800 | [diff] [blame] | 1271 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1272 | found: |
Alexander Duyck | 9f9e636 | 2014-12-31 10:55:54 -0800 | [diff] [blame] | 1273 | /* Step 3: Process the leaf, if that fails fall back to backtracing */ |
Alexander Duyck | 345e9b5 | 2014-12-31 10:56:24 -0800 | [diff] [blame] | 1274 | hlist_for_each_entry_rcu(li, &n->list, hlist) { |
| 1275 | struct fib_alias *fa; |
| 1276 | |
| 1277 | if ((key ^ n->key) & li->mask_plen) |
| 1278 | continue; |
| 1279 | |
| 1280 | list_for_each_entry_rcu(fa, &li->falh, fa_list) { |
| 1281 | struct fib_info *fi = fa->fa_info; |
| 1282 | int nhsel, err; |
| 1283 | |
| 1284 | if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos) |
| 1285 | continue; |
| 1286 | if (fi->fib_dead) |
| 1287 | continue; |
| 1288 | if (fa->fa_info->fib_scope < flp->flowi4_scope) |
| 1289 | continue; |
| 1290 | fib_alias_accessed(fa); |
| 1291 | err = fib_props[fa->fa_type].error; |
| 1292 | if (unlikely(err < 0)) { |
| 1293 | #ifdef CONFIG_IP_FIB_TRIE_STATS |
| 1294 | this_cpu_inc(stats->semantic_match_passed); |
| 1295 | #endif |
| 1296 | return err; |
| 1297 | } |
| 1298 | if (fi->fib_flags & RTNH_F_DEAD) |
| 1299 | continue; |
| 1300 | for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) { |
| 1301 | const struct fib_nh *nh = &fi->fib_nh[nhsel]; |
| 1302 | |
| 1303 | if (nh->nh_flags & RTNH_F_DEAD) |
| 1304 | continue; |
| 1305 | if (flp->flowi4_oif && flp->flowi4_oif != nh->nh_oif) |
| 1306 | continue; |
| 1307 | |
| 1308 | if (!(fib_flags & FIB_LOOKUP_NOREF)) |
| 1309 | atomic_inc(&fi->fib_clntref); |
| 1310 | |
| 1311 | res->prefixlen = li->plen; |
| 1312 | res->nh_sel = nhsel; |
| 1313 | res->type = fa->fa_type; |
| 1314 | res->scope = fi->fib_scope; |
| 1315 | res->fi = fi; |
| 1316 | res->table = tb; |
| 1317 | res->fa_head = &li->falh; |
| 1318 | #ifdef CONFIG_IP_FIB_TRIE_STATS |
| 1319 | this_cpu_inc(stats->semantic_match_passed); |
| 1320 | #endif |
| 1321 | return err; |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | #ifdef CONFIG_IP_FIB_TRIE_STATS |
| 1326 | this_cpu_inc(stats->semantic_match_miss); |
| 1327 | #endif |
| 1328 | } |
| 1329 | goto backtrace; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1330 | } |
Florian Westphal | 6fc0143 | 2011-08-25 13:46:12 +0200 | [diff] [blame] | 1331 | EXPORT_SYMBOL_GPL(fib_table_lookup); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1332 | |
Stephen Hemminger | 9195bef | 2008-01-22 21:56:34 -0800 | [diff] [blame] | 1333 | /* |
| 1334 | * Remove the leaf and return parent. |
| 1335 | */ |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1336 | static void trie_leaf_remove(struct trie *t, struct tnode *l) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1337 | { |
Alexander Duyck | 64c9b6f | 2014-12-31 10:55:35 -0800 | [diff] [blame] | 1338 | struct tnode *tp = node_parent(l); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1339 | |
Stephen Hemminger | 9195bef | 2008-01-22 21:56:34 -0800 | [diff] [blame] | 1340 | pr_debug("entering trie_leaf_remove(%p)\n", l); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1341 | |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 1342 | if (tp) { |
Alexander Duyck | 836a012 | 2014-12-31 10:56:06 -0800 | [diff] [blame] | 1343 | put_child(tp, get_index(l->key, tp), NULL); |
Jarek Poplawski | 7b85576 | 2009-06-18 00:28:51 -0700 | [diff] [blame] | 1344 | trie_rebalance(t, tp); |
Alexander Duyck | 836a012 | 2014-12-31 10:56:06 -0800 | [diff] [blame] | 1345 | } else { |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 1346 | RCU_INIT_POINTER(t->trie, NULL); |
Alexander Duyck | 836a012 | 2014-12-31 10:56:06 -0800 | [diff] [blame] | 1347 | } |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1348 | |
Alexander Duyck | 37fd30f | 2014-12-31 10:55:41 -0800 | [diff] [blame] | 1349 | node_free(l); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1350 | } |
| 1351 | |
Robert Olsson | d562f1f | 2007-03-26 14:22:22 -0700 | [diff] [blame] | 1352 | /* |
| 1353 | * Caller must hold RTNL. |
| 1354 | */ |
Stephen Hemminger | 16c6cf8 | 2009-09-20 10:35:36 +0000 | [diff] [blame] | 1355 | int fib_table_delete(struct fib_table *tb, struct fib_config *cfg) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1356 | { |
| 1357 | struct trie *t = (struct trie *) tb->tb_data; |
| 1358 | u32 key, mask; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1359 | int plen = cfg->fc_dst_len; |
| 1360 | u8 tos = cfg->fc_tos; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1361 | struct fib_alias *fa, *fa_to_delete; |
| 1362 | struct list_head *fa_head; |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1363 | struct tnode *l; |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 1364 | struct leaf_info *li; |
| 1365 | |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 1366 | if (plen > 32) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1367 | return -EINVAL; |
| 1368 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1369 | key = ntohl(cfg->fc_dst); |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 1370 | mask = ntohl(inet_make_mask(plen)); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1371 | |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 1372 | if (key & ~mask) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1373 | return -EINVAL; |
| 1374 | |
| 1375 | key = key & mask; |
| 1376 | l = fib_find_node(t, key); |
| 1377 | |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 1378 | if (!l) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1379 | return -ESRCH; |
| 1380 | |
Igor Maravic | ad5b310 | 2012-08-13 10:26:08 +0200 | [diff] [blame] | 1381 | li = find_leaf_info(l, plen); |
| 1382 | |
| 1383 | if (!li) |
| 1384 | return -ESRCH; |
| 1385 | |
| 1386 | fa_head = &li->falh; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1387 | fa = fib_find_alias(fa_head, tos, 0); |
| 1388 | |
| 1389 | if (!fa) |
| 1390 | return -ESRCH; |
| 1391 | |
Stephen Hemminger | 0c7770c | 2005-08-23 21:59:41 -0700 | [diff] [blame] | 1392 | pr_debug("Deleting %08x/%d tos=%d t=%p\n", key, plen, tos, t); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1393 | |
| 1394 | fa_to_delete = NULL; |
Julian Anastasov | 936f6f8 | 2008-01-28 21:18:06 -0800 | [diff] [blame] | 1395 | fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list); |
| 1396 | list_for_each_entry_continue(fa, fa_head, fa_list) { |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1397 | struct fib_info *fi = fa->fa_info; |
| 1398 | |
| 1399 | if (fa->fa_tos != tos) |
| 1400 | break; |
| 1401 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1402 | if ((!cfg->fc_type || fa->fa_type == cfg->fc_type) && |
| 1403 | (cfg->fc_scope == RT_SCOPE_NOWHERE || |
David S. Miller | 37e826c | 2011-03-24 18:06:47 -0700 | [diff] [blame] | 1404 | fa->fa_info->fib_scope == cfg->fc_scope) && |
Julian Anastasov | 74cb3c1 | 2011-03-19 12:13:46 +0000 | [diff] [blame] | 1405 | (!cfg->fc_prefsrc || |
| 1406 | fi->fib_prefsrc == cfg->fc_prefsrc) && |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1407 | (!cfg->fc_protocol || |
| 1408 | fi->fib_protocol == cfg->fc_protocol) && |
| 1409 | fib_nh_match(cfg, fi) == 0) { |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1410 | fa_to_delete = fa; |
| 1411 | break; |
| 1412 | } |
| 1413 | } |
| 1414 | |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 1415 | if (!fa_to_delete) |
| 1416 | return -ESRCH; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1417 | |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 1418 | fa = fa_to_delete; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1419 | rtmsg_fib(RTM_DELROUTE, htonl(key), fa, plen, tb->tb_id, |
Milan Kocian | b8f5583 | 2007-05-23 14:55:06 -0700 | [diff] [blame] | 1420 | &cfg->fc_nlinfo, 0); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1421 | |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 1422 | list_del_rcu(&fa->fa_list); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1423 | |
David S. Miller | 21d8c49 | 2011-04-14 14:49:37 -0700 | [diff] [blame] | 1424 | if (!plen) |
| 1425 | tb->tb_num_default--; |
| 1426 | |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 1427 | if (list_empty(fa_head)) { |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 1428 | hlist_del_rcu(&li->hlist); |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 1429 | free_leaf_info(li); |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 1430 | } |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 1431 | |
| 1432 | if (hlist_empty(&l->list)) |
Stephen Hemminger | 9195bef | 2008-01-22 21:56:34 -0800 | [diff] [blame] | 1433 | trie_leaf_remove(t, l); |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 1434 | |
| 1435 | if (fa->fa_state & FA_S_ACCESSED) |
Nicolas Dichtel | 4ccfe6d | 2012-09-07 00:45:29 +0000 | [diff] [blame] | 1436 | rt_cache_flush(cfg->fc_nlinfo.nl_net); |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 1437 | |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 1438 | fib_release_info(fa->fa_info); |
| 1439 | alias_free_mem_rcu(fa); |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 1440 | return 0; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1441 | } |
| 1442 | |
Stephen Hemminger | ef3660c | 2008-04-10 03:46:12 -0700 | [diff] [blame] | 1443 | static int trie_flush_list(struct list_head *head) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1444 | { |
| 1445 | struct fib_alias *fa, *fa_node; |
| 1446 | int found = 0; |
| 1447 | |
| 1448 | list_for_each_entry_safe(fa, fa_node, head, fa_list) { |
| 1449 | struct fib_info *fi = fa->fa_info; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1450 | |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 1451 | if (fi && (fi->fib_flags & RTNH_F_DEAD)) { |
| 1452 | list_del_rcu(&fa->fa_list); |
| 1453 | fib_release_info(fa->fa_info); |
| 1454 | alias_free_mem_rcu(fa); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1455 | found++; |
| 1456 | } |
| 1457 | } |
| 1458 | return found; |
| 1459 | } |
| 1460 | |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1461 | static int trie_flush_leaf(struct tnode *l) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1462 | { |
| 1463 | int found = 0; |
| 1464 | struct hlist_head *lih = &l->list; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1465 | struct hlist_node *tmp; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1466 | struct leaf_info *li = NULL; |
| 1467 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1468 | hlist_for_each_entry_safe(li, tmp, lih, hlist) { |
Stephen Hemminger | ef3660c | 2008-04-10 03:46:12 -0700 | [diff] [blame] | 1469 | found += trie_flush_list(&li->falh); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1470 | |
| 1471 | if (list_empty(&li->falh)) { |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 1472 | hlist_del_rcu(&li->hlist); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1473 | free_leaf_info(li); |
| 1474 | } |
| 1475 | } |
| 1476 | return found; |
| 1477 | } |
| 1478 | |
Stephen Hemminger | 82cfbb0 | 2008-01-22 21:55:32 -0800 | [diff] [blame] | 1479 | /* |
| 1480 | * Scan for the next right leaf starting at node p->child[idx] |
| 1481 | * Since we have back pointer, no recursion necessary. |
| 1482 | */ |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1483 | static struct tnode *leaf_walk_rcu(struct tnode *p, struct tnode *c) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1484 | { |
Stephen Hemminger | 82cfbb0 | 2008-01-22 21:55:32 -0800 | [diff] [blame] | 1485 | do { |
Alexander Duyck | 98293e8 | 2014-12-31 10:56:18 -0800 | [diff] [blame] | 1486 | unsigned long idx = c ? idx = get_index(c->key, p) + 1 : 0; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1487 | |
Alexander Duyck | 98293e8 | 2014-12-31 10:56:18 -0800 | [diff] [blame] | 1488 | while (idx < tnode_child_length(p)) { |
Stephen Hemminger | 82cfbb0 | 2008-01-22 21:55:32 -0800 | [diff] [blame] | 1489 | c = tnode_get_child_rcu(p, idx++); |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 1490 | if (!c) |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 1491 | continue; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1492 | |
Eric Dumazet | aab515d | 2013-08-05 11:18:49 -0700 | [diff] [blame] | 1493 | if (IS_LEAF(c)) |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1494 | return c; |
Stephen Hemminger | 82cfbb0 | 2008-01-22 21:55:32 -0800 | [diff] [blame] | 1495 | |
| 1496 | /* Rescan start scanning in new node */ |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1497 | p = c; |
Stephen Hemminger | 82cfbb0 | 2008-01-22 21:55:32 -0800 | [diff] [blame] | 1498 | idx = 0; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1499 | } |
Stephen Hemminger | 82cfbb0 | 2008-01-22 21:55:32 -0800 | [diff] [blame] | 1500 | |
| 1501 | /* Node empty, walk back up to parent */ |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1502 | c = p; |
Eric Dumazet | a034ee3 | 2010-09-09 23:32:28 +0000 | [diff] [blame] | 1503 | } while ((p = node_parent_rcu(c)) != NULL); |
Stephen Hemminger | 82cfbb0 | 2008-01-22 21:55:32 -0800 | [diff] [blame] | 1504 | |
| 1505 | return NULL; /* Root of trie */ |
| 1506 | } |
| 1507 | |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1508 | static struct tnode *trie_firstleaf(struct trie *t) |
Stephen Hemminger | 82cfbb0 | 2008-01-22 21:55:32 -0800 | [diff] [blame] | 1509 | { |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1510 | struct tnode *n = rcu_dereference_rtnl(t->trie); |
Stephen Hemminger | 82cfbb0 | 2008-01-22 21:55:32 -0800 | [diff] [blame] | 1511 | |
| 1512 | if (!n) |
| 1513 | return NULL; |
| 1514 | |
| 1515 | if (IS_LEAF(n)) /* trie is just a leaf */ |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1516 | return n; |
Stephen Hemminger | 82cfbb0 | 2008-01-22 21:55:32 -0800 | [diff] [blame] | 1517 | |
| 1518 | return leaf_walk_rcu(n, NULL); |
| 1519 | } |
| 1520 | |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1521 | static struct tnode *trie_nextleaf(struct tnode *l) |
Stephen Hemminger | 82cfbb0 | 2008-01-22 21:55:32 -0800 | [diff] [blame] | 1522 | { |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1523 | struct tnode *p = node_parent_rcu(l); |
Stephen Hemminger | 82cfbb0 | 2008-01-22 21:55:32 -0800 | [diff] [blame] | 1524 | |
| 1525 | if (!p) |
| 1526 | return NULL; /* trie with just one leaf */ |
| 1527 | |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1528 | return leaf_walk_rcu(p, l); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1529 | } |
| 1530 | |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1531 | static struct tnode *trie_leafindex(struct trie *t, int index) |
Stephen Hemminger | 71d67e6 | 2008-01-31 16:45:47 -0800 | [diff] [blame] | 1532 | { |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1533 | struct tnode *l = trie_firstleaf(t); |
Stephen Hemminger | 71d67e6 | 2008-01-31 16:45:47 -0800 | [diff] [blame] | 1534 | |
Stephen Hemminger | ec28cf7 | 2008-02-11 21:12:49 -0800 | [diff] [blame] | 1535 | while (l && index-- > 0) |
Stephen Hemminger | 71d67e6 | 2008-01-31 16:45:47 -0800 | [diff] [blame] | 1536 | l = trie_nextleaf(l); |
Stephen Hemminger | ec28cf7 | 2008-02-11 21:12:49 -0800 | [diff] [blame] | 1537 | |
Stephen Hemminger | 71d67e6 | 2008-01-31 16:45:47 -0800 | [diff] [blame] | 1538 | return l; |
| 1539 | } |
| 1540 | |
| 1541 | |
Robert Olsson | d562f1f | 2007-03-26 14:22:22 -0700 | [diff] [blame] | 1542 | /* |
| 1543 | * Caller must hold RTNL. |
| 1544 | */ |
Stephen Hemminger | 16c6cf8 | 2009-09-20 10:35:36 +0000 | [diff] [blame] | 1545 | int fib_table_flush(struct fib_table *tb) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1546 | { |
| 1547 | struct trie *t = (struct trie *) tb->tb_data; |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1548 | struct tnode *l, *ll = NULL; |
Stephen Hemminger | 82cfbb0 | 2008-01-22 21:55:32 -0800 | [diff] [blame] | 1549 | int found = 0; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1550 | |
Stephen Hemminger | 82cfbb0 | 2008-01-22 21:55:32 -0800 | [diff] [blame] | 1551 | for (l = trie_firstleaf(t); l; l = trie_nextleaf(l)) { |
Stephen Hemminger | ef3660c | 2008-04-10 03:46:12 -0700 | [diff] [blame] | 1552 | found += trie_flush_leaf(l); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1553 | |
| 1554 | if (ll && hlist_empty(&ll->list)) |
Stephen Hemminger | 9195bef | 2008-01-22 21:56:34 -0800 | [diff] [blame] | 1555 | trie_leaf_remove(t, ll); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1556 | ll = l; |
| 1557 | } |
| 1558 | |
| 1559 | if (ll && hlist_empty(&ll->list)) |
Stephen Hemminger | 9195bef | 2008-01-22 21:56:34 -0800 | [diff] [blame] | 1560 | trie_leaf_remove(t, ll); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1561 | |
Stephen Hemminger | 0c7770c | 2005-08-23 21:59:41 -0700 | [diff] [blame] | 1562 | pr_debug("trie_flush found=%d\n", found); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1563 | return found; |
| 1564 | } |
| 1565 | |
Pavel Emelyanov | 4aa2c46 | 2010-10-28 02:00:43 +0000 | [diff] [blame] | 1566 | void fib_free_table(struct fib_table *tb) |
| 1567 | { |
Alexander Duyck | 8274a97 | 2014-12-31 10:55:29 -0800 | [diff] [blame] | 1568 | #ifdef CONFIG_IP_FIB_TRIE_STATS |
| 1569 | struct trie *t = (struct trie *)tb->tb_data; |
| 1570 | |
| 1571 | free_percpu(t->stats); |
| 1572 | #endif /* CONFIG_IP_FIB_TRIE_STATS */ |
Pavel Emelyanov | 4aa2c46 | 2010-10-28 02:00:43 +0000 | [diff] [blame] | 1573 | kfree(tb); |
| 1574 | } |
| 1575 | |
Stephen Hemminger | a07f5f5 | 2008-01-22 21:53:36 -0800 | [diff] [blame] | 1576 | static int fn_trie_dump_fa(t_key key, int plen, struct list_head *fah, |
| 1577 | struct fib_table *tb, |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1578 | struct sk_buff *skb, struct netlink_callback *cb) |
| 1579 | { |
| 1580 | int i, s_i; |
| 1581 | struct fib_alias *fa; |
Al Viro | 32ab5f8 | 2006-09-26 22:21:45 -0700 | [diff] [blame] | 1582 | __be32 xkey = htonl(key); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1583 | |
Stephen Hemminger | 71d67e6 | 2008-01-31 16:45:47 -0800 | [diff] [blame] | 1584 | s_i = cb->args[5]; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1585 | i = 0; |
| 1586 | |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 1587 | /* rcu_read_lock is hold by caller */ |
| 1588 | |
| 1589 | list_for_each_entry_rcu(fa, fah, fa_list) { |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1590 | if (i < s_i) { |
| 1591 | i++; |
| 1592 | continue; |
| 1593 | } |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1594 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1595 | if (fib_dump_info(skb, NETLINK_CB(cb->skb).portid, |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1596 | cb->nlh->nlmsg_seq, |
| 1597 | RTM_NEWROUTE, |
| 1598 | tb->tb_id, |
| 1599 | fa->fa_type, |
Thomas Graf | be403ea | 2006-08-17 18:15:17 -0700 | [diff] [blame] | 1600 | xkey, |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1601 | plen, |
| 1602 | fa->fa_tos, |
Stephen Hemminger | 64347f7 | 2008-01-22 21:55:01 -0800 | [diff] [blame] | 1603 | fa->fa_info, NLM_F_MULTI) < 0) { |
Stephen Hemminger | 71d67e6 | 2008-01-31 16:45:47 -0800 | [diff] [blame] | 1604 | cb->args[5] = i; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1605 | return -1; |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 1606 | } |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1607 | i++; |
| 1608 | } |
Stephen Hemminger | 71d67e6 | 2008-01-31 16:45:47 -0800 | [diff] [blame] | 1609 | cb->args[5] = i; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1610 | return skb->len; |
| 1611 | } |
| 1612 | |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1613 | static int fn_trie_dump_leaf(struct tnode *l, struct fib_table *tb, |
Stephen Hemminger | a88ee22 | 2008-01-22 21:56:11 -0800 | [diff] [blame] | 1614 | struct sk_buff *skb, struct netlink_callback *cb) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1615 | { |
Stephen Hemminger | a88ee22 | 2008-01-22 21:56:11 -0800 | [diff] [blame] | 1616 | struct leaf_info *li; |
Stephen Hemminger | a88ee22 | 2008-01-22 21:56:11 -0800 | [diff] [blame] | 1617 | int i, s_i; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1618 | |
Stephen Hemminger | 71d67e6 | 2008-01-31 16:45:47 -0800 | [diff] [blame] | 1619 | s_i = cb->args[4]; |
Stephen Hemminger | a88ee22 | 2008-01-22 21:56:11 -0800 | [diff] [blame] | 1620 | i = 0; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1621 | |
Stephen Hemminger | a88ee22 | 2008-01-22 21:56:11 -0800 | [diff] [blame] | 1622 | /* rcu_read_lock is hold by caller */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1623 | hlist_for_each_entry_rcu(li, &l->list, hlist) { |
Stephen Hemminger | a88ee22 | 2008-01-22 21:56:11 -0800 | [diff] [blame] | 1624 | if (i < s_i) { |
| 1625 | i++; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1626 | continue; |
Stephen Hemminger | a88ee22 | 2008-01-22 21:56:11 -0800 | [diff] [blame] | 1627 | } |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1628 | |
Stephen Hemminger | a88ee22 | 2008-01-22 21:56:11 -0800 | [diff] [blame] | 1629 | if (i > s_i) |
Stephen Hemminger | 71d67e6 | 2008-01-31 16:45:47 -0800 | [diff] [blame] | 1630 | cb->args[5] = 0; |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 1631 | |
Stephen Hemminger | a88ee22 | 2008-01-22 21:56:11 -0800 | [diff] [blame] | 1632 | if (list_empty(&li->falh)) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1633 | continue; |
| 1634 | |
Stephen Hemminger | a88ee22 | 2008-01-22 21:56:11 -0800 | [diff] [blame] | 1635 | if (fn_trie_dump_fa(l->key, li->plen, &li->falh, tb, skb, cb) < 0) { |
Stephen Hemminger | 71d67e6 | 2008-01-31 16:45:47 -0800 | [diff] [blame] | 1636 | cb->args[4] = i; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1637 | return -1; |
| 1638 | } |
Stephen Hemminger | a88ee22 | 2008-01-22 21:56:11 -0800 | [diff] [blame] | 1639 | i++; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1640 | } |
Stephen Hemminger | a88ee22 | 2008-01-22 21:56:11 -0800 | [diff] [blame] | 1641 | |
Stephen Hemminger | 71d67e6 | 2008-01-31 16:45:47 -0800 | [diff] [blame] | 1642 | cb->args[4] = i; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1643 | return skb->len; |
| 1644 | } |
| 1645 | |
Stephen Hemminger | 16c6cf8 | 2009-09-20 10:35:36 +0000 | [diff] [blame] | 1646 | int fib_table_dump(struct fib_table *tb, struct sk_buff *skb, |
| 1647 | struct netlink_callback *cb) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1648 | { |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1649 | struct tnode *l; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1650 | struct trie *t = (struct trie *) tb->tb_data; |
Stephen Hemminger | d5ce8a0 | 2008-01-22 21:57:22 -0800 | [diff] [blame] | 1651 | t_key key = cb->args[2]; |
Stephen Hemminger | 71d67e6 | 2008-01-31 16:45:47 -0800 | [diff] [blame] | 1652 | int count = cb->args[3]; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1653 | |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 1654 | rcu_read_lock(); |
Stephen Hemminger | d5ce8a0 | 2008-01-22 21:57:22 -0800 | [diff] [blame] | 1655 | /* Dump starting at last key. |
| 1656 | * Note: 0.0.0.0/0 (ie default) is first key. |
| 1657 | */ |
Stephen Hemminger | 71d67e6 | 2008-01-31 16:45:47 -0800 | [diff] [blame] | 1658 | if (count == 0) |
Stephen Hemminger | d5ce8a0 | 2008-01-22 21:57:22 -0800 | [diff] [blame] | 1659 | l = trie_firstleaf(t); |
| 1660 | else { |
Stephen Hemminger | 71d67e6 | 2008-01-31 16:45:47 -0800 | [diff] [blame] | 1661 | /* Normally, continue from last key, but if that is missing |
| 1662 | * fallback to using slow rescan |
| 1663 | */ |
Stephen Hemminger | d5ce8a0 | 2008-01-22 21:57:22 -0800 | [diff] [blame] | 1664 | l = fib_find_node(t, key); |
Stephen Hemminger | 71d67e6 | 2008-01-31 16:45:47 -0800 | [diff] [blame] | 1665 | if (!l) |
| 1666 | l = trie_leafindex(t, count); |
Stephen Hemminger | d5ce8a0 | 2008-01-22 21:57:22 -0800 | [diff] [blame] | 1667 | } |
Stephen Hemminger | a88ee22 | 2008-01-22 21:56:11 -0800 | [diff] [blame] | 1668 | |
Stephen Hemminger | d5ce8a0 | 2008-01-22 21:57:22 -0800 | [diff] [blame] | 1669 | while (l) { |
| 1670 | cb->args[2] = l->key; |
Stephen Hemminger | a88ee22 | 2008-01-22 21:56:11 -0800 | [diff] [blame] | 1671 | if (fn_trie_dump_leaf(l, tb, skb, cb) < 0) { |
Stephen Hemminger | 71d67e6 | 2008-01-31 16:45:47 -0800 | [diff] [blame] | 1672 | cb->args[3] = count; |
Stephen Hemminger | a88ee22 | 2008-01-22 21:56:11 -0800 | [diff] [blame] | 1673 | rcu_read_unlock(); |
Stephen Hemminger | a88ee22 | 2008-01-22 21:56:11 -0800 | [diff] [blame] | 1674 | return -1; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1675 | } |
Stephen Hemminger | d5ce8a0 | 2008-01-22 21:57:22 -0800 | [diff] [blame] | 1676 | |
Stephen Hemminger | 71d67e6 | 2008-01-31 16:45:47 -0800 | [diff] [blame] | 1677 | ++count; |
Stephen Hemminger | d5ce8a0 | 2008-01-22 21:57:22 -0800 | [diff] [blame] | 1678 | l = trie_nextleaf(l); |
Stephen Hemminger | 71d67e6 | 2008-01-31 16:45:47 -0800 | [diff] [blame] | 1679 | memset(&cb->args[4], 0, |
| 1680 | sizeof(cb->args) - 4*sizeof(cb->args[0])); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1681 | } |
Stephen Hemminger | 71d67e6 | 2008-01-31 16:45:47 -0800 | [diff] [blame] | 1682 | cb->args[3] = count; |
Robert Olsson | 2373ce1 | 2005-08-25 13:01:29 -0700 | [diff] [blame] | 1683 | rcu_read_unlock(); |
Stephen Hemminger | a88ee22 | 2008-01-22 21:56:11 -0800 | [diff] [blame] | 1684 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1685 | return skb->len; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1686 | } |
| 1687 | |
David S. Miller | 5348ba8 | 2011-02-01 15:30:56 -0800 | [diff] [blame] | 1688 | void __init fib_trie_init(void) |
Stephen Hemminger | 7f9b805 | 2008-01-14 23:14:20 -0800 | [diff] [blame] | 1689 | { |
Stephen Hemminger | a07f5f5 | 2008-01-22 21:53:36 -0800 | [diff] [blame] | 1690 | fn_alias_kmem = kmem_cache_create("ip_fib_alias", |
| 1691 | sizeof(struct fib_alias), |
Stephen Hemminger | bc3c8c1 | 2008-01-22 21:51:50 -0800 | [diff] [blame] | 1692 | 0, SLAB_PANIC, NULL); |
| 1693 | |
| 1694 | trie_leaf_kmem = kmem_cache_create("ip_fib_trie", |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1695 | max(sizeof(struct tnode), |
Stephen Hemminger | bc3c8c1 | 2008-01-22 21:51:50 -0800 | [diff] [blame] | 1696 | sizeof(struct leaf_info)), |
| 1697 | 0, SLAB_PANIC, NULL); |
Stephen Hemminger | 7f9b805 | 2008-01-14 23:14:20 -0800 | [diff] [blame] | 1698 | } |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1699 | |
Stephen Hemminger | 7f9b805 | 2008-01-14 23:14:20 -0800 | [diff] [blame] | 1700 | |
David S. Miller | 5348ba8 | 2011-02-01 15:30:56 -0800 | [diff] [blame] | 1701 | struct fib_table *fib_trie_table(u32 id) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1702 | { |
| 1703 | struct fib_table *tb; |
| 1704 | struct trie *t; |
| 1705 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1706 | tb = kmalloc(sizeof(struct fib_table) + sizeof(struct trie), |
| 1707 | GFP_KERNEL); |
| 1708 | if (tb == NULL) |
| 1709 | return NULL; |
| 1710 | |
| 1711 | tb->tb_id = id; |
Denis V. Lunev | 971b893 | 2007-12-08 00:32:23 -0800 | [diff] [blame] | 1712 | tb->tb_default = -1; |
David S. Miller | 21d8c49 | 2011-04-14 14:49:37 -0700 | [diff] [blame] | 1713 | tb->tb_num_default = 0; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1714 | |
| 1715 | t = (struct trie *) tb->tb_data; |
Alexander Duyck | 8274a97 | 2014-12-31 10:55:29 -0800 | [diff] [blame] | 1716 | RCU_INIT_POINTER(t->trie, NULL); |
| 1717 | #ifdef CONFIG_IP_FIB_TRIE_STATS |
| 1718 | t->stats = alloc_percpu(struct trie_use_stats); |
| 1719 | if (!t->stats) { |
| 1720 | kfree(tb); |
| 1721 | tb = NULL; |
| 1722 | } |
| 1723 | #endif |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1724 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1725 | return tb; |
| 1726 | } |
| 1727 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1728 | #ifdef CONFIG_PROC_FS |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1729 | /* Depth first Trie walk iterator */ |
| 1730 | struct fib_trie_iter { |
Denis V. Lunev | 1c340b2 | 2008-01-10 03:27:17 -0800 | [diff] [blame] | 1731 | struct seq_net_private p; |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 1732 | struct fib_table *tb; |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1733 | struct tnode *tnode; |
Eric Dumazet | a034ee3 | 2010-09-09 23:32:28 +0000 | [diff] [blame] | 1734 | unsigned int index; |
| 1735 | unsigned int depth; |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1736 | }; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1737 | |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1738 | static struct tnode *fib_trie_get_next(struct fib_trie_iter *iter) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1739 | { |
Alexander Duyck | 98293e8 | 2014-12-31 10:56:18 -0800 | [diff] [blame] | 1740 | unsigned long cindex = iter->index; |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1741 | struct tnode *tn = iter->tnode; |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1742 | struct tnode *p; |
| 1743 | |
Eric W. Biederman | 6640e69 | 2007-01-24 14:42:04 -0800 | [diff] [blame] | 1744 | /* A single entry routing table */ |
| 1745 | if (!tn) |
| 1746 | return NULL; |
| 1747 | |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1748 | pr_debug("get_next iter={node=%p index=%d depth=%d}\n", |
| 1749 | iter->tnode, iter->index, iter->depth); |
| 1750 | rescan: |
Alexander Duyck | 98293e8 | 2014-12-31 10:56:18 -0800 | [diff] [blame] | 1751 | while (cindex < tnode_child_length(tn)) { |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1752 | struct tnode *n = tnode_get_child_rcu(tn, cindex); |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1753 | |
| 1754 | if (n) { |
| 1755 | if (IS_LEAF(n)) { |
| 1756 | iter->tnode = tn; |
| 1757 | iter->index = cindex + 1; |
| 1758 | } else { |
| 1759 | /* push down one level */ |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1760 | iter->tnode = n; |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1761 | iter->index = 0; |
| 1762 | ++iter->depth; |
| 1763 | } |
| 1764 | return n; |
| 1765 | } |
| 1766 | |
| 1767 | ++cindex; |
| 1768 | } |
| 1769 | |
| 1770 | /* Current node exhausted, pop back up */ |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1771 | p = node_parent_rcu(tn); |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1772 | if (p) { |
Alexander Duyck | e9b4401 | 2014-12-31 10:56:12 -0800 | [diff] [blame] | 1773 | cindex = get_index(tn->key, p) + 1; |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1774 | tn = p; |
| 1775 | --iter->depth; |
| 1776 | goto rescan; |
| 1777 | } |
| 1778 | |
| 1779 | /* got root? */ |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1780 | return NULL; |
| 1781 | } |
| 1782 | |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1783 | static struct tnode *fib_trie_get_first(struct fib_trie_iter *iter, |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1784 | struct trie *t) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1785 | { |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1786 | struct tnode *n; |
Robert Olsson | 5ddf0eb | 2006-03-20 21:34:12 -0800 | [diff] [blame] | 1787 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1788 | if (!t) |
Robert Olsson | 5ddf0eb | 2006-03-20 21:34:12 -0800 | [diff] [blame] | 1789 | return NULL; |
| 1790 | |
| 1791 | n = rcu_dereference(t->trie); |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 1792 | if (!n) |
Robert Olsson | 5ddf0eb | 2006-03-20 21:34:12 -0800 | [diff] [blame] | 1793 | return NULL; |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1794 | |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 1795 | if (IS_TNODE(n)) { |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1796 | iter->tnode = n; |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 1797 | iter->index = 0; |
| 1798 | iter->depth = 1; |
| 1799 | } else { |
| 1800 | iter->tnode = NULL; |
| 1801 | iter->index = 0; |
| 1802 | iter->depth = 0; |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1803 | } |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 1804 | |
| 1805 | return n; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1806 | } |
| 1807 | |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1808 | static void trie_collect_stats(struct trie *t, struct trie_stat *s) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1809 | { |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1810 | struct tnode *n; |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1811 | struct fib_trie_iter iter; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1812 | |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1813 | memset(s, 0, sizeof(*s)); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1814 | |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1815 | rcu_read_lock(); |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 1816 | for (n = fib_trie_get_first(&iter, t); n; n = fib_trie_get_next(&iter)) { |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1817 | if (IS_LEAF(n)) { |
Stephen Hemminger | 9367229 | 2008-01-22 21:54:05 -0800 | [diff] [blame] | 1818 | struct leaf_info *li; |
Stephen Hemminger | 9367229 | 2008-01-22 21:54:05 -0800 | [diff] [blame] | 1819 | |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1820 | s->leaves++; |
| 1821 | s->totdepth += iter.depth; |
| 1822 | if (iter.depth > s->maxdepth) |
| 1823 | s->maxdepth = iter.depth; |
Stephen Hemminger | 9367229 | 2008-01-22 21:54:05 -0800 | [diff] [blame] | 1824 | |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1825 | hlist_for_each_entry_rcu(li, &n->list, hlist) |
Stephen Hemminger | 9367229 | 2008-01-22 21:54:05 -0800 | [diff] [blame] | 1826 | ++s->prefixes; |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1827 | } else { |
Alexander Duyck | 98293e8 | 2014-12-31 10:56:18 -0800 | [diff] [blame] | 1828 | unsigned long i; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1829 | |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1830 | s->tnodes++; |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1831 | if (n->bits < MAX_STAT_DEPTH) |
| 1832 | s->nodesizes[n->bits]++; |
Robert Olsson | 06ef921 | 2006-03-20 21:35:01 -0800 | [diff] [blame] | 1833 | |
Alexander Duyck | 21d1f11 | 2014-12-31 10:57:02 -0800 | [diff] [blame^] | 1834 | for (i = tnode_child_length(n); i--;) { |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1835 | if (!rcu_access_pointer(n->child[i])) |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1836 | s->nullpointers++; |
Alexander Duyck | 98293e8 | 2014-12-31 10:56:18 -0800 | [diff] [blame] | 1837 | } |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1838 | } |
| 1839 | } |
| 1840 | rcu_read_unlock(); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1841 | } |
| 1842 | |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 1843 | /* |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1844 | * This outputs /proc/net/fib_triestats |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1845 | */ |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1846 | static void trie_show_stats(struct seq_file *seq, struct trie_stat *stat) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1847 | { |
Eric Dumazet | a034ee3 | 2010-09-09 23:32:28 +0000 | [diff] [blame] | 1848 | unsigned int i, max, pointers, bytes, avdepth; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1849 | |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1850 | if (stat->leaves) |
| 1851 | avdepth = stat->totdepth*100 / stat->leaves; |
| 1852 | else |
| 1853 | avdepth = 0; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1854 | |
Stephen Hemminger | a07f5f5 | 2008-01-22 21:53:36 -0800 | [diff] [blame] | 1855 | seq_printf(seq, "\tAver depth: %u.%02d\n", |
| 1856 | avdepth / 100, avdepth % 100); |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1857 | seq_printf(seq, "\tMax depth: %u\n", stat->maxdepth); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1858 | |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1859 | seq_printf(seq, "\tLeaves: %u\n", stat->leaves); |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1860 | bytes = sizeof(struct tnode) * stat->leaves; |
Stephen Hemminger | 9367229 | 2008-01-22 21:54:05 -0800 | [diff] [blame] | 1861 | |
| 1862 | seq_printf(seq, "\tPrefixes: %u\n", stat->prefixes); |
| 1863 | bytes += sizeof(struct leaf_info) * stat->prefixes; |
| 1864 | |
Stephen Hemminger | 187b518 | 2008-01-12 20:55:55 -0800 | [diff] [blame] | 1865 | seq_printf(seq, "\tInternal nodes: %u\n\t", stat->tnodes); |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1866 | bytes += sizeof(struct tnode) * stat->tnodes; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1867 | |
Robert Olsson | 06ef921 | 2006-03-20 21:35:01 -0800 | [diff] [blame] | 1868 | max = MAX_STAT_DEPTH; |
| 1869 | while (max > 0 && stat->nodesizes[max-1] == 0) |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1870 | max--; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1871 | |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1872 | pointers = 0; |
Jerry Snitselaar | f585a99 | 2013-07-22 12:01:58 -0700 | [diff] [blame] | 1873 | for (i = 1; i < max; i++) |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1874 | if (stat->nodesizes[i] != 0) { |
Stephen Hemminger | 187b518 | 2008-01-12 20:55:55 -0800 | [diff] [blame] | 1875 | seq_printf(seq, " %u: %u", i, stat->nodesizes[i]); |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1876 | pointers += (1<<i) * stat->nodesizes[i]; |
| 1877 | } |
| 1878 | seq_putc(seq, '\n'); |
Stephen Hemminger | 187b518 | 2008-01-12 20:55:55 -0800 | [diff] [blame] | 1879 | seq_printf(seq, "\tPointers: %u\n", pointers); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1880 | |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1881 | bytes += sizeof(struct tnode *) * pointers; |
Stephen Hemminger | 187b518 | 2008-01-12 20:55:55 -0800 | [diff] [blame] | 1882 | seq_printf(seq, "Null ptrs: %u\n", stat->nullpointers); |
| 1883 | seq_printf(seq, "Total size: %u kB\n", (bytes + 1023) / 1024); |
Stephen Hemminger | 66a2f7f | 2008-01-12 21:23:17 -0800 | [diff] [blame] | 1884 | } |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1885 | |
| 1886 | #ifdef CONFIG_IP_FIB_TRIE_STATS |
Stephen Hemminger | 66a2f7f | 2008-01-12 21:23:17 -0800 | [diff] [blame] | 1887 | static void trie_show_usage(struct seq_file *seq, |
Alexander Duyck | 8274a97 | 2014-12-31 10:55:29 -0800 | [diff] [blame] | 1888 | const struct trie_use_stats __percpu *stats) |
Stephen Hemminger | 66a2f7f | 2008-01-12 21:23:17 -0800 | [diff] [blame] | 1889 | { |
Alexander Duyck | 8274a97 | 2014-12-31 10:55:29 -0800 | [diff] [blame] | 1890 | struct trie_use_stats s = { 0 }; |
| 1891 | int cpu; |
| 1892 | |
| 1893 | /* loop through all of the CPUs and gather up the stats */ |
| 1894 | for_each_possible_cpu(cpu) { |
| 1895 | const struct trie_use_stats *pcpu = per_cpu_ptr(stats, cpu); |
| 1896 | |
| 1897 | s.gets += pcpu->gets; |
| 1898 | s.backtrack += pcpu->backtrack; |
| 1899 | s.semantic_match_passed += pcpu->semantic_match_passed; |
| 1900 | s.semantic_match_miss += pcpu->semantic_match_miss; |
| 1901 | s.null_node_hit += pcpu->null_node_hit; |
| 1902 | s.resize_node_skipped += pcpu->resize_node_skipped; |
| 1903 | } |
| 1904 | |
Stephen Hemminger | 66a2f7f | 2008-01-12 21:23:17 -0800 | [diff] [blame] | 1905 | seq_printf(seq, "\nCounters:\n---------\n"); |
Alexander Duyck | 8274a97 | 2014-12-31 10:55:29 -0800 | [diff] [blame] | 1906 | seq_printf(seq, "gets = %u\n", s.gets); |
| 1907 | seq_printf(seq, "backtracks = %u\n", s.backtrack); |
Stephen Hemminger | a07f5f5 | 2008-01-22 21:53:36 -0800 | [diff] [blame] | 1908 | seq_printf(seq, "semantic match passed = %u\n", |
Alexander Duyck | 8274a97 | 2014-12-31 10:55:29 -0800 | [diff] [blame] | 1909 | s.semantic_match_passed); |
| 1910 | seq_printf(seq, "semantic match miss = %u\n", s.semantic_match_miss); |
| 1911 | seq_printf(seq, "null node hit= %u\n", s.null_node_hit); |
| 1912 | seq_printf(seq, "skipped node resize = %u\n\n", s.resize_node_skipped); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1913 | } |
Stephen Hemminger | 66a2f7f | 2008-01-12 21:23:17 -0800 | [diff] [blame] | 1914 | #endif /* CONFIG_IP_FIB_TRIE_STATS */ |
| 1915 | |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 1916 | static void fib_table_print(struct seq_file *seq, struct fib_table *tb) |
Stephen Hemminger | d717a9a | 2008-01-14 23:11:54 -0800 | [diff] [blame] | 1917 | { |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 1918 | if (tb->tb_id == RT_TABLE_LOCAL) |
| 1919 | seq_puts(seq, "Local:\n"); |
| 1920 | else if (tb->tb_id == RT_TABLE_MAIN) |
| 1921 | seq_puts(seq, "Main:\n"); |
| 1922 | else |
| 1923 | seq_printf(seq, "Id %d:\n", tb->tb_id); |
Stephen Hemminger | d717a9a | 2008-01-14 23:11:54 -0800 | [diff] [blame] | 1924 | } |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1925 | |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 1926 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1927 | static int fib_triestat_seq_show(struct seq_file *seq, void *v) |
| 1928 | { |
Denis V. Lunev | 1c340b2 | 2008-01-10 03:27:17 -0800 | [diff] [blame] | 1929 | struct net *net = (struct net *)seq->private; |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 1930 | unsigned int h; |
Eric W. Biederman | 877a9bf | 2007-12-07 00:47:47 -0800 | [diff] [blame] | 1931 | |
Stephen Hemminger | d717a9a | 2008-01-14 23:11:54 -0800 | [diff] [blame] | 1932 | seq_printf(seq, |
Stephen Hemminger | a07f5f5 | 2008-01-22 21:53:36 -0800 | [diff] [blame] | 1933 | "Basic info: size of leaf:" |
| 1934 | " %Zd bytes, size of tnode: %Zd bytes.\n", |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1935 | sizeof(struct tnode), sizeof(struct tnode)); |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 1936 | |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 1937 | for (h = 0; h < FIB_TABLE_HASHSZ; h++) { |
| 1938 | struct hlist_head *head = &net->ipv4.fib_table_hash[h]; |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 1939 | struct fib_table *tb; |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1940 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1941 | hlist_for_each_entry_rcu(tb, head, tb_hlist) { |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 1942 | struct trie *t = (struct trie *) tb->tb_data; |
| 1943 | struct trie_stat stat; |
| 1944 | |
| 1945 | if (!t) |
| 1946 | continue; |
| 1947 | |
| 1948 | fib_table_print(seq, tb); |
| 1949 | |
| 1950 | trie_collect_stats(t, &stat); |
| 1951 | trie_show_stats(seq, &stat); |
| 1952 | #ifdef CONFIG_IP_FIB_TRIE_STATS |
Alexander Duyck | 8274a97 | 2014-12-31 10:55:29 -0800 | [diff] [blame] | 1953 | trie_show_usage(seq, t->stats); |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 1954 | #endif |
| 1955 | } |
| 1956 | } |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1957 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1958 | return 0; |
| 1959 | } |
| 1960 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1961 | static int fib_triestat_seq_open(struct inode *inode, struct file *file) |
| 1962 | { |
Pavel Emelyanov | de05c55 | 2008-07-18 04:07:21 -0700 | [diff] [blame] | 1963 | return single_open_net(inode, file, fib_triestat_seq_show); |
Denis V. Lunev | 1c340b2 | 2008-01-10 03:27:17 -0800 | [diff] [blame] | 1964 | } |
| 1965 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 1966 | static const struct file_operations fib_triestat_fops = { |
Stephen Hemminger | c877efb | 2005-07-19 14:01:51 -0700 | [diff] [blame] | 1967 | .owner = THIS_MODULE, |
| 1968 | .open = fib_triestat_seq_open, |
| 1969 | .read = seq_read, |
| 1970 | .llseek = seq_lseek, |
Pavel Emelyanov | b6fcbdb | 2008-07-18 04:07:44 -0700 | [diff] [blame] | 1971 | .release = single_release_net, |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1972 | }; |
| 1973 | |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1974 | static struct tnode *fib_trie_get_idx(struct seq_file *seq, loff_t pos) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1975 | { |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 1976 | struct fib_trie_iter *iter = seq->private; |
| 1977 | struct net *net = seq_file_net(seq); |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1978 | loff_t idx = 0; |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 1979 | unsigned int h; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1980 | |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 1981 | for (h = 0; h < FIB_TABLE_HASHSZ; h++) { |
| 1982 | struct hlist_head *head = &net->ipv4.fib_table_hash[h]; |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 1983 | struct fib_table *tb; |
| 1984 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1985 | hlist_for_each_entry_rcu(tb, head, tb_hlist) { |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 1986 | struct tnode *n; |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 1987 | |
| 1988 | for (n = fib_trie_get_first(iter, |
| 1989 | (struct trie *) tb->tb_data); |
| 1990 | n; n = fib_trie_get_next(iter)) |
| 1991 | if (pos == idx++) { |
| 1992 | iter->tb = tb; |
| 1993 | return n; |
| 1994 | } |
| 1995 | } |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 1996 | } |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1997 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 1998 | return NULL; |
| 1999 | } |
| 2000 | |
| 2001 | static void *fib_trie_seq_start(struct seq_file *seq, loff_t *pos) |
Stephen Hemminger | c95aaf9 | 2008-01-12 21:25:02 -0800 | [diff] [blame] | 2002 | __acquires(RCU) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 2003 | { |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2004 | rcu_read_lock(); |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 2005 | return fib_trie_get_idx(seq, *pos); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | static void *fib_trie_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2009 | { |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2010 | struct fib_trie_iter *iter = seq->private; |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 2011 | struct net *net = seq_file_net(seq); |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 2012 | struct fib_table *tb = iter->tb; |
| 2013 | struct hlist_node *tb_node; |
| 2014 | unsigned int h; |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 2015 | struct tnode *n; |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2016 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 2017 | ++*pos; |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 2018 | /* next node in same table */ |
| 2019 | n = fib_trie_get_next(iter); |
| 2020 | if (n) |
| 2021 | return n; |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 2022 | |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 2023 | /* walk rest of this hash chain */ |
| 2024 | h = tb->tb_id & (FIB_TABLE_HASHSZ - 1); |
Eric Dumazet | 0a5c047 | 2011-03-31 01:51:35 -0700 | [diff] [blame] | 2025 | while ((tb_node = rcu_dereference(hlist_next_rcu(&tb->tb_hlist)))) { |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 2026 | tb = hlist_entry(tb_node, struct fib_table, tb_hlist); |
| 2027 | n = fib_trie_get_first(iter, (struct trie *) tb->tb_data); |
| 2028 | if (n) |
| 2029 | goto found; |
| 2030 | } |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2031 | |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 2032 | /* new hash chain */ |
| 2033 | while (++h < FIB_TABLE_HASHSZ) { |
| 2034 | struct hlist_head *head = &net->ipv4.fib_table_hash[h]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 2035 | hlist_for_each_entry_rcu(tb, head, tb_hlist) { |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 2036 | n = fib_trie_get_first(iter, (struct trie *) tb->tb_data); |
| 2037 | if (n) |
| 2038 | goto found; |
| 2039 | } |
| 2040 | } |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2041 | return NULL; |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 2042 | |
| 2043 | found: |
| 2044 | iter->tb = tb; |
| 2045 | return n; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | static void fib_trie_seq_stop(struct seq_file *seq, void *v) |
Stephen Hemminger | c95aaf9 | 2008-01-12 21:25:02 -0800 | [diff] [blame] | 2049 | __releases(RCU) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 2050 | { |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2051 | rcu_read_unlock(); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 2052 | } |
| 2053 | |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2054 | static void seq_indent(struct seq_file *seq, int n) |
| 2055 | { |
Eric Dumazet | a034ee3 | 2010-09-09 23:32:28 +0000 | [diff] [blame] | 2056 | while (n-- > 0) |
| 2057 | seq_puts(seq, " "); |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2058 | } |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 2059 | |
Eric Dumazet | 28d36e3 | 2008-01-14 23:09:56 -0800 | [diff] [blame] | 2060 | static inline const char *rtn_scope(char *buf, size_t len, enum rt_scope_t s) |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2061 | { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 2062 | switch (s) { |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2063 | case RT_SCOPE_UNIVERSE: return "universe"; |
| 2064 | case RT_SCOPE_SITE: return "site"; |
| 2065 | case RT_SCOPE_LINK: return "link"; |
| 2066 | case RT_SCOPE_HOST: return "host"; |
| 2067 | case RT_SCOPE_NOWHERE: return "nowhere"; |
| 2068 | default: |
Eric Dumazet | 28d36e3 | 2008-01-14 23:09:56 -0800 | [diff] [blame] | 2069 | snprintf(buf, len, "scope=%d", s); |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2070 | return buf; |
| 2071 | } |
| 2072 | } |
| 2073 | |
Jan Engelhardt | 36cbd3d | 2009-08-05 10:42:58 -0700 | [diff] [blame] | 2074 | static const char *const rtn_type_names[__RTN_MAX] = { |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2075 | [RTN_UNSPEC] = "UNSPEC", |
| 2076 | [RTN_UNICAST] = "UNICAST", |
| 2077 | [RTN_LOCAL] = "LOCAL", |
| 2078 | [RTN_BROADCAST] = "BROADCAST", |
| 2079 | [RTN_ANYCAST] = "ANYCAST", |
| 2080 | [RTN_MULTICAST] = "MULTICAST", |
| 2081 | [RTN_BLACKHOLE] = "BLACKHOLE", |
| 2082 | [RTN_UNREACHABLE] = "UNREACHABLE", |
| 2083 | [RTN_PROHIBIT] = "PROHIBIT", |
| 2084 | [RTN_THROW] = "THROW", |
| 2085 | [RTN_NAT] = "NAT", |
| 2086 | [RTN_XRESOLVE] = "XRESOLVE", |
| 2087 | }; |
| 2088 | |
Eric Dumazet | a034ee3 | 2010-09-09 23:32:28 +0000 | [diff] [blame] | 2089 | static inline const char *rtn_type(char *buf, size_t len, unsigned int t) |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2090 | { |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2091 | if (t < __RTN_MAX && rtn_type_names[t]) |
| 2092 | return rtn_type_names[t]; |
Eric Dumazet | 28d36e3 | 2008-01-14 23:09:56 -0800 | [diff] [blame] | 2093 | snprintf(buf, len, "type %u", t); |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2094 | return buf; |
| 2095 | } |
| 2096 | |
| 2097 | /* Pretty print the trie */ |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 2098 | static int fib_trie_seq_show(struct seq_file *seq, void *v) |
| 2099 | { |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2100 | const struct fib_trie_iter *iter = seq->private; |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 2101 | struct tnode *n = v; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 2102 | |
Stephen Hemminger | 3d3b2d2 | 2008-03-23 22:43:56 -0700 | [diff] [blame] | 2103 | if (!node_parent_rcu(n)) |
| 2104 | fib_table_print(seq, iter->tb); |
Robert Olsson | 095b850 | 2007-01-26 19:06:01 -0800 | [diff] [blame] | 2105 | |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2106 | if (IS_TNODE(n)) { |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 2107 | __be32 prf = htonl(n->key); |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2108 | |
Alexander Duyck | e9b4401 | 2014-12-31 10:56:12 -0800 | [diff] [blame] | 2109 | seq_indent(seq, iter->depth-1); |
| 2110 | seq_printf(seq, " +-- %pI4/%zu %u %u %u\n", |
| 2111 | &prf, KEYLENGTH - n->pos - n->bits, n->bits, |
| 2112 | n->full_children, n->empty_children); |
Olof Johansson | 91b9a27 | 2005-08-09 20:24:39 -0700 | [diff] [blame] | 2113 | } else { |
Stephen Hemminger | 1328042 | 2008-01-22 21:54:37 -0800 | [diff] [blame] | 2114 | struct leaf_info *li; |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 2115 | __be32 val = htonl(n->key); |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2116 | |
| 2117 | seq_indent(seq, iter->depth); |
Harvey Harrison | 673d57e | 2008-10-31 00:53:57 -0700 | [diff] [blame] | 2118 | seq_printf(seq, " |-- %pI4\n", &val); |
Eric Dumazet | 28d36e3 | 2008-01-14 23:09:56 -0800 | [diff] [blame] | 2119 | |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 2120 | hlist_for_each_entry_rcu(li, &n->list, hlist) { |
Stephen Hemminger | 1328042 | 2008-01-22 21:54:37 -0800 | [diff] [blame] | 2121 | struct fib_alias *fa; |
Eric Dumazet | 28d36e3 | 2008-01-14 23:09:56 -0800 | [diff] [blame] | 2122 | |
Stephen Hemminger | 1328042 | 2008-01-22 21:54:37 -0800 | [diff] [blame] | 2123 | list_for_each_entry_rcu(fa, &li->falh, fa_list) { |
| 2124 | char buf1[32], buf2[32]; |
Eric Dumazet | 28d36e3 | 2008-01-14 23:09:56 -0800 | [diff] [blame] | 2125 | |
Stephen Hemminger | 1328042 | 2008-01-22 21:54:37 -0800 | [diff] [blame] | 2126 | seq_indent(seq, iter->depth+1); |
| 2127 | seq_printf(seq, " /%d %s %s", li->plen, |
| 2128 | rtn_scope(buf1, sizeof(buf1), |
David S. Miller | 37e826c | 2011-03-24 18:06:47 -0700 | [diff] [blame] | 2129 | fa->fa_info->fib_scope), |
Stephen Hemminger | 1328042 | 2008-01-22 21:54:37 -0800 | [diff] [blame] | 2130 | rtn_type(buf2, sizeof(buf2), |
| 2131 | fa->fa_type)); |
| 2132 | if (fa->fa_tos) |
Denis V. Lunev | b9c4d82 | 2008-02-05 02:58:45 -0800 | [diff] [blame] | 2133 | seq_printf(seq, " tos=%d", fa->fa_tos); |
Stephen Hemminger | 1328042 | 2008-01-22 21:54:37 -0800 | [diff] [blame] | 2134 | seq_putc(seq, '\n'); |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2135 | } |
| 2136 | } |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | return 0; |
| 2140 | } |
| 2141 | |
Stephen Hemminger | f690808 | 2007-03-12 14:34:29 -0700 | [diff] [blame] | 2142 | static const struct seq_operations fib_trie_seq_ops = { |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2143 | .start = fib_trie_seq_start, |
| 2144 | .next = fib_trie_seq_next, |
| 2145 | .stop = fib_trie_seq_stop, |
| 2146 | .show = fib_trie_seq_show, |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 2147 | }; |
| 2148 | |
| 2149 | static int fib_trie_seq_open(struct inode *inode, struct file *file) |
| 2150 | { |
Denis V. Lunev | 1c340b2 | 2008-01-10 03:27:17 -0800 | [diff] [blame] | 2151 | return seq_open_net(inode, file, &fib_trie_seq_ops, |
| 2152 | sizeof(struct fib_trie_iter)); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 2153 | } |
| 2154 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 2155 | static const struct file_operations fib_trie_fops = { |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2156 | .owner = THIS_MODULE, |
| 2157 | .open = fib_trie_seq_open, |
| 2158 | .read = seq_read, |
| 2159 | .llseek = seq_lseek, |
Denis V. Lunev | 1c340b2 | 2008-01-10 03:27:17 -0800 | [diff] [blame] | 2160 | .release = seq_release_net, |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2161 | }; |
| 2162 | |
Stephen Hemminger | 8315f5d | 2008-02-11 21:14:39 -0800 | [diff] [blame] | 2163 | struct fib_route_iter { |
| 2164 | struct seq_net_private p; |
| 2165 | struct trie *main_trie; |
| 2166 | loff_t pos; |
| 2167 | t_key key; |
| 2168 | }; |
| 2169 | |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 2170 | static struct tnode *fib_route_get_idx(struct fib_route_iter *iter, loff_t pos) |
Stephen Hemminger | 8315f5d | 2008-02-11 21:14:39 -0800 | [diff] [blame] | 2171 | { |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 2172 | struct tnode *l = NULL; |
Stephen Hemminger | 8315f5d | 2008-02-11 21:14:39 -0800 | [diff] [blame] | 2173 | struct trie *t = iter->main_trie; |
| 2174 | |
| 2175 | /* use cache location of last found key */ |
| 2176 | if (iter->pos > 0 && pos >= iter->pos && (l = fib_find_node(t, iter->key))) |
| 2177 | pos -= iter->pos; |
| 2178 | else { |
| 2179 | iter->pos = 0; |
| 2180 | l = trie_firstleaf(t); |
| 2181 | } |
| 2182 | |
| 2183 | while (l && pos-- > 0) { |
| 2184 | iter->pos++; |
| 2185 | l = trie_nextleaf(l); |
| 2186 | } |
| 2187 | |
| 2188 | if (l) |
| 2189 | iter->key = pos; /* remember it */ |
| 2190 | else |
| 2191 | iter->pos = 0; /* forget it */ |
| 2192 | |
| 2193 | return l; |
| 2194 | } |
| 2195 | |
| 2196 | static void *fib_route_seq_start(struct seq_file *seq, loff_t *pos) |
| 2197 | __acquires(RCU) |
| 2198 | { |
| 2199 | struct fib_route_iter *iter = seq->private; |
| 2200 | struct fib_table *tb; |
| 2201 | |
| 2202 | rcu_read_lock(); |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 2203 | tb = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN); |
Stephen Hemminger | 8315f5d | 2008-02-11 21:14:39 -0800 | [diff] [blame] | 2204 | if (!tb) |
| 2205 | return NULL; |
| 2206 | |
| 2207 | iter->main_trie = (struct trie *) tb->tb_data; |
| 2208 | if (*pos == 0) |
| 2209 | return SEQ_START_TOKEN; |
| 2210 | else |
| 2211 | return fib_route_get_idx(iter, *pos - 1); |
| 2212 | } |
| 2213 | |
| 2214 | static void *fib_route_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2215 | { |
| 2216 | struct fib_route_iter *iter = seq->private; |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 2217 | struct tnode *l = v; |
Stephen Hemminger | 8315f5d | 2008-02-11 21:14:39 -0800 | [diff] [blame] | 2218 | |
| 2219 | ++*pos; |
| 2220 | if (v == SEQ_START_TOKEN) { |
| 2221 | iter->pos = 0; |
| 2222 | l = trie_firstleaf(iter->main_trie); |
| 2223 | } else { |
| 2224 | iter->pos++; |
| 2225 | l = trie_nextleaf(l); |
| 2226 | } |
| 2227 | |
| 2228 | if (l) |
| 2229 | iter->key = l->key; |
| 2230 | else |
| 2231 | iter->pos = 0; |
| 2232 | return l; |
| 2233 | } |
| 2234 | |
| 2235 | static void fib_route_seq_stop(struct seq_file *seq, void *v) |
| 2236 | __releases(RCU) |
| 2237 | { |
| 2238 | rcu_read_unlock(); |
| 2239 | } |
| 2240 | |
Eric Dumazet | a034ee3 | 2010-09-09 23:32:28 +0000 | [diff] [blame] | 2241 | static unsigned int fib_flag_trans(int type, __be32 mask, const struct fib_info *fi) |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2242 | { |
Eric Dumazet | a034ee3 | 2010-09-09 23:32:28 +0000 | [diff] [blame] | 2243 | unsigned int flags = 0; |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2244 | |
Eric Dumazet | a034ee3 | 2010-09-09 23:32:28 +0000 | [diff] [blame] | 2245 | if (type == RTN_UNREACHABLE || type == RTN_PROHIBIT) |
| 2246 | flags = RTF_REJECT; |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2247 | if (fi && fi->fib_nh->nh_gw) |
| 2248 | flags |= RTF_GATEWAY; |
Al Viro | 32ab5f8 | 2006-09-26 22:21:45 -0700 | [diff] [blame] | 2249 | if (mask == htonl(0xFFFFFFFF)) |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2250 | flags |= RTF_HOST; |
| 2251 | flags |= RTF_UP; |
| 2252 | return flags; |
| 2253 | } |
| 2254 | |
| 2255 | /* |
| 2256 | * This outputs /proc/net/route. |
| 2257 | * The format of the file is not supposed to be changed |
Eric Dumazet | a034ee3 | 2010-09-09 23:32:28 +0000 | [diff] [blame] | 2258 | * and needs to be same as fib_hash output to avoid breaking |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2259 | * legacy utilities |
| 2260 | */ |
| 2261 | static int fib_route_seq_show(struct seq_file *seq, void *v) |
| 2262 | { |
Alexander Duyck | adaf981 | 2014-12-31 10:55:47 -0800 | [diff] [blame] | 2263 | struct tnode *l = v; |
Stephen Hemminger | 1328042 | 2008-01-22 21:54:37 -0800 | [diff] [blame] | 2264 | struct leaf_info *li; |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2265 | |
| 2266 | if (v == SEQ_START_TOKEN) { |
| 2267 | seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway " |
| 2268 | "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU" |
| 2269 | "\tWindow\tIRTT"); |
| 2270 | return 0; |
| 2271 | } |
| 2272 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 2273 | hlist_for_each_entry_rcu(li, &l->list, hlist) { |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2274 | struct fib_alias *fa; |
Al Viro | 32ab5f8 | 2006-09-26 22:21:45 -0700 | [diff] [blame] | 2275 | __be32 mask, prefix; |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2276 | |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2277 | mask = inet_make_mask(li->plen); |
| 2278 | prefix = htonl(l->key); |
| 2279 | |
| 2280 | list_for_each_entry_rcu(fa, &li->falh, fa_list) { |
Herbert Xu | 1371e37 | 2005-10-15 09:42:39 +1000 | [diff] [blame] | 2281 | const struct fib_info *fi = fa->fa_info; |
Eric Dumazet | a034ee3 | 2010-09-09 23:32:28 +0000 | [diff] [blame] | 2282 | unsigned int flags = fib_flag_trans(fa->fa_type, mask, fi); |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2283 | |
| 2284 | if (fa->fa_type == RTN_BROADCAST |
| 2285 | || fa->fa_type == RTN_MULTICAST) |
| 2286 | continue; |
| 2287 | |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 2288 | seq_setwidth(seq, 127); |
| 2289 | |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2290 | if (fi) |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 2291 | seq_printf(seq, |
| 2292 | "%s\t%08X\t%08X\t%04X\t%d\t%u\t" |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 2293 | "%d\t%08X\t%d\t%u\t%u", |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2294 | fi->fib_dev ? fi->fib_dev->name : "*", |
| 2295 | prefix, |
| 2296 | fi->fib_nh->nh_gw, flags, 0, 0, |
| 2297 | fi->fib_priority, |
| 2298 | mask, |
Stephen Hemminger | a07f5f5 | 2008-01-22 21:53:36 -0800 | [diff] [blame] | 2299 | (fi->fib_advmss ? |
| 2300 | fi->fib_advmss + 40 : 0), |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2301 | fi->fib_window, |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 2302 | fi->fib_rtt >> 3); |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2303 | else |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 2304 | seq_printf(seq, |
| 2305 | "*\t%08X\t%08X\t%04X\t%d\t%u\t" |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 2306 | "%d\t%08X\t%d\t%u\t%u", |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2307 | prefix, 0, flags, 0, 0, 0, |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 2308 | mask, 0, 0, 0); |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2309 | |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 2310 | seq_pad(seq, '\n'); |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | return 0; |
| 2315 | } |
| 2316 | |
Stephen Hemminger | f690808 | 2007-03-12 14:34:29 -0700 | [diff] [blame] | 2317 | static const struct seq_operations fib_route_seq_ops = { |
Stephen Hemminger | 8315f5d | 2008-02-11 21:14:39 -0800 | [diff] [blame] | 2318 | .start = fib_route_seq_start, |
| 2319 | .next = fib_route_seq_next, |
| 2320 | .stop = fib_route_seq_stop, |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2321 | .show = fib_route_seq_show, |
| 2322 | }; |
| 2323 | |
| 2324 | static int fib_route_seq_open(struct inode *inode, struct file *file) |
| 2325 | { |
Denis V. Lunev | 1c340b2 | 2008-01-10 03:27:17 -0800 | [diff] [blame] | 2326 | return seq_open_net(inode, file, &fib_route_seq_ops, |
Stephen Hemminger | 8315f5d | 2008-02-11 21:14:39 -0800 | [diff] [blame] | 2327 | sizeof(struct fib_route_iter)); |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2328 | } |
| 2329 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 2330 | static const struct file_operations fib_route_fops = { |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2331 | .owner = THIS_MODULE, |
| 2332 | .open = fib_route_seq_open, |
| 2333 | .read = seq_read, |
| 2334 | .llseek = seq_lseek, |
Denis V. Lunev | 1c340b2 | 2008-01-10 03:27:17 -0800 | [diff] [blame] | 2335 | .release = seq_release_net, |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 2336 | }; |
| 2337 | |
Denis V. Lunev | 61a0265 | 2008-01-10 03:21:09 -0800 | [diff] [blame] | 2338 | int __net_init fib_proc_init(struct net *net) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 2339 | { |
Gao feng | d4beaa6 | 2013-02-18 01:34:54 +0000 | [diff] [blame] | 2340 | if (!proc_create("fib_trie", S_IRUGO, net->proc_net, &fib_trie_fops)) |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2341 | goto out1; |
| 2342 | |
Gao feng | d4beaa6 | 2013-02-18 01:34:54 +0000 | [diff] [blame] | 2343 | if (!proc_create("fib_triestat", S_IRUGO, net->proc_net, |
| 2344 | &fib_triestat_fops)) |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2345 | goto out2; |
| 2346 | |
Gao feng | d4beaa6 | 2013-02-18 01:34:54 +0000 | [diff] [blame] | 2347 | if (!proc_create("route", S_IRUGO, net->proc_net, &fib_route_fops)) |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2348 | goto out3; |
| 2349 | |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 2350 | return 0; |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2351 | |
| 2352 | out3: |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 2353 | remove_proc_entry("fib_triestat", net->proc_net); |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2354 | out2: |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 2355 | remove_proc_entry("fib_trie", net->proc_net); |
Stephen Hemminger | cb7b593 | 2005-09-09 13:35:42 -0700 | [diff] [blame] | 2356 | out1: |
| 2357 | return -ENOMEM; |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 2358 | } |
| 2359 | |
Denis V. Lunev | 61a0265 | 2008-01-10 03:21:09 -0800 | [diff] [blame] | 2360 | void __net_exit fib_proc_exit(struct net *net) |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 2361 | { |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 2362 | remove_proc_entry("fib_trie", net->proc_net); |
| 2363 | remove_proc_entry("fib_triestat", net->proc_net); |
| 2364 | remove_proc_entry("route", net->proc_net); |
Robert Olsson | 19baf83 | 2005-06-21 12:43:18 -0700 | [diff] [blame] | 2365 | } |
| 2366 | |
| 2367 | #endif /* CONFIG_PROC_FS */ |