BSD 4_3_Net_1 release
[unix-history] / sys / h / mbuf.h
CommitLineData
da7c5cc6 1/*
ca67e7b4
C
2 * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
3 * All rights reserved.
da7c5cc6 4 *
ca67e7b4 5 * Redistribution and use in source and binary forms are permitted
e3419641
C
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.
ca67e7b4 16 *
e3419641 17 * @(#)mbuf.h 7.8.1.3 (Berkeley) 2/14/89
da7c5cc6 18 */
e7054c38
BJ
19
20/*
21 * Constants related to memory allocator.
22 */
e7054c38 23#define MSIZE 128 /* size of an mbuf */
37ce97ed 24
ca67e7b4
C
25#if CLBYTES > 1024
26#define MCLBYTES 1024
27#define MCLSHIFT 10
28#define MCLOFSET (MCLBYTES - 1)
29#else
30#define MCLBYTES CLBYTES
31#define MCLSHIFT CLSHIFT
32#define MCLOFSET CLOFSET
33#endif
34
e7054c38
BJ
35#define MMINOFF 12 /* mbuf header length */
36#define MTAIL 4
37#define MMAXOFF (MSIZE-MTAIL) /* offset where data ends */
38#define MLEN (MSIZE-MMINOFF-MTAIL) /* mbuf data length */
ca67e7b4
C
39#ifdef GATEWAY
40#define NMBCLUSTERS 512
41#else
1cae93a2 42#define NMBCLUSTERS 256
ca67e7b4 43#endif
73a3ce31 44#define NMBPCL (CLBYTES/MSIZE) /* # mbufs per cluster */
e7054c38
BJ
45
46/*
47 * Macros for type conversion
e7054c38
BJ
48 */
49
114cd253 50/* network cluster number to virtual address, and back */
ca67e7b4
C
51#define cltom(x) ((struct mbuf *)((int)mbutl + ((x) << MCLSHIFT)))
52#define mtocl(x) (((int)x - (int)mbutl) >> MCLSHIFT)
e7054c38
BJ
53
54/* address in mbuf to mbuf head */
114cd253
BJ
55#define dtom(x) ((struct mbuf *)((int)x & ~(MSIZE-1)))
56
57/* mbuf head, to typed data */
e7054c38
BJ
58#define mtod(x,t) ((t)((int)(x) + (x)->m_off))
59
60struct mbuf {
61 struct mbuf *m_next; /* next buffer in chain */
62 u_long m_off; /* offset of data */
63 short m_len; /* amount of data in this mbuf */
cce93e4b 64 short m_type; /* mbuf type (0 == free) */
e7054c38
BJ
65 u_char m_dat[MLEN]; /* data storage */
66 struct mbuf *m_act; /* link in higher-level mbuf list */
67};
f5d87a9a 68
cce93e4b
SL
69/* mbuf types */
70#define MT_FREE 0 /* should be on free list */
71#define MT_DATA 1 /* dynamic (data) allocation */
72#define MT_HEADER 2 /* packet header */
73#define MT_SOCKET 3 /* socket structure */
74#define MT_PCB 4 /* protocol control block */
75#define MT_RTABLE 5 /* routing tables */
76#define MT_HTABLE 6 /* IMP host tables */
77#define MT_ATABLE 7 /* address resolution tables */
78#define MT_SONAME 8 /* socket name */
e3419641 79#define MT_ZOMBIE 9 /* zombie proc status */
cce93e4b
SL
80#define MT_SOOPTS 10 /* socket options */
81#define MT_FTABLE 11 /* fragment reassembly header */
fde4dc17 82#define MT_RIGHTS 12 /* access rights */
a93742e7 83#define MT_IFADDR 13 /* interface address */
cce93e4b 84
cd4baea6
BJ
85/* flags to m_get */
86#define M_DONTWAIT 0
87#define M_WAIT 1
88
89/* flags to m_pgalloc */
90#define MPG_MBUFS 0 /* put new mbufs on free list */
91#define MPG_CLUSTERS 1 /* put new clusters on free list */
e3419641 92#define MPG_SPACE 2 /* don't free; caller wants space */
e7054c38 93
f6963596
BJ
94/* length to m_copy to copy all */
95#define M_COPYALL 1000000000
96
0f6c64a8
MK
97/*
98 * m_pullup will pull up additional length if convenient;
99 * should be enough to hold headers of second-level and higher protocols.
100 */
101#define MPULL_EXTRA 32
102
cce93e4b 103#define MGET(m, i, t) \
b2fc55f0 104 { int ms = splimp(); \
e7054c38 105 if ((m)=mfree) \
cce93e4b 106 { if ((m)->m_type != MT_FREE) panic("mget"); (m)->m_type = t; \
fde4dc17 107 mbstat.m_mtypes[MT_FREE]--; mbstat.m_mtypes[t]++; \
cce93e4b 108 mfree = (m)->m_next; (m)->m_next = 0; \
9dbea978 109 (m)->m_off = MMINOFF; } \
e7054c38 110 else \
cce93e4b 111 (m) = m_more(i, t); \
e7054c38 112 splx(ms); }
40e9657f
MK
113/*
114 * Mbuf page cluster macros.
115 * MCLALLOC allocates mbuf page clusters.
116 * Note that it works only with a count of 1 at the moment.
117 * MCLGET adds such clusters to a normal mbuf.
ca67e7b4 118 * m->m_len is set to MCLBYTES upon success, and to MLEN on failure.
40e9657f
MK
119 * MCLFREE frees clusters allocated by MCLALLOC.
120 */
121#define MCLALLOC(m, i) \
59703a43 122 { int ms = splimp(); \
95f51977
C
123 if (mclfree == 0) \
124 (void)m_clalloc((i), MPG_CLUSTERS, M_DONTWAIT); \
59703a43 125 if ((m)=mclfree) \
73a3ce31 126 {++mclrefcnt[mtocl(m)];mbstat.m_clfree--;mclfree = (m)->m_next;} \
59703a43 127 splx(ms); }
40e9657f 128#define M_HASCL(m) ((m)->m_off >= MSIZE)
ca67e7b4 129#define MTOCL(m) ((struct mbuf *)(mtod((m), int) &~ MCLOFSET))
40e9657f
MK
130
131#define MCLGET(m) \
132 { struct mbuf *p; \
40e9657f
MK
133 MCLALLOC(p, 1); \
134 if (p) { \
135 (m)->m_off = (int)p - (int)(m); \
ca67e7b4
C
136 (m)->m_len = MCLBYTES; \
137 } else \
138 (m)->m_len = MLEN; \
40e9657f
MK
139 }
140#define MCLFREE(m) { \
141 if (--mclrefcnt[mtocl(m)] == 0) \
142 { (m)->m_next = mclfree;mclfree = (m);mbstat.m_clfree++;} \
143 }
e7054c38 144#define MFREE(m, n) \
b2fc55f0 145 { int ms = splimp(); \
cce93e4b 146 if ((m)->m_type == MT_FREE) panic("mfree"); \
fde4dc17
MK
147 mbstat.m_mtypes[(m)->m_type]--; mbstat.m_mtypes[MT_FREE]++; \
148 (m)->m_type = MT_FREE; \
40e9657f
MK
149 if (M_HASCL(m)) { \
150 (n) = MTOCL(m); \
151 MCLFREE(n); \
e7054c38
BJ
152 } \
153 (n) = (m)->m_next; (m)->m_next = mfree; \
fde4dc17 154 (m)->m_off = 0; (m)->m_act = 0; mfree = (m); \
5afb5917
MK
155 splx(ms); \
156 if (m_want) { \
157 m_want = 0; \
ffc83056 158 wakeup((caddr_t)&mfree); \
5afb5917
MK
159 } \
160 }
e7054c38 161
73a3ce31 162/*
3355356e 163 * Mbuf statistics.
73a3ce31 164 */
b2fc55f0 165struct mbstat {
ca67e7b4
C
166 u_long m_mbufs; /* mbufs obtained from page pool */
167 u_long m_clusters; /* clusters obtained from page pool */
e3419641 168 u_long m_space; /* interface pages obtained from page pool */
ca67e7b4
C
169 u_long m_clfree; /* free clusters */
170 u_long m_drops; /* times failed to find space */
171 u_long m_wait; /* times waited for space */
172 u_long m_drain; /* times drained protocols for space */
173 u_short m_mtypes[256]; /* type specific mbuf allocations */
b2fc55f0
BJ
174};
175
e7054c38 176#ifdef KERNEL
f5d87a9a
BJ
177extern struct mbuf mbutl[]; /* virtual address of net free mem */
178extern struct pte Mbmap[]; /* page tables to map Netutl */
b2fc55f0 179struct mbstat mbstat;
5a1f132a 180int nmbclusters;
114cd253 181struct mbuf *mfree, *mclfree;
ffc83056 182char mclrefcnt[NMBCLUSTERS + 1];
5afb5917 183int m_want;
5fd60a4e 184struct mbuf *m_get(),*m_getclr(),*m_free(),*m_more(),*m_copy(),*m_pullup();
cd4baea6 185caddr_t m_clalloc();
e7054c38 186#endif