mbuf.h revision 67146
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.5 (Berkeley) 2/19/95
34 * $FreeBSD: head/sys/sys/mbuf.h 67146 2000-10-15 06:27:01Z bmilekic $
35 */
36
37#ifndef _SYS_MBUF_H_
38#define	_SYS_MBUF_H_
39
40#include <machine/mutex.h>	/* XXX */
41
42/*
43 * Mbufs are of a single size, MSIZE (machine/param.h), which
44 * includes overhead.  An mbuf may add a single "mbuf cluster" of size
45 * MCLBYTES (also in machine/param.h), which has no additional overhead
46 * and is used instead of the internal data area; this is done when
47 * at least MINCLSIZE of data must be stored.
48 */
49
50#define	MLEN		(MSIZE - sizeof(struct m_hdr))	/* normal data len */
51#define	MHLEN		(MLEN - sizeof(struct pkthdr))	/* data len w/pkthdr */
52
53#define	MINCLSIZE	(MHLEN + 1)	/* smallest amount to put in cluster */
54#define	M_MAXCOMPRESS	(MHLEN / 2)	/* max amount to copy for compression */
55
56/*
57 * Maximum number of allocatable counters for external buffers. This
58 * ensures enough VM address space for the allocation of counters
59 * in the extreme case where all possible external buffers are allocated.
60 *
61 * Note: When new types of external storage are allocated, EXT_COUNTERS
62 * 	 must be tuned accordingly. Practically, this isn't a big deal
63 *	 as each counter is only a word long, so we can fit
64 *	 (PAGE_SIZE / length of word) counters in a single page.
65 *
66 * XXX: Must increase this if using any of if_ti, if_wb, if_sk drivers,
67 *	or any other drivers which may manage their own buffers and
68 *	eventually attach them to mbufs.
69 */
70#define EXT_COUNTERS (nmbclusters + nsfbufs)
71
72/*
73 * Macros for type conversion
74 * mtod(m, t) -	convert mbuf pointer to data pointer of correct type
75 * dtom(x) -	convert data pointer within mbuf to mbuf pointer (XXX)
76 */
77#define	mtod(m, t)	((t)((m)->m_data))
78#define	dtom(x)		((struct mbuf *)((intptr_t)(x) & ~(MSIZE-1)))
79
80/* header at beginning of each mbuf: */
81struct m_hdr {
82	struct	mbuf *mh_next;		/* next buffer in chain */
83	struct	mbuf *mh_nextpkt;	/* next chain in queue/record */
84	caddr_t	mh_data;		/* location of data */
85	int	mh_len;			/* amount of data in this mbuf */
86	short	mh_type;		/* type of data in this mbuf */
87	short	mh_flags;		/* flags; see below */
88};
89
90/* record/packet header in first mbuf of chain; valid if M_PKTHDR set */
91struct pkthdr {
92	struct	ifnet *rcvif;		/* rcv interface */
93	int	len;			/* total packet length */
94	/* variables for ip and tcp reassembly */
95	void	*header;		/* pointer to packet header */
96	/* variables for hardware checksum */
97	int	csum_flags;		/* flags regarding checksum */
98	int	csum_data;		/* data field used by csum routines */
99	struct	mbuf *aux;		/* extra data buffer; ipsec/others */
100};
101
102/* description of external storage mapped into mbuf, valid if M_EXT set */
103struct m_ext {
104	caddr_t	ext_buf;		/* start of buffer */
105	void	(*ext_free)		/* free routine if not the usual */
106		__P((caddr_t, void *));
107	void	*ext_args;		/* optional argument pointer */
108	u_int	ext_size;		/* size of buffer, for ext_free */
109	union	mext_refcnt *ref_cnt;	/* pointer to ref count info */
110};
111
112struct mbuf {
113	struct	m_hdr m_hdr;
114	union {
115		struct {
116			struct	pkthdr MH_pkthdr;	/* M_PKTHDR set */
117			union {
118				struct	m_ext MH_ext;	/* M_EXT set */
119				char	MH_databuf[MHLEN];
120			} MH_dat;
121		} MH;
122		char	M_databuf[MLEN];		/* !M_PKTHDR, !M_EXT */
123	} M_dat;
124};
125#define	m_next		m_hdr.mh_next
126#define	m_len		m_hdr.mh_len
127#define	m_data		m_hdr.mh_data
128#define	m_type		m_hdr.mh_type
129#define	m_flags		m_hdr.mh_flags
130#define	m_nextpkt	m_hdr.mh_nextpkt
131#define	m_act		m_nextpkt
132#define	m_pkthdr	M_dat.MH.MH_pkthdr
133#define	m_ext		M_dat.MH.MH_dat.MH_ext
134#define	m_pktdat	M_dat.MH.MH_dat.MH_databuf
135#define	m_dat		M_dat.M_databuf
136
137/* mbuf flags */
138#define	M_EXT		0x0001	/* has associated external storage */
139#define	M_PKTHDR	0x0002	/* start of record */
140#define	M_EOR		0x0004	/* end of record */
141#define	M_PROTO1	0x0008	/* protocol-specific */
142#define	M_PROTO2	0x0010	/* protocol-specific */
143#define	M_PROTO3	0x0020	/* protocol-specific */
144#define	M_PROTO4	0x0040	/* protocol-specific */
145#define	M_PROTO5	0x0080	/* protocol-specific */
146
147/* mbuf pkthdr flags, also in m_flags */
148#define	M_BCAST		0x0100	/* send/received as link-level broadcast */
149#define	M_MCAST		0x0200	/* send/received as link-level multicast */
150#define	M_FRAG		0x0400	/* packet is a fragment of a larger packet */
151#define	M_FIRSTFRAG	0x0800	/* packet is first fragment */
152#define	M_LASTFRAG	0x1000	/* packet is last fragment */
153
154/* flags copied when copying m_pkthdr */
155#define	M_COPYFLAGS	(M_PKTHDR|M_EOR|M_PROTO1|M_PROTO1|M_PROTO2|M_PROTO3 | \
156			    M_PROTO4|M_PROTO5|M_BCAST|M_MCAST|M_FRAG)
157
158/* flags indicating hw checksum support and sw checksum requirements */
159#define CSUM_IP			0x0001		/* will csum IP */
160#define CSUM_TCP		0x0002		/* will csum TCP */
161#define CSUM_UDP		0x0004		/* will csum UDP */
162#define CSUM_IP_FRAGS		0x0008		/* will csum IP fragments */
163#define CSUM_FRAGMENT		0x0010		/* will do IP fragmentation */
164
165#define CSUM_IP_CHECKED		0x0100		/* did csum IP */
166#define CSUM_IP_VALID		0x0200		/*   ... the csum is valid */
167#define CSUM_DATA_VALID		0x0400		/* csum_data field is valid */
168#define CSUM_PSEUDO_HDR		0x0800		/* csum_data has pseudo hdr */
169
170#define CSUM_DELAY_DATA		(CSUM_TCP | CSUM_UDP)
171#define CSUM_DELAY_IP		(CSUM_IP)	/* XXX add ipv6 here too? */
172
173/* mbuf types */
174#define	MT_FREE		0	/* should be on free list */
175#define	MT_DATA		1	/* dynamic (data) allocation */
176#define	MT_HEADER	2	/* packet header */
177#if 0
178#define	MT_SOCKET	3	/* socket structure */
179#define	MT_PCB		4	/* protocol control block */
180#define	MT_RTABLE	5	/* routing tables */
181#define	MT_HTABLE	6	/* IMP host tables */
182#define	MT_ATABLE	7	/* address resolution tables */
183#endif
184#define	MT_SONAME	8	/* socket name */
185#if 0
186#define	MT_SOOPTS	10	/* socket options */
187#endif
188#define	MT_FTABLE	11	/* fragment reassembly header */
189#if 0
190#define	MT_RIGHTS	12	/* access rights */
191#define	MT_IFADDR	13	/* interface address */
192#endif
193#define	MT_CONTROL	14	/* extra-data protocol message */
194#define	MT_OOBDATA	15	/* expedited data  */
195
196#define	MT_NTYPES	16	/* number of mbuf types for mbtypes[] */
197
198/*
199 * mbuf statistics
200 */
201struct mbstat {
202	u_long	m_mbufs;	/* # mbufs obtained from page pool */
203	u_long	m_clusters;	/* # clusters obtained from page pool */
204	u_long	m_clfree;	/* # clusters on freelist (cache) */
205	u_long	m_refcnt;	/* # ref counters obtained from page pool */
206	u_long	m_refree;	/* # ref counters on freelist (cache) */
207	u_long	m_spare;	/* spare field */
208	u_long	m_drops;	/* times failed to find space */
209	u_long	m_wait;		/* times waited for space */
210	u_long	m_drain;	/* times drained protocols for space */
211	u_long	m_mcfail;	/* times m_copym failed */
212	u_long	m_mpfail;	/* times m_pullup failed */
213	u_long	m_msize;	/* length of an mbuf */
214	u_long	m_mclbytes;	/* length of an mbuf cluster */
215	u_long	m_minclsize;	/* min length of data to allocate a cluster */
216	u_long	m_mlen;		/* length of data in an mbuf */
217	u_long	m_mhlen;	/* length of data in a header mbuf */
218};
219
220/* flags to m_get/MGET */
221#define	M_DONTWAIT	1
222#define	M_WAIT		0
223
224/*
225 * Normal mbuf clusters are normally treated as character arrays
226 * after allocation, but use the first word of the buffer as a free list
227 * pointer while on the free list.
228 */
229union mcluster {
230	union	mcluster *mcl_next;
231	char	mcl_buf[MCLBYTES];
232};
233
234/*
235 * The m_ext object reference counter structure.
236 */
237union mext_refcnt {
238	union	mext_refcnt *next_ref;
239	u_long	refcnt;
240};
241
242/*
243 * free list header definitions: mbffree_lst, mclfree_lst, mcntfree_lst
244 */
245struct mbffree_lst {
246	struct mbuf *m_head;
247	struct mtx m_mtx;
248};
249
250struct mclfree_lst {
251        union mcluster *m_head;
252        struct mtx m_mtx;
253};
254
255struct mcntfree_lst {
256        union mext_refcnt *m_head;
257        struct mtx m_mtx;
258};
259
260/*
261 * Wake up the next instance (if any) of a sleeping allocation - which is
262 * waiting for a {cluster, mbuf} to be freed.
263 *
264 * Must be called with the appropriate mutex held.
265 */
266#define	MBWAKEUP(m_wid) do {						\
267	if ((m_wid)) {							\
268		m_wid--;						\
269		wakeup_one(&(m_wid)); 					\
270	}								\
271} while (0)
272
273/*
274 * mbuf external reference count management macros:
275 *
276 * MEXT_IS_REF(m): true if (m) is not the only mbuf referencing
277 *     the external buffer ext_buf
278 * MEXT_REM_REF(m): remove reference to m_ext object
279 * MEXT_ADD_REF(m): add reference to m_ext object already
280 *     referred to by (m)
281 * MEXT_INIT_REF(m): allocate and initialize an external
282 *     object reference counter for (m)
283 */
284#define MEXT_IS_REF(m) ((m)->m_ext.ref_cnt->refcnt > 1)
285
286#define MEXT_REM_REF(m) do {						\
287	KASSERT((m)->m_ext.ref_cnt->refcnt > 0, ("m_ext refcnt < 0"));	\
288	atomic_subtract_long(&((m)->m_ext.ref_cnt->refcnt), 1);		\
289} while(0)
290
291#define MEXT_ADD_REF(m) atomic_add_long(&((m)->m_ext.ref_cnt->refcnt), 1)
292
293#define _MEXT_ALLOC_CNT(m_cnt, how) do {				\
294	union mext_refcnt *__mcnt;					\
295									\
296	mtx_enter(&mcntfree.m_mtx, MTX_DEF);				\
297	if (mcntfree.m_head == NULL)					\
298		m_alloc_ref(1, (how));					\
299	__mcnt = mcntfree.m_head;					\
300	if (__mcnt != NULL) {						\
301		mcntfree.m_head = __mcnt->next_ref;			\
302		mbstat.m_refree--;					\
303		__mcnt->refcnt = 0;					\
304	}								\
305	mtx_exit(&mcntfree.m_mtx, MTX_DEF);				\
306	(m_cnt) = __mcnt;						\
307} while (0)
308
309#define _MEXT_DEALLOC_CNT(m_cnt) do {					\
310	union mext_refcnt *__mcnt = (m_cnt);				\
311									\
312	mtx_enter(&mcntfree.m_mtx, MTX_DEF);				\
313	__mcnt->next_ref = mcntfree.m_head;				\
314	mcntfree.m_head = __mcnt;					\
315	mbstat.m_refree++;						\
316	mtx_exit(&mcntfree.m_mtx, MTX_DEF);				\
317} while (0)
318
319#define MEXT_INIT_REF(m, how) do {					\
320	struct mbuf *__mmm = (m);					\
321									\
322	_MEXT_ALLOC_CNT(__mmm->m_ext.ref_cnt, (how));			\
323	if (__mmm != NULL)						\
324		MEXT_ADD_REF(__mmm);					\
325} while (0)
326
327/*
328 * mbuf allocation/deallocation macros:
329 *
330 *	MGET(struct mbuf *m, int how, int type)
331 * allocates an mbuf and initializes it to contain internal data.
332 *
333 *	MGETHDR(struct mbuf *m, int how, int type)
334 * allocates an mbuf and initializes it to contain a packet header
335 * and internal data.
336 */
337/*
338 * Lower-level macros for MGET(HDR)... Not to be used outside the
339 * subsystem ("non-exportable" macro names are prepended with "_").
340 */
341#define _MGET_SETUP(m_set, m_set_type) do {				\
342	(m_set)->m_type = (m_set_type);					\
343	(m_set)->m_next = NULL;						\
344	(m_set)->m_nextpkt = NULL;					\
345	(m_set)->m_data = (m_set)->m_dat;				\
346	(m_set)->m_flags = 0;						\
347} while (0)
348
349#define	_MGET(m_mget, m_get_how) do {					\
350	if (mmbfree.m_head == NULL)					\
351		m_mballoc(1, (m_get_how));				\
352	(m_mget) = mmbfree.m_head;					\
353	if ((m_mget) != NULL) {						\
354		mmbfree.m_head = (m_mget)->m_next;			\
355		mbtypes[MT_FREE]--;					\
356	} else {							\
357		if ((m_get_how) == M_WAIT)				\
358			(m_mget) = m_mballoc_wait();			\
359	}								\
360} while (0)
361
362#define MGET(m, how, type) do {						\
363	struct mbuf *_mm;						\
364	int _mhow = (how);						\
365	int _mtype = (type);						\
366									\
367	mtx_enter(&mmbfree.m_mtx, MTX_DEF);				\
368	_MGET(_mm, _mhow);						\
369	if (_mm != NULL) {						\
370		 mbtypes[_mtype]++;					\
371		mtx_exit(&mmbfree.m_mtx, MTX_DEF);			\
372		_MGET_SETUP(_mm, _mtype);				\
373	} else								\
374		mtx_exit(&mmbfree.m_mtx, MTX_DEF);			\
375	(m) = _mm;							\
376} while (0)
377
378#define _MGETHDR_SETUP(m_set, m_set_type) do {				\
379	(m_set)->m_type = (m_set_type);					\
380	(m_set)->m_next = NULL;						\
381	(m_set)->m_nextpkt = NULL;					\
382	(m_set)->m_data = (m_set)->m_pktdat;				\
383	(m_set)->m_flags = M_PKTHDR;					\
384	(m_set)->m_pkthdr.rcvif = NULL;					\
385	(m_set)->m_pkthdr.csum_flags = 0;				\
386	(m_set)->m_pkthdr.aux = NULL;					\
387} while (0)
388
389#define MGETHDR(m, how, type) do {					\
390	struct mbuf *_mm;						\
391	int _mhow = (how);						\
392	int _mtype = (type);						\
393									\
394	mtx_enter(&mmbfree.m_mtx, MTX_DEF);				\
395	_MGET(_mm, _mhow);						\
396	if (_mm != NULL) {						\
397		 mbtypes[_mtype]++;					\
398		mtx_exit(&mmbfree.m_mtx, MTX_DEF);			\
399		_MGETHDR_SETUP(_mm, _mtype);				\
400	} else								\
401		mtx_exit(&mmbfree.m_mtx, MTX_DEF);			\
402	(m) = _mm;							\
403} while (0)
404
405/*
406 * mbuf external storage macros:
407 *
408 *   MCLGET allocates and refers an mcluster to an mbuf
409 *   MEXTADD sets up pre-allocated external storage and refers to mbuf
410 *   MEXTFREE removes reference to external object and frees it if
411 *       necessary
412 */
413#define	_MCLALLOC(p, how) do {						\
414	caddr_t _mp;							\
415	int _mhow = (how);						\
416									\
417	if (mclfree.m_head == NULL)					\
418		m_clalloc(1, _mhow);					\
419	_mp = (caddr_t)mclfree.m_head;					\
420	if (_mp != NULL) {						\
421		mbstat.m_clfree--;					\
422		mclfree.m_head = ((union mcluster *)_mp)->mcl_next;	\
423	} else {							\
424		if (_mhow == M_WAIT)					\
425			_mp = m_clalloc_wait();				\
426	}								\
427	(p) = _mp;							\
428} while (0)
429
430#define	MCLGET(m, how) do {						\
431	struct mbuf *_mm = (m);						\
432									\
433	mtx_enter(&mclfree.m_mtx, MTX_DEF);				\
434	_MCLALLOC(_mm->m_ext.ext_buf, (how));				\
435	mtx_exit(&mclfree.m_mtx, MTX_DEF);				\
436	if (_mm->m_ext.ext_buf != NULL) {				\
437		MEXT_INIT_REF(_mm, (how));				\
438		if (_mm->m_ext.ref_cnt == NULL) {			\
439			_MCLFREE(_mm->m_ext.ext_buf);			\
440			_mm->m_ext.ext_buf = NULL;			\
441		} else {						\
442			_mm->m_data = _mm->m_ext.ext_buf;		\
443			_mm->m_flags |= M_EXT;				\
444			_mm->m_ext.ext_free = NULL;			\
445			_mm->m_ext.ext_args = NULL;			\
446			_mm->m_ext.ext_size = MCLBYTES;			\
447		}							\
448	}								\
449} while (0)
450
451#define MEXTADD(m, buf, size, free, args) do {				\
452	struct mbuf *_mm = (m);						\
453									\
454	MEXT_INIT_REF(_mm, M_WAIT);					\
455	if (_mm->m_ext.ref_cnt != NULL) {				\
456		_mm->m_flags |= M_EXT;					\
457		_mm->m_ext.ext_buf = (caddr_t)(buf);			\
458		_mm->m_data = _mm->m_ext.ext_buf;			\
459		_mm->m_ext.ext_size = (size);				\
460		_mm->m_ext.ext_free = (free);				\
461		_mm->m_ext.ext_args = (args);				\
462	}								\
463} while (0)
464
465#define	_MCLFREE(p) do {						\
466	union mcluster *_mp = (union mcluster *)(p);			\
467									\
468	mtx_enter(&mclfree.m_mtx, MTX_DEF);				\
469	_mp->mcl_next = mclfree.m_head;					\
470	mclfree.m_head = _mp;						\
471	mbstat.m_clfree++;						\
472	MBWAKEUP(m_clalloc_wid);					\
473	mtx_exit(&mclfree.m_mtx, MTX_DEF); 				\
474} while (0)
475
476#define	MEXTFREE(m) do {						\
477	struct mbuf *_mmm = (m);					\
478									\
479	if (MEXT_IS_REF(_mmm))						\
480		MEXT_REM_REF(_mmm);					\
481	else if (_mmm->m_ext.ext_free != NULL) {			\
482		(*(_mmm->m_ext.ext_free))(_mmm->m_ext.ext_buf,		\
483		    _mmm->m_ext.ext_args);				\
484		_MEXT_DEALLOC_CNT(_mmm->m_ext.ref_cnt);			\
485	} else {							\
486		_MCLFREE(_mmm->m_ext.ext_buf);				\
487		_MEXT_DEALLOC_CNT(_mmm->m_ext.ref_cnt);			\
488	}								\
489	_mmm->m_flags &= ~M_EXT;					\
490} while (0)
491
492/*
493 * MFREE(struct mbuf *m, struct mbuf *n)
494 * Free a single mbuf and associated external storage.
495 * Place the successor, if any, in n.
496 */
497#define	MFREE(m, n) do {						\
498	struct mbuf *_mm = (m);						\
499									\
500	KASSERT(_mm->m_type != MT_FREE, ("freeing free mbuf"));		\
501	if (_mm->m_flags & M_EXT)					\
502		MEXTFREE(_mm);						\
503	mtx_enter(&mmbfree.m_mtx, MTX_DEF);				\
504	mbtypes[_mm->m_type]--;						\
505	_mm->m_type = MT_FREE;						\
506	mbtypes[MT_FREE]++;						\
507	(n) = _mm->m_next;						\
508	_mm->m_next = mmbfree.m_head;					\
509	mmbfree.m_head = _mm;						\
510	MBWAKEUP(m_mballoc_wid);					\
511	mtx_exit(&mmbfree.m_mtx, MTX_DEF); 				\
512} while (0)
513
514/*
515 * Copy mbuf pkthdr from "from" to "to".
516 * from must have M_PKTHDR set, and to must be empty.
517 * aux pointer will be moved to `to'.
518 */
519#define	M_COPY_PKTHDR(to, from) do {					\
520	struct mbuf *_mfrom = (from);					\
521	struct mbuf *_mto = (to);					\
522									\
523	_mto->m_data = _mto->m_pktdat;					\
524	_mto->m_flags = _mfrom->m_flags & M_COPYFLAGS;			\
525	_mto->m_pkthdr = _mfrom->m_pkthdr;				\
526	_mfrom->m_pkthdr.aux = (struct mbuf *)NULL;			\
527} while (0)
528
529/*
530 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place
531 * an object of the specified size at the end of the mbuf, longword aligned.
532 */
533#define	M_ALIGN(m, len) do {						\
534	(m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1);		\
535} while (0)
536
537/*
538 * As above, for mbufs allocated with m_gethdr/MGETHDR
539 * or initialized by M_COPY_PKTHDR.
540 */
541#define	MH_ALIGN(m, len) do {						\
542	(m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1);		\
543} while (0)
544
545/*
546 * Compute the amount of space available
547 * before the current start of data in an mbuf.
548 */
549#define	M_LEADINGSPACE(m)						\
550	((m)->m_flags & M_EXT ?						\
551	    /* (m)->m_data - (m)->m_ext.ext_buf */ 0 :			\
552	    (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat :	\
553	    (m)->m_data - (m)->m_dat)
554
555/*
556 * Compute the amount of space available
557 * after the end of data in an mbuf.
558 */
559#define	M_TRAILINGSPACE(m)						\
560	((m)->m_flags & M_EXT ? (m)->m_ext.ext_buf +			\
561	    (m)->m_ext.ext_size - ((m)->m_data + (m)->m_len) :		\
562	    &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len))
563
564/*
565 * Arrange to prepend space of size plen to mbuf m.
566 * If a new mbuf must be allocated, how specifies whether to wait.
567 * If how is M_DONTWAIT and allocation fails, the original mbuf chain
568 * is freed and m is set to NULL.
569 */
570#define	M_PREPEND(m, plen, how) do {					\
571	struct mbuf **_mmp = &(m);					\
572	struct mbuf *_mm = *_mmp;					\
573	int _mplen = (plen);						\
574	int __mhow = (how);						\
575									\
576	if (M_LEADINGSPACE(_mm) >= _mplen) {				\
577		_mm->m_data -= _mplen;					\
578		_mm->m_len += _mplen;					\
579	} else								\
580		_mm = m_prepend(_mm, _mplen, __mhow);			\
581	if (_mm != NULL && _mm->m_flags & M_PKTHDR)			\
582		_mm->m_pkthdr.len += _mplen;				\
583	*_mmp = _mm;							\
584} while (0)
585
586/*
587 * change mbuf to new type
588 */
589#define	MCHTYPE(m, t) do {						\
590	struct mbuf *_mm = (m);						\
591	int _mt = (t);							\
592									\
593	atomic_subtract_long(&mbtypes[_mm->m_type], 1);			\
594	atomic_add_long(&mbtypes[_mt], 1);				\
595	_mm->m_type = (_mt);						\
596} while (0)
597
598/* length to m_copy to copy all */
599#define	M_COPYALL	1000000000
600
601/* compatibility with 4.3 */
602#define	m_copy(m, o, l)	m_copym((m), (o), (l), M_DONTWAIT)
603
604/*
605 * pkthdr.aux type tags.
606 */
607struct mauxtag {
608	int	af;
609	int	type;
610};
611
612#ifdef _KERNEL
613extern	u_long		 m_clalloc_wid;	/* mbuf cluster wait count */
614extern	u_long		 m_mballoc_wid;	/* mbuf wait count */
615extern	int		 max_linkhdr;	/* largest link-level header */
616extern	int		 max_protohdr;	/* largest protocol header */
617extern	int		 max_hdr;	/* largest link+protocol header */
618extern	int		 max_datalen;	/* MHLEN - max_hdr */
619extern	struct mbstat	 mbstat;
620extern	u_long		 mbtypes[MT_NTYPES]; /* per-type mbuf allocations */
621extern	int		 mbuf_wait;	/* mbuf sleep time */
622extern	struct mbuf	*mbutl;		/* virtual address of mclusters */
623extern	struct mclfree_lst	mclfree;
624extern	struct mbffree_lst	mmbfree;
625extern	struct mcntfree_lst	mcntfree;
626extern	int		 nmbclusters;
627extern	int		 nmbufs;
628extern	int		 nsfbufs;
629
630void	m_adj __P((struct mbuf *, int));
631int	m_alloc_ref __P((u_int, int));
632void	m_cat __P((struct mbuf *,struct mbuf *));
633int	m_clalloc __P((int, int));
634caddr_t	m_clalloc_wait __P((void));
635void	m_copyback __P((struct mbuf *, int, int, caddr_t));
636void	m_copydata __P((struct mbuf *,int,int,caddr_t));
637struct	mbuf *m_copym __P((struct mbuf *, int, int, int));
638struct	mbuf *m_copypacket __P((struct mbuf *, int));
639struct	mbuf *m_devget __P((char *, int, int, struct ifnet *,
640    void (*copy)(char *, caddr_t, u_int)));
641struct	mbuf *m_dup __P((struct mbuf *, int));
642struct	mbuf *m_free __P((struct mbuf *));
643void	m_freem __P((struct mbuf *));
644struct	mbuf *m_get __P((int, int));
645struct	mbuf *m_getclr __P((int, int));
646struct	mbuf *m_gethdr __P((int, int));
647int	m_mballoc __P((int, int));
648struct	mbuf *m_mballoc_wait __P((void));
649struct	mbuf *m_prepend __P((struct mbuf *,int,int));
650struct	mbuf *m_pulldown __P((struct mbuf *, int, int, int *));
651void	m_print __P((const struct mbuf *m));
652struct	mbuf *m_pullup __P((struct mbuf *, int));
653struct	mbuf *m_split __P((struct mbuf *,int,int));
654struct	mbuf *m_aux_add __P((struct mbuf *, int, int));
655struct	mbuf *m_aux_find __P((struct mbuf *, int, int));
656void	m_aux_delete __P((struct mbuf *, struct mbuf *));
657#endif /* _KERNEL */
658
659#endif /* !_SYS_MBUF_H_ */
660