mbuf.h revision 127976
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 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	@(#)mbuf.h	8.5 (Berkeley) 2/19/95
30 * $FreeBSD: head/sys/sys/mbuf.h 127976 2004-04-07 04:19:52Z imp $
31 */
32
33#ifndef _SYS_MBUF_H_
34#define	_SYS_MBUF_H_
35
36#include <sys/queue.h>
37
38/*
39 * Mbufs are of a single size, MSIZE (sys/param.h), which
40 * includes overhead.  An mbuf may add a single "mbuf cluster" of size
41 * MCLBYTES (also in sys/param.h), which has no additional overhead
42 * and is used instead of the internal data area; this is done when
43 * at least MINCLSIZE of data must be stored.  Additionally, it is possible
44 * to allocate a separate buffer externally and attach it to the mbuf in
45 * a way similar to that of mbuf clusters.
46 */
47#define	MLEN		(MSIZE - sizeof(struct m_hdr))	/* normal data len */
48#define	MHLEN		(MLEN - sizeof(struct pkthdr))	/* data len w/pkthdr */
49#define	MINCLSIZE	(MHLEN + 1)	/* smallest amount to put in cluster */
50#define	M_MAXCOMPRESS	(MHLEN / 2)	/* max amount to copy for compression */
51
52#ifdef _KERNEL
53/*-
54 * Macros for type conversion:
55 * mtod(m, t)	-- Convert mbuf pointer to data pointer of correct type.
56 * dtom(x)	-- Convert data pointer within mbuf to mbuf pointer (XXX).
57 */
58#define	mtod(m, t)	((t)((m)->m_data))
59#define	dtom(x)		((struct mbuf *)((intptr_t)(x) & ~(MSIZE-1)))
60#endif /* _KERNEL */
61
62/*
63 * Header present at the beginning of every mbuf.
64 */
65struct m_hdr {
66	struct	mbuf *mh_next;		/* next buffer in chain */
67	struct	mbuf *mh_nextpkt;	/* next chain in queue/record */
68	caddr_t	mh_data;		/* location of data */
69	int	mh_len;			/* amount of data in this mbuf */
70	int	mh_flags;		/* flags; see below */
71	short	mh_type;		/* type of data in this mbuf */
72};
73
74/*
75 * Packet tag structure (see below for details).
76 */
77struct m_tag {
78	SLIST_ENTRY(m_tag)	m_tag_link;	/* List of packet tags */
79	u_int16_t		m_tag_id;	/* Tag ID */
80	u_int16_t		m_tag_len;	/* Length of data */
81	u_int32_t		m_tag_cookie;	/* ABI/Module ID */
82	void			(*m_tag_free)(struct m_tag *);
83};
84
85/*
86 * Record/packet header in first mbuf of chain; valid only if M_PKTHDR is set.
87 */
88struct pkthdr {
89	struct	ifnet *rcvif;		/* rcv interface */
90	int	len;			/* total packet length */
91	/* variables for ip and tcp reassembly */
92	void	*header;		/* pointer to packet header */
93	/* variables for hardware checksum */
94	int	csum_flags;		/* flags regarding checksum */
95	int	csum_data;		/* data field used by csum routines */
96	SLIST_HEAD(packet_tags, m_tag) tags; /* list of packet tags */
97};
98
99/*
100 * Description of external storage mapped into mbuf; valid only if M_EXT is set.
101 */
102struct m_ext {
103	caddr_t	ext_buf;		/* start of buffer */
104	void	(*ext_free)		/* free routine if not the usual */
105		    (void *, void *);
106	void	*ext_args;		/* optional argument pointer */
107	u_int	ext_size;		/* size of buffer, for ext_free */
108	u_int	*ref_cnt;		/* pointer to ref count info */
109	int	ext_type;		/* type of external storage */
110};
111
112/*
113 * The core of the mbuf object along with some shortcut defines for
114 * practical purposes.
115 */
116struct mbuf {
117	struct	m_hdr m_hdr;
118	union {
119		struct {
120			struct	pkthdr MH_pkthdr;	/* M_PKTHDR set */
121			union {
122				struct	m_ext MH_ext;	/* M_EXT set */
123				char	MH_databuf[MHLEN];
124			} MH_dat;
125		} MH;
126		char	M_databuf[MLEN];		/* !M_PKTHDR, !M_EXT */
127	} M_dat;
128};
129#define	m_next		m_hdr.mh_next
130#define	m_len		m_hdr.mh_len
131#define	m_data		m_hdr.mh_data
132#define	m_type		m_hdr.mh_type
133#define	m_flags		m_hdr.mh_flags
134#define	m_nextpkt	m_hdr.mh_nextpkt
135#define	m_act		m_nextpkt
136#define	m_pkthdr	M_dat.MH.MH_pkthdr
137#define	m_ext		M_dat.MH.MH_dat.MH_ext
138#define	m_pktdat	M_dat.MH.MH_dat.MH_databuf
139#define	m_dat		M_dat.M_databuf
140
141/*
142 * mbuf flags.
143 */
144#define	M_EXT		0x0001	/* has associated external storage */
145#define	M_PKTHDR	0x0002	/* start of record */
146#define	M_EOR		0x0004	/* end of record */
147#define	M_RDONLY	0x0008	/* associated data is marked read-only */
148#define	M_PROTO1	0x0010	/* protocol-specific */
149#define	M_PROTO2	0x0020	/* protocol-specific */
150#define	M_PROTO3	0x0040	/* protocol-specific */
151#define	M_PROTO4	0x0080	/* protocol-specific */
152#define	M_PROTO5	0x0100	/* protocol-specific */
153#define M_PROTO6	0x4000	/* protocol-specific (avoid M_BCAST conflict) */
154#define	M_FREELIST	0x8000	/* mbuf is on the free list */
155
156/*
157 * mbuf pkthdr flags (also stored in m_flags).
158 */
159#define	M_BCAST		0x0200	/* send/received as link-level broadcast */
160#define	M_MCAST		0x0400	/* send/received as link-level multicast */
161#define	M_FRAG		0x0800	/* packet is a fragment of a larger packet */
162#define	M_FIRSTFRAG	0x1000	/* packet is first fragment */
163#define	M_LASTFRAG	0x2000	/* packet is last fragment */
164
165/*
166 * External buffer types: identify ext_buf type.
167 */
168#define	EXT_CLUSTER	1	/* mbuf cluster */
169#define	EXT_SFBUF	2	/* sendfile(2)'s sf_bufs */
170#define	EXT_NET_DRV	100	/* custom ext_buf provided by net driver(s) */
171#define	EXT_MOD_TYPE	200	/* custom module's ext_buf type */
172#define	EXT_DISPOSABLE	300	/* can throw this buffer away w/page flipping */
173#define	EXT_EXTREF	400	/* has externally maintained ref_cnt ptr*/
174
175/*
176 * Flags copied when copying m_pkthdr.
177 */
178#define	M_COPYFLAGS	(M_PKTHDR|M_EOR|M_RDONLY|M_PROTO1|M_PROTO1|M_PROTO2|\
179			    M_PROTO3|M_PROTO4|M_PROTO5|M_PROTO6|\
180			    M_BCAST|M_MCAST|M_FRAG|M_FIRSTFRAG|M_LASTFRAG)
181
182/*
183 * Flags indicating hw checksum support and sw checksum requirements.
184 */
185#define	CSUM_IP			0x0001		/* will csum IP */
186#define	CSUM_TCP		0x0002		/* will csum TCP */
187#define	CSUM_UDP		0x0004		/* will csum UDP */
188#define	CSUM_IP_FRAGS		0x0008		/* will csum IP fragments */
189#define	CSUM_FRAGMENT		0x0010		/* will do IP fragmentation */
190
191#define	CSUM_IP_CHECKED		0x0100		/* did csum IP */
192#define	CSUM_IP_VALID		0x0200		/*   ... the csum is valid */
193#define	CSUM_DATA_VALID		0x0400		/* csum_data field is valid */
194#define	CSUM_PSEUDO_HDR		0x0800		/* csum_data has pseudo hdr */
195
196#define	CSUM_DELAY_DATA		(CSUM_TCP | CSUM_UDP)
197#define	CSUM_DELAY_IP		(CSUM_IP)	/* XXX add ipv6 here too? */
198
199/*
200 * mbuf types.
201 */
202#define	MT_NOTMBUF	0	/* USED INTERNALLY ONLY! Object is not mbuf */
203#define	MT_DATA		1	/* dynamic (data) allocation */
204#define	MT_HEADER	2	/* packet header */
205#if 0
206#define	MT_SOCKET	3	/* socket structure */
207#define	MT_PCB		4	/* protocol control block */
208#define	MT_RTABLE	5	/* routing tables */
209#define	MT_HTABLE	6	/* IMP host tables */
210#define	MT_ATABLE	7	/* address resolution tables */
211#endif
212#define	MT_SONAME	8	/* socket name */
213#if 0
214#define	MT_SOOPTS	10	/* socket options */
215#endif
216#define	MT_FTABLE	11	/* fragment reassembly header */
217#if 0
218#define	MT_RIGHTS	12	/* access rights */
219#define	MT_IFADDR	13	/* interface address */
220#endif
221#define	MT_CONTROL	14	/* extra-data protocol message */
222#define	MT_OOBDATA	15	/* expedited data  */
223#define	MT_NTYPES	16	/* number of mbuf types for mbtypes[] */
224
225/*
226 * Mbuf and cluster allocation statistics PCPU structure.
227 */
228struct mbpstat {
229	u_long	mb_mbfree;
230	u_long	mb_mbbucks;
231	u_long	mb_clfree;
232	u_long	mb_clbucks;
233	long	mb_mbtypes[MT_NTYPES];
234	short	mb_active;
235};
236
237/*
238 * General mbuf allocator statistics structure.
239 * XXX: Modifications of these are not protected by any mutex locks nor by
240 * any atomic() manipulations.  As a result, we may occasionally lose
241 * a count or two.  Luckily, not all of these fields are modified at all
242 * and remain static, and those that are manipulated are only manipulated
243 * in failure situations, which do not occur (hopefully) very often.
244 */
245struct mbstat {
246	u_long	m_drops;	/* times failed to allocate */
247	u_long	m_wait;		/* times succesfully returned from wait */
248	u_long	m_drain;	/* times drained protocols for space */
249	u_long	m_mcfail;	/* XXX: times m_copym failed */
250	u_long	m_mpfail;	/* XXX: times m_pullup failed */
251	u_long	m_msize;	/* length of an mbuf */
252	u_long	m_mclbytes;	/* length of an mbuf cluster */
253	u_long	m_minclsize;	/* min length of data to allocate a cluster */
254	u_long	m_mlen;		/* length of data in an mbuf */
255	u_long	m_mhlen;	/* length of data in a header mbuf */
256	u_int	m_mbperbuck;	/* number of mbufs per "bucket" */
257	u_int	m_clperbuck;	/* number of clusters per "bucket" */
258	/* Number of mbtypes (gives # elems in mbpstat's mb_mbtypes[] array: */
259	short	m_numtypes;
260	/* XXX: Sendfile stats should eventually move to their own struct */
261	u_long	sf_iocnt;	/* times sendfile had to do disk I/O */
262	u_long	sf_allocfail;	/* times sfbuf allocation failed */
263	u_long	sf_allocwait;	/* times sfbuf allocation had to wait */
264};
265
266/*
267 * Flags specifying how an allocation should be made.
268 * M_DONTWAIT means "don't block if nothing is available" whereas
269 * M_TRYWAIT means "block for mbuf_wait ticks at most if nothing is
270 * available."
271 */
272#define	M_DONTWAIT	0x4		/* don't conflict with M_NOWAIT */
273#define	M_TRYWAIT	0x8		/* or M_WAITOK */
274#define	M_WAIT		M_TRYWAIT	/* XXX: deprecated */
275#define	MBTOM(how)	((how) & M_TRYWAIT ? M_WAITOK : M_NOWAIT)
276
277#ifdef _KERNEL
278/*-
279 * mbuf external reference count management macros.
280 *
281 * MEXT_IS_REF(m): true if (m) is not the only mbuf referencing
282 *     the external buffer ext_buf.
283 *
284 * MEXT_REM_REF(m): remove reference to m_ext object.
285 *
286 * MEXT_ADD_REF(m): add reference to m_ext object already
287 *     referred to by (m).
288 */
289#define	MEXT_IS_REF(m)	(*((m)->m_ext.ref_cnt) > 1)
290
291#define	MEXT_REM_REF(m) do {						\
292	KASSERT(*((m)->m_ext.ref_cnt) > 0, ("m_ext refcnt < 0"));	\
293	atomic_subtract_int((m)->m_ext.ref_cnt, 1);			\
294} while(0)
295
296#define	MEXT_ADD_REF(m)	atomic_add_int((m)->m_ext.ref_cnt, 1)
297
298/*
299 * mbuf, cluster, and external object allocation macros
300 * (for compatibility purposes).
301 */
302/* NB: M_COPY_PKTHDR is deprecated.  Use M_MOVE_PKTHDR or m_dup_pktdr. */
303#define	M_MOVE_PKTHDR(to, from)	m_move_pkthdr((to), (from))
304#define	m_getclr(how, type)	m_get_clrd((how), (type))
305#define	MGET(m, how, type)	((m) = m_get((how), (type)))
306#define	MGETHDR(m, how, type)	((m) = m_gethdr((how), (type)))
307#define	MCLGET(m, how)		m_clget((m), (how))
308#define	MEXTADD(m, buf, size, free, args, flags, type) 			\
309    m_extadd((m), (caddr_t)(buf), (size), (free), (args), (flags), (type))
310
311/*
312 * MEXTFREE(m): disassociate (and possibly free) an external object from (m).
313 *
314 * If the atomic_cmpset_int() returns 0, then we effectively do nothing
315 * in terms of "cleaning up" (freeing the ext buf and ref. counter) as
316 * this means that either there are still references, or another thread
317 * is taking care of the clean-up.
318 */
319#define	MEXTFREE(m) do {						\
320	struct mbuf *_mb = (m);						\
321									\
322	MEXT_REM_REF(_mb);						\
323	if (atomic_cmpset_int(_mb->m_ext.ref_cnt, 0, 1))		\
324		_mext_free(_mb);					\
325	_mb->m_flags &= ~M_EXT;						\
326} while (0)
327
328/*
329 * Evaluate TRUE if it's safe to write to the mbuf m's data region (this
330 * can be both the local data payload, or an external buffer area,
331 * depending on whether M_EXT is set).
332 */
333#define	M_WRITABLE(m)	(!((m)->m_flags & M_RDONLY) && (!((m)->m_flags  \
334			    & M_EXT) || !MEXT_IS_REF(m)))
335
336/*
337 * Check if the supplied mbuf has a packet header, or else panic.
338 */
339#define M_ASSERTPKTHDR(m)				\
340	KASSERT(m != NULL && m->m_flags & M_PKTHDR,	\
341		("%s: no mbuf packet header!", __func__))
342
343/*
344 * Ensure that the supplied mbuf is a valid, non-free mbuf.
345 */
346#define M_ASSERTVALID(m)					\
347	KASSERT((((struct mbuf *)m)->m_flags & M_FREELIST) == 0,			\
348		("%s: attempted use of a free mbuf!", __func__))
349
350/*
351 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place
352 * an object of the specified size at the end of the mbuf, longword aligned.
353 */
354#define	M_ALIGN(m, len) do {						\
355	(m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1);		\
356} while (0)
357
358/*
359 * As above, for mbufs allocated with m_gethdr/MGETHDR
360 * or initialized by M_COPY_PKTHDR.
361 */
362#define	MH_ALIGN(m, len) do {						\
363	(m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1);		\
364} while (0)
365
366/*
367 * Compute the amount of space available
368 * before the current start of data in an mbuf.
369 *
370 * The M_WRITABLE() is a temporary, conservative safety measure: the burden
371 * of checking writability of the mbuf data area rests solely with the caller.
372 */
373#define	M_LEADINGSPACE(m)						\
374	((m)->m_flags & M_EXT ?						\
375	    (M_WRITABLE(m) ? (m)->m_data - (m)->m_ext.ext_buf : 0):	\
376	    (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat :	\
377	    (m)->m_data - (m)->m_dat)
378
379/*
380 * Compute the amount of space available
381 * after the end of data in an mbuf.
382 *
383 * The M_WRITABLE() is a temporary, conservative safety measure: the burden
384 * of checking writability of the mbuf data area rests solely with the caller.
385 */
386#define	M_TRAILINGSPACE(m)						\
387	((m)->m_flags & M_EXT ?						\
388	    (M_WRITABLE(m) ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size	\
389		- ((m)->m_data + (m)->m_len) : 0) :			\
390	    &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len))
391
392/*
393 * Arrange to prepend space of size plen to mbuf m.
394 * If a new mbuf must be allocated, how specifies whether to wait.
395 * If the allocation fails, the original mbuf chain is freed and m is
396 * set to NULL.
397 */
398#define	M_PREPEND(m, plen, how) do {					\
399	struct mbuf **_mmp = &(m);					\
400	struct mbuf *_mm = *_mmp;					\
401	int _mplen = (plen);						\
402	int __mhow = (how);						\
403									\
404	if (M_LEADINGSPACE(_mm) >= _mplen) {				\
405		_mm->m_data -= _mplen;					\
406		_mm->m_len += _mplen;					\
407	} else								\
408		_mm = m_prepend(_mm, _mplen, __mhow);			\
409	if (_mm != NULL && _mm->m_flags & M_PKTHDR)			\
410		_mm->m_pkthdr.len += _mplen;				\
411	*_mmp = _mm;							\
412} while (0)
413
414/*
415 * Change mbuf to new type.
416 * This is a relatively expensive operation and should be avoided.
417 */
418#define	MCHTYPE(m, t)	m_chtype((m), (t))
419
420/* Length to m_copy to copy all. */
421#define	M_COPYALL	1000000000
422
423/* Compatibility with 4.3. */
424#define	m_copy(m, o, l)	m_copym((m), (o), (l), M_DONTWAIT)
425
426extern	int max_datalen;		/* MHLEN - max_hdr */
427extern	int max_hdr;			/* Largest link + protocol header */
428extern	int max_linkhdr;		/* Largest link-level header */
429extern	int max_protohdr;		/* Largest protocol header */
430extern	struct mbstat mbstat;		/* General mbuf stats/infos */
431extern	int nmbclusters;		/* Maximum number of clusters */
432extern	int nmbcnt;			/* Scale kmem_map for counter space */
433extern	int nmbufs;			/* Maximum number of mbufs */
434
435struct	uio;
436
437void		 _mext_free(struct mbuf *);
438void		 m_adj(struct mbuf *, int);
439int		 m_apply(struct mbuf *, int, int,
440		 int (*)(void *, void *, unsigned int), void *);
441void		 m_cat(struct mbuf *, struct mbuf *);
442void		 m_chtype(struct mbuf *, short);
443void		 m_clget(struct mbuf *, int);
444void		 m_extadd(struct mbuf *, caddr_t, u_int,
445		    void (*)(void *, void *), void *, int, int);
446void		 m_copyback(struct mbuf *, int, int, caddr_t);
447void		 m_copydata(const struct mbuf *, int, int, caddr_t);
448struct	mbuf	*m_copym(struct mbuf *, int, int, int);
449struct	mbuf	*m_copypacket(struct mbuf *, int);
450void		 m_copy_pkthdr(struct mbuf *, struct mbuf *);
451struct	mbuf	*m_defrag(struct mbuf *, int);
452struct	mbuf	*m_devget(char *, int, int, struct ifnet *,
453		    void (*)(char *, caddr_t, u_int));
454struct	mbuf	*m_dup(struct mbuf *, int);
455int		 m_dup_pkthdr(struct mbuf *, struct mbuf *, int);
456u_int		 m_fixhdr(struct mbuf *);
457struct	mbuf	*m_fragment(struct mbuf *, int, int);
458struct	mbuf	*m_free(struct mbuf *);
459void		 m_freem(struct mbuf *);
460struct	mbuf	*m_get(int, short);
461struct	mbuf	*m_get_clrd(int, short);
462struct	mbuf	*m_getcl(int, short, int);
463struct	mbuf	*m_gethdr(int, short);
464struct	mbuf	*m_gethdr_clrd(int, short);
465struct	mbuf	*m_getm(struct mbuf *, int, int, short);
466struct	mbuf	*m_getptr(struct mbuf *, int, int *);
467u_int		 m_length(struct mbuf *, struct mbuf **);
468void		 m_move_pkthdr(struct mbuf *, struct mbuf *);
469struct	mbuf	*m_prepend(struct mbuf *, int, int);
470void		 m_print(const struct mbuf *);
471struct	mbuf	*m_pulldown(struct mbuf *, int, int, int *);
472struct	mbuf	*m_pullup(struct mbuf *, int);
473struct	mbuf	*m_split(struct mbuf *, int, int);
474struct	mbuf *
475	m_uiotombuf(struct uio *uio, int how, int len);
476
477/*
478 * Packets may have annotations attached by affixing a list
479 * of "packet tags" to the pkthdr structure.  Packet tags are
480 * dynamically allocated semi-opaque data structures that have
481 * a fixed header (struct m_tag) that specifies the size of the
482 * memory block and a <cookie,type> pair that identifies it.
483 * The cookie is a 32-bit unique unsigned value used to identify
484 * a module or ABI.  By convention this value is chose as the
485 * date+time that the module is created, expressed as the number of
486 * seconds since the epoch (e.g., using date -u +'%s').  The type value
487 * is an ABI/module-specific value that identifies a particular annotation
488 * and is private to the module.  For compatibility with systems
489 * like OpenBSD that define packet tags w/o an ABI/module cookie,
490 * the value PACKET_ABI_COMPAT is used to implement m_tag_get and
491 * m_tag_find compatibility shim functions and several tag types are
492 * defined below.  Users that do not require compatibility should use
493 * a private cookie value so that packet tag-related definitions
494 * can be maintained privately.
495 *
496 * Note that the packet tag returned by m_tag_allocate has the default
497 * memory alignment implemented by malloc.  To reference private data
498 * one can use a construct like:
499 *
500 *	struct m_tag *mtag = m_tag_allocate(...);
501 *	struct foo *p = (struct foo *)(mtag+1);
502 *
503 * if the alignment of struct m_tag is sufficient for referencing members
504 * of struct foo.  Otherwise it is necessary to embed struct m_tag within
505 * the private data structure to insure proper alignment; e.g.,
506 *
507 *	struct foo {
508 *		struct m_tag	tag;
509 *		...
510 *	};
511 *	struct foo *p = (struct foo *) m_tag_allocate(...);
512 *	struct m_tag *mtag = &p->tag;
513 */
514
515/*
516 * Persistent tags stay with an mbuf until the mbuf is reclaimed.
517 * Otherwise tags are expected to ``vanish'' when they pass through
518 * a network interface.  For most interfaces this happens normally
519 * as the tags are reclaimed when the mbuf is free'd.  However in
520 * some special cases reclaiming must be done manually.  An example
521 * is packets that pass through the loopback interface.  Also, one
522 * must be careful to do this when ``turning around'' packets (e.g.,
523 * icmp_reflect).
524 *
525 * To mark a tag persistent bit-or this flag in when defining the
526 * tag id.  The tag will then be treated as described above.
527 */
528#define	MTAG_PERSISTENT				0x800
529
530#define	PACKET_TAG_NONE				0  /* Nadda */
531
532/* Packet tag for use with PACKET_ABI_COMPAT. */
533#define	PACKET_TAG_IPSEC_IN_DONE		1  /* IPsec applied, in */
534#define	PACKET_TAG_IPSEC_OUT_DONE		2  /* IPsec applied, out */
535#define	PACKET_TAG_IPSEC_IN_CRYPTO_DONE		3  /* NIC IPsec crypto done */
536#define	PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED	4  /* NIC IPsec crypto req'ed */
537#define	PACKET_TAG_IPSEC_IN_COULD_DO_CRYPTO	5  /* NIC notifies IPsec */
538#define	PACKET_TAG_IPSEC_PENDING_TDB		6  /* Reminder to do IPsec */
539#define	PACKET_TAG_BRIDGE			7  /* Bridge processing done */
540#define	PACKET_TAG_GIF				8  /* GIF processing done */
541#define	PACKET_TAG_GRE				9  /* GRE processing done */
542#define	PACKET_TAG_IN_PACKET_CHECKSUM		10 /* NIC checksumming done */
543#define	PACKET_TAG_ENCAP			11 /* Encap.  processing */
544#define	PACKET_TAG_IPSEC_SOCKET			12 /* IPSEC socket ref */
545#define	PACKET_TAG_IPSEC_HISTORY		13 /* IPSEC history */
546#define	PACKET_TAG_IPV6_INPUT			14 /* IPV6 input processing */
547#define	PACKET_TAG_DUMMYNET			15 /* dummynet info */
548#define	PACKET_TAG_DIVERT			17 /* divert info */
549#define	PACKET_TAG_IPFORWARD			18 /* ipforward info */
550#define	PACKET_TAG_MACLABEL	(19 | MTAG_PERSISTENT) /* MAC label */
551#define	PACKET_TAG_PF_GENERATED	(20 | MTAG_PERSISTENT) /* PF, pass always */
552#define	PACKET_TAG_PF_ROUTED			21 /* PF routed, avoid loops */
553#define	PACKET_TAG_PF_FRAGCACHE			22 /* PF fragment cached */
554#define	PACKET_TAG_PF_QID			23 /* PF ALTQ queue id */
555#define	PACKET_TAG_PF_TAG			24 /* PF tagged */
556
557/* Packet tag routines. */
558struct	m_tag 	*m_tag_alloc(u_int32_t, int, int, int);
559void		 m_tag_delete(struct mbuf *, struct m_tag *);
560void		 m_tag_delete_chain(struct mbuf *, struct m_tag *);
561struct	m_tag	*m_tag_locate(struct mbuf *, u_int32_t, int, struct m_tag *);
562struct	m_tag	*m_tag_copy(struct m_tag *, int);
563int		 m_tag_copy_chain(struct mbuf *, struct mbuf *, int);
564void		 m_tag_delete_nonpersistent(struct mbuf *);
565
566/*
567 * Initialize the list of tags associated with an mbuf.
568 */
569static __inline void
570m_tag_init(struct mbuf *m)
571{
572	SLIST_INIT(&m->m_pkthdr.tags);
573}
574
575/*
576 * Setup the contents of a tag.  Note that this does not
577 * fillin the free method; the caller is expected to do that.
578 *
579 * XXX probably should be called m_tag_init; but that was
580 * already taken.
581 */
582static __inline void
583m_tag_setup(struct m_tag *t, u_int32_t cookie, int type, int len)
584{
585	t->m_tag_id = type;
586	t->m_tag_len = len;
587	t->m_tag_cookie = cookie;
588}
589
590/*
591 * Reclaim resources associated with a tag.
592 */
593static __inline void
594m_tag_free(struct m_tag *t)
595{
596	(*t->m_tag_free)(t);
597}
598
599/*
600 * Return the first tag associated with an mbuf.
601 */
602static __inline struct m_tag *
603m_tag_first(struct mbuf *m)
604{
605	return SLIST_FIRST(&m->m_pkthdr.tags);
606}
607
608/*
609 * Return the next tag in the list of tags associated with an mbuf.
610 */
611static __inline struct m_tag *
612m_tag_next(struct mbuf *m, struct m_tag *t)
613{
614	return SLIST_NEXT(t, m_tag_link);
615}
616
617/*
618 * Prepend a tag to the list of tags associated with an mbuf.
619 */
620static __inline void
621m_tag_prepend(struct mbuf *m, struct m_tag *t)
622{
623	SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link);
624}
625
626/*
627 * Unlink a tag from the list of tags associated with an mbuf.
628 */
629static __inline void
630m_tag_unlink(struct mbuf *m, struct m_tag *t)
631{
632	SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link);
633}
634
635/* These are for OpenBSD compatibility. */
636#define	MTAG_ABI_COMPAT		0		/* compatibility ABI */
637
638static __inline struct m_tag *
639m_tag_get(int type, int length, int wait)
640{
641	return m_tag_alloc(MTAG_ABI_COMPAT, type, length, wait);
642}
643
644static __inline struct m_tag *
645m_tag_find(struct mbuf *m, int type, struct m_tag *start)
646{
647	return SLIST_EMPTY(&m->m_pkthdr.tags) ?
648		NULL : m_tag_locate(m, MTAG_ABI_COMPAT, type, start);
649}
650#endif /* _KERNEL */
651
652#endif /* !_SYS_MBUF_H_ */
653