xref: /freebsd/sys/sys/mbuf.h (revision 0c43d89a0d8e976ca494d4837f4c1f3734d2c300)
1 /*
2  * Copyright (c) 1982, 1986, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)mbuf.h	8.3 (Berkeley) 1/21/94
34  * $Id: mbuf.h,v 1.4 1994/08/21 04:41:51 paul Exp $
35  */
36 
37 #ifndef _SYS_MBUF_H_
38 #define _SYS_MBUF_H_
39 
40 #ifndef M_WAITOK
41 #include <sys/malloc.h>
42 #endif
43 
44 /*
45  * Mbufs are of a single size, MSIZE (machine/machparam.h), which
46  * includes overhead.  An mbuf may add a single "mbuf cluster" of size
47  * MCLBYTES (also in machine/machparam.h), which has no additional overhead
48  * and is used instead of the internal data area; this is done when
49  * at least MINCLSIZE of data must be stored.
50  */
51 
52 #define	MLEN		(MSIZE - sizeof(struct m_hdr))	/* normal data len */
53 #define	MHLEN		(MLEN - sizeof(struct pkthdr))	/* data len w/pkthdr */
54 
55 #define	MINCLSIZE	(MHLEN + MLEN)	/* smallest amount to put in cluster */
56 #define	M_MAXCOMPRESS	(MHLEN / 2)	/* max amount to copy for compression */
57 
58 /*
59  * Macros for type conversion
60  * mtod(m,t) -	convert mbuf pointer to data pointer of correct type
61  * dtom(x) -	convert data pointer within mbuf to mbuf pointer (XXX)
62  * mtocl(x) -	convert pointer within cluster to cluster index #
63  * cltom(x) -	convert cluster # to ptr to beginning of cluster
64  */
65 #define mtod(m,t)	((t)((m)->m_data))
66 #define	dtom(x)		((struct mbuf *)((int)(x) & ~(MSIZE-1)))
67 #define	mtocl(x)	(((u_int)(x) - (u_int)mbutl) >> MCLSHIFT)
68 #define	cltom(x)	((caddr_t)((u_int)mbutl + ((u_int)(x) << MCLSHIFT)))
69 
70 /* header at beginning of each mbuf: */
71 struct m_hdr {
72 	struct	mbuf *mh_next;		/* next buffer in chain */
73 	struct	mbuf *mh_nextpkt;	/* next chain in queue/record */
74 	int	mh_len;			/* amount of data in this mbuf */
75 	caddr_t	mh_data;		/* location of data */
76 	short	mh_type;		/* type of data in this mbuf */
77 	short	mh_flags;		/* flags; see below */
78 };
79 
80 /* record/packet header in first mbuf of chain; valid if M_PKTHDR set */
81 struct	pkthdr {
82 	int	len;		/* total packet length */
83 	struct	ifnet *rcvif;	/* rcv interface */
84 };
85 
86 /* description of external storage mapped into mbuf, valid if M_EXT set */
87 struct m_ext {
88 	caddr_t	ext_buf;		/* start of buffer */
89 	void	(*ext_free)();		/* free routine if not the usual */
90 	u_int	ext_size;		/* size of buffer, for ext_free */
91 };
92 
93 struct mbuf {
94 	struct	m_hdr m_hdr;
95 	union {
96 		struct {
97 			struct	pkthdr MH_pkthdr;	/* M_PKTHDR set */
98 			union {
99 				struct	m_ext MH_ext;	/* M_EXT set */
100 				char	MH_databuf[MHLEN];
101 			} MH_dat;
102 		} MH;
103 		char	M_databuf[MLEN];		/* !M_PKTHDR, !M_EXT */
104 	} M_dat;
105 };
106 #define	m_next		m_hdr.mh_next
107 #define	m_len		m_hdr.mh_len
108 #define	m_data		m_hdr.mh_data
109 #define	m_type		m_hdr.mh_type
110 #define	m_flags		m_hdr.mh_flags
111 #define	m_nextpkt	m_hdr.mh_nextpkt
112 #define	m_act		m_nextpkt
113 #define	m_pkthdr	M_dat.MH.MH_pkthdr
114 #define	m_ext		M_dat.MH.MH_dat.MH_ext
115 #define	m_pktdat	M_dat.MH.MH_dat.MH_databuf
116 #define	m_dat		M_dat.M_databuf
117 
118 /* mbuf flags */
119 #define	M_EXT		0x0001	/* has associated external storage */
120 #define	M_PKTHDR	0x0002	/* start of record */
121 #define	M_EOR		0x0004	/* end of record */
122 
123 /* mbuf pkthdr flags, also in m_flags */
124 #define	M_BCAST		0x0100	/* send/received as link-level broadcast */
125 #define	M_MCAST		0x0200	/* send/received as link-level multicast */
126 
127 /* flags copied when copying m_pkthdr */
128 #define	M_COPYFLAGS	(M_PKTHDR|M_EOR|M_BCAST|M_MCAST)
129 
130 /* mbuf types */
131 #define	MT_FREE		0	/* should be on free list */
132 #define	MT_DATA		1	/* dynamic (data) allocation */
133 #define	MT_HEADER	2	/* packet header */
134 #define	MT_SOCKET	3	/* socket structure */
135 #define	MT_PCB		4	/* protocol control block */
136 #define	MT_RTABLE	5	/* routing tables */
137 #define	MT_HTABLE	6	/* IMP host tables */
138 #define	MT_ATABLE	7	/* address resolution tables */
139 #define	MT_SONAME	8	/* socket name */
140 #define	MT_SOOPTS	10	/* socket options */
141 #define	MT_FTABLE	11	/* fragment reassembly header */
142 #define	MT_RIGHTS	12	/* access rights */
143 #define	MT_IFADDR	13	/* interface address */
144 #define MT_CONTROL	14	/* extra-data protocol message */
145 #define MT_OOBDATA	15	/* expedited data  */
146 
147 /* flags to m_get/MGET */
148 #define	M_DONTWAIT	M_NOWAIT
149 #define	M_WAIT		M_WAITOK
150 
151 /*
152  * mbuf utility macros:
153  *
154  *	MBUFLOCK(code)
155  * prevents a section of code from from being interrupted by network
156  * drivers.
157  */
158 #define	MBUFLOCK(code) \
159 	{ int ms = splimp(); \
160 	  { code } \
161 	  splx(ms); \
162 	}
163 
164 /*
165  * mbuf allocation/deallocation macros:
166  *
167  *	MGET(struct mbuf *m, int how, int type)
168  * allocates an mbuf and initializes it to contain internal data.
169  *
170  *	MGETHDR(struct mbuf *m, int how, int type)
171  * allocates an mbuf and initializes it to contain a packet header
172  * and internal data.
173  */
174 struct mbuf *mbuffree;
175 int mbuffreecnt;
176 #define	MGET(m, how, type) { \
177 	int s = splimp(); \
178 	if (mbuffree == 0) { \
179 		splx(s); \
180 		MALLOC((m), struct mbuf *, MSIZE, mbtypes[type], (how)); \
181 	} else { \
182 		--mbuffreecnt; \
183 		(m) = mbuffree; \
184 		mbuffree = (m)->m_next; \
185 		splx(s); \
186 	} \
187 	if (m) { \
188 		(m)->m_type = (type); \
189 		MBUFLOCK(mbstat.m_mtypes[type]++;) \
190 		(m)->m_next = (struct mbuf *)NULL; \
191 		(m)->m_nextpkt = (struct mbuf *)NULL; \
192 		(m)->m_data = (m)->m_dat; \
193 		(m)->m_flags = 0; \
194 	} else \
195 		(m) = m_retry((how), (type)); \
196 }
197 
198 #define	MGETHDR(m, how, type) { \
199 	int s = splimp(); \
200 	if (mbuffree == 0) { \
201 		splx(s); \
202 		MALLOC((m), struct mbuf *, MSIZE, mbtypes[type], (how)); \
203 	} else { \
204 		--mbuffreecnt; \
205 		(m) = mbuffree; \
206 		mbuffree = (m)->m_next; \
207 		splx(s); \
208 	} \
209 	if (m) { \
210 		(m)->m_type = (type); \
211 		MBUFLOCK(mbstat.m_mtypes[type]++;) \
212 		(m)->m_next = (struct mbuf *)NULL; \
213 		(m)->m_nextpkt = (struct mbuf *)NULL; \
214 		(m)->m_data = (m)->m_pktdat; \
215 		(m)->m_flags = M_PKTHDR; \
216 	} else \
217 		(m) = m_retryhdr((how), (type)); \
218 }
219 
220 /*
221  * Mbuf cluster macros.
222  * MCLALLOC(caddr_t p, int how) allocates an mbuf cluster.
223  * MCLGET adds such clusters to a normal mbuf;
224  * the flag M_EXT is set upon success.
225  * MCLFREE releases a reference to a cluster allocated by MCLALLOC,
226  * freeing the cluster if the reference count has reached 0.
227  *
228  * Normal mbuf clusters are normally treated as character arrays
229  * after allocation, but use the first word of the buffer as a free list
230  * pointer while on the free list.
231  */
232 union mcluster {
233 	union	mcluster *mcl_next;
234 	char	mcl_buf[MCLBYTES];
235 };
236 
237 #define	MCLALLOC(p, how) \
238 	MBUFLOCK( \
239 	  if (mclfree == 0) \
240 		(void)m_clalloc(1, (how)); \
241 	  if ((p) = (caddr_t)mclfree) { \
242 		++mclrefcnt[mtocl(p)]; \
243 		mbstat.m_clfree--; \
244 		mclfree = ((union mcluster *)(p))->mcl_next; \
245 	  } \
246 	)
247 
248 #define	MCLGET(m, how) \
249 	{ MCLALLOC((m)->m_ext.ext_buf, (how)); \
250 	  if ((m)->m_ext.ext_buf != NULL) { \
251 		(m)->m_data = (m)->m_ext.ext_buf; \
252 		(m)->m_flags |= M_EXT; \
253 		(m)->m_ext.ext_size = MCLBYTES;  \
254 	  } \
255 	}
256 
257 #define	MCLFREE(p) \
258 	MBUFLOCK ( \
259 	  if (--mclrefcnt[mtocl(p)] == 0) { \
260 		((union mcluster *)(p))->mcl_next = mclfree; \
261 		mclfree = (union mcluster *)(p); \
262 		mbstat.m_clfree++; \
263 	  } \
264 	)
265 
266 /*
267  * MFREE(struct mbuf *m, struct mbuf *n)
268  * Free a single mbuf and associated external storage.
269  * Place the successor, if any, in n.
270  */
271 #ifdef notyet
272 #define	MFREE(m, n) \
273 	{ MBUFLOCK(mbstat.m_mtypes[(m)->m_type]--;) \
274 	  if ((m)->m_flags & M_EXT) { \
275 		if ((m)->m_ext.ext_free) \
276 			(*((m)->m_ext.ext_free))((m)->m_ext.ext_buf, \
277 			    (m)->m_ext.ext_size); \
278 		else \
279 			MCLFREE((m)->m_ext.ext_buf); \
280 	  } \
281 	  (n) = (m)->m_next; \
282 	  FREE((m), mbtypes[(m)->m_type]); \
283 	}
284 #else /* notyet */
285 #define	MFREE(m, nn) \
286 	{ MBUFLOCK(mbstat.m_mtypes[(m)->m_type]--;) \
287 	  if ((m)->m_flags & M_EXT) { \
288 		MCLFREE((m)->m_ext.ext_buf); \
289 	  } \
290 	  (nn) = (m)->m_next; \
291 	  if (mbuffreecnt < 256) { \
292 		int s = splimp(); \
293 		++mbuffreecnt; \
294 		(m)->m_next = mbuffree; \
295 		mbuffree = (m); \
296 		splx(s); \
297 	  } else { \
298 	  	FREE((m), mbtypes[(m)->m_type]); \
299 	  } \
300 	}
301 #endif
302 
303 /*
304  * Copy mbuf pkthdr from from to to.
305  * from must have M_PKTHDR set, and to must be empty.
306  */
307 #define	M_COPY_PKTHDR(to, from) { \
308 	(to)->m_pkthdr = (from)->m_pkthdr; \
309 	(to)->m_flags = (from)->m_flags & M_COPYFLAGS; \
310 	(to)->m_data = (to)->m_pktdat; \
311 }
312 
313 /*
314  * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place
315  * an object of the specified size at the end of the mbuf, longword aligned.
316  */
317 #define	M_ALIGN(m, len) \
318 	{ (m)->m_data += (MLEN - (len)) &~ (sizeof(long) - 1); }
319 /*
320  * As above, for mbufs allocated with m_gethdr/MGETHDR
321  * or initialized by M_COPY_PKTHDR.
322  */
323 #define	MH_ALIGN(m, len) \
324 	{ (m)->m_data += (MHLEN - (len)) &~ (sizeof(long) - 1); }
325 
326 /*
327  * Compute the amount of space available
328  * before the current start of data in an mbuf.
329  */
330 #define	M_LEADINGSPACE(m) \
331 	((m)->m_flags & M_EXT ? /* (m)->m_data - (m)->m_ext.ext_buf */ 0 : \
332 	    (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \
333 	    (m)->m_data - (m)->m_dat)
334 
335 /*
336  * Compute the amount of space available
337  * after the end of data in an mbuf.
338  */
339 #define	M_TRAILINGSPACE(m) \
340 	((m)->m_flags & M_EXT ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size - \
341 	    ((m)->m_data + (m)->m_len) : \
342 	    &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len))
343 
344 /*
345  * Arrange to prepend space of size plen to mbuf m.
346  * If a new mbuf must be allocated, how specifies whether to wait.
347  * If how is M_DONTWAIT and allocation fails, the original mbuf chain
348  * is freed and m is set to NULL.
349  */
350 #define	M_PREPEND(m, plen, how) { \
351 	if (M_LEADINGSPACE(m) >= (plen)) { \
352 		(m)->m_data -= (plen); \
353 		(m)->m_len += (plen); \
354 	} else \
355 		(m) = m_prepend((m), (plen), (how)); \
356 	if ((m) && (m)->m_flags & M_PKTHDR) \
357 		(m)->m_pkthdr.len += (plen); \
358 }
359 
360 /* change mbuf to new type */
361 #define MCHTYPE(m, t) { \
362 	MBUFLOCK(mbstat.m_mtypes[(m)->m_type]--; mbstat.m_mtypes[t]++;) \
363 	(m)->m_type = t;\
364 }
365 
366 /* length to m_copy to copy all */
367 #define	M_COPYALL	1000000000
368 
369 /* compatiblity with 4.3 */
370 #define  m_copy(m, o, l)	m_copym((m), (o), (l), M_DONTWAIT)
371 
372 /*
373  * Mbuf statistics.
374  */
375 struct mbstat {
376 	u_long	m_mbufs;	/* mbufs obtained from page pool */
377 	u_long	m_clusters;	/* clusters obtained from page pool */
378 	u_long	m_spare;	/* spare field */
379 	u_long	m_clfree;	/* free clusters */
380 	u_long	m_drops;	/* times failed to find space */
381 	u_long	m_wait;		/* times waited for space */
382 	u_long	m_drain;	/* times drained protocols for space */
383 	u_short	m_mtypes[256];	/* type specific mbuf allocations */
384 };
385 
386 #ifdef	KERNEL
387 extern	struct mbuf *mbutl;		/* virtual address of mclusters */
388 extern	char *mclrefcnt;		/* cluster reference counts */
389 struct	mbstat mbstat;
390 extern	int nmbclusters;
391 union	mcluster *mclfree;
392 int	max_linkhdr;			/* largest link-level header */
393 int	max_protohdr;			/* largest protocol header */
394 int	max_hdr;			/* largest link+protocol header */
395 int	max_datalen;			/* MHLEN - max_hdr */
396 extern	int mbtypes[];			/* XXX */
397 
398 struct	mbuf *m_copym __P((struct mbuf *, int, int, int));
399 struct	mbuf *m_free __P((struct mbuf *));
400 struct	mbuf *m_get __P((int, int));
401 struct	mbuf *m_getclr __P((int, int));
402 struct	mbuf *m_gethdr __P((int, int));
403 struct	mbuf *m_prepend __P((struct mbuf *, int, int));
404 struct	mbuf *m_pullup __P((struct mbuf *, int));
405 struct	mbuf *m_retry __P((int, int));
406 struct	mbuf *m_retryhdr __P((int, int));
407 int	m_clalloc __P((int, int));
408 void	m_copyback __P((struct mbuf *, int, int, caddr_t));
409 void	m_freem __P((struct mbuf *));
410 
411 #ifdef MBTYPES
412 int mbtypes[] = {				/* XXX */
413 	M_FREE,		/* MT_FREE	0	   should be on free list */
414 	M_MBUF,		/* MT_DATA	1	   dynamic (data) allocation */
415 	M_MBUF,		/* MT_HEADER	2	   packet header */
416 	M_SOCKET,	/* MT_SOCKET	3	   socket structure */
417 	M_PCB,		/* MT_PCB	4	   protocol control block */
418 	M_RTABLE,	/* MT_RTABLE	5	   routing tables */
419 	M_HTABLE,	/* MT_HTABLE	6	   IMP host tables */
420 	0,		/* MT_ATABLE	7	   address resolution tables */
421 	M_MBUF,		/* MT_SONAME	8	   socket name */
422 	0,		/* 		9 */
423 	M_SOOPTS,	/* MT_SOOPTS	10	   socket options */
424 	M_FTABLE,	/* MT_FTABLE	11	   fragment reassembly header */
425 	M_MBUF,		/* MT_RIGHTS	12	   access rights */
426 	M_IFADDR,	/* MT_IFADDR	13	   interface address */
427 	M_MBUF,		/* MT_CONTROL	14	   extra-data protocol message */
428 	M_MBUF,		/* MT_OOBDATA	15	   expedited data  */
429 #ifdef DATAKIT
430 	25, 26, 27, 28, 29, 30, 31, 32		/* datakit ugliness */
431 #endif
432 };
433 #endif
434 #endif
435 
436 #endif
437