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