mbuf.h revision 168638
1/*-
2 * Copyright (c) 1982, 1986, 1988, 1993
3 *	The Regents of the University of California.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University nor the names of its contributors
15 *    may be used to endorse or promote products derived from this software
16 *    without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 *	@(#)mbuf.h	8.5 (Berkeley) 2/19/95
31 * $FreeBSD: head/sys/sys/mbuf.h 168638 2007-04-11 23:13:12Z andre $
32 */
33
34#ifndef _SYS_MBUF_H_
35#define	_SYS_MBUF_H_
36
37/* XXX: These includes suck. Sorry! */
38#include <sys/queue.h>
39#ifdef _KERNEL
40#include <sys/systm.h>
41#include <vm/uma.h>
42#ifdef WITNESS
43#include <sys/lock.h>
44#endif
45#endif
46
47/*
48 * Mbufs are of a single size, MSIZE (sys/param.h), which includes overhead.
49 * An mbuf may add a single "mbuf cluster" of size MCLBYTES (also in
50 * sys/param.h), which has no additional overhead and is used instead of the
51 * internal data area; this is done when at least MINCLSIZE of data must be
52 * stored.  Additionally, it is possible to allocate a separate buffer
53 * externally and attach it to the mbuf in a way similar to that of mbuf
54 * clusters.
55 */
56#define	MLEN		(MSIZE - sizeof(struct m_hdr))	/* normal data len */
57#define	MHLEN		(MLEN - sizeof(struct pkthdr))	/* data len w/pkthdr */
58#define	MINCLSIZE	(MHLEN + 1)	/* smallest amount to put in cluster */
59#define	M_MAXCOMPRESS	(MHLEN / 2)	/* max amount to copy for compression */
60
61#ifdef _KERNEL
62/*-
63 * Macros for type conversion:
64 * mtod(m, t)	-- Convert mbuf pointer to data pointer of correct type.
65 * dtom(x)	-- Convert data pointer within mbuf to mbuf pointer (XXX).
66 */
67#define	mtod(m, t)	((t)((m)->m_data))
68#define	dtom(x)		((struct mbuf *)((intptr_t)(x) & ~(MSIZE-1)))
69
70/*
71 * Argument structure passed to UMA routines during mbuf and packet
72 * allocations.
73 */
74struct mb_args {
75	int	flags;	/* Flags for mbuf being allocated */
76	short	type;	/* Type of mbuf being allocated */
77};
78#endif /* _KERNEL */
79
80/*
81 * Header present at the beginning of every mbuf.
82 */
83struct m_hdr {
84	struct mbuf	*mh_next;	/* next buffer in chain */
85	struct mbuf	*mh_nextpkt;	/* next chain in queue/record */
86	caddr_t		 mh_data;	/* location of data */
87	int		 mh_len;	/* amount of data in this mbuf */
88	int		 mh_flags;	/* flags; see below */
89	short		 mh_type;	/* type of data in this mbuf */
90};
91
92/*
93 * Packet tag structure (see below for details).
94 */
95struct m_tag {
96	SLIST_ENTRY(m_tag)	m_tag_link;	/* List of packet tags */
97	u_int16_t		m_tag_id;	/* Tag ID */
98	u_int16_t		m_tag_len;	/* Length of data */
99	u_int32_t		m_tag_cookie;	/* ABI/Module ID */
100	void			(*m_tag_free)(struct m_tag *);
101};
102
103/*
104 * Record/packet header in first mbuf of chain; valid only if M_PKTHDR is set.
105 */
106struct pkthdr {
107	struct ifnet	*rcvif;		/* rcv interface */
108	int		 len;		/* total packet length */
109	/* variables for ip and tcp reassembly */
110	void		*header;	/* pointer to packet header */
111	/* variables for hardware checksum */
112	int		 csum_flags;	/* flags regarding checksum */
113	int		 csum_data;	/* data field used by csum routines */
114	u_int16_t	 tso_segsz;	/* TSO segment size */
115	u_int16_t	 ether_vtag;	/* Ethernet 802.1p+q vlan tag */
116	SLIST_HEAD(packet_tags, m_tag) tags; /* list of packet tags */
117};
118
119/*
120 * Description of external storage mapped into mbuf; valid only if M_EXT is
121 * set.
122 */
123struct m_ext {
124	caddr_t		 ext_buf;	/* start of buffer */
125	void		(*ext_free)	/* free routine if not the usual */
126			    (void *, void *);
127	void		*ext_args;	/* optional argument pointer */
128	u_int		 ext_size;	/* size of buffer, for ext_free */
129	volatile u_int	*ref_cnt;	/* pointer to ref count info */
130	int		 ext_type;	/* type of external storage */
131};
132
133/*
134 * The core of the mbuf object along with some shortcut defines for practical
135 * purposes.
136 */
137struct mbuf {
138	struct m_hdr	m_hdr;
139	union {
140		struct {
141			struct pkthdr	MH_pkthdr;	/* M_PKTHDR set */
142			union {
143				struct m_ext	MH_ext;	/* M_EXT set */
144				char		MH_databuf[MHLEN];
145			} MH_dat;
146		} MH;
147		char	M_databuf[MLEN];		/* !M_PKTHDR, !M_EXT */
148	} M_dat;
149};
150#define	m_next		m_hdr.mh_next
151#define	m_len		m_hdr.mh_len
152#define	m_data		m_hdr.mh_data
153#define	m_type		m_hdr.mh_type
154#define	m_flags		m_hdr.mh_flags
155#define	m_nextpkt	m_hdr.mh_nextpkt
156#define	m_act		m_nextpkt
157#define	m_pkthdr	M_dat.MH.MH_pkthdr
158#define	m_ext		M_dat.MH.MH_dat.MH_ext
159#define	m_pktdat	M_dat.MH.MH_dat.MH_databuf
160#define	m_dat		M_dat.M_databuf
161
162/*
163 * mbuf flags.
164 */
165#define	M_EXT		0x0001	/* has associated external storage */
166#define	M_PKTHDR	0x0002	/* start of record */
167#define	M_EOR		0x0004	/* end of record */
168#define	M_RDONLY	0x0008	/* associated data is marked read-only */
169#define	M_PROTO1	0x0010	/* protocol-specific */
170#define	M_PROTO2	0x0020	/* protocol-specific */
171#define	M_PROTO3	0x0040	/* protocol-specific */
172#define	M_PROTO4	0x0080	/* protocol-specific */
173#define	M_PROTO5	0x0100	/* protocol-specific */
174#define	M_NOTIFICATION	0x2000	/* SCTP notification */
175#define	M_SKIP_FIREWALL	0x4000	/* skip firewall processing */
176#define	M_FREELIST	0x8000	/* mbuf is on the free list */
177
178/*
179 * mbuf pkthdr flags (also stored in m_flags).
180 */
181#define	M_BCAST		0x0200	/* send/received as link-level broadcast */
182#define	M_MCAST		0x0400	/* send/received as link-level multicast */
183#define	M_FRAG		0x0800	/* packet is a fragment of a larger packet */
184#define	M_FIRSTFRAG	0x1000	/* packet is first fragment */
185#define	M_LASTFRAG	0x2000	/* packet is last fragment */
186#define	M_VLANTAG	0x10000	/* ether_vtag is valid */
187#define	M_PROMISC	0x20000	/* packet was not for us */
188
189/*
190 * External buffer types: identify ext_buf type.
191 */
192#define	EXT_CLUSTER	1	/* mbuf cluster */
193#define	EXT_SFBUF	2	/* sendfile(2)'s sf_bufs */
194#define	EXT_JUMBOP	3	/* jumbo cluster 4096 bytes */
195#define	EXT_JUMBO9	4	/* jumbo cluster 9216 bytes */
196#define	EXT_JUMBO16	5	/* jumbo cluster 16184 bytes */
197#define	EXT_PACKET	6	/* mbuf+cluster from packet zone */
198#define	EXT_MBUF	7	/* external mbuf reference (M_IOVEC) */
199#define	EXT_NET_DRV	100	/* custom ext_buf provided by net driver(s) */
200#define	EXT_MOD_TYPE	200	/* custom module's ext_buf type */
201#define	EXT_DISPOSABLE	300	/* can throw this buffer away w/page flipping */
202#define	EXT_EXTREF	400	/* has externally maintained ref_cnt ptr */
203
204/*
205 * Flags copied when copying m_pkthdr.
206 */
207#define	M_COPYFLAGS	(M_PKTHDR|M_EOR|M_RDONLY|M_PROTO1|M_PROTO1|M_PROTO2|\
208			    M_PROTO3|M_PROTO4|M_PROTO5|M_SKIP_FIREWALL|\
209			    M_BCAST|M_MCAST|M_FRAG|M_FIRSTFRAG|M_LASTFRAG|\
210			    M_VLANTAG|M_PROMISC)
211
212/*
213 * Flags to purge when crossing layers.
214 */
215#define	M_PROTOFLAGS	(M_PROTO1|M_PROTO2|M_PROTO3|M_PROTO4|M_PROTO5)
216
217/*
218 * Flags indicating hw checksum support and sw checksum requirements.  This
219 * field can be directly tested against if_data.ifi_hwassist.
220 */
221#define	CSUM_IP			0x0001		/* will csum IP */
222#define	CSUM_TCP		0x0002		/* will csum TCP */
223#define	CSUM_UDP		0x0004		/* will csum UDP */
224#define	CSUM_IP_FRAGS		0x0008		/* will csum IP fragments */
225#define	CSUM_FRAGMENT		0x0010		/* will do IP fragmentation */
226#define	CSUM_TSO		0x0020		/* will do TSO */
227
228#define	CSUM_IP_CHECKED		0x0100		/* did csum IP */
229#define	CSUM_IP_VALID		0x0200		/*   ... the csum is valid */
230#define	CSUM_DATA_VALID		0x0400		/* csum_data field is valid */
231#define	CSUM_PSEUDO_HDR		0x0800		/* csum_data has pseudo hdr */
232
233#define	CSUM_DELAY_DATA		(CSUM_TCP | CSUM_UDP)
234#define	CSUM_DELAY_IP		(CSUM_IP)	/* XXX add ipv6 here too? */
235
236/*
237 * mbuf types.
238 */
239#define	MT_NOTMBUF	0	/* USED INTERNALLY ONLY! Object is not mbuf */
240#define	MT_DATA		1	/* dynamic (data) allocation */
241#define	MT_HEADER	MT_DATA	/* packet header, use M_PKTHDR instead */
242#define	MT_SONAME	8	/* socket name */
243#define	MT_CONTROL	14	/* extra-data protocol message */
244#define	MT_OOBDATA	15	/* expedited data  */
245#define	MT_NTYPES	16	/* number of mbuf types for mbtypes[] */
246
247#define	MT_NOINIT	255	/* Not a type but a flag to allocate
248				   a non-initialized mbuf */
249
250/*
251 * General mbuf allocator statistics structure.
252 *
253 * Many of these statistics are no longer used; we instead track many
254 * allocator statistics through UMA's built in statistics mechanism.
255 */
256struct mbstat {
257	u_long	m_mbufs;	/* XXX */
258	u_long	m_mclusts;	/* XXX */
259
260	u_long	m_drain;	/* times drained protocols for space */
261	u_long	m_mcfail;	/* XXX: times m_copym failed */
262	u_long	m_mpfail;	/* XXX: times m_pullup failed */
263	u_long	m_msize;	/* length of an mbuf */
264	u_long	m_mclbytes;	/* length of an mbuf cluster */
265	u_long	m_minclsize;	/* min length of data to allocate a cluster */
266	u_long	m_mlen;		/* length of data in an mbuf */
267	u_long	m_mhlen;	/* length of data in a header mbuf */
268
269	/* Number of mbtypes (gives # elems in mbtypes[] array: */
270	short	m_numtypes;
271
272	/* XXX: Sendfile stats should eventually move to their own struct */
273	u_long	sf_iocnt;	/* times sendfile had to do disk I/O */
274	u_long	sf_allocfail;	/* times sfbuf allocation failed */
275	u_long	sf_allocwait;	/* times sfbuf allocation had to wait */
276};
277
278/*
279 * Flags specifying how an allocation should be made.
280 *
281 * The flag to use is as follows:
282 * - M_DONTWAIT or M_NOWAIT from an interrupt handler to not block allocation.
283 * - M_WAIT or M_WAITOK or M_TRYWAIT from wherever it is safe to block.
284 *
285 * M_DONTWAIT/M_NOWAIT means that we will not block the thread explicitly and
286 * if we cannot allocate immediately we may return NULL, whereas
287 * M_WAIT/M_WAITOK/M_TRYWAIT means that if we cannot allocate resources we
288 * will block until they are available, and thus never return NULL.
289 *
290 * XXX Eventually just phase this out to use M_WAITOK/M_NOWAIT.
291 */
292#define	MBTOM(how)	(how)
293#define	M_DONTWAIT	M_NOWAIT
294#define	M_TRYWAIT	M_WAITOK
295#define	M_WAIT		M_WAITOK
296
297/*
298 * String names of mbuf-related UMA(9) and malloc(9) types.  Exposed to
299 * !_KERNEL so that monitoring tools can look up the zones with
300 * libmemstat(3).
301 */
302#define	MBUF_MEM_NAME		"mbuf"
303#define	MBUF_CLUSTER_MEM_NAME	"mbuf_cluster"
304#define	MBUF_PACKET_MEM_NAME	"mbuf_packet"
305#define	MBUF_JUMBOP_MEM_NAME	"mbuf_jumbo_pagesize"
306#define	MBUF_JUMBO9_MEM_NAME	"mbuf_jumbo_9k"
307#define	MBUF_JUMBO16_MEM_NAME	"mbuf_jumbo_16k"
308#define	MBUF_TAG_MEM_NAME	"mbuf_tag"
309#define	MBUF_EXTREFCNT_MEM_NAME	"mbuf_ext_refcnt"
310
311#ifdef _KERNEL
312
313#ifdef WITNESS
314#define	MBUF_CHECKSLEEP(how) do {					\
315	if (how == M_WAITOK)						\
316		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,		\
317		    "Sleeping in \"%s\"", __func__);			\
318} while (0)
319#else
320#define	MBUF_CHECKSLEEP(how)
321#endif
322
323/*
324 * Network buffer allocation API
325 *
326 * The rest of it is defined in kern/kern_mbuf.c
327 */
328
329extern uma_zone_t	zone_mbuf;
330extern uma_zone_t	zone_clust;
331extern uma_zone_t	zone_pack;
332extern uma_zone_t	zone_jumbop;
333extern uma_zone_t	zone_jumbo9;
334extern uma_zone_t	zone_jumbo16;
335extern uma_zone_t	zone_ext_refcnt;
336
337static __inline struct mbuf	*m_get(int how, short type);
338static __inline struct mbuf	*m_gethdr(int how, short type);
339static __inline struct mbuf	*m_getcl(int how, short type, int flags);
340static __inline struct mbuf	*m_getjcl(int how, short type, int flags,
341				    int size);
342static __inline struct mbuf	*m_getclr(int how, short type);	/* XXX */
343static __inline struct mbuf	*m_free(struct mbuf *m);
344static __inline void		 m_clget(struct mbuf *m, int how);
345static __inline void		*m_cljget(struct mbuf *m, int how, int size);
346static __inline void		 m_chtype(struct mbuf *m, short new_type);
347void				 mb_free_ext(struct mbuf *);
348static __inline struct mbuf	*m_last(struct mbuf *m);
349
350static __inline int
351m_gettype(int size)
352{
353	int type;
354
355	switch (size) {
356	case MSIZE:
357		type = EXT_MBUF;
358		break;
359	case MCLBYTES:
360		type = EXT_CLUSTER;
361		break;
362#if MJUMPAGESIZE != MCLBYTES
363	case MJUMPAGESIZE:
364		type = EXT_JUMBOP;
365		break;
366#endif
367	case MJUM9BYTES:
368		type = EXT_JUMBO9;
369		break;
370	case MJUM16BYTES:
371		type = EXT_JUMBO16;
372		break;
373	default:
374		panic("%s: m_getjcl: invalid cluster size", __func__);
375	}
376
377	return (type);
378}
379
380static __inline uma_zone_t
381m_getzone(int size)
382{
383	uma_zone_t zone;
384
385	switch (size) {
386	case MSIZE:
387		zone = zone_mbuf;
388		break;
389	case MCLBYTES:
390		zone = zone_clust;
391		break;
392#if MJUMPAGESIZE != MCLBYTES
393	case MJUMPAGESIZE:
394		zone = zone_jumbop;
395		break;
396#endif
397	case MJUM9BYTES:
398		zone = zone_jumbo9;
399		break;
400	case MJUM16BYTES:
401		zone = zone_jumbo16;
402		break;
403	default:
404		panic("%s: m_getjcl: invalid cluster type", __func__);
405	}
406
407	return (zone);
408}
409
410static __inline struct mbuf *
411m_get(int how, short type)
412{
413	struct mb_args args;
414
415	args.flags = 0;
416	args.type = type;
417	return ((struct mbuf *)(uma_zalloc_arg(zone_mbuf, &args, how)));
418}
419
420/*
421 * XXX This should be deprecated, very little use.
422 */
423static __inline struct mbuf *
424m_getclr(int how, short type)
425{
426	struct mbuf *m;
427	struct mb_args args;
428
429	args.flags = 0;
430	args.type = type;
431	m = uma_zalloc_arg(zone_mbuf, &args, how);
432	if (m != NULL)
433		bzero(m->m_data, MLEN);
434	return (m);
435}
436
437static __inline struct mbuf *
438m_gethdr(int how, short type)
439{
440	struct mb_args args;
441
442	args.flags = M_PKTHDR;
443	args.type = type;
444	return ((struct mbuf *)(uma_zalloc_arg(zone_mbuf, &args, how)));
445}
446
447static __inline struct mbuf *
448m_getcl(int how, short type, int flags)
449{
450	struct mb_args args;
451
452	args.flags = flags;
453	args.type = type;
454	return ((struct mbuf *)(uma_zalloc_arg(zone_pack, &args, how)));
455}
456
457/*
458 * m_getjcl() returns an mbuf with a cluster of the specified size attached.
459 * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES.
460 *
461 * XXX: This is rather large, should be real function maybe.
462 */
463static __inline struct mbuf *
464m_getjcl(int how, short type, int flags, int size)
465{
466	struct mb_args args;
467	struct mbuf *m, *n;
468	uma_zone_t zone;
469
470	args.flags = flags;
471	args.type = type;
472
473	m = uma_zalloc_arg(zone_mbuf, &args, how);
474	if (m == NULL)
475		return (NULL);
476
477	zone = m_getzone(size);
478	n = uma_zalloc_arg(zone, m, how);
479	if (n == NULL) {
480		uma_zfree(zone_mbuf, m);
481		return (NULL);
482	}
483	return (m);
484}
485
486static __inline struct mbuf *
487m_free(struct mbuf *m)
488{
489	struct mbuf *n = m->m_next;
490
491	if (m->m_flags & M_EXT)
492		mb_free_ext(m);
493	else
494		uma_zfree(zone_mbuf, m);
495	return (n);
496}
497
498static __inline void
499m_clget(struct mbuf *m, int how)
500{
501
502	if (m->m_flags & M_EXT)
503		printf("%s: %p mbuf already has cluster\n", __func__, m);
504	m->m_ext.ext_buf = (char *)NULL;
505	uma_zalloc_arg(zone_clust, m, how);
506	/*
507	 * On a cluster allocation failure, drain the packet zone and retry,
508	 * we might be able to loosen a few clusters up on the drain.
509	 */
510	if ((how & M_NOWAIT) && (m->m_ext.ext_buf == NULL)) {
511		zone_drain(zone_pack);
512		uma_zalloc_arg(zone_clust, m, how);
513	}
514}
515
516/*
517 * m_cljget() is different from m_clget() as it can allocate clusters without
518 * attaching them to an mbuf.  In that case the return value is the pointer
519 * to the cluster of the requested size.  If an mbuf was specified, it gets
520 * the cluster attached to it and the return value can be safely ignored.
521 * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES.
522 */
523static __inline void *
524m_cljget(struct mbuf *m, int how, int size)
525{
526	uma_zone_t zone;
527
528	if (m && m->m_flags & M_EXT)
529		printf("%s: %p mbuf already has cluster\n", __func__, m);
530	if (m != NULL)
531		m->m_ext.ext_buf = NULL;
532
533	zone = m_getzone(size);
534	return (uma_zalloc_arg(zone, m, how));
535}
536
537static __inline void
538m_cljset(struct mbuf *m, void *cl, int type)
539{
540	uma_zone_t zone;
541	int size;
542
543	switch (type) {
544	case EXT_CLUSTER:
545		size = MCLBYTES;
546		zone = zone_clust;
547		break;
548#if MJUMPAGESIZE != MCLBYTES
549	case EXT_JUMBOP:
550		size = MJUMPAGESIZE;
551		zone = zone_jumbop;
552		break;
553#endif
554	case EXT_JUMBO9:
555		size = MJUM9BYTES;
556		zone = zone_jumbo9;
557		break;
558	case EXT_JUMBO16:
559		size = MJUM16BYTES;
560		zone = zone_jumbo16;
561		break;
562	default:
563		panic("unknown cluster type");
564		break;
565	}
566
567	m->m_data = m->m_ext.ext_buf = cl;
568	m->m_ext.ext_free = m->m_ext.ext_args = NULL;
569	m->m_ext.ext_size = size;
570	m->m_ext.ext_type = type;
571	m->m_ext.ref_cnt = uma_find_refcnt(zone, cl);
572	m->m_flags |= M_EXT;
573
574}
575
576static __inline void
577m_chtype(struct mbuf *m, short new_type)
578{
579
580	m->m_type = new_type;
581}
582
583static __inline struct mbuf *
584m_last(struct mbuf *m)
585{
586
587	while (m->m_next)
588		m = m->m_next;
589	return (m);
590}
591
592/*
593 * mbuf, cluster, and external object allocation macros (for compatibility
594 * purposes).
595 */
596#define	M_MOVE_PKTHDR(to, from)	m_move_pkthdr((to), (from))
597#define	MGET(m, how, type)	((m) = m_get((how), (type)))
598#define	MGETHDR(m, how, type)	((m) = m_gethdr((how), (type)))
599#define	MCLGET(m, how)		m_clget((m), (how))
600#define	MEXTADD(m, buf, size, free, args, flags, type) 			\
601    m_extadd((m), (caddr_t)(buf), (size), (free), (args), (flags), (type))
602#define	m_getm(m, len, how, type)					\
603    m_getm2((m), (len), (how), (type), M_PKTHDR)
604
605/*
606 * Evaluate TRUE if it's safe to write to the mbuf m's data region (this can
607 * be both the local data payload, or an external buffer area, depending on
608 * whether M_EXT is set).
609 */
610#define	M_WRITABLE(m)	(!((m)->m_flags & M_RDONLY) &&			\
611			 (!(((m)->m_flags & M_EXT)) ||			\
612			 (*((m)->m_ext.ref_cnt) == 1)) )		\
613
614/* Check if the supplied mbuf has a packet header, or else panic. */
615#define	M_ASSERTPKTHDR(m)						\
616	KASSERT(m != NULL && m->m_flags & M_PKTHDR,			\
617	    ("%s: no mbuf packet header!", __func__))
618
619/*
620 * Ensure that the supplied mbuf is a valid, non-free mbuf.
621 *
622 * XXX: Broken at the moment.  Need some UMA magic to make it work again.
623 */
624#define	M_ASSERTVALID(m)						\
625	KASSERT((((struct mbuf *)m)->m_flags & 0) == 0,			\
626	    ("%s: attempted use of a free mbuf!", __func__))
627
628/*
629 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place an
630 * object of the specified size at the end of the mbuf, longword aligned.
631 */
632#define	M_ALIGN(m, len) do {						\
633	KASSERT(!((m)->m_flags & (M_PKTHDR|M_EXT)),			\
634		("%s: M_ALIGN not normal mbuf", __func__));		\
635	KASSERT((m)->m_data == (m)->m_dat,				\
636		("%s: M_ALIGN not a virgin mbuf", __func__));		\
637	(m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1);		\
638} while (0)
639
640/*
641 * As above, for mbufs allocated with m_gethdr/MGETHDR or initialized by
642 * M_DUP/MOVE_PKTHDR.
643 */
644#define	MH_ALIGN(m, len) do {						\
645	KASSERT((m)->m_flags & M_PKTHDR && !((m)->m_flags & M_EXT),	\
646		("%s: MH_ALIGN not PKTHDR mbuf", __func__));		\
647	KASSERT((m)->m_data == (m)->m_pktdat,				\
648		("%s: MH_ALIGN not a virgin mbuf", __func__));		\
649	(m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1);		\
650} while (0)
651
652/*
653 * Compute the amount of space available before the current start of data in
654 * an mbuf.
655 *
656 * The M_WRITABLE() is a temporary, conservative safety measure: the burden
657 * of checking writability of the mbuf data area rests solely with the caller.
658 */
659#define	M_LEADINGSPACE(m)						\
660	((m)->m_flags & M_EXT ?						\
661	    (M_WRITABLE(m) ? (m)->m_data - (m)->m_ext.ext_buf : 0):	\
662	    (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat :	\
663	    (m)->m_data - (m)->m_dat)
664
665/*
666 * Compute the amount of space available after the end of data in an mbuf.
667 *
668 * The M_WRITABLE() is a temporary, conservative safety measure: the burden
669 * of checking writability of the mbuf data area rests solely with the caller.
670 */
671#define	M_TRAILINGSPACE(m)						\
672	((m)->m_flags & M_EXT ?						\
673	    (M_WRITABLE(m) ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size	\
674		- ((m)->m_data + (m)->m_len) : 0) :			\
675	    &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len))
676
677/*
678 * Arrange to prepend space of size plen to mbuf m.  If a new mbuf must be
679 * allocated, how specifies whether to wait.  If the allocation fails, the
680 * original mbuf chain is freed and m is set to NULL.
681 */
682#define	M_PREPEND(m, plen, how) do {					\
683	struct mbuf **_mmp = &(m);					\
684	struct mbuf *_mm = *_mmp;					\
685	int _mplen = (plen);						\
686	int __mhow = (how);						\
687									\
688	MBUF_CHECKSLEEP(how);						\
689	if (M_LEADINGSPACE(_mm) >= _mplen) {				\
690		_mm->m_data -= _mplen;					\
691		_mm->m_len += _mplen;					\
692	} else								\
693		_mm = m_prepend(_mm, _mplen, __mhow);			\
694	if (_mm != NULL && _mm->m_flags & M_PKTHDR)			\
695		_mm->m_pkthdr.len += _mplen;				\
696	*_mmp = _mm;							\
697} while (0)
698
699/*
700 * Change mbuf to new type.  This is a relatively expensive operation and
701 * should be avoided.
702 */
703#define	MCHTYPE(m, t)	m_chtype((m), (t))
704
705/* Length to m_copy to copy all. */
706#define	M_COPYALL	1000000000
707
708/* Compatibility with 4.3. */
709#define	m_copy(m, o, l)	m_copym((m), (o), (l), M_DONTWAIT)
710
711extern int		max_datalen;	/* MHLEN - max_hdr */
712extern int		max_hdr;	/* Largest link + protocol header */
713extern int		max_linkhdr;	/* Largest link-level header */
714extern int		max_protohdr;	/* Largest protocol header */
715extern struct mbstat	mbstat;		/* General mbuf stats/infos */
716extern int		nmbclusters;	/* Maximum number of clusters */
717
718struct uio;
719
720void		 m_adj(struct mbuf *, int);
721void		 m_align(struct mbuf *, int);
722int		 m_apply(struct mbuf *, int, int,
723		    int (*)(void *, void *, u_int), void *);
724int		 m_append(struct mbuf *, int, c_caddr_t);
725void		 m_cat(struct mbuf *, struct mbuf *);
726void		 m_extadd(struct mbuf *, caddr_t, u_int,
727		    void (*)(void *, void *), void *, int, int);
728void		 m_copyback(struct mbuf *, int, int, c_caddr_t);
729void		 m_copydata(const struct mbuf *, int, int, caddr_t);
730struct mbuf	*m_copym(struct mbuf *, int, int, int);
731struct mbuf	*m_copymdata(struct mbuf *, struct mbuf *,
732		    int, int, int, int);
733struct mbuf	*m_copypacket(struct mbuf *, int);
734void		 m_copy_pkthdr(struct mbuf *, struct mbuf *);
735struct mbuf	*m_copyup(struct mbuf *n, int len, int dstoff);
736struct mbuf	*m_defrag(struct mbuf *, int);
737void		 m_demote(struct mbuf *, int);
738struct mbuf	*m_devget(char *, int, int, struct ifnet *,
739		    void (*)(char *, caddr_t, u_int));
740struct mbuf	*m_dup(struct mbuf *, int);
741int		 m_dup_pkthdr(struct mbuf *, struct mbuf *, int);
742u_int		 m_fixhdr(struct mbuf *);
743struct mbuf	*m_fragment(struct mbuf *, int, int);
744void		 m_freem(struct mbuf *);
745struct mbuf	*m_getm2(struct mbuf *, int, int, short, int);
746struct mbuf	*m_getptr(struct mbuf *, int, int *);
747u_int		 m_length(struct mbuf *, struct mbuf **);
748void		 m_move_pkthdr(struct mbuf *, struct mbuf *);
749struct mbuf	*m_prepend(struct mbuf *, int, int);
750void		 m_print(const struct mbuf *, int);
751struct mbuf	*m_pulldown(struct mbuf *, int, int, int *);
752struct mbuf	*m_pullup(struct mbuf *, int);
753int		m_sanity(struct mbuf *, int);
754struct mbuf	*m_split(struct mbuf *, int, int);
755struct mbuf	*m_uiotombuf(struct uio *, int, int, int, int);
756struct mbuf	*m_unshare(struct mbuf *, int how);
757
758/*-
759 * Network packets may have annotations attached by affixing a list of
760 * "packet tags" to the pkthdr structure.  Packet tags are dynamically
761 * allocated semi-opaque data structures that have a fixed header
762 * (struct m_tag) that specifies the size of the memory block and a
763 * <cookie,type> pair that identifies it.  The cookie is a 32-bit unique
764 * unsigned value used to identify a module or ABI.  By convention this value
765 * is chosen as the date+time that the module is created, expressed as the
766 * number of seconds since the epoch (e.g., using date -u +'%s').  The type
767 * value is an ABI/module-specific value that identifies a particular
768 * annotation and is private to the module.  For compatibility with systems
769 * like OpenBSD that define packet tags w/o an ABI/module cookie, the value
770 * PACKET_ABI_COMPAT is used to implement m_tag_get and m_tag_find
771 * compatibility shim functions and several tag types are defined below.
772 * Users that do not require compatibility should use a private cookie value
773 * so that packet tag-related definitions can be maintained privately.
774 *
775 * Note that the packet tag returned by m_tag_alloc has the default memory
776 * alignment implemented by malloc.  To reference private data one can use a
777 * construct like:
778 *
779 *	struct m_tag *mtag = m_tag_alloc(...);
780 *	struct foo *p = (struct foo *)(mtag+1);
781 *
782 * if the alignment of struct m_tag is sufficient for referencing members of
783 * struct foo.  Otherwise it is necessary to embed struct m_tag within the
784 * private data structure to insure proper alignment; e.g.,
785 *
786 *	struct foo {
787 *		struct m_tag	tag;
788 *		...
789 *	};
790 *	struct foo *p = (struct foo *) m_tag_alloc(...);
791 *	struct m_tag *mtag = &p->tag;
792 */
793
794/*
795 * Persistent tags stay with an mbuf until the mbuf is reclaimed.  Otherwise
796 * tags are expected to ``vanish'' when they pass through a network
797 * interface.  For most interfaces this happens normally as the tags are
798 * reclaimed when the mbuf is free'd.  However in some special cases
799 * reclaiming must be done manually.  An example is packets that pass through
800 * the loopback interface.  Also, one must be careful to do this when
801 * ``turning around'' packets (e.g., icmp_reflect).
802 *
803 * To mark a tag persistent bit-or this flag in when defining the tag id.
804 * The tag will then be treated as described above.
805 */
806#define	MTAG_PERSISTENT				0x800
807
808#define	PACKET_TAG_NONE				0  /* Nadda */
809
810/* Packet tags for use with PACKET_ABI_COMPAT. */
811#define	PACKET_TAG_IPSEC_IN_DONE		1  /* IPsec applied, in */
812#define	PACKET_TAG_IPSEC_OUT_DONE		2  /* IPsec applied, out */
813#define	PACKET_TAG_IPSEC_IN_CRYPTO_DONE		3  /* NIC IPsec crypto done */
814#define	PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED	4  /* NIC IPsec crypto req'ed */
815#define	PACKET_TAG_IPSEC_IN_COULD_DO_CRYPTO	5  /* NIC notifies IPsec */
816#define	PACKET_TAG_IPSEC_PENDING_TDB		6  /* Reminder to do IPsec */
817#define	PACKET_TAG_BRIDGE			7  /* Bridge processing done */
818#define	PACKET_TAG_GIF				8  /* GIF processing done */
819#define	PACKET_TAG_GRE				9  /* GRE processing done */
820#define	PACKET_TAG_IN_PACKET_CHECKSUM		10 /* NIC checksumming done */
821#define	PACKET_TAG_ENCAP			11 /* Encap.  processing */
822#define	PACKET_TAG_IPSEC_SOCKET			12 /* IPSEC socket ref */
823#define	PACKET_TAG_IPSEC_HISTORY		13 /* IPSEC history */
824#define	PACKET_TAG_IPV6_INPUT			14 /* IPV6 input processing */
825#define	PACKET_TAG_DUMMYNET			15 /* dummynet info */
826#define	PACKET_TAG_DIVERT			17 /* divert info */
827#define	PACKET_TAG_IPFORWARD			18 /* ipforward info */
828#define	PACKET_TAG_MACLABEL	(19 | MTAG_PERSISTENT) /* MAC label */
829#define	PACKET_TAG_PF_ROUTED			21 /* PF routed, avoid loops */
830#define	PACKET_TAG_PF_FRAGCACHE			22 /* PF fragment cached */
831#define	PACKET_TAG_PF_QID			23 /* PF ALTQ queue id */
832#define	PACKET_TAG_PF_TAG			24 /* PF tagged */
833#define	PACKET_TAG_RTSOCKFAM			25 /* rtsock sa family */
834#define	PACKET_TAG_PF_TRANSLATE_LOCALHOST	26 /* PF translate localhost */
835#define	PACKET_TAG_IPOPTIONS			27 /* Saved IP options */
836#define	PACKET_TAG_CARP                         28 /* CARP info */
837
838/* Specific cookies and tags. */
839
840/* Packet tag routines. */
841struct m_tag	*m_tag_alloc(u_int32_t, int, int, int);
842void		 m_tag_delete(struct mbuf *, struct m_tag *);
843void		 m_tag_delete_chain(struct mbuf *, struct m_tag *);
844void		 m_tag_free_default(struct m_tag *);
845struct m_tag	*m_tag_locate(struct mbuf *, u_int32_t, int, struct m_tag *);
846struct m_tag	*m_tag_copy(struct m_tag *, int);
847int		 m_tag_copy_chain(struct mbuf *, struct mbuf *, int);
848void		 m_tag_delete_nonpersistent(struct mbuf *);
849
850/*
851 * Initialize the list of tags associated with an mbuf.
852 */
853static __inline void
854m_tag_init(struct mbuf *m)
855{
856
857	SLIST_INIT(&m->m_pkthdr.tags);
858}
859
860/*
861 * Set up the contents of a tag.  Note that this does not fill in the free
862 * method; the caller is expected to do that.
863 *
864 * XXX probably should be called m_tag_init, but that was already taken.
865 */
866static __inline void
867m_tag_setup(struct m_tag *t, u_int32_t cookie, int type, int len)
868{
869
870	t->m_tag_id = type;
871	t->m_tag_len = len;
872	t->m_tag_cookie = cookie;
873}
874
875/*
876 * Reclaim resources associated with a tag.
877 */
878static __inline void
879m_tag_free(struct m_tag *t)
880{
881
882	(*t->m_tag_free)(t);
883}
884
885/*
886 * Return the first tag associated with an mbuf.
887 */
888static __inline struct m_tag *
889m_tag_first(struct mbuf *m)
890{
891
892	return (SLIST_FIRST(&m->m_pkthdr.tags));
893}
894
895/*
896 * Return the next tag in the list of tags associated with an mbuf.
897 */
898static __inline struct m_tag *
899m_tag_next(struct mbuf *m, struct m_tag *t)
900{
901
902	return (SLIST_NEXT(t, m_tag_link));
903}
904
905/*
906 * Prepend a tag to the list of tags associated with an mbuf.
907 */
908static __inline void
909m_tag_prepend(struct mbuf *m, struct m_tag *t)
910{
911
912	SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link);
913}
914
915/*
916 * Unlink a tag from the list of tags associated with an mbuf.
917 */
918static __inline void
919m_tag_unlink(struct mbuf *m, struct m_tag *t)
920{
921
922	SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link);
923}
924
925/* These are for OpenBSD compatibility. */
926#define	MTAG_ABI_COMPAT		0		/* compatibility ABI */
927
928static __inline struct m_tag *
929m_tag_get(int type, int length, int wait)
930{
931	return (m_tag_alloc(MTAG_ABI_COMPAT, type, length, wait));
932}
933
934static __inline struct m_tag *
935m_tag_find(struct mbuf *m, int type, struct m_tag *start)
936{
937	return (SLIST_EMPTY(&m->m_pkthdr.tags) ? (struct m_tag *)NULL :
938	    m_tag_locate(m, MTAG_ABI_COMPAT, type, start));
939}
940
941#endif /* _KERNEL */
942
943#endif /* !_SYS_MBUF_H_ */
944