xref: /original-bsd/sys/sys/mbuf.h (revision 87febec0)
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  *	@(#)mbuf.h	7.8.1.3 (Berkeley) 02/14/89
18  */
19 
20 /*
21  * Constants related to memory allocator.
22  */
23 #define	MSIZE		128			/* size of an mbuf */
24 
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 
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 */
39 #ifdef GATEWAY
40 #define	NMBCLUSTERS	512
41 #else
42 #define	NMBCLUSTERS	256
43 #endif
44 #define	NMBPCL		(CLBYTES/MSIZE)		/* # mbufs per cluster */
45 
46 /*
47  * Macros for type conversion
48  */
49 
50 /* network cluster number to virtual address, and back */
51 #define	cltom(x) ((struct mbuf *)((int)mbutl + ((x) << MCLSHIFT)))
52 #define	mtocl(x) (((int)x - (int)mbutl) >> MCLSHIFT)
53 
54 /* address in mbuf to mbuf head */
55 #define	dtom(x)		((struct mbuf *)((int)x & ~(MSIZE-1)))
56 
57 /* mbuf head, to typed data */
58 #define	mtod(x,t)	((t)((int)(x) + (x)->m_off))
59 
60 struct 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 */
64 	short	m_type;			/* mbuf type (0 == free) */
65 	u_char	m_dat[MLEN];		/* data storage */
66 	struct	mbuf *m_act;		/* link in higher-level mbuf list */
67 };
68 
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 */
79 #define	MT_ZOMBIE	9	/* zombie proc status */
80 #define	MT_SOOPTS	10	/* socket options */
81 #define	MT_FTABLE	11	/* fragment reassembly header */
82 #define	MT_RIGHTS	12	/* access rights */
83 #define	MT_IFADDR	13	/* interface address */
84 
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 */
92 #define	MPG_SPACE	2		/* don't free; caller wants space */
93 
94 /* length to m_copy to copy all */
95 #define	M_COPYALL	1000000000
96 
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 
103 #define	MGET(m, i, t) \
104 	{ int ms = splimp(); \
105 	  if ((m)=mfree) \
106 		{ if ((m)->m_type != MT_FREE) panic("mget"); (m)->m_type = t; \
107 		  mbstat.m_mtypes[MT_FREE]--; mbstat.m_mtypes[t]++; \
108 		  mfree = (m)->m_next; (m)->m_next = 0; \
109 		  (m)->m_off = MMINOFF; } \
110 	  else \
111 		(m) = m_more(i, t); \
112 	  splx(ms); }
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.
118  * m->m_len is set to MCLBYTES upon success, and to MLEN on failure.
119  * MCLFREE frees clusters allocated by MCLALLOC.
120  */
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 #define	M_HASCL(m)	((m)->m_off >= MSIZE)
129 #define	MTOCL(m)	((struct mbuf *)(mtod((m), int) &~ MCLOFSET))
130 
131 #define	MCLGET(m) \
132 	{ struct mbuf *p; \
133 	  MCLALLOC(p, 1); \
134 	  if (p) { \
135 		(m)->m_off = (int)p - (int)(m); \
136 		(m)->m_len = MCLBYTES; \
137 	  } else \
138 		(m)->m_len = MLEN; \
139 	}
140 #define	MCLFREE(m) { \
141 	if (--mclrefcnt[mtocl(m)] == 0) \
142 	    { (m)->m_next = mclfree;mclfree = (m);mbstat.m_clfree++;} \
143 	}
144 #define	MFREE(m, n) \
145 	{ int ms = splimp(); \
146 	  if ((m)->m_type == MT_FREE) panic("mfree"); \
147 	  mbstat.m_mtypes[(m)->m_type]--; mbstat.m_mtypes[MT_FREE]++; \
148 	  (m)->m_type = MT_FREE; \
149 	  if (M_HASCL(m)) { \
150 		(n) = MTOCL(m); \
151 		MCLFREE(n); \
152 	  } \
153 	  (n) = (m)->m_next; (m)->m_next = mfree; \
154 	  (m)->m_off = 0; (m)->m_act = 0; mfree = (m); \
155 	  splx(ms); \
156 	  if (m_want) { \
157 		  m_want = 0; \
158 		  wakeup((caddr_t)&mfree); \
159 	  } \
160 	}
161 
162 /*
163  * Mbuf statistics.
164  */
165 struct mbstat {
166 	u_long	m_mbufs;	/* mbufs obtained from page pool */
167 	u_long	m_clusters;	/* clusters obtained from page pool */
168 	u_long	m_space;	/* interface pages obtained from page pool */
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 */
174 };
175 
176 #ifdef	KERNEL
177 extern	struct mbuf mbutl[];		/* virtual address of net free mem */
178 extern	struct pte Mbmap[];		/* page tables to map Netutl */
179 struct	mbstat mbstat;
180 int	nmbclusters;
181 struct	mbuf *mfree, *mclfree;
182 char	mclrefcnt[NMBCLUSTERS + 1];
183 int	m_want;
184 struct	mbuf *m_get(),*m_getclr(),*m_free(),*m_more(),*m_copy(),*m_pullup();
185 caddr_t	m_clalloc();
186 #endif
187