xref: /openbsd/sys/sys/mbuf.h (revision 7019ae97)
1 /*	$OpenBSD: mbuf.h,v 1.263 2024/04/14 20:46:27 bluhm Exp $	*/
2 /*	$NetBSD: mbuf.h,v 1.19 1996/02/09 18:25:14 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1988, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)mbuf.h	8.5 (Berkeley) 2/19/95
33  */
34 
35 #ifndef _SYS_MBUF_H_
36 #define _SYS_MBUF_H_
37 
38 #include <sys/queue.h>
39 
40 /*
41  * Constants related to network buffer management.
42  * MCLBYTES must be no larger than PAGE_SIZE (the software page size) and,
43  * on machines that exchange pages of input or output buffers with mbuf
44  * clusters (MAPPED_MBUFS), MCLBYTES must also be an integral multiple
45  * of the hardware page size.
46  */
47 #define	MSIZE		256		/* size of an mbuf */
48 
49 /*
50  * Mbufs are of a single size, MSIZE, which includes overhead.  An mbuf may
51  * add a single "mbuf cluster" of size MCLBYTES, which has no additional
52  * overhead and is used instead of the internal data area; this is done when
53  * at least MINCLSIZE of data must be stored.
54  */
55 
56 #define	MLEN		(MSIZE - sizeof(struct m_hdr))	/* normal data len */
57 #define	MHLEN		(MLEN - sizeof(struct pkthdr))	/* data len w/pkthdr */
58 
59 #define	MAXMCLBYTES	(64 * 1024)		/* largest cluster from the stack */
60 #define	MINCLSIZE	(MHLEN + MLEN + 1)	/* smallest amount to put in cluster */
61 #define	M_MAXCOMPRESS	(MHLEN / 2)		/* max amount to copy for compression */
62 
63 #define	MCLSHIFT	11		/* convert bytes to m_buf clusters */
64 					/* 2K cluster can hold Ether frame */
65 #define	MCLBYTES	(1 << MCLSHIFT)	/* size of a m_buf cluster */
66 #define	MCLOFSET	(MCLBYTES - 1)
67 
68 /* Packet tags structure */
69 struct m_tag {
70 	SLIST_ENTRY(m_tag)	m_tag_link;	/* List of packet tags */
71 	u_int16_t		m_tag_id;	/* Tag ID */
72 	u_int16_t		m_tag_len;	/* Length of data */
73 };
74 
75 /*
76  * Macros for type conversion
77  * mtod(m,t) -	convert mbuf pointer to data pointer of correct type
78  */
79 #define	mtod(m,t)	((t)((m)->m_data))
80 
81 /* header at beginning of each mbuf: */
82 struct m_hdr {
83 	struct	mbuf *mh_next;		/* next buffer in chain */
84 	struct	mbuf *mh_nextpkt;	/* next chain in queue/record */
85 	caddr_t	mh_data;		/* location of data */
86 	u_int	mh_len;			/* amount of data in this mbuf */
87 	short	mh_type;		/* type of data in this mbuf */
88 	u_short	mh_flags;		/* flags; see below */
89 #ifndef __LP64__
90 	u_int	mh_pad;			/* pad to 8-byte boundary */
91 #endif
92 };
93 
94 /* pf stuff */
95 struct pf_state_key;
96 struct inpcb;
97 
98 struct pkthdr_pf {
99 	struct pf_state_key *statekey;	/* pf stackside statekey */
100 	struct inpcb	*inp;		/* connected pcb for outgoing packet */
101 	u_int32_t	 qid;		/* queue id */
102 	u_int16_t	 tag;		/* tag id */
103 	u_int16_t	 delay;		/* delay packet by X ms */
104 	u_int8_t	 flags;
105 	u_int8_t	 routed;
106 	u_int8_t	 prio;
107 	u_int8_t	 pad[1];
108 };
109 
110 /* pkthdr_pf.flags */
111 #define	PF_TAG_GENERATED		0x01
112 #define	PF_TAG_SYNCOOKIE_RECREATED	0x02
113 #define	PF_TAG_TRANSLATE_LOCALHOST	0x04
114 #define	PF_TAG_DIVERTED			0x08
115 #define	PF_TAG_DIVERTED_PACKET		0x10
116 #define	PF_TAG_REROUTE			0x20
117 #define	PF_TAG_REFRAGMENTED		0x40	/* refragmented ipv6 packet */
118 #define	PF_TAG_PROCESSED		0x80	/* packet was checked by pf */
119 
120 #ifdef _KERNEL
121 #define MPF_BITS \
122     ("\20\1GENERATED\2SYNCOOKIE_RECREATED\3TRANSLATE_LOCALHOST\4DIVERTED" \
123     "\5DIVERTED_PACKET\6REROUTE\7REFRAGMENTED\10PROCESSED")
124 #endif
125 
126 /* record/packet header in first mbuf of chain; valid if M_PKTHDR set */
127 struct	pkthdr {
128 	void			*ph_cookie;	/* additional data */
129 	SLIST_HEAD(, m_tag)	 ph_tags;	/* list of packet tags */
130 	int64_t			 ph_timestamp;	/* packet timestamp */
131 	int			 len;		/* total packet length */
132 	u_int			 ph_rtableid;	/* routing table id */
133 	u_int			 ph_ifidx;	/* rcv interface index */
134 	u_int16_t		 ph_tagsset;	/* mtags attached */
135 	u_int16_t		 ph_flowid;	/* pseudo unique flow id */
136 	u_int16_t		 csum_flags;	/* checksum flags */
137 	u_int16_t		 ether_vtag;	/* Ethernet 802.1p+Q vlan tag */
138 	u_int16_t		 ph_mss;	/* TCP max segment size */
139 	u_int8_t		 ph_loopcnt;	/* mbuf is looping in kernel */
140 	u_int8_t		 ph_family;	/* af, used when queueing */
141 	struct pkthdr_pf	 pf;
142 };
143 
144 /* description of external storage mapped into mbuf, valid if M_EXT set */
145 struct mbuf_ext {
146 	caddr_t	ext_buf;		/* start of buffer */
147 	void	*ext_arg;
148 	u_int	ext_free_fn;		/* index of free function */
149 	u_int	ext_size;		/* size of buffer, for ext_free_fn */
150 	struct mbuf *ext_nextref;
151 	struct mbuf *ext_prevref;
152 #ifdef DEBUG
153 	const char *ext_ofile;
154 	const char *ext_nfile;
155 	int ext_oline;
156 	int ext_nline;
157 #endif
158 };
159 
160 struct mbuf {
161 	struct	m_hdr m_hdr;
162 	union {
163 		struct {
164 			struct	pkthdr MH_pkthdr;	/* M_PKTHDR set */
165 			union {
166 				struct	mbuf_ext MH_ext; /* M_EXT set */
167 				char	MH_databuf[MHLEN];
168 			} MH_dat;
169 		} MH;
170 		char	M_databuf[MLEN];		/* !M_PKTHDR, !M_EXT */
171 	} M_dat;
172 };
173 #define	m_next		m_hdr.mh_next
174 #define	m_len		m_hdr.mh_len
175 #define	m_data		m_hdr.mh_data
176 #define	m_type		m_hdr.mh_type
177 #define	m_flags		m_hdr.mh_flags
178 #define	m_nextpkt	m_hdr.mh_nextpkt
179 #define	m_pkthdr	M_dat.MH.MH_pkthdr
180 #define	m_ext		M_dat.MH.MH_dat.MH_ext
181 #define	m_pktdat	M_dat.MH.MH_dat.MH_databuf
182 #define	m_dat		M_dat.M_databuf
183 
184 /* mbuf flags */
185 #define	M_EXT		0x0001	/* has associated external storage */
186 #define	M_PKTHDR	0x0002	/* start of record */
187 #define	M_EOR		0x0004	/* end of record */
188 #define M_EXTWR		0x0008	/* external storage is writable */
189 #define	M_PROTO1	0x0010	/* protocol-specific */
190 
191 /* mbuf pkthdr flags, also in m_flags */
192 #define M_VLANTAG	0x0020	/* ether_vtag is valid */
193 #define M_LOOP		0x0040	/* packet has been sent from local machine */
194 #define M_BCAST		0x0100	/* sent/received as link-level broadcast */
195 #define M_MCAST		0x0200	/* sent/received as link-level multicast */
196 #define M_CONF		0x0400  /* payload was encrypted (ESP-transport) */
197 #define M_AUTH		0x0800  /* payload was authenticated (AH or ESP auth) */
198 #define M_TUNNEL	0x1000  /* IP-in-IP added by tunnel mode IPsec */
199 #define M_ZEROIZE	0x2000  /* Zeroize data part on free */
200 #define M_COMP		0x4000  /* header was decompressed */
201 #define M_LINK0		0x8000	/* link layer specific flag */
202 
203 #ifdef _KERNEL
204 #define M_BITS \
205     ("\20\1M_EXT\2M_PKTHDR\3M_EOR\4M_EXTWR\5M_PROTO1\6M_VLANTAG\7M_LOOP" \
206     "\11M_BCAST\12M_MCAST\13M_CONF\14M_AUTH\15M_TUNNEL" \
207     "\16M_ZEROIZE\17M_COMP\20M_LINK0")
208 #endif
209 
210 /* flags copied when copying m_pkthdr */
211 #define	M_COPYFLAGS	(M_PKTHDR|M_EOR|M_PROTO1|M_BCAST|M_MCAST|M_CONF|M_COMP|\
212 			 M_AUTH|M_LOOP|M_TUNNEL|M_LINK0|M_VLANTAG|M_ZEROIZE)
213 
214 /* Checksumming flags */
215 #define	M_IPV4_CSUM_OUT		0x0001	/* IPv4 checksum needed */
216 #define	M_TCP_CSUM_OUT		0x0002	/* TCP checksum needed */
217 #define	M_UDP_CSUM_OUT		0x0004	/* UDP checksum needed */
218 #define	M_IPV4_CSUM_IN_OK	0x0008	/* IPv4 checksum verified */
219 #define	M_IPV4_CSUM_IN_BAD	0x0010	/* IPv4 checksum bad */
220 #define	M_TCP_CSUM_IN_OK	0x0020	/* TCP checksum verified */
221 #define	M_TCP_CSUM_IN_BAD	0x0040	/* TCP checksum bad */
222 #define	M_UDP_CSUM_IN_OK	0x0080	/* UDP checksum verified */
223 #define	M_UDP_CSUM_IN_BAD	0x0100	/* UDP checksum bad */
224 #define	M_ICMP_CSUM_OUT		0x0200	/* ICMP/ICMPv6 checksum needed */
225 #define	M_ICMP_CSUM_IN_OK	0x0400	/* ICMP/ICMPv6 checksum verified */
226 #define	M_ICMP_CSUM_IN_BAD	0x0800	/* ICMP/ICMPv6 checksum bad */
227 #define	M_IPV6_DF_OUT		0x1000	/* don't fragment outgoing IPv6 */
228 #define	M_TIMESTAMP		0x2000	/* ph_timestamp is set */
229 #define	M_FLOWID		0x4000	/* ph_flowid is set */
230 #define	M_TCP_TSO		0x8000	/* TCP Segmentation Offload needed */
231 
232 #ifdef _KERNEL
233 #define MCS_BITS \
234     ("\20\1IPV4_CSUM_OUT\2TCP_CSUM_OUT\3UDP_CSUM_OUT\4IPV4_CSUM_IN_OK" \
235     "\5IPV4_CSUM_IN_BAD\6TCP_CSUM_IN_OK\7TCP_CSUM_IN_BAD\10UDP_CSUM_IN_OK" \
236     "\11UDP_CSUM_IN_BAD\12ICMP_CSUM_OUT\13ICMP_CSUM_IN_OK\14ICMP_CSUM_IN_BAD" \
237     "\15IPV6_NODF_OUT" "\16TIMESTAMP" "\17FLOWID" "\20TCP_TSO")
238 #endif
239 
240 /* mbuf types */
241 #define	MT_FREE		0	/* should be on free list */
242 #define	MT_DATA		1	/* dynamic (data) allocation */
243 #define	MT_HEADER	2	/* packet header */
244 #define	MT_SONAME	3	/* socket name */
245 #define	MT_SOOPTS	4	/* socket options */
246 #define	MT_FTABLE	5	/* fragment reassembly header */
247 #define	MT_CONTROL	6	/* extra-data protocol message */
248 #define	MT_OOBDATA	7	/* expedited data  */
249 #define	MT_NTYPES	8
250 
251 /* flags to m_get/MGET */
252 #include <sys/malloc.h>
253 #define	M_DONTWAIT	M_NOWAIT
254 #define	M_WAIT		M_WAITOK
255 
256 /*
257  * mbuf allocation/deallocation macros:
258  *
259  *	MGET(struct mbuf *m, int how, int type)
260  * allocates an mbuf and initializes it to contain internal data.
261  *
262  *	MGETHDR(struct mbuf *m, int how, int type)
263  * allocates an mbuf and initializes it to contain a packet header
264  * and internal data.
265  */
266 #define MGET(m, how, type) m = m_get((how), (type))
267 
268 #define MGETHDR(m, how, type) m = m_gethdr((how), (type))
269 
270 /*
271  * Macros for tracking external storage associated with an mbuf.
272  */
273 #ifdef DEBUG
274 #define MCLREFDEBUGN(m, file, line) do {				\
275 		(m)->m_ext.ext_nfile = (file);				\
276 		(m)->m_ext.ext_nline = (line);				\
277 	} while (/* CONSTCOND */ 0)
278 #define MCLREFDEBUGO(m, file, line) do {				\
279 		(m)->m_ext.ext_ofile = (file);				\
280 		(m)->m_ext.ext_oline = (line);				\
281 	} while (/* CONSTCOND */ 0)
282 #else
283 #define MCLREFDEBUGN(m, file, line)
284 #define MCLREFDEBUGO(m, file, line)
285 #endif
286 
287 #define	MCLISREFERENCED(m)	((m)->m_ext.ext_nextref != (m))
288 
289 #define	MCLADDREFERENCE(o, n)	m_extref((o), (n))
290 
291 #define	MCLINITREFERENCE(m)	do {					\
292 		(m)->m_ext.ext_prevref = (m);				\
293 		(m)->m_ext.ext_nextref = (m);				\
294 		MCLREFDEBUGO((m), __FILE__, __LINE__);			\
295 		MCLREFDEBUGN((m), NULL, 0);				\
296 	} while (/* CONSTCOND */ 0)
297 
298 /*
299  * Macros for mbuf external storage.
300  *
301  * MEXTADD adds pre-allocated external storage to
302  * a normal mbuf; the flag M_EXT is set.
303  *
304  * MCLGET allocates and adds an mbuf cluster to a normal mbuf;
305  * the flag M_EXT is set upon success.
306  */
307 #define	MEXTADD(m, buf, size, mflags, freefn, arg) do {			\
308 	(m)->m_data = (m)->m_ext.ext_buf = (caddr_t)(buf);		\
309 	(m)->m_flags |= M_EXT | (mflags & M_EXTWR);			\
310 	(m)->m_ext.ext_size = (size);					\
311 	(m)->m_ext.ext_free_fn = (freefn);					\
312 	(m)->m_ext.ext_arg = (arg);					\
313 	MCLINITREFERENCE(m);						\
314 } while (/* CONSTCOND */ 0)
315 
316 #define MCLGET(m, how) (void) m_clget((m), (how), MCLBYTES)
317 #define MCLGETL(m, how, l) m_clget((m), (how), (l))
318 
319 u_int mextfree_register(void (*)(caddr_t, u_int, void *));
320 #define	MEXTFREE_POOL 0
321 
322 /*
323  * Move just m_pkthdr from from to to,
324  * remove M_PKTHDR and clean flags/tags for from.
325  */
326 #define M_MOVE_HDR(to, from) do {					\
327 	(to)->m_pkthdr = (from)->m_pkthdr;				\
328 	(from)->m_flags &= ~M_PKTHDR;					\
329 	SLIST_INIT(&(from)->m_pkthdr.ph_tags);				\
330 	(from)->m_pkthdr.pf.statekey = NULL;				\
331 } while (/* CONSTCOND */ 0)
332 
333 /*
334  * MOVE mbuf pkthdr from from to to.
335  * from must have M_PKTHDR set, and to must be empty.
336  */
337 #define	M_MOVE_PKTHDR(to, from) do {					\
338 	(to)->m_flags = ((to)->m_flags & (M_EXT | M_EXTWR));		\
339 	(to)->m_flags |= (from)->m_flags & M_COPYFLAGS;			\
340 	M_MOVE_HDR((to), (from));					\
341 	if (((to)->m_flags & M_EXT) == 0)				\
342 		(to)->m_data = (to)->m_pktdat;				\
343 } while (/* CONSTCOND */ 0)
344 
345 /*
346  * Determine if an mbuf's data area is read-only. This is true for
347  * non-cluster external storage and for clusters that are being
348  * referenced by more than one mbuf.
349  */
350 #define	M_READONLY(m)							\
351 	(((m)->m_flags & M_EXT) != 0 &&					\
352 	  (((m)->m_flags & M_EXTWR) == 0 || MCLISREFERENCED(m)))
353 
354 /*
355  * Arrange to prepend space of size plen to mbuf m.
356  * If a new mbuf must be allocated, how specifies whether to wait.
357  * If how is M_DONTWAIT and allocation fails, the original mbuf chain
358  * is freed and m is set to NULL.
359  */
360 #define	M_PREPEND(m, plen, how) \
361 		(m) = m_prepend((m), (plen), (how))
362 
363 /* length to m_copy to copy all */
364 #define	M_COPYALL	1000000000
365 
366 #define MBSTAT_TYPES           MT_NTYPES
367 #define MBSTAT_DROPS           (MBSTAT_TYPES + 0)
368 #define MBSTAT_WAIT            (MBSTAT_TYPES + 1)
369 #define MBSTAT_DRAIN           (MBSTAT_TYPES + 2)
370 #define MBSTAT_COUNT           (MBSTAT_TYPES + 3)
371 
372 /*
373  * Mbuf statistics.
374  * For statistics related to mbuf and cluster allocations, see also the
375  * pool headers (mbpool and mclpool).
376  */
377 struct mbstat {
378 	u_long	m_drops;	/* times failed to find space */
379 	u_long	m_wait;		/* times waited for space */
380 	u_long	m_drain;	/* times drained protocols for space */
381 	u_long	m_mtypes[MBSTAT_COUNT];
382 				/* type specific mbuf allocations */
383 };
384 
385 #include <sys/mutex.h>
386 
387 struct mbuf_list {
388 	struct mbuf		*ml_head;
389 	struct mbuf		*ml_tail;
390 	u_int			ml_len;
391 };
392 
393 struct mbuf_queue {
394 	struct mutex		mq_mtx;
395 	struct mbuf_list	mq_list;
396 	u_int			mq_maxlen;
397 	u_int			mq_drops;
398 };
399 
400 #ifdef	_KERNEL
401 struct pool;
402 
403 extern	long nmbclust;			/* limit on the # of clusters */
404 extern	int max_linkhdr;		/* largest link-level header */
405 extern	int max_protohdr;		/* largest protocol header */
406 extern	int max_hdr;			/* largest link+protocol header */
407 
408 void	mbinit(void);
409 void	mbcpuinit(void);
410 int	nmbclust_update(long);
411 struct	mbuf *m_copym(struct mbuf *, int, int, int);
412 struct	mbuf *m_free(struct mbuf *);
413 struct	mbuf *m_get(int, int);
414 struct	mbuf *m_getclr(int, int);
415 struct	mbuf *m_gethdr(int, int);
416 struct	mbuf *m_inithdr(struct mbuf *);
417 void	m_removehdr(struct mbuf *);
418 void	m_resethdr(struct mbuf *);
419 void	m_calchdrlen(struct mbuf *);
420 int	m_defrag(struct mbuf *, int);
421 struct	mbuf *m_prepend(struct mbuf *, int, int);
422 struct	mbuf *m_pulldown(struct mbuf *, int, int, int *);
423 struct	mbuf *m_pullup(struct mbuf *, int);
424 struct	mbuf *m_split(struct mbuf *, int, int);
425 struct	mbuf *m_makespace(struct mbuf *, int, int, int *);
426 struct  mbuf *m_getptr(struct mbuf *, int, int *);
427 int	m_leadingspace(struct mbuf *);
428 int	m_trailingspace(struct mbuf *);
429 void	m_align(struct mbuf *, int);
430 struct mbuf *m_clget(struct mbuf *, int, u_int);
431 void	m_extref(struct mbuf *, struct mbuf *);
432 void	m_pool_init(struct pool *, u_int, u_int, const char *);
433 u_int	m_pool_used(void);
434 void	m_extfree_pool(caddr_t, u_int, void *);
435 void	m_adj(struct mbuf *, int);
436 int	m_copyback(struct mbuf *, int, int, const void *, int);
437 struct mbuf *m_freem(struct mbuf *);
438 void	m_purge(struct mbuf *);
439 void	m_copydata(struct mbuf *, int, int, void *);
440 void	m_cat(struct mbuf *, struct mbuf *);
441 struct mbuf *m_devget(char *, int, int);
442 int	m_apply(struct mbuf *, int, int,
443 	    int (*)(caddr_t, caddr_t, unsigned int), caddr_t);
444 struct mbuf *m_dup_pkt(struct mbuf *, unsigned int, int);
445 int	m_dup_pkthdr(struct mbuf *, struct mbuf *, int);
446 
447 void	m_microtime(const struct mbuf *, struct timeval *);
448 
449 static inline struct mbuf *
m_freemp(struct mbuf ** mp)450 m_freemp(struct mbuf **mp)
451 {
452 	struct mbuf *m = *mp;
453 
454 	*mp = NULL;
455 	return m_freem(m);
456 }
457 
458 /* Packet tag routines */
459 struct m_tag *m_tag_get(int, int, int);
460 void	m_tag_prepend(struct mbuf *, struct m_tag *);
461 void	m_tag_delete(struct mbuf *, struct m_tag *);
462 void	m_tag_delete_chain(struct mbuf *);
463 struct m_tag *m_tag_find(struct mbuf *, int, struct m_tag *);
464 struct m_tag *m_tag_copy(struct m_tag *, int);
465 int	m_tag_copy_chain(struct mbuf *, struct mbuf *, int);
466 void	m_tag_init(struct mbuf *);
467 struct m_tag *m_tag_first(struct mbuf *);
468 struct m_tag *m_tag_next(struct mbuf *, struct m_tag *);
469 
470 /* Packet tag types */
471 #define PACKET_TAG_IPSEC_IN_DONE	0x0001  /* IPsec applied, in */
472 #define PACKET_TAG_IPSEC_OUT_DONE	0x0002  /* IPsec applied, out */
473 #define PACKET_TAG_IPSEC_FLOWINFO	0x0004	/* IPsec flowinfo */
474 #define PACKET_TAG_IP_OFFNXT		0x0010  /* IPv4 offset and next proto */
475 #define PACKET_TAG_IP6_OFFNXT		0x0020  /* IPv6 offset and next proto */
476 #define PACKET_TAG_WIREGUARD		0x0040  /* WireGuard data */
477 #define PACKET_TAG_GRE			0x0080  /* GRE processing done */
478 #define PACKET_TAG_DLT			0x0100 /* data link layer type */
479 #define PACKET_TAG_PF_DIVERT		0x0200 /* pf(4) diverted packet */
480 #define PACKET_TAG_PF_REASSEMBLED	0x0800 /* pf reassembled ipv6 packet */
481 #define PACKET_TAG_SRCROUTE		0x1000 /* IPv4 source routing options */
482 #define PACKET_TAG_TUNNEL		0x2000	/* Tunnel endpoint address */
483 #define PACKET_TAG_CARP_BAL_IP		0x4000  /* carp(4) ip balanced marker */
484 
485 #define MTAG_BITS \
486     ("\20\1IPSEC_IN_DONE\2IPSEC_OUT_DONE\3IPSEC_FLOWINFO" \
487     "\4IPSEC_OUT_CRYPTO_NEEDED\5IPSEC_PENDING_TDB\6BRIDGE\7WG\10GRE\11DLT" \
488     "\12PF_DIVERT\14PF_REASSEMBLED\15SRCROUTE\16TUNNEL\17CARP_BAL_IP")
489 
490 /*
491  * Maximum tag payload length (that is excluding the m_tag structure).
492  * Please make sure to update this value when increasing the payload
493  * length for an existing packet tag type or when adding a new one that
494  * has payload larger than the value below.
495  */
496 #define PACKET_TAG_MAXSIZE		80
497 
498 /* Detect mbufs looping in the kernel when spliced too often. */
499 #define M_MAXLOOP	128
500 
501 /*
502  * mbuf lists
503  */
504 
505 #define MBUF_LIST_INITIALIZER() { NULL, NULL, 0 }
506 
507 void			ml_init(struct mbuf_list *);
508 void			ml_enqueue(struct mbuf_list *, struct mbuf *);
509 struct mbuf *		ml_dequeue(struct mbuf_list *);
510 void			ml_enlist(struct mbuf_list *, struct mbuf_list *);
511 struct mbuf *		ml_dechain(struct mbuf_list *);
512 unsigned int		ml_purge(struct mbuf_list *);
513 unsigned int		ml_hdatalen(struct mbuf_list *);
514 
515 #define	ml_len(_ml)		((_ml)->ml_len)
516 #define	ml_empty(_ml)		((_ml)->ml_len == 0)
517 
518 #define MBUF_LIST_FIRST(_ml)	((_ml)->ml_head)
519 #define MBUF_LIST_NEXT(_m)	((_m)->m_nextpkt)
520 
521 #define MBUF_LIST_FOREACH(_ml, _m)					\
522 	for ((_m) = MBUF_LIST_FIRST(_ml);				\
523 	    (_m) != NULL;						\
524 	    (_m) = MBUF_LIST_NEXT(_m))
525 
526 /*
527  * mbuf queues
528  */
529 
530 #include <sys/atomic.h>
531 
532 #define MBUF_QUEUE_INITIALIZER(_maxlen, _ipl) \
533     { MUTEX_INITIALIZER(_ipl), MBUF_LIST_INITIALIZER(), (_maxlen), 0 }
534 
535 void			mq_init(struct mbuf_queue *, u_int, int);
536 int			mq_push(struct mbuf_queue *, struct mbuf *);
537 int			mq_enqueue(struct mbuf_queue *, struct mbuf *);
538 struct mbuf *		mq_dequeue(struct mbuf_queue *);
539 int			mq_enlist(struct mbuf_queue *, struct mbuf_list *);
540 void			mq_delist(struct mbuf_queue *, struct mbuf_list *);
541 struct mbuf *		mq_dechain(struct mbuf_queue *);
542 unsigned int		mq_purge(struct mbuf_queue *);
543 unsigned int		mq_hdatalen(struct mbuf_queue *);
544 void			mq_set_maxlen(struct mbuf_queue *, u_int);
545 
546 #define mq_len(_mq)		READ_ONCE((_mq)->mq_list.ml_len)
547 #define mq_empty(_mq)		(mq_len(_mq) == 0)
548 #define mq_full(_mq)		(mq_len((_mq)) >= READ_ONCE((_mq)->mq_maxlen))
549 #define mq_drops(_mq)		READ_ONCE((_mq)->mq_drops)
550 
551 #endif /* _KERNEL */
552 #endif /* _SYS_MBUF_H_ */
553