1 /*
2  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  *	@(#)if_imphost.h	7.6 (Berkeley) 06/29/88
18  */
19 
20 /*
21  * Host structure used with IMP's.
22  * Used to hold outgoing packets which
23  * would exceed allowed RFNM count.
24  *
25  * These structures are packed into
26  * mbuf's and kept as small as possible.
27  */
28 struct host {
29 	struct	mbuf *h_q;		/* holding queue */
30 	u_short	h_timer;		/* used to stay off deletion */
31 	u_short	h_imp;			/* host's imp number */
32 	u_char	h_host;			/* host's number on imp */
33 	u_char	h_qcnt;          	/* size of holding q */
34 	u_char	h_rfnm;			/* # outstanding rfnm's */
35 	u_char	h_flags;		/* see below */
36 };
37 
38 /*
39  * A host structure is kept around (even when there are no
40  * references to it) for a spell to avoid constant reallocation
41  * and also to reflect IMP status back to sites which aren't
42  * directly connected to the IMP.  When structures are marked
43  * idle, a timer is started; when the timer expires the structure
44  * is deallocated.  A structure may be reused before the timer expires.
45  * A structure holds a reference on the containing mbuf when it is marked
46  * HF_INUSE.
47  */
48 #define	HF_INUSE	0x1
49 #define	HF_DEAD		(1<<IMPTYPE_HOSTDEAD)
50 #define	HF_UNREACH	(1<<IMPTYPE_HOSTUNREACH)
51 
52 #define	HOSTTIMER	128		/* keep structure around awhile */
53 
54 /*
55  * Mark a host structure free; used if host entry returned by hostlookup
56  * isn't needed.  h_rfnm must be zero.
57  */
58 #define	hostfree(hp) { \
59 	if ((hp)->h_timer == 0 && (hp)->h_qcnt == 0 && \
60 	    (hp)->h_flags & HF_INUSE) \
61 		hostrelease(hp); \
62 }
63 
64 /*
65  * Release a host entry when last rfnm is received.
66  */
67 #define	hostidle(hp)	{ (hp)->h_timer = HOSTTIMER; }
68 
69 /*
70  * Host structures, as seen inside an mbuf.
71  * Hashing on the host and imp is used to
72  * select an index into the first mbuf.  Collisions
73  * are then resolved by searching successive
74  * mbuf's at the same index.  Reclamation is done
75  * automatically at the time a structure is freed.
76  */
77 #define	HPMBUF	((MLEN - sizeof(int)) / sizeof(struct host))
78 /* don't need to swab as long as HPMBUF is odd */
79 #if defined(notdef) && BYTE_ORDER == BIG_ENDIAN
80 #define	HOSTHASH(imp, host)	((unsigned)(ntohs(imp)+(host)) % HPMBUF)
81 #else
82 #define	HOSTHASH(imp, host)	((unsigned)((imp)+(host)) % HPMBUF)
83 #endif
84 
85 /*
86  * In-line expansions for queuing operations on
87  * host message holding queue.  Queue is maintained
88  * as circular list with the head pointing to the
89  * last message in the queue.
90  */
91 #define	HOST_ENQUE(hp, m) { \
92 	register struct mbuf *n; \
93 	(hp)->h_qcnt++; \
94 	if ((n = (hp)->h_q) == 0) \
95 		(hp)->h_q = (m)->m_act = (m); \
96 	else { \
97 		(m)->m_act = n->m_act; \
98 		(hp)->h_q = n->m_act = (m); \
99 	} \
100 }
101 #define	HOST_DEQUE(hp, m) { \
102 	if ((m) = (hp)->h_q) { \
103 		if ((m)->m_act == (m)) \
104 			(hp)->h_q = 0; \
105 		else { \
106 			(m) = (m)->m_act; \
107 			(hp)->h_q->m_act = (m)->m_act; \
108 		} \
109 		(hp)->h_qcnt--; \
110 		(m)->m_act = 0; \
111 	} \
112 }
113 
114 struct hmbuf {
115 	int	hm_count;		/* # of struct's in use */
116 	struct	host hm_hosts[HPMBUF];	/* data structures proper */
117 };
118 
119 #ifdef KERNEL
120 struct host *hostlookup();
121 struct host *hostenter();
122 #endif
123