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