xref: /freebsd/sys/sys/mbuf.h (revision a2aef24aa3c8458e4036735dd6928b4ef77294e5)
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$
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 #ifdef _KERNEL
48 #include <sys/sdt.h>
49 
50 #define	MBUF_PROBE1(probe, arg0)					\
51 	SDT_PROBE1(sdt, , , probe, arg0)
52 #define	MBUF_PROBE2(probe, arg0, arg1)					\
53 	SDT_PROBE2(sdt, , , probe, arg0, arg1)
54 #define	MBUF_PROBE3(probe, arg0, arg1, arg2)				\
55 	SDT_PROBE3(sdt, , , probe, arg0, arg1, arg2)
56 #define	MBUF_PROBE4(probe, arg0, arg1, arg2, arg3)			\
57 	SDT_PROBE4(sdt, , , probe, arg0, arg1, arg2, arg3)
58 #define	MBUF_PROBE5(probe, arg0, arg1, arg2, arg3, arg4)		\
59 	SDT_PROBE5(sdt, , , probe, arg0, arg1, arg2, arg3, arg4)
60 
61 SDT_PROBE_DECLARE(sdt, , , m__init);
62 SDT_PROBE_DECLARE(sdt, , , m__gethdr);
63 SDT_PROBE_DECLARE(sdt, , , m__get);
64 SDT_PROBE_DECLARE(sdt, , , m__getcl);
65 SDT_PROBE_DECLARE(sdt, , , m__clget);
66 SDT_PROBE_DECLARE(sdt, , , m__cljget);
67 SDT_PROBE_DECLARE(sdt, , , m__cljset);
68 SDT_PROBE_DECLARE(sdt, , , m__free);
69 SDT_PROBE_DECLARE(sdt, , , m__freem);
70 
71 #endif /* _KERNEL */
72 
73 /*
74  * Mbufs are of a single size, MSIZE (sys/param.h), which includes overhead.
75  * An mbuf may add a single "mbuf cluster" of size MCLBYTES (also in
76  * sys/param.h), which has no additional overhead and is used instead of the
77  * internal data area; this is done when at least MINCLSIZE of data must be
78  * stored.  Additionally, it is possible to allocate a separate buffer
79  * externally and attach it to the mbuf in a way similar to that of mbuf
80  * clusters.
81  *
82  * NB: These calculation do not take actual compiler-induced alignment and
83  * padding inside the complete struct mbuf into account.  Appropriate
84  * attention is required when changing members of struct mbuf.
85  *
86  * MLEN is data length in a normal mbuf.
87  * MHLEN is data length in an mbuf with pktheader.
88  * MINCLSIZE is a smallest amount of data that should be put into cluster.
89  *
90  * Compile-time assertions in uipc_mbuf.c test these values to ensure that
91  * they are sensible.
92  */
93 struct mbuf;
94 #define	MHSIZE		offsetof(struct mbuf, m_dat)
95 #define	MPKTHSIZE	offsetof(struct mbuf, m_pktdat)
96 #define	MLEN		((int)(MSIZE - MHSIZE))
97 #define	MHLEN		((int)(MSIZE - MPKTHSIZE))
98 #define	MINCLSIZE	(MHLEN + 1)
99 
100 #ifdef _KERNEL
101 /*-
102  * Macro for type conversion: convert mbuf pointer to data pointer of correct
103  * type:
104  *
105  * mtod(m, t)	-- Convert mbuf pointer to data pointer of correct type.
106  * mtodo(m, o) -- Same as above but with offset 'o' into data.
107  */
108 #define	mtod(m, t)	((t)((m)->m_data))
109 #define	mtodo(m, o)	((void *)(((m)->m_data) + (o)))
110 
111 /*
112  * Argument structure passed to UMA routines during mbuf and packet
113  * allocations.
114  */
115 struct mb_args {
116 	int	flags;	/* Flags for mbuf being allocated */
117 	short	type;	/* Type of mbuf being allocated */
118 };
119 #endif /* _KERNEL */
120 
121 /*
122  * Packet tag structure (see below for details).
123  */
124 struct m_tag {
125 	SLIST_ENTRY(m_tag)	m_tag_link;	/* List of packet tags */
126 	u_int16_t		m_tag_id;	/* Tag ID */
127 	u_int16_t		m_tag_len;	/* Length of data */
128 	u_int32_t		m_tag_cookie;	/* ABI/Module ID */
129 	void			(*m_tag_free)(struct m_tag *);
130 };
131 
132 /*
133  * Static network interface owned tag.
134  * Allocated through ifp->if_snd_tag_alloc().
135  */
136 struct m_snd_tag {
137 	struct ifnet *ifp;		/* network interface tag belongs to */
138 };
139 
140 /*
141  * Record/packet header in first mbuf of chain; valid only if M_PKTHDR is set.
142  * Size ILP32: 48
143  *	 LP64: 56
144  * Compile-time assertions in uipc_mbuf.c test these values to ensure that
145  * they are correct.
146  */
147 struct pkthdr {
148 	union {
149 		struct m_snd_tag *snd_tag;	/* send tag, if any */
150 		struct ifnet	*rcvif;		/* rcv interface */
151 	};
152 	SLIST_HEAD(packet_tags, m_tag) tags; /* list of packet tags */
153 	int32_t		 len;		/* total packet length */
154 
155 	/* Layer crossing persistent information. */
156 	uint32_t	 flowid;	/* packet's 4-tuple system */
157 	uint64_t	 csum_flags;	/* checksum and offload features */
158 	uint16_t	 fibnum;	/* this packet should use this fib */
159 	uint8_t		 cosqos;	/* class/quality of service */
160 	uint8_t		 rsstype;	/* hash type */
161 	uint8_t		 l2hlen;	/* layer 2 header length */
162 	uint8_t		 l3hlen;	/* layer 3 header length */
163 	uint8_t		 l4hlen;	/* layer 4 header length */
164 	uint8_t		 l5hlen;	/* layer 5 header length */
165 	union {
166 		uint8_t  eight[8];
167 		uint16_t sixteen[4];
168 		uint32_t thirtytwo[2];
169 		uint64_t sixtyfour[1];
170 		uintptr_t unintptr[1];
171 		void	*ptr;
172 	} PH_per;
173 
174 	/* Layer specific non-persistent local storage for reassembly, etc. */
175 	union {
176 		uint8_t  eight[8];
177 		uint16_t sixteen[4];
178 		uint32_t thirtytwo[2];
179 		uint64_t sixtyfour[1];
180 		uintptr_t unintptr[1];
181 		void 	*ptr;
182 	} PH_loc;
183 };
184 #define	ether_vtag	PH_per.sixteen[0]
185 #define	PH_vt		PH_per
186 #define	vt_nrecs	sixteen[0]
187 #define	tso_segsz	PH_per.sixteen[1]
188 #define	lro_nsegs	tso_segsz
189 #define	csum_phsum	PH_per.sixteen[2]
190 #define	csum_data	PH_per.thirtytwo[1]
191 
192 /*
193  * Description of external storage mapped into mbuf; valid only if M_EXT is
194  * set.
195  * Size ILP32: 28
196  *	 LP64: 48
197  * Compile-time assertions in uipc_mbuf.c test these values to ensure that
198  * they are correct.
199  */
200 typedef	void m_ext_free_t(struct mbuf *);
201 struct m_ext {
202 	union {
203 		/*
204 		 * If EXT_FLAG_EMBREF is set, then we use refcount in the
205 		 * mbuf, the 'ext_count' member.  Otherwise, we have a
206 		 * shadow copy and we use pointer 'ext_cnt'.  The original
207 		 * mbuf is responsible to carry the pointer to free routine
208 		 * and its arguments.  They aren't copied into shadows in
209 		 * mb_dupcl() to avoid dereferencing next cachelines.
210 		 */
211 		volatile u_int	 ext_count;
212 		volatile u_int	*ext_cnt;
213 	};
214 	char		*ext_buf;	/* start of buffer */
215 	uint32_t	 ext_size;	/* size of buffer, for ext_free */
216 	uint32_t	 ext_type:8,	/* type of external storage */
217 			 ext_flags:24;	/* external storage mbuf flags */
218 	/*
219 	 * Fields below store the free context for the external storage.
220 	 * They are valid only in the refcount carrying mbuf, the one with
221 	 * EXT_FLAG_EMBREF flag, with exclusion for EXT_EXTREF type, where
222 	 * the free context is copied into all mbufs that use same external
223 	 * storage.
224 	 */
225 #define	m_ext_copylen	offsetof(struct m_ext, ext_free)
226 	m_ext_free_t	*ext_free;	/* free routine if not the usual */
227 	void		*ext_arg1;	/* optional argument pointer */
228 	void		*ext_arg2;	/* optional argument pointer */
229 };
230 
231 /*
232  * The core of the mbuf object along with some shortcut defines for practical
233  * purposes.
234  */
235 struct mbuf {
236 	/*
237 	 * Header present at the beginning of every mbuf.
238 	 * Size ILP32: 24
239 	 *      LP64: 32
240 	 * Compile-time assertions in uipc_mbuf.c test these values to ensure
241 	 * that they are correct.
242 	 */
243 	union {	/* next buffer in chain */
244 		struct mbuf		*m_next;
245 		SLIST_ENTRY(mbuf)	m_slist;
246 		STAILQ_ENTRY(mbuf)	m_stailq;
247 	};
248 	union {	/* next chain in queue/record */
249 		struct mbuf		*m_nextpkt;
250 		SLIST_ENTRY(mbuf)	m_slistpkt;
251 		STAILQ_ENTRY(mbuf)	m_stailqpkt;
252 	};
253 	caddr_t		 m_data;	/* location of data */
254 	int32_t		 m_len;		/* amount of data in this mbuf */
255 	uint32_t	 m_type:8,	/* type of data in this mbuf */
256 			 m_flags:24;	/* flags; see below */
257 #if !defined(__LP64__)
258 	uint32_t	 m_pad;		/* pad for 64bit alignment */
259 #endif
260 
261 	/*
262 	 * A set of optional headers (packet header, external storage header)
263 	 * and internal data storage.  Historically, these arrays were sized
264 	 * to MHLEN (space left after a packet header) and MLEN (space left
265 	 * after only a regular mbuf header); they are now variable size in
266 	 * order to support future work on variable-size mbufs.
267 	 */
268 	union {
269 		struct {
270 			struct pkthdr	m_pkthdr;	/* M_PKTHDR set */
271 			union {
272 				struct m_ext	m_ext;	/* M_EXT set */
273 				char		m_pktdat[0];
274 			};
275 		};
276 		char	m_dat[0];			/* !M_PKTHDR, !M_EXT */
277 	};
278 };
279 
280 /*
281  * mbuf flags of global significance and layer crossing.
282  * Those of only protocol/layer specific significance are to be mapped
283  * to M_PROTO[1-12] and cleared at layer handoff boundaries.
284  * NB: Limited to the lower 24 bits.
285  */
286 #define	M_EXT		0x00000001 /* has associated external storage */
287 #define	M_PKTHDR	0x00000002 /* start of record */
288 #define	M_EOR		0x00000004 /* end of record */
289 #define	M_RDONLY	0x00000008 /* associated data is marked read-only */
290 #define	M_BCAST		0x00000010 /* send/received as link-level broadcast */
291 #define	M_MCAST		0x00000020 /* send/received as link-level multicast */
292 #define	M_PROMISC	0x00000040 /* packet was not for us */
293 #define	M_VLANTAG	0x00000080 /* ether_vtag is valid */
294 #define	M_UNUSED_8	0x00000100 /* --available-- */
295 #define	M_NOFREE	0x00000200 /* do not free mbuf, embedded in cluster */
296 
297 #define	M_PROTO1	0x00001000 /* protocol-specific */
298 #define	M_PROTO2	0x00002000 /* protocol-specific */
299 #define	M_PROTO3	0x00004000 /* protocol-specific */
300 #define	M_PROTO4	0x00008000 /* protocol-specific */
301 #define	M_PROTO5	0x00010000 /* protocol-specific */
302 #define	M_PROTO6	0x00020000 /* protocol-specific */
303 #define	M_PROTO7	0x00040000 /* protocol-specific */
304 #define	M_PROTO8	0x00080000 /* protocol-specific */
305 #define	M_PROTO9	0x00100000 /* protocol-specific */
306 #define	M_PROTO10	0x00200000 /* protocol-specific */
307 #define	M_PROTO11	0x00400000 /* protocol-specific */
308 #define	M_PROTO12	0x00800000 /* protocol-specific */
309 
310 #define MB_DTOR_SKIP	0x1	/* don't pollute the cache by touching a freed mbuf */
311 
312 /*
313  * Flags to purge when crossing layers.
314  */
315 #define	M_PROTOFLAGS \
316     (M_PROTO1|M_PROTO2|M_PROTO3|M_PROTO4|M_PROTO5|M_PROTO6|M_PROTO7|M_PROTO8|\
317      M_PROTO9|M_PROTO10|M_PROTO11|M_PROTO12)
318 
319 /*
320  * Flags preserved when copying m_pkthdr.
321  */
322 #define M_COPYFLAGS \
323     (M_PKTHDR|M_EOR|M_RDONLY|M_BCAST|M_MCAST|M_PROMISC|M_VLANTAG| \
324      M_PROTOFLAGS)
325 
326 /*
327  * Mbuf flag description for use with printf(9) %b identifier.
328  */
329 #define	M_FLAG_BITS \
330     "\20\1M_EXT\2M_PKTHDR\3M_EOR\4M_RDONLY\5M_BCAST\6M_MCAST" \
331     "\7M_PROMISC\10M_VLANTAG"
332 #define	M_FLAG_PROTOBITS \
333     "\15M_PROTO1\16M_PROTO2\17M_PROTO3\20M_PROTO4\21M_PROTO5" \
334     "\22M_PROTO6\23M_PROTO7\24M_PROTO8\25M_PROTO9\26M_PROTO10" \
335     "\27M_PROTO11\30M_PROTO12"
336 #define	M_FLAG_PRINTF (M_FLAG_BITS M_FLAG_PROTOBITS)
337 
338 /*
339  * Network interface cards are able to hash protocol fields (such as IPv4
340  * addresses and TCP port numbers) classify packets into flows.  These flows
341  * can then be used to maintain ordering while delivering packets to the OS
342  * via parallel input queues, as well as to provide a stateless affinity
343  * model.  NIC drivers can pass up the hash via m->m_pkthdr.flowid, and set
344  * m_flag fields to indicate how the hash should be interpreted by the
345  * network stack.
346  *
347  * Most NICs support RSS, which provides ordering and explicit affinity, and
348  * use the hash m_flag bits to indicate what header fields were covered by
349  * the hash.  M_HASHTYPE_OPAQUE and M_HASHTYPE_OPAQUE_HASH can be set by non-
350  * RSS cards or configurations that provide an opaque flow identifier, allowing
351  * for ordering and distribution without explicit affinity.  Additionally,
352  * M_HASHTYPE_OPAQUE_HASH indicates that the flow identifier has hash
353  * properties.
354  *
355  * The meaning of the IPV6_EX suffix:
356  * "o  Home address from the home address option in the IPv6 destination
357  *     options header.  If the extension header is not present, use the Source
358  *     IPv6 Address.
359  *  o  IPv6 address that is contained in the Routing-Header-Type-2 from the
360  *     associated extension header.  If the extension header is not present,
361  *     use the Destination IPv6 Address."
362  * Quoted from:
363  * https://docs.microsoft.com/en-us/windows-hardware/drivers/network/rss-hashing-types#ndishashipv6ex
364  */
365 #define	M_HASHTYPE_HASHPROP		0x80	/* has hash properties */
366 #define	M_HASHTYPE_HASH(t)		(M_HASHTYPE_HASHPROP | (t))
367 /* Microsoft RSS standard hash types */
368 #define	M_HASHTYPE_NONE			0
369 #define	M_HASHTYPE_RSS_IPV4		M_HASHTYPE_HASH(1) /* IPv4 2-tuple */
370 #define	M_HASHTYPE_RSS_TCP_IPV4		M_HASHTYPE_HASH(2) /* TCPv4 4-tuple */
371 #define	M_HASHTYPE_RSS_IPV6		M_HASHTYPE_HASH(3) /* IPv6 2-tuple */
372 #define	M_HASHTYPE_RSS_TCP_IPV6		M_HASHTYPE_HASH(4) /* TCPv6 4-tuple */
373 #define	M_HASHTYPE_RSS_IPV6_EX		M_HASHTYPE_HASH(5) /* IPv6 2-tuple +
374 							    * ext hdrs */
375 #define	M_HASHTYPE_RSS_TCP_IPV6_EX	M_HASHTYPE_HASH(6) /* TCPv6 4-tuple +
376 							    * ext hdrs */
377 #define	M_HASHTYPE_RSS_UDP_IPV4		M_HASHTYPE_HASH(7) /* IPv4 UDP 4-tuple*/
378 #define	M_HASHTYPE_RSS_UDP_IPV6		M_HASHTYPE_HASH(9) /* IPv6 UDP 4-tuple*/
379 #define	M_HASHTYPE_RSS_UDP_IPV6_EX	M_HASHTYPE_HASH(10)/* IPv6 UDP 4-tuple +
380 							    * ext hdrs */
381 
382 #define	M_HASHTYPE_OPAQUE		63	/* ordering, not affinity */
383 #define	M_HASHTYPE_OPAQUE_HASH		M_HASHTYPE_HASH(M_HASHTYPE_OPAQUE)
384 						/* ordering+hash, not affinity*/
385 
386 #define	M_HASHTYPE_CLEAR(m)	((m)->m_pkthdr.rsstype = 0)
387 #define	M_HASHTYPE_GET(m)	((m)->m_pkthdr.rsstype)
388 #define	M_HASHTYPE_SET(m, v)	((m)->m_pkthdr.rsstype = (v))
389 #define	M_HASHTYPE_TEST(m, v)	(M_HASHTYPE_GET(m) == (v))
390 #define	M_HASHTYPE_ISHASH(m)	(M_HASHTYPE_GET(m) & M_HASHTYPE_HASHPROP)
391 
392 /*
393  * COS/QOS class and quality of service tags.
394  * It uses DSCP code points as base.
395  */
396 #define	QOS_DSCP_CS0		0x00
397 #define	QOS_DSCP_DEF		QOS_DSCP_CS0
398 #define	QOS_DSCP_CS1		0x20
399 #define	QOS_DSCP_AF11		0x28
400 #define	QOS_DSCP_AF12		0x30
401 #define	QOS_DSCP_AF13		0x38
402 #define	QOS_DSCP_CS2		0x40
403 #define	QOS_DSCP_AF21		0x48
404 #define	QOS_DSCP_AF22		0x50
405 #define	QOS_DSCP_AF23		0x58
406 #define	QOS_DSCP_CS3		0x60
407 #define	QOS_DSCP_AF31		0x68
408 #define	QOS_DSCP_AF32		0x70
409 #define	QOS_DSCP_AF33		0x78
410 #define	QOS_DSCP_CS4		0x80
411 #define	QOS_DSCP_AF41		0x88
412 #define	QOS_DSCP_AF42		0x90
413 #define	QOS_DSCP_AF43		0x98
414 #define	QOS_DSCP_CS5		0xa0
415 #define	QOS_DSCP_EF		0xb8
416 #define	QOS_DSCP_CS6		0xc0
417 #define	QOS_DSCP_CS7		0xe0
418 
419 /*
420  * External mbuf storage buffer types.
421  */
422 #define	EXT_CLUSTER	1	/* mbuf cluster */
423 #define	EXT_SFBUF	2	/* sendfile(2)'s sf_buf */
424 #define	EXT_JUMBOP	3	/* jumbo cluster page sized */
425 #define	EXT_JUMBO9	4	/* jumbo cluster 9216 bytes */
426 #define	EXT_JUMBO16	5	/* jumbo cluster 16184 bytes */
427 #define	EXT_PACKET	6	/* mbuf+cluster from packet zone */
428 #define	EXT_MBUF	7	/* external mbuf reference */
429 
430 #define	EXT_VENDOR1	224	/* for vendor-internal use */
431 #define	EXT_VENDOR2	225	/* for vendor-internal use */
432 #define	EXT_VENDOR3	226	/* for vendor-internal use */
433 #define	EXT_VENDOR4	227	/* for vendor-internal use */
434 
435 #define	EXT_EXP1	244	/* for experimental use */
436 #define	EXT_EXP2	245	/* for experimental use */
437 #define	EXT_EXP3	246	/* for experimental use */
438 #define	EXT_EXP4	247	/* for experimental use */
439 
440 #define	EXT_NET_DRV	252	/* custom ext_buf provided by net driver(s) */
441 #define	EXT_MOD_TYPE	253	/* custom module's ext_buf type */
442 #define	EXT_DISPOSABLE	254	/* can throw this buffer away w/page flipping */
443 #define	EXT_EXTREF	255	/* has externally maintained ext_cnt ptr */
444 
445 /*
446  * Flags for external mbuf buffer types.
447  * NB: limited to the lower 24 bits.
448  */
449 #define	EXT_FLAG_EMBREF		0x000001	/* embedded ext_count */
450 #define	EXT_FLAG_EXTREF		0x000002	/* external ext_cnt, notyet */
451 
452 #define	EXT_FLAG_NOFREE		0x000010	/* don't free mbuf to pool, notyet */
453 
454 #define	EXT_FLAG_VENDOR1	0x010000	/* These flags are vendor */
455 #define	EXT_FLAG_VENDOR2	0x020000	/* or submodule specific, */
456 #define	EXT_FLAG_VENDOR3	0x040000	/* not used by mbuf code. */
457 #define	EXT_FLAG_VENDOR4	0x080000	/* Set/read by submodule. */
458 
459 #define	EXT_FLAG_EXP1		0x100000	/* for experimental use */
460 #define	EXT_FLAG_EXP2		0x200000	/* for experimental use */
461 #define	EXT_FLAG_EXP3		0x400000	/* for experimental use */
462 #define	EXT_FLAG_EXP4		0x800000	/* for experimental use */
463 
464 /*
465  * EXT flag description for use with printf(9) %b identifier.
466  */
467 #define	EXT_FLAG_BITS \
468     "\20\1EXT_FLAG_EMBREF\2EXT_FLAG_EXTREF\5EXT_FLAG_NOFREE" \
469     "\21EXT_FLAG_VENDOR1\22EXT_FLAG_VENDOR2\23EXT_FLAG_VENDOR3" \
470     "\24EXT_FLAG_VENDOR4\25EXT_FLAG_EXP1\26EXT_FLAG_EXP2\27EXT_FLAG_EXP3" \
471     "\30EXT_FLAG_EXP4"
472 
473 /*
474  * Flags indicating checksum, segmentation and other offload work to be
475  * done, or already done, by hardware or lower layers.  It is split into
476  * separate inbound and outbound flags.
477  *
478  * Outbound flags that are set by upper protocol layers requesting lower
479  * layers, or ideally the hardware, to perform these offloading tasks.
480  * For outbound packets this field and its flags can be directly tested
481  * against ifnet if_hwassist.
482  */
483 #define	CSUM_IP			0x00000001	/* IP header checksum offload */
484 #define	CSUM_IP_UDP		0x00000002	/* UDP checksum offload */
485 #define	CSUM_IP_TCP		0x00000004	/* TCP checksum offload */
486 #define	CSUM_IP_SCTP		0x00000008	/* SCTP checksum offload */
487 #define	CSUM_IP_TSO		0x00000010	/* TCP segmentation offload */
488 #define	CSUM_IP_ISCSI		0x00000020	/* iSCSI checksum offload */
489 
490 #define	CSUM_IP6_UDP		0x00000200	/* UDP checksum offload */
491 #define	CSUM_IP6_TCP		0x00000400	/* TCP checksum offload */
492 #define	CSUM_IP6_SCTP		0x00000800	/* SCTP checksum offload */
493 #define	CSUM_IP6_TSO		0x00001000	/* TCP segmentation offload */
494 #define	CSUM_IP6_ISCSI		0x00002000	/* iSCSI checksum offload */
495 
496 /* Inbound checksum support where the checksum was verified by hardware. */
497 #define	CSUM_L3_CALC		0x01000000	/* calculated layer 3 csum */
498 #define	CSUM_L3_VALID		0x02000000	/* checksum is correct */
499 #define	CSUM_L4_CALC		0x04000000	/* calculated layer 4 csum */
500 #define	CSUM_L4_VALID		0x08000000	/* checksum is correct */
501 #define	CSUM_L5_CALC		0x10000000	/* calculated layer 5 csum */
502 #define	CSUM_L5_VALID		0x20000000	/* checksum is correct */
503 #define	CSUM_COALESCED		0x40000000	/* contains merged segments */
504 
505 /*
506  * CSUM flag description for use with printf(9) %b identifier.
507  */
508 #define	CSUM_BITS \
509     "\20\1CSUM_IP\2CSUM_IP_UDP\3CSUM_IP_TCP\4CSUM_IP_SCTP\5CSUM_IP_TSO" \
510     "\6CSUM_IP_ISCSI" \
511     "\12CSUM_IP6_UDP\13CSUM_IP6_TCP\14CSUM_IP6_SCTP\15CSUM_IP6_TSO" \
512     "\16CSUM_IP6_ISCSI" \
513     "\31CSUM_L3_CALC\32CSUM_L3_VALID\33CSUM_L4_CALC\34CSUM_L4_VALID" \
514     "\35CSUM_L5_CALC\36CSUM_L5_VALID\37CSUM_COALESCED"
515 
516 /* CSUM flags compatibility mappings. */
517 #define	CSUM_IP_CHECKED		CSUM_L3_CALC
518 #define	CSUM_IP_VALID		CSUM_L3_VALID
519 #define	CSUM_DATA_VALID		CSUM_L4_VALID
520 #define	CSUM_PSEUDO_HDR		CSUM_L4_CALC
521 #define	CSUM_SCTP_VALID		CSUM_L4_VALID
522 #define	CSUM_DELAY_DATA		(CSUM_TCP|CSUM_UDP)
523 #define	CSUM_DELAY_IP		CSUM_IP		/* Only v4, no v6 IP hdr csum */
524 #define	CSUM_DELAY_DATA_IPV6	(CSUM_TCP_IPV6|CSUM_UDP_IPV6)
525 #define	CSUM_DATA_VALID_IPV6	CSUM_DATA_VALID
526 #define	CSUM_TCP		CSUM_IP_TCP
527 #define	CSUM_UDP		CSUM_IP_UDP
528 #define	CSUM_SCTP		CSUM_IP_SCTP
529 #define	CSUM_TSO		(CSUM_IP_TSO|CSUM_IP6_TSO)
530 #define	CSUM_UDP_IPV6		CSUM_IP6_UDP
531 #define	CSUM_TCP_IPV6		CSUM_IP6_TCP
532 #define	CSUM_SCTP_IPV6		CSUM_IP6_SCTP
533 
534 /*
535  * mbuf types describing the content of the mbuf (including external storage).
536  */
537 #define	MT_NOTMBUF	0	/* USED INTERNALLY ONLY! Object is not mbuf */
538 #define	MT_DATA		1	/* dynamic (data) allocation */
539 #define	MT_HEADER	MT_DATA	/* packet header, use M_PKTHDR instead */
540 
541 #define	MT_VENDOR1	4	/* for vendor-internal use */
542 #define	MT_VENDOR2	5	/* for vendor-internal use */
543 #define	MT_VENDOR3	6	/* for vendor-internal use */
544 #define	MT_VENDOR4	7	/* for vendor-internal use */
545 
546 #define	MT_SONAME	8	/* socket name */
547 
548 #define	MT_EXP1		9	/* for experimental use */
549 #define	MT_EXP2		10	/* for experimental use */
550 #define	MT_EXP3		11	/* for experimental use */
551 #define	MT_EXP4		12	/* for experimental use */
552 
553 #define	MT_CONTROL	14	/* extra-data protocol message */
554 #define	MT_OOBDATA	15	/* expedited data  */
555 #define	MT_NTYPES	16	/* number of mbuf types for mbtypes[] */
556 
557 #define	MT_NOINIT	255	/* Not a type but a flag to allocate
558 				   a non-initialized mbuf */
559 
560 /*
561  * String names of mbuf-related UMA(9) and malloc(9) types.  Exposed to
562  * !_KERNEL so that monitoring tools can look up the zones with
563  * libmemstat(3).
564  */
565 #define	MBUF_MEM_NAME		"mbuf"
566 #define	MBUF_CLUSTER_MEM_NAME	"mbuf_cluster"
567 #define	MBUF_PACKET_MEM_NAME	"mbuf_packet"
568 #define	MBUF_JUMBOP_MEM_NAME	"mbuf_jumbo_page"
569 #define	MBUF_JUMBO9_MEM_NAME	"mbuf_jumbo_9k"
570 #define	MBUF_JUMBO16_MEM_NAME	"mbuf_jumbo_16k"
571 #define	MBUF_TAG_MEM_NAME	"mbuf_tag"
572 #define	MBUF_EXTREFCNT_MEM_NAME	"mbuf_ext_refcnt"
573 
574 #ifdef _KERNEL
575 
576 #ifdef WITNESS
577 #define	MBUF_CHECKSLEEP(how) do {					\
578 	if (how == M_WAITOK)						\
579 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,		\
580 		    "Sleeping in \"%s\"", __func__);			\
581 } while (0)
582 #else
583 #define	MBUF_CHECKSLEEP(how)
584 #endif
585 
586 /*
587  * Network buffer allocation API
588  *
589  * The rest of it is defined in kern/kern_mbuf.c
590  */
591 extern uma_zone_t	zone_mbuf;
592 extern uma_zone_t	zone_clust;
593 extern uma_zone_t	zone_pack;
594 extern uma_zone_t	zone_jumbop;
595 extern uma_zone_t	zone_jumbo9;
596 extern uma_zone_t	zone_jumbo16;
597 
598 void		 mb_dupcl(struct mbuf *, struct mbuf *);
599 void		 mb_free_ext(struct mbuf *);
600 void		 m_adj(struct mbuf *, int);
601 int		 m_apply(struct mbuf *, int, int,
602 		    int (*)(void *, void *, u_int), void *);
603 int		 m_append(struct mbuf *, int, c_caddr_t);
604 void		 m_cat(struct mbuf *, struct mbuf *);
605 void		 m_catpkt(struct mbuf *, struct mbuf *);
606 int		 m_clget(struct mbuf *m, int how);
607 void 		*m_cljget(struct mbuf *m, int how, int size);
608 struct mbuf	*m_collapse(struct mbuf *, int, int);
609 void		 m_copyback(struct mbuf *, int, int, c_caddr_t);
610 void		 m_copydata(const struct mbuf *, int, int, caddr_t);
611 struct mbuf	*m_copym(struct mbuf *, int, int, int);
612 struct mbuf	*m_copypacket(struct mbuf *, int);
613 void		 m_copy_pkthdr(struct mbuf *, struct mbuf *);
614 struct mbuf	*m_copyup(struct mbuf *, int, int);
615 struct mbuf	*m_defrag(struct mbuf *, int);
616 void		 m_demote_pkthdr(struct mbuf *);
617 void		 m_demote(struct mbuf *, int, int);
618 struct mbuf	*m_devget(char *, int, int, struct ifnet *,
619 		    void (*)(char *, caddr_t, u_int));
620 struct mbuf	*m_dup(const struct mbuf *, int);
621 int		 m_dup_pkthdr(struct mbuf *, const struct mbuf *, int);
622 void		 m_extadd(struct mbuf *, char *, u_int, m_ext_free_t,
623 		    void *, void *, int, int);
624 u_int		 m_fixhdr(struct mbuf *);
625 struct mbuf	*m_fragment(struct mbuf *, int, int);
626 void		 m_freem(struct mbuf *);
627 struct mbuf	*m_get2(int, int, short, int);
628 struct mbuf	*m_getjcl(int, short, int, int);
629 struct mbuf	*m_getm2(struct mbuf *, int, int, short, int);
630 struct mbuf	*m_getptr(struct mbuf *, int, int *);
631 u_int		 m_length(struct mbuf *, struct mbuf **);
632 int		 m_mbuftouio(struct uio *, const struct mbuf *, int);
633 void		 m_move_pkthdr(struct mbuf *, struct mbuf *);
634 int		 m_pkthdr_init(struct mbuf *, int);
635 struct mbuf	*m_prepend(struct mbuf *, int, int);
636 void		 m_print(const struct mbuf *, int);
637 struct mbuf	*m_pulldown(struct mbuf *, int, int, int *);
638 struct mbuf	*m_pullup(struct mbuf *, int);
639 int		 m_sanity(struct mbuf *, int);
640 struct mbuf	*m_split(struct mbuf *, int, int);
641 struct mbuf	*m_uiotombuf(struct uio *, int, int, int, int);
642 struct mbuf	*m_unshare(struct mbuf *, int);
643 
644 static __inline int
645 m_gettype(int size)
646 {
647 	int type;
648 
649 	switch (size) {
650 	case MSIZE:
651 		type = EXT_MBUF;
652 		break;
653 	case MCLBYTES:
654 		type = EXT_CLUSTER;
655 		break;
656 #if MJUMPAGESIZE != MCLBYTES
657 	case MJUMPAGESIZE:
658 		type = EXT_JUMBOP;
659 		break;
660 #endif
661 	case MJUM9BYTES:
662 		type = EXT_JUMBO9;
663 		break;
664 	case MJUM16BYTES:
665 		type = EXT_JUMBO16;
666 		break;
667 	default:
668 		panic("%s: invalid cluster size %d", __func__, size);
669 	}
670 
671 	return (type);
672 }
673 
674 /*
675  * Associated an external reference counted buffer with an mbuf.
676  */
677 static __inline void
678 m_extaddref(struct mbuf *m, char *buf, u_int size, u_int *ref_cnt,
679     m_ext_free_t freef, void *arg1, void *arg2)
680 {
681 
682 	KASSERT(ref_cnt != NULL, ("%s: ref_cnt not provided", __func__));
683 
684 	atomic_add_int(ref_cnt, 1);
685 	m->m_flags |= M_EXT;
686 	m->m_ext.ext_buf = buf;
687 	m->m_ext.ext_cnt = ref_cnt;
688 	m->m_data = m->m_ext.ext_buf;
689 	m->m_ext.ext_size = size;
690 	m->m_ext.ext_free = freef;
691 	m->m_ext.ext_arg1 = arg1;
692 	m->m_ext.ext_arg2 = arg2;
693 	m->m_ext.ext_type = EXT_EXTREF;
694 	m->m_ext.ext_flags = 0;
695 }
696 
697 static __inline uma_zone_t
698 m_getzone(int size)
699 {
700 	uma_zone_t zone;
701 
702 	switch (size) {
703 	case MCLBYTES:
704 		zone = zone_clust;
705 		break;
706 #if MJUMPAGESIZE != MCLBYTES
707 	case MJUMPAGESIZE:
708 		zone = zone_jumbop;
709 		break;
710 #endif
711 	case MJUM9BYTES:
712 		zone = zone_jumbo9;
713 		break;
714 	case MJUM16BYTES:
715 		zone = zone_jumbo16;
716 		break;
717 	default:
718 		panic("%s: invalid cluster size %d", __func__, size);
719 	}
720 
721 	return (zone);
722 }
723 
724 /*
725  * Initialize an mbuf with linear storage.
726  *
727  * Inline because the consumer text overhead will be roughly the same to
728  * initialize or call a function with this many parameters and M_PKTHDR
729  * should go away with constant propagation for !MGETHDR.
730  */
731 static __inline int
732 m_init(struct mbuf *m, int how, short type, int flags)
733 {
734 	int error;
735 
736 	m->m_next = NULL;
737 	m->m_nextpkt = NULL;
738 	m->m_data = m->m_dat;
739 	m->m_len = 0;
740 	m->m_flags = flags;
741 	m->m_type = type;
742 	if (flags & M_PKTHDR)
743 		error = m_pkthdr_init(m, how);
744 	else
745 		error = 0;
746 
747 	MBUF_PROBE5(m__init, m, how, type, flags, error);
748 	return (error);
749 }
750 
751 static __inline struct mbuf *
752 m_get(int how, short type)
753 {
754 	struct mbuf *m;
755 	struct mb_args args;
756 
757 	args.flags = 0;
758 	args.type = type;
759 	m = uma_zalloc_arg(zone_mbuf, &args, how);
760 	MBUF_PROBE3(m__get, how, type, m);
761 	return (m);
762 }
763 
764 static __inline struct mbuf *
765 m_gethdr(int how, short type)
766 {
767 	struct mbuf *m;
768 	struct mb_args args;
769 
770 	args.flags = M_PKTHDR;
771 	args.type = type;
772 	m = uma_zalloc_arg(zone_mbuf, &args, how);
773 	MBUF_PROBE3(m__gethdr, how, type, m);
774 	return (m);
775 }
776 
777 static __inline struct mbuf *
778 m_getcl(int how, short type, int flags)
779 {
780 	struct mbuf *m;
781 	struct mb_args args;
782 
783 	args.flags = flags;
784 	args.type = type;
785 	m = uma_zalloc_arg(zone_pack, &args, how);
786 	MBUF_PROBE4(m__getcl, how, type, flags, m);
787 	return (m);
788 }
789 
790 /*
791  * XXX: m_cljset() is a dangerous API.  One must attach only a new,
792  * unreferenced cluster to an mbuf(9).  It is not possible to assert
793  * that, so care can be taken only by users of the API.
794  */
795 static __inline void
796 m_cljset(struct mbuf *m, void *cl, int type)
797 {
798 	int size;
799 
800 	switch (type) {
801 	case EXT_CLUSTER:
802 		size = MCLBYTES;
803 		break;
804 #if MJUMPAGESIZE != MCLBYTES
805 	case EXT_JUMBOP:
806 		size = MJUMPAGESIZE;
807 		break;
808 #endif
809 	case EXT_JUMBO9:
810 		size = MJUM9BYTES;
811 		break;
812 	case EXT_JUMBO16:
813 		size = MJUM16BYTES;
814 		break;
815 	default:
816 		panic("%s: unknown cluster type %d", __func__, type);
817 		break;
818 	}
819 
820 	m->m_data = m->m_ext.ext_buf = cl;
821 	m->m_ext.ext_free = m->m_ext.ext_arg1 = m->m_ext.ext_arg2 = NULL;
822 	m->m_ext.ext_size = size;
823 	m->m_ext.ext_type = type;
824 	m->m_ext.ext_flags = EXT_FLAG_EMBREF;
825 	m->m_ext.ext_count = 1;
826 	m->m_flags |= M_EXT;
827 	MBUF_PROBE3(m__cljset, m, cl, type);
828 }
829 
830 static __inline void
831 m_chtype(struct mbuf *m, short new_type)
832 {
833 
834 	m->m_type = new_type;
835 }
836 
837 static __inline void
838 m_clrprotoflags(struct mbuf *m)
839 {
840 
841 	while (m) {
842 		m->m_flags &= ~M_PROTOFLAGS;
843 		m = m->m_next;
844 	}
845 }
846 
847 static __inline struct mbuf *
848 m_last(struct mbuf *m)
849 {
850 
851 	while (m->m_next)
852 		m = m->m_next;
853 	return (m);
854 }
855 
856 static inline u_int
857 m_extrefcnt(struct mbuf *m)
858 {
859 
860 	KASSERT(m->m_flags & M_EXT, ("%s: M_EXT missing", __func__));
861 
862 	return ((m->m_ext.ext_flags & EXT_FLAG_EMBREF) ? m->m_ext.ext_count :
863 	    *m->m_ext.ext_cnt);
864 }
865 
866 /*
867  * mbuf, cluster, and external object allocation macros (for compatibility
868  * purposes).
869  */
870 #define	M_MOVE_PKTHDR(to, from)	m_move_pkthdr((to), (from))
871 #define	MGET(m, how, type)	((m) = m_get((how), (type)))
872 #define	MGETHDR(m, how, type)	((m) = m_gethdr((how), (type)))
873 #define	MCLGET(m, how)		m_clget((m), (how))
874 #define	MEXTADD(m, buf, size, free, arg1, arg2, flags, type)		\
875     m_extadd((m), (char *)(buf), (size), (free), (arg1), (arg2),	\
876     (flags), (type))
877 #define	m_getm(m, len, how, type)					\
878     m_getm2((m), (len), (how), (type), M_PKTHDR)
879 
880 /*
881  * Evaluate TRUE if it's safe to write to the mbuf m's data region (this can
882  * be both the local data payload, or an external buffer area, depending on
883  * whether M_EXT is set).
884  */
885 #define	M_WRITABLE(m)	(!((m)->m_flags & M_RDONLY) &&			\
886 			 (!(((m)->m_flags & M_EXT)) ||			\
887 			 (m_extrefcnt(m) == 1)))
888 
889 /* Check if the supplied mbuf has a packet header, or else panic. */
890 #define	M_ASSERTPKTHDR(m)						\
891 	KASSERT((m) != NULL && (m)->m_flags & M_PKTHDR,			\
892 	    ("%s: no mbuf packet header!", __func__))
893 
894 /*
895  * Ensure that the supplied mbuf is a valid, non-free mbuf.
896  *
897  * XXX: Broken at the moment.  Need some UMA magic to make it work again.
898  */
899 #define	M_ASSERTVALID(m)						\
900 	KASSERT((((struct mbuf *)m)->m_flags & 0) == 0,			\
901 	    ("%s: attempted use of a free mbuf!", __func__))
902 
903 /*
904  * Return the address of the start of the buffer associated with an mbuf,
905  * handling external storage, packet-header mbufs, and regular data mbufs.
906  */
907 #define	M_START(m)							\
908 	(((m)->m_flags & M_EXT) ? (m)->m_ext.ext_buf :			\
909 	 ((m)->m_flags & M_PKTHDR) ? &(m)->m_pktdat[0] :		\
910 	 &(m)->m_dat[0])
911 
912 /*
913  * Return the size of the buffer associated with an mbuf, handling external
914  * storage, packet-header mbufs, and regular data mbufs.
915  */
916 #define	M_SIZE(m)							\
917 	(((m)->m_flags & M_EXT) ? (m)->m_ext.ext_size :			\
918 	 ((m)->m_flags & M_PKTHDR) ? MHLEN :				\
919 	 MLEN)
920 
921 /*
922  * Set the m_data pointer of a newly allocated mbuf to place an object of the
923  * specified size at the end of the mbuf, longword aligned.
924  *
925  * NB: Historically, we had M_ALIGN(), MH_ALIGN(), and MEXT_ALIGN() as
926  * separate macros, each asserting that it was called at the proper moment.
927  * This required callers to themselves test the storage type and call the
928  * right one.  Rather than require callers to be aware of those layout
929  * decisions, we centralize here.
930  */
931 static __inline void
932 m_align(struct mbuf *m, int len)
933 {
934 #ifdef INVARIANTS
935 	const char *msg = "%s: not a virgin mbuf";
936 #endif
937 	int adjust;
938 
939 	KASSERT(m->m_data == M_START(m), (msg, __func__));
940 
941 	adjust = M_SIZE(m) - len;
942 	m->m_data += adjust &~ (sizeof(long)-1);
943 }
944 
945 #define	M_ALIGN(m, len)		m_align(m, len)
946 #define	MH_ALIGN(m, len)	m_align(m, len)
947 #define	MEXT_ALIGN(m, len)	m_align(m, len)
948 
949 /*
950  * Compute the amount of space available before the current start of data in
951  * an mbuf.
952  *
953  * The M_WRITABLE() is a temporary, conservative safety measure: the burden
954  * of checking writability of the mbuf data area rests solely with the caller.
955  *
956  * NB: In previous versions, M_LEADINGSPACE() would only check M_WRITABLE()
957  * for mbufs with external storage.  We now allow mbuf-embedded data to be
958  * read-only as well.
959  */
960 #define	M_LEADINGSPACE(m)						\
961 	(M_WRITABLE(m) ? ((m)->m_data - M_START(m)) : 0)
962 
963 /*
964  * Compute the amount of space available after the end of data in an mbuf.
965  *
966  * The M_WRITABLE() is a temporary, conservative safety measure: the burden
967  * of checking writability of the mbuf data area rests solely with the caller.
968  *
969  * NB: In previous versions, M_TRAILINGSPACE() would only check M_WRITABLE()
970  * for mbufs with external storage.  We now allow mbuf-embedded data to be
971  * read-only as well.
972  */
973 #define	M_TRAILINGSPACE(m)						\
974 	(M_WRITABLE(m) ?						\
975 	    ((M_START(m) + M_SIZE(m)) - ((m)->m_data + (m)->m_len)) : 0)
976 
977 /*
978  * Arrange to prepend space of size plen to mbuf m.  If a new mbuf must be
979  * allocated, how specifies whether to wait.  If the allocation fails, the
980  * original mbuf chain is freed and m is set to NULL.
981  */
982 #define	M_PREPEND(m, plen, how) do {					\
983 	struct mbuf **_mmp = &(m);					\
984 	struct mbuf *_mm = *_mmp;					\
985 	int _mplen = (plen);						\
986 	int __mhow = (how);						\
987 									\
988 	MBUF_CHECKSLEEP(how);						\
989 	if (M_LEADINGSPACE(_mm) >= _mplen) {				\
990 		_mm->m_data -= _mplen;					\
991 		_mm->m_len += _mplen;					\
992 	} else								\
993 		_mm = m_prepend(_mm, _mplen, __mhow);			\
994 	if (_mm != NULL && _mm->m_flags & M_PKTHDR)			\
995 		_mm->m_pkthdr.len += _mplen;				\
996 	*_mmp = _mm;							\
997 } while (0)
998 
999 /*
1000  * Change mbuf to new type.  This is a relatively expensive operation and
1001  * should be avoided.
1002  */
1003 #define	MCHTYPE(m, t)	m_chtype((m), (t))
1004 
1005 /* Length to m_copy to copy all. */
1006 #define	M_COPYALL	1000000000
1007 
1008 extern int		max_datalen;	/* MHLEN - max_hdr */
1009 extern int		max_hdr;	/* Largest link + protocol header */
1010 extern int		max_linkhdr;	/* Largest link-level header */
1011 extern int		max_protohdr;	/* Largest protocol header */
1012 extern int		nmbclusters;	/* Maximum number of clusters */
1013 
1014 /*-
1015  * Network packets may have annotations attached by affixing a list of
1016  * "packet tags" to the pkthdr structure.  Packet tags are dynamically
1017  * allocated semi-opaque data structures that have a fixed header
1018  * (struct m_tag) that specifies the size of the memory block and a
1019  * <cookie,type> pair that identifies it.  The cookie is a 32-bit unique
1020  * unsigned value used to identify a module or ABI.  By convention this value
1021  * is chosen as the date+time that the module is created, expressed as the
1022  * number of seconds since the epoch (e.g., using date -u +'%s').  The type
1023  * value is an ABI/module-specific value that identifies a particular
1024  * annotation and is private to the module.  For compatibility with systems
1025  * like OpenBSD that define packet tags w/o an ABI/module cookie, the value
1026  * PACKET_ABI_COMPAT is used to implement m_tag_get and m_tag_find
1027  * compatibility shim functions and several tag types are defined below.
1028  * Users that do not require compatibility should use a private cookie value
1029  * so that packet tag-related definitions can be maintained privately.
1030  *
1031  * Note that the packet tag returned by m_tag_alloc has the default memory
1032  * alignment implemented by malloc.  To reference private data one can use a
1033  * construct like:
1034  *
1035  *	struct m_tag *mtag = m_tag_alloc(...);
1036  *	struct foo *p = (struct foo *)(mtag+1);
1037  *
1038  * if the alignment of struct m_tag is sufficient for referencing members of
1039  * struct foo.  Otherwise it is necessary to embed struct m_tag within the
1040  * private data structure to insure proper alignment; e.g.,
1041  *
1042  *	struct foo {
1043  *		struct m_tag	tag;
1044  *		...
1045  *	};
1046  *	struct foo *p = (struct foo *) m_tag_alloc(...);
1047  *	struct m_tag *mtag = &p->tag;
1048  */
1049 
1050 /*
1051  * Persistent tags stay with an mbuf until the mbuf is reclaimed.  Otherwise
1052  * tags are expected to ``vanish'' when they pass through a network
1053  * interface.  For most interfaces this happens normally as the tags are
1054  * reclaimed when the mbuf is free'd.  However in some special cases
1055  * reclaiming must be done manually.  An example is packets that pass through
1056  * the loopback interface.  Also, one must be careful to do this when
1057  * ``turning around'' packets (e.g., icmp_reflect).
1058  *
1059  * To mark a tag persistent bit-or this flag in when defining the tag id.
1060  * The tag will then be treated as described above.
1061  */
1062 #define	MTAG_PERSISTENT				0x800
1063 
1064 #define	PACKET_TAG_NONE				0  /* Nadda */
1065 
1066 /* Packet tags for use with PACKET_ABI_COMPAT. */
1067 #define	PACKET_TAG_IPSEC_IN_DONE		1  /* IPsec applied, in */
1068 #define	PACKET_TAG_IPSEC_OUT_DONE		2  /* IPsec applied, out */
1069 #define	PACKET_TAG_IPSEC_IN_CRYPTO_DONE		3  /* NIC IPsec crypto done */
1070 #define	PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED	4  /* NIC IPsec crypto req'ed */
1071 #define	PACKET_TAG_IPSEC_IN_COULD_DO_CRYPTO	5  /* NIC notifies IPsec */
1072 #define	PACKET_TAG_IPSEC_PENDING_TDB		6  /* Reminder to do IPsec */
1073 #define	PACKET_TAG_BRIDGE			7  /* Bridge processing done */
1074 #define	PACKET_TAG_GIF				8  /* GIF processing done */
1075 #define	PACKET_TAG_GRE				9  /* GRE processing done */
1076 #define	PACKET_TAG_IN_PACKET_CHECKSUM		10 /* NIC checksumming done */
1077 #define	PACKET_TAG_ENCAP			11 /* Encap.  processing */
1078 #define	PACKET_TAG_IPSEC_SOCKET			12 /* IPSEC socket ref */
1079 #define	PACKET_TAG_IPSEC_HISTORY		13 /* IPSEC history */
1080 #define	PACKET_TAG_IPV6_INPUT			14 /* IPV6 input processing */
1081 #define	PACKET_TAG_DUMMYNET			15 /* dummynet info */
1082 #define	PACKET_TAG_DIVERT			17 /* divert info */
1083 #define	PACKET_TAG_IPFORWARD			18 /* ipforward info */
1084 #define	PACKET_TAG_MACLABEL	(19 | MTAG_PERSISTENT) /* MAC label */
1085 #define	PACKET_TAG_PF		(21 | MTAG_PERSISTENT) /* PF/ALTQ information */
1086 #define	PACKET_TAG_RTSOCKFAM			25 /* rtsock sa family */
1087 #define	PACKET_TAG_IPOPTIONS			27 /* Saved IP options */
1088 #define	PACKET_TAG_CARP				28 /* CARP info */
1089 #define	PACKET_TAG_IPSEC_NAT_T_PORTS		29 /* two uint16_t */
1090 #define	PACKET_TAG_ND_OUTGOING			30 /* ND outgoing */
1091 
1092 /* Specific cookies and tags. */
1093 
1094 /* Packet tag routines. */
1095 struct m_tag	*m_tag_alloc(u_int32_t, int, int, int);
1096 void		 m_tag_delete(struct mbuf *, struct m_tag *);
1097 void		 m_tag_delete_chain(struct mbuf *, struct m_tag *);
1098 void		 m_tag_free_default(struct m_tag *);
1099 struct m_tag	*m_tag_locate(struct mbuf *, u_int32_t, int, struct m_tag *);
1100 struct m_tag	*m_tag_copy(struct m_tag *, int);
1101 int		 m_tag_copy_chain(struct mbuf *, const struct mbuf *, int);
1102 void		 m_tag_delete_nonpersistent(struct mbuf *);
1103 
1104 /*
1105  * Initialize the list of tags associated with an mbuf.
1106  */
1107 static __inline void
1108 m_tag_init(struct mbuf *m)
1109 {
1110 
1111 	SLIST_INIT(&m->m_pkthdr.tags);
1112 }
1113 
1114 /*
1115  * Set up the contents of a tag.  Note that this does not fill in the free
1116  * method; the caller is expected to do that.
1117  *
1118  * XXX probably should be called m_tag_init, but that was already taken.
1119  */
1120 static __inline void
1121 m_tag_setup(struct m_tag *t, u_int32_t cookie, int type, int len)
1122 {
1123 
1124 	t->m_tag_id = type;
1125 	t->m_tag_len = len;
1126 	t->m_tag_cookie = cookie;
1127 }
1128 
1129 /*
1130  * Reclaim resources associated with a tag.
1131  */
1132 static __inline void
1133 m_tag_free(struct m_tag *t)
1134 {
1135 
1136 	(*t->m_tag_free)(t);
1137 }
1138 
1139 /*
1140  * Return the first tag associated with an mbuf.
1141  */
1142 static __inline struct m_tag *
1143 m_tag_first(struct mbuf *m)
1144 {
1145 
1146 	return (SLIST_FIRST(&m->m_pkthdr.tags));
1147 }
1148 
1149 /*
1150  * Return the next tag in the list of tags associated with an mbuf.
1151  */
1152 static __inline struct m_tag *
1153 m_tag_next(struct mbuf *m __unused, struct m_tag *t)
1154 {
1155 
1156 	return (SLIST_NEXT(t, m_tag_link));
1157 }
1158 
1159 /*
1160  * Prepend a tag to the list of tags associated with an mbuf.
1161  */
1162 static __inline void
1163 m_tag_prepend(struct mbuf *m, struct m_tag *t)
1164 {
1165 
1166 	SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link);
1167 }
1168 
1169 /*
1170  * Unlink a tag from the list of tags associated with an mbuf.
1171  */
1172 static __inline void
1173 m_tag_unlink(struct mbuf *m, struct m_tag *t)
1174 {
1175 
1176 	SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link);
1177 }
1178 
1179 /* These are for OpenBSD compatibility. */
1180 #define	MTAG_ABI_COMPAT		0		/* compatibility ABI */
1181 
1182 static __inline struct m_tag *
1183 m_tag_get(int type, int length, int wait)
1184 {
1185 	return (m_tag_alloc(MTAG_ABI_COMPAT, type, length, wait));
1186 }
1187 
1188 static __inline struct m_tag *
1189 m_tag_find(struct mbuf *m, int type, struct m_tag *start)
1190 {
1191 	return (SLIST_EMPTY(&m->m_pkthdr.tags) ? (struct m_tag *)NULL :
1192 	    m_tag_locate(m, MTAG_ABI_COMPAT, type, start));
1193 }
1194 
1195 static __inline struct mbuf *
1196 m_free(struct mbuf *m)
1197 {
1198 	struct mbuf *n = m->m_next;
1199 
1200 	MBUF_PROBE1(m__free, m);
1201 	if ((m->m_flags & (M_PKTHDR|M_NOFREE)) == (M_PKTHDR|M_NOFREE))
1202 		m_tag_delete_chain(m, NULL);
1203 	if (m->m_flags & M_EXT)
1204 		mb_free_ext(m);
1205 	else if ((m->m_flags & M_NOFREE) == 0)
1206 		uma_zfree(zone_mbuf, m);
1207 	return (n);
1208 }
1209 
1210 static __inline int
1211 rt_m_getfib(struct mbuf *m)
1212 {
1213 	KASSERT(m->m_flags & M_PKTHDR , ("Attempt to get FIB from non header mbuf."));
1214 	return (m->m_pkthdr.fibnum);
1215 }
1216 
1217 #define M_GETFIB(_m)   rt_m_getfib(_m)
1218 
1219 #define M_SETFIB(_m, _fib) do {						\
1220         KASSERT((_m)->m_flags & M_PKTHDR, ("Attempt to set FIB on non header mbuf."));	\
1221 	((_m)->m_pkthdr.fibnum) = (_fib);				\
1222 } while (0)
1223 
1224 /* flags passed as first argument for "m_ether_tcpip_hash()" */
1225 #define	MBUF_HASHFLAG_L2	(1 << 2)
1226 #define	MBUF_HASHFLAG_L3	(1 << 3)
1227 #define	MBUF_HASHFLAG_L4	(1 << 4)
1228 
1229 /* mbuf hashing helper routines */
1230 uint32_t	m_ether_tcpip_hash_init(void);
1231 uint32_t	m_ether_tcpip_hash(const uint32_t, const struct mbuf *, const uint32_t);
1232 
1233 #ifdef MBUF_PROFILING
1234  void m_profile(struct mbuf *m);
1235  #define M_PROFILE(m) m_profile(m)
1236 #else
1237  #define M_PROFILE(m)
1238 #endif
1239 
1240 struct mbufq {
1241 	STAILQ_HEAD(, mbuf)	mq_head;
1242 	int			mq_len;
1243 	int			mq_maxlen;
1244 };
1245 
1246 static inline void
1247 mbufq_init(struct mbufq *mq, int maxlen)
1248 {
1249 
1250 	STAILQ_INIT(&mq->mq_head);
1251 	mq->mq_maxlen = maxlen;
1252 	mq->mq_len = 0;
1253 }
1254 
1255 static inline struct mbuf *
1256 mbufq_flush(struct mbufq *mq)
1257 {
1258 	struct mbuf *m;
1259 
1260 	m = STAILQ_FIRST(&mq->mq_head);
1261 	STAILQ_INIT(&mq->mq_head);
1262 	mq->mq_len = 0;
1263 	return (m);
1264 }
1265 
1266 static inline void
1267 mbufq_drain(struct mbufq *mq)
1268 {
1269 	struct mbuf *m, *n;
1270 
1271 	n = mbufq_flush(mq);
1272 	while ((m = n) != NULL) {
1273 		n = STAILQ_NEXT(m, m_stailqpkt);
1274 		m_freem(m);
1275 	}
1276 }
1277 
1278 static inline struct mbuf *
1279 mbufq_first(const struct mbufq *mq)
1280 {
1281 
1282 	return (STAILQ_FIRST(&mq->mq_head));
1283 }
1284 
1285 static inline struct mbuf *
1286 mbufq_last(const struct mbufq *mq)
1287 {
1288 
1289 	return (STAILQ_LAST(&mq->mq_head, mbuf, m_stailqpkt));
1290 }
1291 
1292 static inline int
1293 mbufq_full(const struct mbufq *mq)
1294 {
1295 
1296 	return (mq->mq_len >= mq->mq_maxlen);
1297 }
1298 
1299 static inline int
1300 mbufq_len(const struct mbufq *mq)
1301 {
1302 
1303 	return (mq->mq_len);
1304 }
1305 
1306 static inline int
1307 mbufq_enqueue(struct mbufq *mq, struct mbuf *m)
1308 {
1309 
1310 	if (mbufq_full(mq))
1311 		return (ENOBUFS);
1312 	STAILQ_INSERT_TAIL(&mq->mq_head, m, m_stailqpkt);
1313 	mq->mq_len++;
1314 	return (0);
1315 }
1316 
1317 static inline struct mbuf *
1318 mbufq_dequeue(struct mbufq *mq)
1319 {
1320 	struct mbuf *m;
1321 
1322 	m = STAILQ_FIRST(&mq->mq_head);
1323 	if (m) {
1324 		STAILQ_REMOVE_HEAD(&mq->mq_head, m_stailqpkt);
1325 		m->m_nextpkt = NULL;
1326 		mq->mq_len--;
1327 	}
1328 	return (m);
1329 }
1330 
1331 static inline void
1332 mbufq_prepend(struct mbufq *mq, struct mbuf *m)
1333 {
1334 
1335 	STAILQ_INSERT_HEAD(&mq->mq_head, m, m_stailqpkt);
1336 	mq->mq_len++;
1337 }
1338 
1339 /*
1340  * Note: this doesn't enforce the maximum list size for dst.
1341  */
1342 static inline void
1343 mbufq_concat(struct mbufq *mq_dst, struct mbufq *mq_src)
1344 {
1345 
1346 	mq_dst->mq_len += mq_src->mq_len;
1347 	STAILQ_CONCAT(&mq_dst->mq_head, &mq_src->mq_head);
1348 	mq_src->mq_len = 0;
1349 }
1350 
1351 #endif /* _KERNEL */
1352 #endif /* !_SYS_MBUF_H_ */
1353