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