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