http://lxr.free-electrons.com/source/net/ipv4/route.c187 188 /* 189 * Route cache. 190 */ 191 192 /* The locking scheme is rather straight forward: 193 * 194 * 1) Read-Copy Update protects the buckets of the central route hash. 195 * 2) Only writers remove entries, and they hold the lock 196 * as they look at rtable reference counts. 197 * 3) Only readers acquire references to rtable entries, 198 * they do so with atomic increments and with the 199 * lock held. 200 */ 201 202 struct rt_hash_bucket { 203 struct rtable *chain; 204 }; 205 206 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \ 207 defined(CONFIG_PROVE_LOCKING) 208 /* 209 * Instead of using one spinlock for each rt_hash_bucket, we use a table of spinlocks 210 * The size of this table is a power of two and depends on the number of CPUS. 211 * (on lockdep we have a quite big spinlock_t, so keep the size down there) 212 */ 213 #ifdef CONFIG_LOCKDEP 214 # define RT_HASH_LOCK_SZ 256 215 #else 216 # if NR_CPUS >= 32 217 # define RT_HASH_LOCK_SZ 4096 218 # elif NR_CPUS >= 16 219 # define RT_HASH_LOCK_SZ 2048 220 # elif NR_CPUS >= 8 221 # define RT_HASH_LOCK_SZ 1024 222 # elif NR_CPUS >= 4 223 # define RT_HASH_LOCK_SZ 512 224 # else 225 # define RT_HASH_LOCK_SZ 256 226 # endif 227 #endif 228 229 static spinlock_t *rt_hash_locks; 230 # define rt_hash_lock_addr(slot) &rt_hash_locks[(slot) & (RT_HASH_LOCK_SZ - 1)] 231 232 static __init void rt_hash_lock_init(void) 233 { 234 int i; 235 236 rt_hash_locks = kmalloc(sizeof(spinlock_t) * RT_HASH_LOCK_SZ, 237 GFP_KERNEL); 238 if (!rt_hash_locks) 239 panic("IP: failed to allocate rt_hash_locks\n"); 240 241 for (i = 0; i < RT_HASH_LOCK_SZ; i++) 242 spin_lock_init(&rt_hash_locks[i]); 243 } 244 #else 245 # define rt_hash_lock_addr(slot) NULL 246 247 static inline void rt_hash_lock_init(void) 248 { 249 } 250 #endif 251 252 static struct rt_hash_bucket *rt_hash_table __read_mostly; 253 static unsigned rt_hash_mask __read_mostly; 254 static unsigned int rt_hash_log __read_mostly; 255 256 static DEFINE_PER_CPU(struct rt_cache_stat, rt_cache_stat); 257 #define RT_CACHE_STAT_INC(field) \ 258 (__raw_get_cpu_var(rt_cache_stat).field++) 259 260 static inline unsigned int rt_hash(__be32 daddr, __be32 saddr, int idx, 261 int genid) 262 { 263 return jhash_3words((__force u32)(__be32)(daddr), 264 (__force u32)(__be32)(saddr), 265 idx, genid) 266 & rt_hash_mask; 267 } 268 269 static inline int rt_genid(struct net *net) 270 { 271 return atomic_read(&net->ipv4.rt_genid); 272 } 273 274 #ifdef CONFIG_PROC_FS 275 struct rt_cache_iter_state { 276 struct seq_net_private p; 277 int bucket; 278 int genid; 279 }; 280 281 static struct rtable *rt_cache_get_first(struct seq_file *seq) 282 { 283 struct rt_cache_iter_state *st = seq->private; 284 struct rtable *r = NULL; 285 286 for (st->bucket = rt_hash_mask; st->bucket >= 0; --st->bucket) { 287 if (!rt_hash_table[st->bucket].chain) 288 continue; 289 rcu_read_lock_bh(); 290 r = rcu_dereference(rt_hash_table[st->bucket].chain); 291 while (r) { 292 if (dev_net(r->u.dst.dev) == seq_file_net(seq) && 293 r->rt_genid == st->genid) 294 return r; 295 r = rcu_dereference(r->u.dst.rt_next); 296 } 297 rcu_read_unlock_bh(); 298 } 299 return r; 300 } 301 302 static struct rtable *__rt_cache_get_next(struct seq_file *seq, 303 struct rtable *r) 304 { 305 struct rt_cache_iter_state *st = seq->private; 306 307 r = r->u.dst.rt_next; 308 while (!r) { 309 rcu_read_unlock_bh(); 310 do { 311 if (--st->bucket < 0) 312 return NULL; 313 } while (!rt_hash_table[st->bucket].chain); 314 rcu_read_lock_bh(); 315 r = rt_hash_table[st->bucket].chain; 316 } 317 return rcu_dereference(r); 318 } 319 320 static struct rtable *rt_cache_get_next(struct seq_file *seq, 321 struct rtable *r) 322 { 323 struct rt_cache_iter_state *st = seq->private; 324 while ((r = __rt_cache_get_next(seq, r)) != NULL) { 325 if (dev_net(r->u.dst.dev) != seq_file_net(seq)) 326 continue; 327 if (r->rt_genid == st->genid) 328 break; 329 } 330 return r; 331 } 332 333 static struct rtable *rt_cache_get_idx(struct seq_file *seq, loff_t pos) 334 { 335 struct rtable *r = rt_cache_get_first(seq); 336 337 if (r) 338 while (pos && (r = rt_cache_get_next(seq, r))) 339 --pos; 340 return pos ? NULL : r; 341 } 342 343 static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos) 344 { 345 struct rt_cache_iter_state *st = seq->private; 346 if (*pos) 347 return rt_cache_get_idx(seq, *pos - 1); 348 st->genid = rt_genid(seq_file_net(seq)); 349 return SEQ_START_TOKEN; 350 } 351 352 static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos) 353 { 354 struct rtable *r; 355 356 if (v == SEQ_START_TOKEN) 357 r = rt_cache_get_first(seq); 358 else 359 r = rt_cache_get_next(seq, v); 360 ++*pos; 361 return r; 362 } 363 |