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