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