[PATCH] sched: fix interactive ceiling code
[linux-2.6/linux-mips.git] / net / ipv4 / fib_hash.c
blobe2890ec8159ec39edabd8e9d463c20aa88defdaa
1 /*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * IPv4 FIB: lookup engine and maintenance routines.
8 * Version: $Id: fib_hash.c,v 1.13 2001/10/31 21:55:54 davem Exp $
10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
18 #include <linux/config.h>
19 #include <asm/uaccess.h>
20 #include <asm/system.h>
21 #include <linux/bitops.h>
22 #include <linux/types.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/mm.h>
26 #include <linux/string.h>
27 #include <linux/socket.h>
28 #include <linux/sockios.h>
29 #include <linux/errno.h>
30 #include <linux/in.h>
31 #include <linux/inet.h>
32 #include <linux/inetdevice.h>
33 #include <linux/netdevice.h>
34 #include <linux/if_arp.h>
35 #include <linux/proc_fs.h>
36 #include <linux/skbuff.h>
37 #include <linux/netlink.h>
38 #include <linux/init.h>
40 #include <net/ip.h>
41 #include <net/protocol.h>
42 #include <net/route.h>
43 #include <net/tcp.h>
44 #include <net/sock.h>
45 #include <net/ip_fib.h>
47 #include "fib_lookup.h"
49 static kmem_cache_t *fn_hash_kmem __read_mostly;
50 static kmem_cache_t *fn_alias_kmem __read_mostly;
52 struct fib_node {
53 struct hlist_node fn_hash;
54 struct list_head fn_alias;
55 u32 fn_key;
58 struct fn_zone {
59 struct fn_zone *fz_next; /* Next not empty zone */
60 struct hlist_head *fz_hash; /* Hash table pointer */
61 int fz_nent; /* Number of entries */
63 int fz_divisor; /* Hash divisor */
64 u32 fz_hashmask; /* (fz_divisor - 1) */
65 #define FZ_HASHMASK(fz) ((fz)->fz_hashmask)
67 int fz_order; /* Zone order */
68 u32 fz_mask;
69 #define FZ_MASK(fz) ((fz)->fz_mask)
72 /* NOTE. On fast computers evaluation of fz_hashmask and fz_mask
73 * can be cheaper than memory lookup, so that FZ_* macros are used.
76 struct fn_hash {
77 struct fn_zone *fn_zones[33];
78 struct fn_zone *fn_zone_list;
81 static inline u32 fn_hash(u32 key, struct fn_zone *fz)
83 u32 h = ntohl(key)>>(32 - fz->fz_order);
84 h ^= (h>>20);
85 h ^= (h>>10);
86 h ^= (h>>5);
87 h &= FZ_HASHMASK(fz);
88 return h;
91 static inline u32 fz_key(u32 dst, struct fn_zone *fz)
93 return dst & FZ_MASK(fz);
96 static DEFINE_RWLOCK(fib_hash_lock);
97 static unsigned int fib_hash_genid;
99 #define FZ_MAX_DIVISOR ((PAGE_SIZE<<MAX_ORDER) / sizeof(struct hlist_head))
101 static struct hlist_head *fz_hash_alloc(int divisor)
103 unsigned long size = divisor * sizeof(struct hlist_head);
105 if (size <= PAGE_SIZE) {
106 return kmalloc(size, GFP_KERNEL);
107 } else {
108 return (struct hlist_head *)
109 __get_free_pages(GFP_KERNEL, get_order(size));
113 /* The fib hash lock must be held when this is called. */
114 static inline void fn_rebuild_zone(struct fn_zone *fz,
115 struct hlist_head *old_ht,
116 int old_divisor)
118 int i;
120 for (i = 0; i < old_divisor; i++) {
121 struct hlist_node *node, *n;
122 struct fib_node *f;
124 hlist_for_each_entry_safe(f, node, n, &old_ht[i], fn_hash) {
125 struct hlist_head *new_head;
127 hlist_del(&f->fn_hash);
129 new_head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
130 hlist_add_head(&f->fn_hash, new_head);
135 static void fz_hash_free(struct hlist_head *hash, int divisor)
137 unsigned long size = divisor * sizeof(struct hlist_head);
139 if (size <= PAGE_SIZE)
140 kfree(hash);
141 else
142 free_pages((unsigned long)hash, get_order(size));
145 static void fn_rehash_zone(struct fn_zone *fz)
147 struct hlist_head *ht, *old_ht;
148 int old_divisor, new_divisor;
149 u32 new_hashmask;
151 old_divisor = fz->fz_divisor;
153 switch (old_divisor) {
154 case 16:
155 new_divisor = 256;
156 break;
157 case 256:
158 new_divisor = 1024;
159 break;
160 default:
161 if ((old_divisor << 1) > FZ_MAX_DIVISOR) {
162 printk(KERN_CRIT "route.c: bad divisor %d!\n", old_divisor);
163 return;
165 new_divisor = (old_divisor << 1);
166 break;
169 new_hashmask = (new_divisor - 1);
171 #if RT_CACHE_DEBUG >= 2
172 printk("fn_rehash_zone: hash for zone %d grows from %d\n", fz->fz_order, old_divisor);
173 #endif
175 ht = fz_hash_alloc(new_divisor);
177 if (ht) {
178 memset(ht, 0, new_divisor * sizeof(struct hlist_head));
180 write_lock_bh(&fib_hash_lock);
181 old_ht = fz->fz_hash;
182 fz->fz_hash = ht;
183 fz->fz_hashmask = new_hashmask;
184 fz->fz_divisor = new_divisor;
185 fn_rebuild_zone(fz, old_ht, old_divisor);
186 fib_hash_genid++;
187 write_unlock_bh(&fib_hash_lock);
189 fz_hash_free(old_ht, old_divisor);
193 static inline void fn_free_node(struct fib_node * f)
195 kmem_cache_free(fn_hash_kmem, f);
198 static inline void fn_free_alias(struct fib_alias *fa)
200 fib_release_info(fa->fa_info);
201 kmem_cache_free(fn_alias_kmem, fa);
204 static struct fn_zone *
205 fn_new_zone(struct fn_hash *table, int z)
207 int i;
208 struct fn_zone *fz = kmalloc(sizeof(struct fn_zone), GFP_KERNEL);
209 if (!fz)
210 return NULL;
212 memset(fz, 0, sizeof(struct fn_zone));
213 if (z) {
214 fz->fz_divisor = 16;
215 } else {
216 fz->fz_divisor = 1;
218 fz->fz_hashmask = (fz->fz_divisor - 1);
219 fz->fz_hash = fz_hash_alloc(fz->fz_divisor);
220 if (!fz->fz_hash) {
221 kfree(fz);
222 return NULL;
224 memset(fz->fz_hash, 0, fz->fz_divisor * sizeof(struct hlist_head *));
225 fz->fz_order = z;
226 fz->fz_mask = inet_make_mask(z);
228 /* Find the first not empty zone with more specific mask */
229 for (i=z+1; i<=32; i++)
230 if (table->fn_zones[i])
231 break;
232 write_lock_bh(&fib_hash_lock);
233 if (i>32) {
234 /* No more specific masks, we are the first. */
235 fz->fz_next = table->fn_zone_list;
236 table->fn_zone_list = fz;
237 } else {
238 fz->fz_next = table->fn_zones[i]->fz_next;
239 table->fn_zones[i]->fz_next = fz;
241 table->fn_zones[z] = fz;
242 fib_hash_genid++;
243 write_unlock_bh(&fib_hash_lock);
244 return fz;
247 static int
248 fn_hash_lookup(struct fib_table *tb, const struct flowi *flp, struct fib_result *res)
250 int err;
251 struct fn_zone *fz;
252 struct fn_hash *t = (struct fn_hash*)tb->tb_data;
254 read_lock(&fib_hash_lock);
255 for (fz = t->fn_zone_list; fz; fz = fz->fz_next) {
256 struct hlist_head *head;
257 struct hlist_node *node;
258 struct fib_node *f;
259 u32 k = fz_key(flp->fl4_dst, fz);
261 head = &fz->fz_hash[fn_hash(k, fz)];
262 hlist_for_each_entry(f, node, head, fn_hash) {
263 if (f->fn_key != k)
264 continue;
266 err = fib_semantic_match(&f->fn_alias,
267 flp, res,
268 f->fn_key, fz->fz_mask,
269 fz->fz_order);
270 if (err <= 0)
271 goto out;
274 err = 1;
275 out:
276 read_unlock(&fib_hash_lock);
277 return err;
280 static int fn_hash_last_dflt=-1;
282 static void
283 fn_hash_select_default(struct fib_table *tb, const struct flowi *flp, struct fib_result *res)
285 int order, last_idx;
286 struct hlist_node *node;
287 struct fib_node *f;
288 struct fib_info *fi = NULL;
289 struct fib_info *last_resort;
290 struct fn_hash *t = (struct fn_hash*)tb->tb_data;
291 struct fn_zone *fz = t->fn_zones[0];
293 if (fz == NULL)
294 return;
296 last_idx = -1;
297 last_resort = NULL;
298 order = -1;
300 read_lock(&fib_hash_lock);
301 hlist_for_each_entry(f, node, &fz->fz_hash[0], fn_hash) {
302 struct fib_alias *fa;
304 list_for_each_entry(fa, &f->fn_alias, fa_list) {
305 struct fib_info *next_fi = fa->fa_info;
307 if (fa->fa_scope != res->scope ||
308 fa->fa_type != RTN_UNICAST)
309 continue;
311 if (next_fi->fib_priority > res->fi->fib_priority)
312 break;
313 if (!next_fi->fib_nh[0].nh_gw ||
314 next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
315 continue;
316 fa->fa_state |= FA_S_ACCESSED;
318 if (fi == NULL) {
319 if (next_fi != res->fi)
320 break;
321 } else if (!fib_detect_death(fi, order, &last_resort,
322 &last_idx, &fn_hash_last_dflt)) {
323 if (res->fi)
324 fib_info_put(res->fi);
325 res->fi = fi;
326 atomic_inc(&fi->fib_clntref);
327 fn_hash_last_dflt = order;
328 goto out;
330 fi = next_fi;
331 order++;
335 if (order <= 0 || fi == NULL) {
336 fn_hash_last_dflt = -1;
337 goto out;
340 if (!fib_detect_death(fi, order, &last_resort, &last_idx, &fn_hash_last_dflt)) {
341 if (res->fi)
342 fib_info_put(res->fi);
343 res->fi = fi;
344 atomic_inc(&fi->fib_clntref);
345 fn_hash_last_dflt = order;
346 goto out;
349 if (last_idx >= 0) {
350 if (res->fi)
351 fib_info_put(res->fi);
352 res->fi = last_resort;
353 if (last_resort)
354 atomic_inc(&last_resort->fib_clntref);
356 fn_hash_last_dflt = last_idx;
357 out:
358 read_unlock(&fib_hash_lock);
361 /* Insert node F to FZ. */
362 static inline void fib_insert_node(struct fn_zone *fz, struct fib_node *f)
364 struct hlist_head *head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
366 hlist_add_head(&f->fn_hash, head);
369 /* Return the node in FZ matching KEY. */
370 static struct fib_node *fib_find_node(struct fn_zone *fz, u32 key)
372 struct hlist_head *head = &fz->fz_hash[fn_hash(key, fz)];
373 struct hlist_node *node;
374 struct fib_node *f;
376 hlist_for_each_entry(f, node, head, fn_hash) {
377 if (f->fn_key == key)
378 return f;
381 return NULL;
384 static int
385 fn_hash_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
386 struct nlmsghdr *n, struct netlink_skb_parms *req)
388 struct fn_hash *table = (struct fn_hash *) tb->tb_data;
389 struct fib_node *new_f, *f;
390 struct fib_alias *fa, *new_fa;
391 struct fn_zone *fz;
392 struct fib_info *fi;
393 int z = r->rtm_dst_len;
394 int type = r->rtm_type;
395 u8 tos = r->rtm_tos;
396 u32 key;
397 int err;
399 if (z > 32)
400 return -EINVAL;
401 fz = table->fn_zones[z];
402 if (!fz && !(fz = fn_new_zone(table, z)))
403 return -ENOBUFS;
405 key = 0;
406 if (rta->rta_dst) {
407 u32 dst;
408 memcpy(&dst, rta->rta_dst, 4);
409 if (dst & ~FZ_MASK(fz))
410 return -EINVAL;
411 key = fz_key(dst, fz);
414 if ((fi = fib_create_info(r, rta, n, &err)) == NULL)
415 return err;
417 if (fz->fz_nent > (fz->fz_divisor<<1) &&
418 fz->fz_divisor < FZ_MAX_DIVISOR &&
419 (z==32 || (1<<z) > fz->fz_divisor))
420 fn_rehash_zone(fz);
422 f = fib_find_node(fz, key);
424 if (!f)
425 fa = NULL;
426 else
427 fa = fib_find_alias(&f->fn_alias, tos, fi->fib_priority);
429 /* Now fa, if non-NULL, points to the first fib alias
430 * with the same keys [prefix,tos,priority], if such key already
431 * exists or to the node before which we will insert new one.
433 * If fa is NULL, we will need to allocate a new one and
434 * insert to the head of f.
436 * If f is NULL, no fib node matched the destination key
437 * and we need to allocate a new one of those as well.
440 if (fa && fa->fa_tos == tos &&
441 fa->fa_info->fib_priority == fi->fib_priority) {
442 struct fib_alias *fa_orig;
444 err = -EEXIST;
445 if (n->nlmsg_flags & NLM_F_EXCL)
446 goto out;
448 if (n->nlmsg_flags & NLM_F_REPLACE) {
449 struct fib_info *fi_drop;
450 u8 state;
452 write_lock_bh(&fib_hash_lock);
453 fi_drop = fa->fa_info;
454 fa->fa_info = fi;
455 fa->fa_type = type;
456 fa->fa_scope = r->rtm_scope;
457 state = fa->fa_state;
458 fa->fa_state &= ~FA_S_ACCESSED;
459 fib_hash_genid++;
460 write_unlock_bh(&fib_hash_lock);
462 fib_release_info(fi_drop);
463 if (state & FA_S_ACCESSED)
464 rt_cache_flush(-1);
465 return 0;
468 /* Error if we find a perfect match which
469 * uses the same scope, type, and nexthop
470 * information.
472 fa_orig = fa;
473 fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
474 list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
475 if (fa->fa_tos != tos)
476 break;
477 if (fa->fa_info->fib_priority != fi->fib_priority)
478 break;
479 if (fa->fa_type == type &&
480 fa->fa_scope == r->rtm_scope &&
481 fa->fa_info == fi)
482 goto out;
484 if (!(n->nlmsg_flags & NLM_F_APPEND))
485 fa = fa_orig;
488 err = -ENOENT;
489 if (!(n->nlmsg_flags&NLM_F_CREATE))
490 goto out;
492 err = -ENOBUFS;
493 new_fa = kmem_cache_alloc(fn_alias_kmem, SLAB_KERNEL);
494 if (new_fa == NULL)
495 goto out;
497 new_f = NULL;
498 if (!f) {
499 new_f = kmem_cache_alloc(fn_hash_kmem, SLAB_KERNEL);
500 if (new_f == NULL)
501 goto out_free_new_fa;
503 INIT_HLIST_NODE(&new_f->fn_hash);
504 INIT_LIST_HEAD(&new_f->fn_alias);
505 new_f->fn_key = key;
506 f = new_f;
509 new_fa->fa_info = fi;
510 new_fa->fa_tos = tos;
511 new_fa->fa_type = type;
512 new_fa->fa_scope = r->rtm_scope;
513 new_fa->fa_state = 0;
516 * Insert new entry to the list.
519 write_lock_bh(&fib_hash_lock);
520 if (new_f)
521 fib_insert_node(fz, new_f);
522 list_add_tail(&new_fa->fa_list,
523 (fa ? &fa->fa_list : &f->fn_alias));
524 fib_hash_genid++;
525 write_unlock_bh(&fib_hash_lock);
527 if (new_f)
528 fz->fz_nent++;
529 rt_cache_flush(-1);
531 rtmsg_fib(RTM_NEWROUTE, key, new_fa, z, tb->tb_id, n, req);
532 return 0;
534 out_free_new_fa:
535 kmem_cache_free(fn_alias_kmem, new_fa);
536 out:
537 fib_release_info(fi);
538 return err;
542 static int
543 fn_hash_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
544 struct nlmsghdr *n, struct netlink_skb_parms *req)
546 struct fn_hash *table = (struct fn_hash*)tb->tb_data;
547 struct fib_node *f;
548 struct fib_alias *fa, *fa_to_delete;
549 int z = r->rtm_dst_len;
550 struct fn_zone *fz;
551 u32 key;
552 u8 tos = r->rtm_tos;
554 if (z > 32)
555 return -EINVAL;
556 if ((fz = table->fn_zones[z]) == NULL)
557 return -ESRCH;
559 key = 0;
560 if (rta->rta_dst) {
561 u32 dst;
562 memcpy(&dst, rta->rta_dst, 4);
563 if (dst & ~FZ_MASK(fz))
564 return -EINVAL;
565 key = fz_key(dst, fz);
568 f = fib_find_node(fz, key);
570 if (!f)
571 fa = NULL;
572 else
573 fa = fib_find_alias(&f->fn_alias, tos, 0);
574 if (!fa)
575 return -ESRCH;
577 fa_to_delete = NULL;
578 fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
579 list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
580 struct fib_info *fi = fa->fa_info;
582 if (fa->fa_tos != tos)
583 break;
585 if ((!r->rtm_type ||
586 fa->fa_type == r->rtm_type) &&
587 (r->rtm_scope == RT_SCOPE_NOWHERE ||
588 fa->fa_scope == r->rtm_scope) &&
589 (!r->rtm_protocol ||
590 fi->fib_protocol == r->rtm_protocol) &&
591 fib_nh_match(r, n, rta, fi) == 0) {
592 fa_to_delete = fa;
593 break;
597 if (fa_to_delete) {
598 int kill_fn;
600 fa = fa_to_delete;
601 rtmsg_fib(RTM_DELROUTE, key, fa, z, tb->tb_id, n, req);
603 kill_fn = 0;
604 write_lock_bh(&fib_hash_lock);
605 list_del(&fa->fa_list);
606 if (list_empty(&f->fn_alias)) {
607 hlist_del(&f->fn_hash);
608 kill_fn = 1;
610 fib_hash_genid++;
611 write_unlock_bh(&fib_hash_lock);
613 if (fa->fa_state & FA_S_ACCESSED)
614 rt_cache_flush(-1);
615 fn_free_alias(fa);
616 if (kill_fn) {
617 fn_free_node(f);
618 fz->fz_nent--;
621 return 0;
623 return -ESRCH;
626 static int fn_flush_list(struct fn_zone *fz, int idx)
628 struct hlist_head *head = &fz->fz_hash[idx];
629 struct hlist_node *node, *n;
630 struct fib_node *f;
631 int found = 0;
633 hlist_for_each_entry_safe(f, node, n, head, fn_hash) {
634 struct fib_alias *fa, *fa_node;
635 int kill_f;
637 kill_f = 0;
638 list_for_each_entry_safe(fa, fa_node, &f->fn_alias, fa_list) {
639 struct fib_info *fi = fa->fa_info;
641 if (fi && (fi->fib_flags&RTNH_F_DEAD)) {
642 write_lock_bh(&fib_hash_lock);
643 list_del(&fa->fa_list);
644 if (list_empty(&f->fn_alias)) {
645 hlist_del(&f->fn_hash);
646 kill_f = 1;
648 fib_hash_genid++;
649 write_unlock_bh(&fib_hash_lock);
651 fn_free_alias(fa);
652 found++;
655 if (kill_f) {
656 fn_free_node(f);
657 fz->fz_nent--;
660 return found;
663 static int fn_hash_flush(struct fib_table *tb)
665 struct fn_hash *table = (struct fn_hash *) tb->tb_data;
666 struct fn_zone *fz;
667 int found = 0;
669 for (fz = table->fn_zone_list; fz; fz = fz->fz_next) {
670 int i;
672 for (i = fz->fz_divisor - 1; i >= 0; i--)
673 found += fn_flush_list(fz, i);
675 return found;
679 static inline int
680 fn_hash_dump_bucket(struct sk_buff *skb, struct netlink_callback *cb,
681 struct fib_table *tb,
682 struct fn_zone *fz,
683 struct hlist_head *head)
685 struct hlist_node *node;
686 struct fib_node *f;
687 int i, s_i;
689 s_i = cb->args[3];
690 i = 0;
691 hlist_for_each_entry(f, node, head, fn_hash) {
692 struct fib_alias *fa;
694 list_for_each_entry(fa, &f->fn_alias, fa_list) {
695 if (i < s_i)
696 goto next;
698 if (fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
699 cb->nlh->nlmsg_seq,
700 RTM_NEWROUTE,
701 tb->tb_id,
702 fa->fa_type,
703 fa->fa_scope,
704 &f->fn_key,
705 fz->fz_order,
706 fa->fa_tos,
707 fa->fa_info,
708 NLM_F_MULTI) < 0) {
709 cb->args[3] = i;
710 return -1;
712 next:
713 i++;
716 cb->args[3] = i;
717 return skb->len;
720 static inline int
721 fn_hash_dump_zone(struct sk_buff *skb, struct netlink_callback *cb,
722 struct fib_table *tb,
723 struct fn_zone *fz)
725 int h, s_h;
727 s_h = cb->args[2];
728 for (h=0; h < fz->fz_divisor; h++) {
729 if (h < s_h) continue;
730 if (h > s_h)
731 memset(&cb->args[3], 0,
732 sizeof(cb->args) - 3*sizeof(cb->args[0]));
733 if (fz->fz_hash == NULL ||
734 hlist_empty(&fz->fz_hash[h]))
735 continue;
736 if (fn_hash_dump_bucket(skb, cb, tb, fz, &fz->fz_hash[h])<0) {
737 cb->args[2] = h;
738 return -1;
741 cb->args[2] = h;
742 return skb->len;
745 static int fn_hash_dump(struct fib_table *tb, struct sk_buff *skb, struct netlink_callback *cb)
747 int m, s_m;
748 struct fn_zone *fz;
749 struct fn_hash *table = (struct fn_hash*)tb->tb_data;
751 s_m = cb->args[1];
752 read_lock(&fib_hash_lock);
753 for (fz = table->fn_zone_list, m=0; fz; fz = fz->fz_next, m++) {
754 if (m < s_m) continue;
755 if (m > s_m)
756 memset(&cb->args[2], 0,
757 sizeof(cb->args) - 2*sizeof(cb->args[0]));
758 if (fn_hash_dump_zone(skb, cb, tb, fz) < 0) {
759 cb->args[1] = m;
760 read_unlock(&fib_hash_lock);
761 return -1;
764 read_unlock(&fib_hash_lock);
765 cb->args[1] = m;
766 return skb->len;
769 #ifdef CONFIG_IP_MULTIPLE_TABLES
770 struct fib_table * fib_hash_init(int id)
771 #else
772 struct fib_table * __init fib_hash_init(int id)
773 #endif
775 struct fib_table *tb;
777 if (fn_hash_kmem == NULL)
778 fn_hash_kmem = kmem_cache_create("ip_fib_hash",
779 sizeof(struct fib_node),
780 0, SLAB_HWCACHE_ALIGN,
781 NULL, NULL);
783 if (fn_alias_kmem == NULL)
784 fn_alias_kmem = kmem_cache_create("ip_fib_alias",
785 sizeof(struct fib_alias),
786 0, SLAB_HWCACHE_ALIGN,
787 NULL, NULL);
789 tb = kmalloc(sizeof(struct fib_table) + sizeof(struct fn_hash),
790 GFP_KERNEL);
791 if (tb == NULL)
792 return NULL;
794 tb->tb_id = id;
795 tb->tb_lookup = fn_hash_lookup;
796 tb->tb_insert = fn_hash_insert;
797 tb->tb_delete = fn_hash_delete;
798 tb->tb_flush = fn_hash_flush;
799 tb->tb_select_default = fn_hash_select_default;
800 tb->tb_dump = fn_hash_dump;
801 memset(tb->tb_data, 0, sizeof(struct fn_hash));
802 return tb;
805 /* ------------------------------------------------------------------------ */
806 #ifdef CONFIG_PROC_FS
808 struct fib_iter_state {
809 struct fn_zone *zone;
810 int bucket;
811 struct hlist_head *hash_head;
812 struct fib_node *fn;
813 struct fib_alias *fa;
814 loff_t pos;
815 unsigned int genid;
816 int valid;
819 static struct fib_alias *fib_get_first(struct seq_file *seq)
821 struct fib_iter_state *iter = seq->private;
822 struct fn_hash *table = (struct fn_hash *) ip_fib_main_table->tb_data;
824 iter->bucket = 0;
825 iter->hash_head = NULL;
826 iter->fn = NULL;
827 iter->fa = NULL;
828 iter->pos = 0;
829 iter->genid = fib_hash_genid;
830 iter->valid = 1;
832 for (iter->zone = table->fn_zone_list; iter->zone;
833 iter->zone = iter->zone->fz_next) {
834 int maxslot;
836 if (!iter->zone->fz_nent)
837 continue;
839 iter->hash_head = iter->zone->fz_hash;
840 maxslot = iter->zone->fz_divisor;
842 for (iter->bucket = 0; iter->bucket < maxslot;
843 ++iter->bucket, ++iter->hash_head) {
844 struct hlist_node *node;
845 struct fib_node *fn;
847 hlist_for_each_entry(fn,node,iter->hash_head,fn_hash) {
848 struct fib_alias *fa;
850 list_for_each_entry(fa,&fn->fn_alias,fa_list) {
851 iter->fn = fn;
852 iter->fa = fa;
853 goto out;
858 out:
859 return iter->fa;
862 static struct fib_alias *fib_get_next(struct seq_file *seq)
864 struct fib_iter_state *iter = seq->private;
865 struct fib_node *fn;
866 struct fib_alias *fa;
868 /* Advance FA, if any. */
869 fn = iter->fn;
870 fa = iter->fa;
871 if (fa) {
872 BUG_ON(!fn);
873 list_for_each_entry_continue(fa, &fn->fn_alias, fa_list) {
874 iter->fa = fa;
875 goto out;
879 fa = iter->fa = NULL;
881 /* Advance FN. */
882 if (fn) {
883 struct hlist_node *node = &fn->fn_hash;
884 hlist_for_each_entry_continue(fn, node, fn_hash) {
885 iter->fn = fn;
887 list_for_each_entry(fa, &fn->fn_alias, fa_list) {
888 iter->fa = fa;
889 goto out;
894 fn = iter->fn = NULL;
896 /* Advance hash chain. */
897 if (!iter->zone)
898 goto out;
900 for (;;) {
901 struct hlist_node *node;
902 int maxslot;
904 maxslot = iter->zone->fz_divisor;
906 while (++iter->bucket < maxslot) {
907 iter->hash_head++;
909 hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
910 list_for_each_entry(fa, &fn->fn_alias, fa_list) {
911 iter->fn = fn;
912 iter->fa = fa;
913 goto out;
918 iter->zone = iter->zone->fz_next;
920 if (!iter->zone)
921 goto out;
923 iter->bucket = 0;
924 iter->hash_head = iter->zone->fz_hash;
926 hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
927 list_for_each_entry(fa, &fn->fn_alias, fa_list) {
928 iter->fn = fn;
929 iter->fa = fa;
930 goto out;
934 out:
935 iter->pos++;
936 return fa;
939 static struct fib_alias *fib_get_idx(struct seq_file *seq, loff_t pos)
941 struct fib_iter_state *iter = seq->private;
942 struct fib_alias *fa;
944 if (iter->valid && pos >= iter->pos && iter->genid == fib_hash_genid) {
945 fa = iter->fa;
946 pos -= iter->pos;
947 } else
948 fa = fib_get_first(seq);
950 if (fa)
951 while (pos && (fa = fib_get_next(seq)))
952 --pos;
953 return pos ? NULL : fa;
956 static void *fib_seq_start(struct seq_file *seq, loff_t *pos)
958 void *v = NULL;
960 read_lock(&fib_hash_lock);
961 if (ip_fib_main_table)
962 v = *pos ? fib_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
963 return v;
966 static void *fib_seq_next(struct seq_file *seq, void *v, loff_t *pos)
968 ++*pos;
969 return v == SEQ_START_TOKEN ? fib_get_first(seq) : fib_get_next(seq);
972 static void fib_seq_stop(struct seq_file *seq, void *v)
974 read_unlock(&fib_hash_lock);
977 static unsigned fib_flag_trans(int type, u32 mask, struct fib_info *fi)
979 static const unsigned type2flags[RTN_MAX + 1] = {
980 [7] = RTF_REJECT, [8] = RTF_REJECT,
982 unsigned flags = type2flags[type];
984 if (fi && fi->fib_nh->nh_gw)
985 flags |= RTF_GATEWAY;
986 if (mask == 0xFFFFFFFF)
987 flags |= RTF_HOST;
988 flags |= RTF_UP;
989 return flags;
993 * This outputs /proc/net/route.
995 * It always works in backward compatibility mode.
996 * The format of the file is not supposed to be changed.
998 static int fib_seq_show(struct seq_file *seq, void *v)
1000 struct fib_iter_state *iter;
1001 char bf[128];
1002 u32 prefix, mask;
1003 unsigned flags;
1004 struct fib_node *f;
1005 struct fib_alias *fa;
1006 struct fib_info *fi;
1008 if (v == SEQ_START_TOKEN) {
1009 seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
1010 "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
1011 "\tWindow\tIRTT");
1012 goto out;
1015 iter = seq->private;
1016 f = iter->fn;
1017 fa = iter->fa;
1018 fi = fa->fa_info;
1019 prefix = f->fn_key;
1020 mask = FZ_MASK(iter->zone);
1021 flags = fib_flag_trans(fa->fa_type, mask, fi);
1022 if (fi)
1023 snprintf(bf, sizeof(bf),
1024 "%s\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
1025 fi->fib_dev ? fi->fib_dev->name : "*", prefix,
1026 fi->fib_nh->nh_gw, flags, 0, 0, fi->fib_priority,
1027 mask, (fi->fib_advmss ? fi->fib_advmss + 40 : 0),
1028 fi->fib_window,
1029 fi->fib_rtt >> 3);
1030 else
1031 snprintf(bf, sizeof(bf),
1032 "*\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
1033 prefix, 0, flags, 0, 0, 0, mask, 0, 0, 0);
1034 seq_printf(seq, "%-127s\n", bf);
1035 out:
1036 return 0;
1039 static struct seq_operations fib_seq_ops = {
1040 .start = fib_seq_start,
1041 .next = fib_seq_next,
1042 .stop = fib_seq_stop,
1043 .show = fib_seq_show,
1046 static int fib_seq_open(struct inode *inode, struct file *file)
1048 struct seq_file *seq;
1049 int rc = -ENOMEM;
1050 struct fib_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
1052 if (!s)
1053 goto out;
1055 rc = seq_open(file, &fib_seq_ops);
1056 if (rc)
1057 goto out_kfree;
1059 seq = file->private_data;
1060 seq->private = s;
1061 memset(s, 0, sizeof(*s));
1062 out:
1063 return rc;
1064 out_kfree:
1065 kfree(s);
1066 goto out;
1069 static struct file_operations fib_seq_fops = {
1070 .owner = THIS_MODULE,
1071 .open = fib_seq_open,
1072 .read = seq_read,
1073 .llseek = seq_lseek,
1074 .release = seq_release_private,
1077 int __init fib_proc_init(void)
1079 if (!proc_net_fops_create("route", S_IRUGO, &fib_seq_fops))
1080 return -ENOMEM;
1081 return 0;
1084 void __init fib_proc_exit(void)
1086 proc_net_remove("route");
1088 #endif /* CONFIG_PROC_FS */