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