xref: /freebsd/sys/sys/mbuf.h (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
1 /*
2  * Copyright (c) 1982, 1986, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)mbuf.h	8.5 (Berkeley) 2/19/95
34  * $FreeBSD$
35  */
36 
37 #ifndef _SYS_MBUF_H_
38 #define	_SYS_MBUF_H_
39 
40 /*
41  * Mbufs are of a single size, MSIZE (machine/param.h), which
42  * includes overhead.  An mbuf may add a single "mbuf cluster" of size
43  * MCLBYTES (also in machine/param.h), which has no additional overhead
44  * and is used instead of the internal data area; this is done when
45  * at least MINCLSIZE of data must be stored. Additionally, it is possible
46  * to allocate a separate buffer externally and attach it to the mbuf in
47  * a way similar to that of mbuf clusters.
48  */
49 #define	MLEN		(MSIZE - sizeof(struct m_hdr))	/* normal data len */
50 #define	MHLEN		(MLEN - sizeof(struct pkthdr))	/* data len w/pkthdr */
51 #define	MINCLSIZE	(MHLEN + 1)	/* smallest amount to put in cluster */
52 #define	M_MAXCOMPRESS	(MHLEN / 2)	/* max amount to copy for compression */
53 
54 #ifdef _KERNEL
55 /*
56  * Macros for type conversion
57  * mtod(m, t)	- convert mbuf pointer to data pointer of correct type
58  * dtom(x)	- convert data pointer within mbuf to mbuf pointer (XXX)
59  */
60 #define	mtod(m, t)	((t)((m)->m_data))
61 #define	dtom(x)		((struct mbuf *)((intptr_t)(x) & ~(MSIZE-1)))
62 #endif /* _KERNEL */
63 
64 /*
65  * Header present at the beginning of every mbuf.
66  */
67 struct m_hdr {
68 	struct	mbuf *mh_next;		/* next buffer in chain */
69 	struct	mbuf *mh_nextpkt;	/* next chain in queue/record */
70 	caddr_t	mh_data;		/* location of data */
71 	int	mh_len;			/* amount of data in this mbuf */
72 	short	mh_type;		/* type of data in this mbuf */
73 	short	mh_flags;		/* flags; see below */
74 };
75 
76 /*
77  * Record/packet header in first mbuf of chain; valid only if M_PKTHDR is set.
78  */
79 struct pkthdr {
80 	struct	ifnet *rcvif;		/* rcv interface */
81 	int	len;			/* total packet length */
82 	/* variables for ip and tcp reassembly */
83 	void	*header;		/* pointer to packet header */
84 	/* variables for hardware checksum */
85 	int	csum_flags;		/* flags regarding checksum */
86 	int	csum_data;		/* data field used by csum routines */
87 	struct	mbuf *aux;		/* extra data buffer; ipsec/others */
88 };
89 
90 /*
91  * Description of external storage mapped into mbuf; valid only if M_EXT is set.
92  */
93 struct m_ext {
94 	caddr_t	ext_buf;		/* start of buffer */
95 	void	(*ext_free)		/* free routine if not the usual */
96 		    (caddr_t, void *);
97 	void	*ext_args;		/* optional argument pointer */
98 	u_int	ext_size;		/* size of buffer, for ext_free */
99 	u_int	*ref_cnt;		/* pointer to ref count info */
100 	int	ext_type;		/* type of external storage */
101 };
102 
103 /*
104  * The core of the mbuf object along with some shortcut defines for
105  * practical purposes.
106  */
107 struct mbuf {
108 	struct	m_hdr m_hdr;
109 	union {
110 		struct {
111 			struct	pkthdr MH_pkthdr;	/* M_PKTHDR set */
112 			union {
113 				struct	m_ext MH_ext;	/* M_EXT set */
114 				char	MH_databuf[MHLEN];
115 			} MH_dat;
116 		} MH;
117 		char	M_databuf[MLEN];		/* !M_PKTHDR, !M_EXT */
118 	} M_dat;
119 };
120 #define	m_next		m_hdr.mh_next
121 #define	m_len		m_hdr.mh_len
122 #define	m_data		m_hdr.mh_data
123 #define	m_type		m_hdr.mh_type
124 #define	m_flags		m_hdr.mh_flags
125 #define	m_nextpkt	m_hdr.mh_nextpkt
126 #define	m_act		m_nextpkt
127 #define	m_pkthdr	M_dat.MH.MH_pkthdr
128 #define	m_ext		M_dat.MH.MH_dat.MH_ext
129 #define	m_pktdat	M_dat.MH.MH_dat.MH_databuf
130 #define	m_dat		M_dat.M_databuf
131 
132 /*
133  * mbuf flags
134  */
135 #define	M_EXT		0x0001	/* has associated external storage */
136 #define	M_PKTHDR	0x0002	/* start of record */
137 #define	M_EOR		0x0004	/* end of record */
138 #define M_RDONLY	0x0008	/* associated data is marked read-only */
139 #define	M_PROTO1	0x0010	/* protocol-specific */
140 #define	M_PROTO2	0x0020	/* protocol-specific */
141 #define	M_PROTO3	0x0040	/* protocol-specific */
142 #define	M_PROTO4	0x0080	/* protocol-specific */
143 #define	M_PROTO5	0x0100	/* protocol-specific */
144 
145 /*
146  * mbuf pkthdr flags (also stored in m_flags)
147  */
148 #define	M_BCAST		0x0200	/* send/received as link-level broadcast */
149 #define	M_MCAST		0x0400	/* send/received as link-level multicast */
150 #define	M_FRAG		0x0800	/* packet is a fragment of a larger packet */
151 #define	M_FIRSTFRAG	0x1000	/* packet is first fragment */
152 #define	M_LASTFRAG	0x2000	/* packet is last fragment */
153 
154 /*
155  * External buffer types: identify ext_buf type
156  */
157 #define	EXT_CLUSTER	1	/* mbuf cluster */
158 #define	EXT_SFBUF	2	/* sendfile(2)'s sf_bufs */
159 #define	EXT_NET_DRV	100	/* custom ext_buf provided by net driver(s) */
160 #define	EXT_MOD_TYPE	200	/* custom module's ext_buf type */
161 
162 /*
163  * Flags copied when copying m_pkthdr
164  */
165 #define	M_COPYFLAGS	(M_PKTHDR|M_EOR|M_PROTO1|M_PROTO1|M_PROTO2|M_PROTO3 | \
166 			    M_PROTO4|M_PROTO5|M_BCAST|M_MCAST|M_FRAG|M_RDONLY)
167 
168 /*
169  * Flags indicating hw checksum support and sw checksum requirements
170  */
171 #define CSUM_IP			0x0001		/* will csum IP */
172 #define CSUM_TCP		0x0002		/* will csum TCP */
173 #define CSUM_UDP		0x0004		/* will csum UDP */
174 #define CSUM_IP_FRAGS		0x0008		/* will csum IP fragments */
175 #define CSUM_FRAGMENT		0x0010		/* will do IP fragmentation */
176 
177 #define CSUM_IP_CHECKED		0x0100		/* did csum IP */
178 #define CSUM_IP_VALID		0x0200		/*   ... the csum is valid */
179 #define CSUM_DATA_VALID		0x0400		/* csum_data field is valid */
180 #define CSUM_PSEUDO_HDR		0x0800		/* csum_data has pseudo hdr */
181 
182 #define CSUM_DELAY_DATA		(CSUM_TCP | CSUM_UDP)
183 #define CSUM_DELAY_IP		(CSUM_IP)	/* XXX add ipv6 here too? */
184 
185 /*
186  * mbuf types
187  */
188 #define	MT_FREE		0	/* should be on free list */
189 #define	MT_DATA		1	/* dynamic (data) allocation */
190 #define	MT_HEADER	2	/* packet header */
191 #if 0
192 #define	MT_SOCKET	3	/* socket structure */
193 #define	MT_PCB		4	/* protocol control block */
194 #define	MT_RTABLE	5	/* routing tables */
195 #define	MT_HTABLE	6	/* IMP host tables */
196 #define	MT_ATABLE	7	/* address resolution tables */
197 #endif
198 #define	MT_SONAME	8	/* socket name */
199 #if 0
200 #define	MT_SOOPTS	10	/* socket options */
201 #endif
202 #define	MT_FTABLE	11	/* fragment reassembly header */
203 #if 0
204 #define	MT_RIGHTS	12	/* access rights */
205 #define	MT_IFADDR	13	/* interface address */
206 #endif
207 #define	MT_CONTROL	14	/* extra-data protocol message */
208 #define	MT_OOBDATA	15	/* expedited data  */
209 #define	MT_NTYPES	16	/* number of mbuf types for mbtypes[] */
210 
211 /*
212  * Mbuf and cluster allocation statistics PCPU structure.
213  */
214 struct mbpstat {
215 	u_long	mb_mbfree;
216 	u_long	mb_mbpgs;
217 	u_long	mb_clfree;
218 	u_long	mb_clpgs;
219 };
220 
221 /*
222  * General mbuf statistics structure.
223  * XXX: Modifications of these are not protected by any mutex locks nor by
224  *	any atomic() manipulations. As a result, we may occasionally lose
225  *	a count or two. Luckily, not all of these fields are modified at all
226  *	and remain static, and those that are manipulated are only manipulated
227  *	in failure situations, which do not occur (hopefully) very often.
228  */
229 struct mbstat {
230 	u_long	m_drops;	/* times failed to allocate */
231 	u_long	m_wait;		/* times succesfully returned from wait */
232 	u_long	m_drain;	/* times drained protocols for space */
233 	u_long	m_mcfail;	/* times m_copym failed */
234 	u_long	m_mpfail;	/* times m_pullup failed */
235 	u_long	m_msize;	/* length of an mbuf */
236 	u_long	m_mclbytes;	/* length of an mbuf cluster */
237 	u_long	m_minclsize;	/* min length of data to allocate a cluster */
238 	u_long	m_mlen;		/* length of data in an mbuf */
239 	u_long	m_mhlen;	/* length of data in a header mbuf */
240 };
241 
242 /*
243  * Flags specifying how an allocation should be made.
244  * M_DONTWAIT means "don't block if nothing is available" whereas
245  * M_TRYWAIT means "block for mbuf_wait ticks at most if nothing is
246  * available."
247  */
248 #define	M_DONTWAIT	1
249 #define	M_TRYWAIT	0
250 #define	M_WAIT		M_TRYWAIT	/* XXX: Deprecated. */
251 
252 #ifdef _KERNEL
253 /*
254  * mbuf external reference count management macros
255  *
256  * MEXT_IS_REF(m): true if (m) is not the only mbuf referencing
257  *     the external buffer ext_buf
258  *
259  * MEXT_REM_REF(m): remove reference to m_ext object
260  *
261  * MEXT_ADD_REF(m): add reference to m_ext object already
262  *     referred to by (m)
263  */
264 #define MEXT_IS_REF(m)	(*((m)->m_ext.ref_cnt) > 1)
265 
266 #define MEXT_REM_REF(m) do {						\
267 	KASSERT(*((m)->m_ext.ref_cnt) > 0, ("m_ext refcnt < 0"));	\
268 	atomic_subtract_int((m)->m_ext.ref_cnt, 1);			\
269 } while(0)
270 
271 #define MEXT_ADD_REF(m)	atomic_add_int((m)->m_ext.ref_cnt, 1)
272 
273 /*
274  * mbuf, cluster, and external object allocation macros
275  * (for compatibility purposes)
276  */
277 #define MGET(m, how, type) 						\
278 	(m) = m_get((how), (type))
279 
280 #define MGETHDR(m, how, type)		 				\
281 	(m) = m_gethdr((how), (type))
282 
283 #define	MCLGET(m, how)							\
284 	m_clget((m), (how))
285 
286 #define MEXTADD(m, buf, size, free, args, flags, type) 			\
287     m_extadd((m), (caddr_t)(buf), (size), (free), (args), (flags), (type))
288 
289 #define	MFREE(m, n) do {						\
290 	(n) = m_free((m));						\
291 	(m) = NULL;							\
292 } while (0)
293 
294 #define	m_getclr	m_get_clrd
295 
296 /*
297  * MEXTFREE(m): disassociate (and possibly free) an external object from `m'
298  *
299  * If the atomic_cmpset_int() returns 0, then we effectively do nothing
300  * in terms of "cleaning up" (freeing the ext buf and ref. counter) as
301  * this means that either there are still references, or another thread
302  * is taking care of the clean-up.
303  */
304 #define	MEXTFREE(m) do {						\
305 	struct mbuf *_mb = (m);						\
306 									\
307 	MEXT_REM_REF(_mb);						\
308 	if (atomic_cmpset_int(_mb->m_ext.ref_cnt, 0, 1))		\
309 		_mext_free(_mb);					\
310 	_mb->m_flags &= ~M_EXT;						\
311 } while (0)
312 
313 /*
314  * M_WRITABLE(m)
315  * Evaluate TRUE if it's safe to write to the mbuf m's data region (this
316  * can be both the local data payload, or an external buffer area,
317  * depending on whether M_EXT is set).
318  */
319 #define M_WRITABLE(m)	(!((m)->m_flags & M_RDONLY) && (!((m)->m_flags  \
320 			    & M_EXT) || !MEXT_IS_REF(m)))
321 
322 /*
323  * Copy mbuf pkthdr from "from" to "to".
324  * from must have M_PKTHDR set, and to must be empty.
325  * aux pointer will be moved to `to'.
326  */
327 #define	M_COPY_PKTHDR(to, from) do {					\
328 	struct	mbuf *_mfrom = (from);					\
329 	struct	mbuf *_mto = (to);					\
330 									\
331 	_mto->m_data = _mto->m_pktdat;					\
332 	_mto->m_flags = _mfrom->m_flags & M_COPYFLAGS;			\
333 	_mto->m_pkthdr = _mfrom->m_pkthdr;				\
334 	_mfrom->m_pkthdr.aux = NULL;					\
335 } while (0)
336 
337 /*
338  * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place
339  * an object of the specified size at the end of the mbuf, longword aligned.
340  */
341 #define	M_ALIGN(m, len) do {						\
342 	(m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1);		\
343 } while (0)
344 
345 /*
346  * As above, for mbufs allocated with m_gethdr/MGETHDR
347  * or initialized by M_COPY_PKTHDR.
348  */
349 #define	MH_ALIGN(m, len) do {						\
350 	(m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1);		\
351 } while (0)
352 
353 /*
354  * Compute the amount of space available
355  * before the current start of data in an mbuf.
356  */
357 #define	M_LEADINGSPACE(m)						\
358 	((m)->m_flags & M_EXT ?						\
359 	    /* (m)->m_data - (m)->m_ext.ext_buf */ 0 :			\
360 	    (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat :	\
361 	    (m)->m_data - (m)->m_dat)
362 
363 /*
364  * Compute the amount of space available
365  * after the end of data in an mbuf.
366  */
367 #define	M_TRAILINGSPACE(m)						\
368 	((m)->m_flags & M_EXT ? (m)->m_ext.ext_buf +			\
369 	    (m)->m_ext.ext_size - ((m)->m_data + (m)->m_len) :		\
370 	    &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len))
371 
372 /*
373  * Arrange to prepend space of size plen to mbuf m.
374  * If a new mbuf must be allocated, how specifies whether to wait.
375  * If the allocation fails, the original mbuf chain is freed and m is
376  * set to NULL.
377  */
378 #define	M_PREPEND(m, plen, how) do {					\
379 	struct	mbuf **_mmp = &(m);					\
380 	struct	mbuf *_mm = *_mmp;					\
381 	int	_mplen = (plen);					\
382 	int	__mhow = (how);						\
383 									\
384 	if (M_LEADINGSPACE(_mm) >= _mplen) {				\
385 		_mm->m_data -= _mplen;					\
386 		_mm->m_len += _mplen;					\
387 	} else								\
388 		_mm = m_prepend(_mm, _mplen, __mhow);			\
389 	if (_mm != NULL && _mm->m_flags & M_PKTHDR)			\
390 		_mm->m_pkthdr.len += _mplen;				\
391 	*_mmp = _mm;							\
392 } while (0)
393 
394 /*
395  * change mbuf to new type
396  */
397 #define	MCHTYPE(m, t)	(m)->m_type = (t)
398 
399 /* length to m_copy to copy all */
400 #define	M_COPYALL	1000000000
401 
402 /* compatibility with 4.3 */
403 #define	m_copy(m, o, l)	m_copym((m), (o), (l), M_DONTWAIT)
404 
405 /*
406  * pkthdr.aux type tags.
407  */
408 struct mauxtag {
409 	int	af;
410 	int	type;
411 	void*	p;
412 };
413 
414 extern	int		max_datalen;	/* MHLEN - max_hdr */
415 extern	int		max_hdr;	/* largest link + protocol header */
416 extern	int		max_linkhdr;	/* largest link-level header */
417 extern	int		max_protohdr;	/* largest protocol header */
418 extern	struct mbpstat	mb_statpcpu[];	/* Per-CPU allocation stats. */
419 extern	struct mbstat	mbstat;		/* General mbuf stats/infos. */
420 extern	int		nmbclusters;	/* Maximum number of clusters */
421 extern	int		nmbcnt;		/* Scale kmem_map for counter space */
422 extern	int		nmbufs;		/* Maximum number of mbufs */
423 extern	int		nsfbufs;	/* Number of sendfile(2) bufs */
424 
425 void		 m_adj(struct mbuf *, int);
426 struct	mbuf 	*m_aux_add(struct mbuf *, int, int);
427 struct	mbuf 	*m_aux_add2(struct mbuf *, int, int, void *);
428 void		 m_aux_delete(struct mbuf *, struct mbuf *);
429 struct	mbuf 	*m_aux_find(struct mbuf *, int, int);
430 struct	mbuf 	*m_aux_find2(struct mbuf *, int, int, void *);
431 void		 m_cat(struct mbuf *, struct mbuf *);
432 void		 m_clget(struct mbuf *, int);
433 void		 m_extadd(struct mbuf *, caddr_t, u_int,
434 		    void (*free)(caddr_t, void *), void *, short, int);
435 void		 m_copyback(struct mbuf *, int, int, caddr_t);
436 void		 m_copydata(struct mbuf *, int, int, caddr_t);
437 struct	mbuf 	*m_copym(struct mbuf *, int, int, int);
438 struct	mbuf 	*m_copypacket(struct mbuf *, int);
439 struct	mbuf 	*m_devget(char *, int, int, struct ifnet *,
440 		    void (*copy)(char *, caddr_t, u_int));
441 struct	mbuf	*m_dup(struct mbuf *, int);
442 struct	mbuf	*m_free(struct mbuf *);
443 void		 m_freem(struct mbuf *);
444 struct	mbuf 	*m_get(int, int);
445 struct	mbuf 	*m_get_clrd(int, int);
446 struct	mbuf 	*m_gethdr(int, int);
447 struct	mbuf	*m_gethdr_clrd(int, int);
448 struct	mbuf 	*m_getm(struct mbuf *, int, int, int);
449 struct	mbuf 	*m_prepend(struct mbuf *, int, int);
450 void		 m_print(const struct mbuf *m);
451 struct	mbuf 	*m_pulldown(struct mbuf *, int, int, int *);
452 struct	mbuf 	*m_pullup(struct mbuf *, int);
453 struct	mbuf 	*m_split(struct mbuf *, int, int);
454 #endif /* _KERNEL */
455 
456 #endif /* !_SYS_MBUF_H_ */
457