Merge branch 'vendor/DIFFUTILS'
[dragonfly.git] / share / man / man9 / mbuf.9
1 .\" Copyright (c) 2000 FreeBSD Inc.
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\"
13 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED.  IN NO EVENT SHALL [your name] OR CONTRIBUTORS BE LIABLE
17 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 .\" SUCH DAMAGE.
24 .\"
25 .\" $FreeBSD: src/share/man/man9/mbuf.9,v 1.27.2.1 2003/05/28 13:53:18 yar Exp $
26 .\" $DragonFly: src/share/man/man9/mbuf.9,v 1.8 2007/08/18 18:58:20 swildner Exp $
27 .\"
28 .Dd August 18, 2007
29 .Dt MBUF 9
30 .Os
31 .\"
32 .Sh NAME
33 .Nm mbuf
34 .Nd "memory management in the kernel IPC subsystem"
35 .\"
36 .Sh SYNOPSIS
37 .In sys/param.h
38 .In sys/systm.h
39 .In sys/mbuf.h
40 .\"
41 .Ss Mbuf allocation macros
42 .Fn MGET "struct mbuf *mbuf" "int how" "short type"
43 .Fn MGETHDR "struct mbuf *mbuf" "int how" "short type"
44 .Fn MCLGET "struct mbuf *mbuf" "int how"
45 .\"
46 .Ss Mbuf utility macros
47 .Ft void *
48 .Fn mtod "struct mbuf *mbuf" "type"
49 .Fn M_ALIGN "struct mbuf *mbuf" "u_int len"
50 .Fn MH_ALIGN "struct mbuf *mbuf" "u_int len"
51 .Ft int
52 .Fn M_LEADINGSPACE "struct mbuf *mbuf"
53 .Ft int
54 .Fn M_TRAILINGSPACE "struct mbuf *mbuf"
55 .Fn M_PREPEND "struct mbuf *mbuf" "int len" "int how"
56 .\"
57 .Ss Mbuf allocation functions
58 .Ft struct mbuf *
59 .Fn m_get "int how" "int type"
60 .Ft struct mbuf *
61 .Fn m_getm "struct mbuf *orig" "int len" "int how" "int type"
62 .Ft struct mbuf *
63 .Fn m_getclr "int how" "int type"
64 .Ft struct mbuf *
65 .Fn m_gethdr "int how" "int type"
66 .Ft struct mbuf *
67 .Fn m_free "struct mbuf *mbuf"
68 .Ft void
69 .Fn m_freem "struct mbuf *mbuf"
70 .\"
71 .Ss Mbuf utility functions
72 .Ft void
73 .Fn m_adj "struct mbuf *mbuf" "int len"
74 .Ft struct mbuf *
75 .Fn m_prepend "struct mbuf *mbuf" "int len" "int how"
76 .Ft struct mbuf *
77 .Fn m_pullup "struct mbuf *mbuf" "int len"
78 .Ft struct mbuf *
79 .Fn m_copym "struct mbuf *mbuf" "int offset" "int len" "int how"
80 .Ft struct mbuf *
81 .Fn m_copypacket "struct mbuf *mbuf" "int how"
82 .Ft struct mbuf *
83 .Fn m_dup "struct mbuf *mbuf" "int how"
84 .Ft void
85 .Fn m_copydata "const struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
86 .Ft void
87 .Fn m_copyback "struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
88 .Ft struct mbuf *
89 .Fo m_devget
90 .Fa "char *buf"
91 .Fa "int len"
92 .Fa "int offset"
93 .Fa "struct ifnet *ifp"
94 .Fa "void (*copy)(char *from, caddr_t to, u_int len)"
95 .Fc
96 .Ft void
97 .Fn m_cat "struct mbuf *m" "struct mbuf *n"
98 .Ft struct mbuf *
99 .Fn m_split "struct mbuf *mbuf" "int len" "int how"
100 .\"
101 .Sh DESCRIPTION
102 An mbuf is a basic unit of memory management in the kernel IPC subsystem.
103 Network packets and socket buffers are stored in mbufs.
104 A network packet may span multiple mbufs arranged into a chain
105 (linked list),
106 which allows adding or trimming
107 network headers with little overhead.
108 .Pp
109 While a developer should not bother with mbuf internals without serious
110 reason in order to avoid incompatibilities with future changes, it
111 is useful to understand the mbuf's general structure.
112 .Pp
113 An mbuf consists of a variable-sized header and a small internal
114 buffer for data.
115 The mbuf's total size,
116 .Dv MSIZE ,
117 is a machine-dependent constant defined in
118 .In machine/param.h .
119 The mbuf header includes:
120 .Pp
121 .Bl -tag -width "m_nextpkt" -compact -offset indent
122 .It Fa m_next
123 a pointer to the next buffer in the chain
124 .It Fa m_nextpkt
125 a pointer to the next chain in the queue
126 .It Fa m_data
127 a pointer to the data
128 .It Fa m_len
129 the length of the data
130 .It Fa m_type
131 the type of data
132 .It Fa m_flags
133 the mbuf flags
134 .El
135 .Pp
136 The mbuf flag bits are defined as follows:
137 .Bd -literal
138 /* mbuf flags */
139 #define M_EXT           0x0001  /* has associated external storage */
140 #define M_PKTHDR        0x0002  /* start of record */
141 #define M_EOR           0x0004  /* end of record */
142 #define M_PROTO1        0x0010  /* protocol-specific */
143 #define M_PROTO2        0x0020  /* protocol-specific */
144 #define M_PROTO3        0x0040  /* protocol-specific */
145 #define M_PROTO4        0x0080  /* protocol-specific */
146 #define M_PROTO5        0x0100  /* protocol-specific */
147
148 /* mbuf pkthdr flags, also in m_flags */
149 #define M_BCAST         0x0200  /* send/received as link-level broadcast */
150 #define M_MCAST         0x0400  /* send/received as link-level multicast */
151 #define M_FRAG          0x0800  /* packet is fragment of larger packet */
152 #define M_FIRSTFRAG     0x1000  /* packet is first fragment */
153 #define M_LASTFRAG      0x2000  /* packet is last fragment */
154 .Ed
155 .Pp
156 The available mbuf types are defined as follows:
157 .Bd -literal
158 /* mbuf types */
159 #define MT_FREE         0       /* should be on free list */
160 #define MT_DATA         1       /* dynamic (data) allocation */
161 #define MT_HEADER       2       /* packet header */
162 #define MT_SONAME       8       /* socket name */
163 #define MT_FTABLE       11      /* fragment reassembly header */
164 #define MT_CONTROL      14      /* extra-data protocol message */
165 #define MT_OOBDATA      15      /* expedited data  */
166 .Ed
167 .Pp
168 If the
169 .Dv M_PKTHDR
170 flag is set, a
171 .Li struct pkthdr m_pkthdr
172 is added to the mbuf header.
173 It contains a pointer to the interface
174 the packet has been received from
175 .Pq Fa struct ifnet *rcvif ,
176 and the total packet length
177 .Pq Fa int len .
178 .Pp
179 If small enough, data is stored in the mbuf's internal data buffer.
180 If the data is sufficiently large, another mbuf may be added to the chain,
181 or external storage may be associated with the mbuf.
182 .Dv MHLEN
183 bytes of data can fit into an mbuf with the
184 .Dv M_PKTHDR
185 flag set,
186 .Dv MLEN
187 bytes can otherwise.
188 .Pp
189 If external storage is being associated with an mbuf, the
190 .Dv m_ext
191 header is added at the cost of losing the internal data buffer.
192 It includes a pointer to external storage, the size of the storage,
193 a pointer to a function used for freeing the storage,
194 a pointer to an optional argument that can be passed to the function,
195 and a pointer to a reference counter.
196 An mbuf using external storage has the
197 .Dv M_EXT
198 flag set.
199 .Pp
200 The system supplies a default type of external storage buffer called an
201 .Dq mbuf cluster .
202 Mbuf clusters can be allocated and configured with the use of the
203 .Dv MCLGET
204 macro.
205 Each cluster is
206 .Dv MCLBYTES
207 in size, where
208 .Dv MCLBYTES
209 is a machine-dependent constant.
210 The system defines an advisory macro
211 .Dv MINCLSIZE ,
212 which is the smallest amount of data to put into a cluster.
213 It's equal to the sum of
214 .Dv MLEN
215 and
216 .Dv MHLEN .
217 It is typically preferable to store data into an mbuf's data region, if size
218 permits, as opposed to allocating a separate mbuf cluster to hold the same
219 data.
220 .\"
221 .Ss Macros and Functions
222 There are numerous predefined macros and functions that provide the
223 developer with common utilities.
224 .\"
225 .Bl -ohang -offset indent
226 .It Fn mtod mbuf type
227 Convert an mbuf pointer to a data pointer.
228 The macro expands to the data pointer cast to the pointer of the specified type.
229 .Sy Note :
230 It is advisable to ensure that there is enough contiguous data in the mbuf.
231 See
232 .Fn m_pullup
233 for details.
234 .It Fn MGET mbuf how type
235 Allocate an mbuf and initialize it to contain internal data.
236 .Fa mbuf
237 will point to the allocated mbuf on success, or be set to
238 .Dv NULL
239 on failure.
240 The
241 .Fa how
242 argument is to be set to
243 .Dv MB_WAIT
244 or
245 .Dv MB_DONTWAIT .
246 It specifies whether the caller is willing to block if necessary.
247 If
248 .Fa how
249 is set to
250 .Dv MB_WAIT ,
251 a failed allocation will result in the caller being put
252 to sleep for a designated
253 .Va kern.ipc.mbuf_wait
254 .Xr ( sysctl 8
255 tunable)
256 number of ticks.
257 A number of other mbuf-related
258 functions and macros have the same argument because they may
259 at some point need to allocate new mbufs.
260 .Pp
261 Programmers should be careful not to confuse the mbuf allocation flag
262 .Dv MB_DONTWAIT
263 with the
264 .Xr kmalloc 9
265 allocation flag,
266 .Dv M_NOWAIT .
267 They are not the same.
268 .It Fn MGETHDR mbuf how type
269 Allocate an mbuf and initialize it to contain a packet header
270 and internal data.
271 See
272 .Fn MGET
273 for details.
274 .It Fn MCLGET mbuf how
275 Allocate and attach an mbuf cluster to an mbuf.
276 If the macro fails, the
277 .Dv M_EXT
278 flag won't be set in the mbuf.
279 .It Fn M_PREPEND mbuf len how
280 This macro operates on an mbuf chain.
281 It is an optimized wrapper for
282 .Fn m_prepend
283 that can make use of possible empty space before data
284 (e.g. left after trimming of a link-layer header).
285 The new chain pointer or
286 .Dv NULL
287 is in
288 .Fa mbuf
289 after the call.
290 .El
291 .Pp
292 The functions are:
293 .Bl -ohang -offset indent
294 .It Fn m_get how type
295 A function version of
296 .Fn MGET
297 for non-critical paths.
298 .It Fn m_getm orig len how type
299 Allocate
300 .Fa len
301 bytes worth of mbufs and mbuf clusters if necessary and append the resulting
302 allocated chain to the
303 .Fa orig
304 mbuf chain, if it is
305 .No non- Ns Dv NULL .
306 If the allocation fails at any point,
307 free whatever was allocated and return
308 .Dv NULL .
309 If
310 .Fa orig
311 is
312 .No non- Ns Dv NULL ,
313 it will not be freed.
314 It is possible to use
315 .Fn m_getm
316 to either append
317 .Fa len
318 bytes to an existing mbuf or mbuf chain
319 (for example, one which may be sitting in a pre-allocated ring)
320 or to simply perform an all-or-nothing mbuf and mbuf cluster allocation.
321 .It Fn m_gethdr how type
322 A function version of
323 .Fn MGETHDR
324 for non-critical paths.
325 .It Fn m_getclr how type
326 Allocate an mbuf and zero out the data region.
327 .El
328 .Pp
329 The functions below operate on mbuf chains.
330 .Bl -ohang -offset indent
331 .It Fn m_freem mbuf
332 Free an entire mbuf chain, including any external
333 storage.
334 .\"
335 .It Fn m_adj mbuf len
336 Trim
337 .Fa len
338 bytes from the head of an mbuf chain if
339 .Fa len
340 is positive, from the tail otherwise.
341 .\"
342 .It Fn m_prepend mbuf len how
343 Allocate a new mbuf and prepend it to the chain, handle
344 .Dv M_PKTHDR
345 properly.
346 .Sy Note :
347 It doesn't allocate any clusters, so
348 .Fa len
349 must be less than
350 .Dv MLEN
351 or
352 .Dv MHLEN ,
353 depending on the
354 .Dv M_PKTHDR
355 flag setting.
356 .\"
357 .It Fn m_pullup mbuf len
358 Arrange that the first
359 .Fa len
360 bytes of an mbuf chain are contiguous and lay in the data area of
361 .Fa mbuf ,
362 so they are accessible with
363 .Fn mtod mbuf type .
364 Return the new chain on success,
365 .Dv NULL
366 on failure
367 (the chain is freed in this case).
368 .Sy Note :
369 It doesn't allocate any clusters, so
370 .Fa len
371 must be less than
372 .Dv MHLEN .
373 .\"
374 .It Fn m_copym mbuf offset len how
375 Make a copy of an mbuf chain starting
376 .Fa offset
377 bytes from the beginning, continuing for
378 .Fa len
379 bytes.
380 If
381 .Fa len
382 is
383 .Dv M_COPYALL ,
384 copy to the end of the mbuf chain.
385 .Sy Note :
386 The copy is read-only, because clusters are not
387 copied, only their reference counts are incremented.
388 .\"
389 .It Fn m_copypacket mbuf how
390 Copy an entire packet including header, which must be present.
391 This is an optimized version of the common case
392 .Fn m_copym mbuf 0 M_COPYALL how .
393 .Sy Note :
394 the copy is read-only, because clusters are not
395 copied, only their reference counts are incremented.
396 .\"
397 .It Fn m_dup mbuf how
398 Copy a packet header mbuf chain into a completely new chain, including
399 copying any mbuf clusters.
400 Use this instead of
401 .Fn m_copypacket
402 when you need a writable copy of an mbuf chain.
403 .\"
404 .It Fn m_copydata mbuf offset len buf
405 Copy data from an mbuf chain starting
406 .Fa off
407 bytes from the beginning, continuing for
408 .Fa len
409 bytes, into the indicated buffer
410 .Fa buf .
411 .\"
412 .It Fn m_copyback mbuf offset len buf
413 Copy
414 .Fa len
415 bytes from the buffer
416 .Fa buf
417 back into the indicated mbuf chain,
418 starting at
419 .Fa offset
420 bytes from the beginning of the chain, extending the mbuf chain if necessary.
421 .Sy Note :
422 It doesn't allocate any clusters, just adds mbufs to the chain.
423 It's safe to set
424 .Fa offset
425 beyond the current chain end: zeroed mbufs will be allocated to fill the
426 space.
427 .\"
428 .It Fn m_devget buf len offset ifp copy
429 Copy data from a device local memory pointed to by
430 .Fa buf
431 to an mbuf chain.
432 The copy is done using a specified copy routine
433 .Fa copy ,
434 or
435 .Fn bcopy
436 if
437 .Fa copy
438 is
439 .Dv NULL .
440 .\"
441 .It Fn m_cat m n
442 Concatenate
443 .Fa n
444 to
445 .Fa m .
446 Both chains must be of the same type.
447 .Fa N
448 is still valid after the function returned.
449 .Sy Note :
450 It does not handle
451 .Dv M_PKTHDR
452 and friends.
453 .\"
454 .It Fn m_split mbuf len how
455 Partition an mbuf chain in two pieces, returning the tail:
456 all but the first
457 .Fa len
458 bytes.
459 In case of failure, it returns
460 .Dv NULL
461 and attempts to restore the chain to its original state.
462 .El
463 .Sh STRESS TESTING
464 When running a kernel compiled with the option
465 .Dv MBUF_STRESS_TEST ,
466 the following
467 .Xr sysctl 8 Ns
468 -controlled options may be used to create
469 various failure/extreme cases for testing of network drivers
470 and other parts of the kernel that rely on
471 .Vt mbufs .
472 .Bl -tag -width ident
473 .It Va net.inet.ip.mbuf_frag_size
474 Causes
475 .Fn ip_output
476 to fragment outgoing
477 .Vt mbuf chains
478 into fragments of the specified size.
479 Setting this variable to 1 is an excellent way to
480 test the long
481 .Vt mbuf chain
482 handling ability of network drivers.
483 .It Va kern.ipc.m_defragrandomfailures
484 Causes the function
485 .Fn m_defrag
486 to randomly fail, returning
487 .Dv NULL .
488 Any piece of code which uses
489 .Fn m_defrag
490 should be tested with this feature.
491 .El
492 .Sh RETURN VALUES
493 See above.
494 .Sh HISTORY
495 .\" Please correct me if I'm wrong
496 Mbufs appeared in an early version of
497 .Bx .
498 Besides for being used for network packets, they were used
499 to store various dynamic structures, such as routing table
500 entries, interface addresses, protocol control blocks, etc.
501 .Sh AUTHORS
502 The original
503 .Nm
504 man page was written by Yar Tikhiy.