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 this notice is preserved and that due credit is given
7: * to the University of California at Berkeley. The name of the University
8: * may not be used to endorse or promote products derived from this
9: * software without specific prior written permission. This software
10: * is provided ``as is'' without express or implied warranty.
11: *
12: * @(#)mbuf.h 7.8.2 (2.11BSD GTE) 12/31/93
13: */
14:
15: /*
16: * The default values for NMBUFS and NMBCLUSTERS (160 and 12 respectively)
17: * result in approximately 32K bytes of buffer memory being allocated to
18: * the network. Taking into account the other data used by the network,
19: * this leaves approximately 8K bytes free, so the formula is roughly:
20: *
21: * (NMBUFS / 8) + NMBCLUSTERS < 40
22: */
23: #define NMBUFS 170 /* number of mbufs */
24: #define MSIZE 128 /* size of an mbuf */
25:
26: #if CLBYTES > 1024
27: #define MCLBYTES 1024
28: #define MCLSHIFT 10
29: #define MCLOFSET (MCLBYTES - 1)
30: #else
31: #define MCLBYTES CLBYTES
32: #define MCLSHIFT CLSHIFT
33: #define MCLOFSET CLOFSET
34: #endif
35:
36: #define MMINOFF 8 /* mbuf header length */
37: #define MTAIL 2
38: #define MMAXOFF (MSIZE-MTAIL) /* offset where data ends */
39: #define MLEN (MSIZE-MMINOFF-MTAIL) /* mbuf data length */
40: #define NMBCLUSTERS 12
41: #define NMBPCL (CLBYTES/MSIZE) /* # mbufs per cluster */
42:
43: /*
44: * Macros for type conversion
45: */
46:
47: /* network cluster number to virtual address, and back */
48: #define cltom(x) ((struct mbuf *)((int)mbutl + ((x) << MCLSHIFT)))
49: #define mtocl(x) (((int)x - (int)mbutl) >> MCLSHIFT)
50:
51: /* address in mbuf to mbuf head */
52: #define dtom(x) ((struct mbuf *)((int)x & ~(MSIZE-1)))
53:
54: /* mbuf head, to typed data */
55: #define mtod(x,t) ((t)((int)(x) + (x)->m_off))
56:
57: struct mbuf {
58: struct mbuf *m_next; /* next buffer in chain */
59: u_short m_off; /* offset of data */
60: short m_len; /* amount of data in this mbuf */
61: short m_type; /* mbuf type (0 == free) */
62: u_char m_dat[MLEN]; /* data storage */
63: struct mbuf *m_act; /* link in higher-level mbuf list */
64: };
65:
66: /* mbuf types */
67: #define MT_FREE 0 /* should be on free list */
68: #define MT_DATA 1 /* dynamic (data) allocation */
69: #define 2 /* packet header */
70: #define MT_SOCKET 3 /* socket structure */
71: #define MT_PCB 4 /* protocol control block */
72: #define MT_RTABLE 5 /* routing tables */
73: #define MT_HTABLE 6 /* IMP host tables */
74: #define MT_ATABLE 7 /* address resolution tables */
75: #define MT_SONAME 8 /* socket name */
76: #define MT_ZOMBIE 9 /* zombie proc status */
77: #define MT_SOOPTS 10 /* socket options */
78: #define MT_FTABLE 11 /* fragment reassembly header */
79: #define MT_RIGHTS 12 /* access rights */
80: #define MT_IFADDR 13 /* interface address */
81: #define NMBTYPES 16
82:
83: /* flags to m_get */
84: #define M_DONTWAIT 0
85: #define M_WAIT 1
86: #define M_DONTWAITLONG 2
87:
88: /* flags to m_pgalloc */
89: #define MPG_MBUFS 0 /* put new mbufs on free list */
90: #define MPG_CLUSTERS 1 /* put new clusters on free list */
91: #define MPG_SPACE 2 /* don't free; caller wants space */
92:
93: /* length to m_copy to copy all */
94: #define M_COPYALL 077776
95:
96: /*
97: * m_pullup will pull up additional length if convenient;
98: * should be enough to hold headers of second-level and higher protocols.
99: */
100: #define 32
101:
102: #define MGET(m, i, t) \
103: { int ms = splimp(); \
104: if ((m)=mfree) \
105: { if ((m)->m_type != MT_FREE) panic("mget"); (m)->m_type = t; \
106: mbstat.m_mtypes[MT_FREE]--; mbstat.m_mtypes[t]++; \
107: mfree = (m)->m_next; (m)->m_next = 0; \
108: (m)->m_off = MMINOFF; } \
109: else \
110: (m) = m_more((((ms&0340) <= 0100) && (i==M_DONTWAIT)) ? M_DONTWAITLONG : i, t); \
111: splx(ms); }
112: /*
113: * Mbuf page cluster macros.
114: * MCLALLOC allocates mbuf page clusters.
115: * Note that it works only with a count of 1 at the moment.
116: * MCLGET adds such clusters to a normal mbuf.
117: * m->m_len is set to MCLBYTES upon success, and to MLEN on failure.
118: * MCLFREE frees clusters allocated by MCLALLOC.
119: */
120: #ifndef pdp11
121: #define MCLALLOC(m, i) \
122: { int ms = splimp(); \
123: if (mclfree == 0) \
124: (void)m_clalloc((i), MPG_CLUSTERS, M_DONTWAIT); \
125: if ((m)=mclfree) \
126: {++mclrefcnt[mtocl(m)];mbstat.m_clfree--;mclfree = (m)->m_next;} \
127: splx(ms); }
128: #else
129: #define MCLALLOC(m, i) \
130: { int ms = splimp(); \
131: if ((m)=mclfree) \
132: {++mclrefcnt[mtocl(m)];mbstat.m_clfree--;mclfree = (m)->m_next;} \
133: splx(ms); }
134: #endif
135: #define M_HASCL(m) ((m)->m_off >= MSIZE)
136: #define MTOCL(m) ((struct mbuf *)(mtod((m), int) &~ MCLOFSET))
137:
138: #define MCLGET(m) \
139: { struct mbuf *p; \
140: MCLALLOC(p, 1); \
141: if (p) { \
142: (m)->m_off = (int)p - (int)(m); \
143: (m)->m_len = MCLBYTES; \
144: } else \
145: (m)->m_len = MLEN; \
146: }
147: #define MCLFREE(m) { \
148: if (--mclrefcnt[mtocl(m)] == 0) \
149: { (m)->m_next = mclfree;mclfree = (m);mbstat.m_clfree++;} \
150: }
151: #define MFREE(m, n) \
152: { int ms = splimp(); \
153: if ((m)->m_type == MT_FREE) panic("mfree"); \
154: mbstat.m_mtypes[(m)->m_type]--; mbstat.m_mtypes[MT_FREE]++; \
155: (m)->m_type = MT_FREE; \
156: if (M_HASCL(m)) { \
157: (n) = MTOCL(m); \
158: MCLFREE(n); \
159: } \
160: (n) = (m)->m_next; (m)->m_next = mfree; \
161: (m)->m_off = 0; (m)->m_act = 0; mfree = (m); \
162: splx(ms); \
163: if (m_want) { \
164: m_want = 0; \
165: WAKEUP((caddr_t)&mfree); \
166: } \
167: }
168:
169: /*
170: * Mbuf statistics.
171: */
172: struct mbstat {
173: u_short m_mbufs; /* mbufs obtained from page pool */
174: u_short m_clusters; /* clusters obtained from page pool */
175: u_short m_space; /* interface pages obtained from page pool */
176: u_short m_clfree; /* free clusters */
177: u_short m_drops; /* times failed to find space */
178: u_short m_wait; /* times waited for space */
179: u_short m_drain; /* times drained protocols for space */
180: u_short m_mtypes[NMBTYPES]; /* type specific mbuf allocations */
181: };
182:
183: #ifdef SUPERVISOR
184: extern struct mbuf *mbutl; /* virtual address of net free mem */
185: struct mbstat mbstat;
186: int nmbclusters;
187: struct mbuf *mfree, *mclfree;
188: char mclrefcnt[NMBCLUSTERS + 1];
189: int m_want;
190: struct mbuf *m_get(),*m_getclr(),*m_free(),*m_more(),*m_copy(),*m_pullup();
191: #ifndef pdp11
192: caddr_t m_clalloc();
193: #endif
194: #endif