xref: /original-bsd/sys/netiso/tuba_table.c (revision 4fd8edf4)
1 /*
2  * Copyright (c) 1992 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)tuba_table.c	7.6 (Berkeley) 11/08/92
8  */
9 #include <sys/param.h>
10 #include <sys/systm.h>
11 #include <sys/proc.h>
12 #include <sys/mbuf.h>
13 #include <sys/socket.h>
14 #include <sys/socketvar.h>
15 #include <sys/domain.h>
16 #include <sys/protosw.h>
17 #include <sys/ioctl.h>
18 #include <sys/time.h>
19 #include <sys/kernel.h>
20 
21 #include <net/if.h>
22 #include <net/af.h>
23 #include <net/radix.h>
24 
25 #include <netiso/iso.h>
26 #include <netiso/tuba_table.h>
27 
28 int	tuba_table_size;
29 struct	tuba_cache **tuba_table;
30 struct	radix_node_head *tuba_tree;
31 extern	int arpt_keep, arpt_prune;	/* use same values as arp cache */
32 
33 void
34 tuba_timer()
35 {
36 	int s = splnet();
37 	int	i;
38 	register struct	tuba_cache *tc;
39 	long	timelimit = time.tv_sec - arpt_keep;
40 
41 	timeout(tuba_timer, (caddr_t)0, arpt_prune * hz);
42 	for (i = tuba_table_size; i > 0; i--)
43 		if ((tc = tuba_table[i]) && (tc->tc_refcnt == 0) &&
44 		    (tc->tc_time < timelimit)) {
45 			tuba_table[i] = 0;
46 			rn_delete((caddr_t)&tc->tc_EID, (caddr_t)0,
47 					tuba_tree->rnh_treetop);
48 			free((caddr_t)tc, M_RTABLE);
49 		}
50 	splx(s);
51 }
52 
53 tuba_table_init()
54 {
55 	rn_inithead((void **)&tuba_tree, 40);
56 	timeout(tuba_timer, (caddr_t)0, arpt_prune * hz);
57 }
58 
59 int
60 tuba_lookup(isoa, wait)
61 	register struct iso_addr *isoa;
62 {
63 	struct radix_node *rn, *rn_match();
64 	register struct tuba_cache *tc;
65 	struct tuba_cache **new;
66 	int dupentry = 0, sum_a = 0, sum_b = 0, old_size, i;
67 	char EID[7];
68 
69 	if (isoa->isoa_len < 7)
70 		return (0);
71 	bcopy(isoa->isoa_genaddr + isoa->isoa_len - 7, EID + 1, EID[0] = 6);
72 	if ((rn = rn_match((caddr_t)EID, tuba_tree->rnh_treetop)) &&
73 	    ((rn->rn_flags & RNF_ROOT) == 0)) {
74 		tc = (struct tuba_cache *)rn;
75 		tc->tc_time = time.tv_sec;
76 		return (tc->tc_index);
77 	}
78 	if ((tc = (struct tuba_cache *)malloc(sizeof(*tc), M_RTABLE, wait))
79 		== NULL)
80 		return (0);
81 	bzero((caddr_t)tc, sizeof (*tc));
82 	bcopy((caddr_t)EID, (caddr_t)&tc->tc_EID, sizeof(EID));
83 	rn_insert(tc->tc_EID, tuba_tree->rnh_treetop, &dupentry, tc->tc_nodes);
84 	if (dupentry)
85 		panic("tuba_lookup 1");
86 	bcopy((caddr_t)isoa, (caddr_t)&tc->tc_siso.siso_addr,
87 		1 + isoa->isoa_len);
88 	tc->tc_siso.siso_family = AF_ISO;
89 	tc->tc_siso.siso_len = sizeof(tc->tc_siso);
90 	tc->tc_time = time.tv_sec;
91 	for (i = EID[0]; i > 0; i--)
92 		(i & 1 ? sum_a : sum_b) += EID[i];
93 	REDUCE(tc->tc_sum_in, (sum_a << 8) + sum_b);
94 	HTONS(tc->tc_sum_in);
95 	for (i = tuba_table_size; i > 0; i--)
96 		if (tuba_table[i] == 0)
97 			break;
98 	if (i) {
99 		tc->tc_index = i;
100 		REDUCE(tc->tc_sum_out, tc->tc_sum_in + (0xffff ^ tc->tc_index));
101 		tuba_table[i] = tc;
102 		return (i);
103 	}
104 	old_size = tuba_table_size;
105 	if (tuba_table_size == 0)
106 		tuba_table_size = 15;
107 	if (tuba_table_size > 0x7fff)
108 		return (0);
109 	tuba_table_size = 1 + 2 * tuba_table_size;
110 	i = (tuba_table_size + 1) * sizeof(tc);
111 	new = (struct tuba_cache **)malloc((unsigned)i, M_RTABLE, wait);
112 	if (new == 0) {
113 		tuba_table_size = old_size;
114 		rn_delete((caddr_t)&tc->tc_EID, (caddr_t)0, tuba_tree);
115 		free((caddr_t)tc, M_RTABLE);
116 		return (0);
117 	}
118 	bzero((caddr_t)new, (unsigned)i);
119 	if (tuba_table) {
120 		bcopy((caddr_t)tuba_table, (caddr_t)new, i >> 1);
121 		free((caddr_t)tuba_table, M_RTABLE);
122 	}
123 	tuba_table = new;
124 	tuba_table[tc->tc_index = tuba_table_size] = tc;
125 	REDUCE(tc->tc_sum_out, tc->tc_sum_in + (0xffff ^ tc->tc_index));
126 	return (tc->tc_index);
127 }
128