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