1 .\" Copyright (c) 2000 FreeBSD Inc.
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
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.
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
33 .Nd "memory management in the kernel IPC subsystem"
40 .Ss Mbuf allocation macros
41 .Fn MGET "struct mbuf *mbuf" "int how" "short type"
42 .Fn MGETHDR "struct mbuf *mbuf" "int how" "short type"
44 .Fn MCLGET "struct mbuf *mbuf" "int how"
46 .Fa "struct mbuf *mbuf"
49 .Fa "void (*free)(void *opt_arg1, void *opt_arg2)"
56 .Ss Mbuf utility macros
57 .Fn mtod "struct mbuf *mbuf" "type"
58 .Fn M_ALIGN "struct mbuf *mbuf" "u_int len"
59 .Fn MH_ALIGN "struct mbuf *mbuf" "u_int len"
61 .Fn M_LEADINGSPACE "struct mbuf *mbuf"
63 .Fn M_TRAILINGSPACE "struct mbuf *mbuf"
64 .Fn M_MOVE_PKTHDR "struct mbuf *to" "struct mbuf *from"
65 .Fn M_PREPEND "struct mbuf *mbuf" "int len" "int how"
66 .Fn MCHTYPE "struct mbuf *mbuf" "short type"
68 .Fn M_WRITABLE "struct mbuf *mbuf"
70 .Ss Mbuf allocation functions
72 .Fn m_get "int how" "short type"
74 .Fn m_get2 "int size" "int how" "short type" "int flags"
76 .Fn m_getm "struct mbuf *orig" "int len" "int how" "short type"
78 .Fn m_getjcl "int how" "short type" "int flags" "int size"
80 .Fn m_getcl "int how" "short type" "int flags"
82 .Fn m_getclr "int how" "short type"
84 .Fn m_gethdr "int how" "short type"
86 .Fn m_free "struct mbuf *mbuf"
88 .Fn m_freem "struct mbuf *mbuf"
90 .Ss Mbuf utility functions
92 .Fn m_adj "struct mbuf *mbuf" "int len"
94 .Fn m_align "struct mbuf *mbuf" "int len"
96 .Fn m_append "struct mbuf *mbuf" "int len" "c_caddr_t cp"
98 .Fn m_prepend "struct mbuf *mbuf" "int len" "int how"
100 .Fn m_copyup "struct mbuf *mbuf" "int len" "int dstoff"
102 .Fn m_pullup "struct mbuf *mbuf" "int len"
104 .Fn m_pulldown "struct mbuf *mbuf" "int offset" "int len" "int *offsetp"
106 .Fn m_copym "struct mbuf *mbuf" "int offset" "int len" "int how"
108 .Fn m_copypacket "struct mbuf *mbuf" "int how"
110 .Fn m_dup "struct mbuf *mbuf" "int how"
112 .Fn m_copydata "const struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
114 .Fn m_copyback "struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
120 .Fa "struct ifnet *ifp"
121 .Fa "void (*copy)(char *from, caddr_t to, u_int len)"
124 .Fn m_cat "struct mbuf *m" "struct mbuf *n"
126 .Fn m_fixhdr "struct mbuf *mbuf"
128 .Fn m_dup_pkthdr "struct mbuf *to" "struct mbuf *from"
130 .Fn m_move_pkthdr "struct mbuf *to" "struct mbuf *from"
132 .Fn m_length "struct mbuf *mbuf" "struct mbuf **last"
134 .Fn m_split "struct mbuf *mbuf" "int len" "int how"
136 .Fn m_apply "struct mbuf *mbuf" "int off" "int len" "int (*f)(void *arg, void *data, u_int len)" "void *arg"
138 .Fn m_getptr "struct mbuf *mbuf" "int loc" "int *off"
140 .Fn m_defrag "struct mbuf *m0" "int how"
142 .Fn m_collapse "struct mbuf *m0" "int how" "int maxfrags"
144 .Fn m_unshare "struct mbuf *m0" "int how"
149 is a basic unit of memory management in the kernel IPC subsystem.
150 Network packets and socket buffers are stored in
152 A network packet may span multiple
157 which allows adding or trimming
158 network headers with little overhead.
160 While a developer should not bother with
162 internals without serious
163 reason in order to avoid incompatibilities with future changes, it
164 is useful to understand the general structure of an
169 consists of a variable-sized header and a small internal
174 is a constant defined in
179 .Bl -tag -width "m_nextpkt" -offset indent
182 A pointer to the next
188 A pointer to the next
193 A pointer to data attached to this
197 The length of the data.
200 The type of the data.
210 flag bits are defined as follows:
213 #define M_EXT 0x00000001 /* has associated external storage */
214 #define M_PKTHDR 0x00000002 /* start of record */
215 #define M_EOR 0x00000004 /* end of record */
216 #define M_RDONLY 0x00000008 /* associated data marked read-only */
217 #define M_PROTO1 0x00001000 /* protocol-specific */
218 #define M_PROTO2 0x00002000 /* protocol-specific */
219 #define M_PROTO3 0x00004000 /* protocol-specific */
220 #define M_PROTO4 0x00008000 /* protocol-specific */
221 #define M_PROTO5 0x00010000 /* protocol-specific */
222 #define M_PROTO6 0x00020000 /* protocol-specific */
223 #define M_PROTO7 0x00040000 /* protocol-specific */
224 #define M_PROTO8 0x00080000 /* protocol-specific */
225 #define M_PROTO9 0x00100000 /* protocol-specific */
226 #define M_PROTO10 0x00200000 /* protocol-specific */
227 #define M_PROTO11 0x00400000 /* protocol-specific */
228 #define M_PROTO12 0x00800000 /* protocol-specific */
230 /* mbuf pkthdr flags (also stored in m_flags) */
231 #define M_BCAST 0x00000010 /* send/received as link-level broadcast */
232 #define M_MCAST 0x00000020 /* send/received as link-level multicast */
237 types are defined as follows:
240 #define MT_DATA 1 /* dynamic (data) allocation */
241 #define MT_HEADER MT_DATA /* packet header */
242 #define MT_SONAME 8 /* socket name */
243 #define MT_CONTROL 14 /* extra-data protocol message */
244 #define MT_OOBDATA 15 /* expedited data */
247 The available external buffer types are defined as follows:
249 /* external buffer types */
250 #define EXT_CLUSTER 1 /* mbuf cluster */
251 #define EXT_SFBUF 2 /* sendfile(2)'s sf_bufs */
252 #define EXT_JUMBOP 3 /* jumbo cluster 4096 bytes */
253 #define EXT_JUMBO9 4 /* jumbo cluster 9216 bytes */
254 #define EXT_JUMBO16 5 /* jumbo cluster 16184 bytes */
255 #define EXT_PACKET 6 /* mbuf+cluster from packet zone */
256 #define EXT_MBUF 7 /* external mbuf reference (M_IOVEC) */
257 #define EXT_NET_DRV 252 /* custom ext_buf provided by net driver(s) */
258 #define EXT_MOD_TYPE 253 /* custom module's ext_buf type */
259 #define EXT_DISPOSABLE 254 /* can throw this buffer away w/page flipping */
260 #define EXT_EXTREF 255 /* has externally maintained ref_cnt ptr */
266 .Vt struct pkthdr Va m_pkthdr
270 It contains a pointer to the interface
271 the packet has been received from
272 .Pq Vt struct ifnet Va *rcvif ,
273 and the total packet length
275 Optionally, it may also contain an attached list of packet tags
276 .Pq Vt "struct m_tag" .
280 Fields used in offloading checksum calculation to the hardware are kept in
284 .Sx HARDWARE-ASSISTED CHECKSUM CALCULATION
287 If small enough, data is stored in the internal data buffer of an
289 If the data is sufficiently large, another
293 or external storage may be associated with the
296 bytes of data can fit into an
304 If external storage is being associated with an
308 header is added at the cost of losing the internal data buffer.
309 It includes a pointer to external storage, the size of the storage,
310 a pointer to a function used for freeing the storage,
311 a pointer to an optional argument that can be passed to the function,
312 and a pointer to a reference counter.
315 using external storage has the
319 The system supplies a macro for allocating the desired external storage
323 The allocation and management of the reference counter is handled by the
326 The system also supplies a default type of external storage buffer called an
329 can be allocated and configured with the use of the
336 in size, where MCLBYTES is a machine-dependent constant.
337 The system defines an advisory macro
339 which is the smallest amount of data to put into an
344 It is typically preferable to store data into the data region of an
346 if size permits, as opposed to allocating a separate
348 to hold the same data.
350 .Ss Macros and Functions
351 There are numerous predefined macros and functions that provide the
352 developer with common utilities.
354 .Bl -ohang -offset indent
355 .It Fn mtod mbuf type
358 pointer to a data pointer.
359 The macro expands to the data pointer cast to the specified
362 It is advisable to ensure that there is enough contiguous data in
367 .It Fn MGET mbuf how type
370 and initialize it to contain internal data.
372 will point to the allocated
374 on success, or be set to
379 argument is to be set to
383 It specifies whether the caller is willing to block if necessary.
384 A number of other functions and macros related to
386 have the same argument because they may
387 at some point need to allocate new
389 .It Fn MGETHDR mbuf how type
392 and initialize it to contain a packet header
397 .It Fn MEXTADD mbuf buf size free opt_arg1 opt_arg2 flags type
398 Associate externally managed data with
400 Any internal data contained in the mbuf will be discarded, and the
407 arguments are the address and length, respectively, of the data.
410 argument points to a function which will be called to free the data
411 when the mbuf is freed; it is only used if
419 arguments will be passed unmodified to
423 argument specifies additional
425 flags; it is not necessary to specify
429 argument specifies the type of external data, which controls how it
430 will be disposed of when the
433 In most cases, the correct value is
435 .It Fn MCLGET mbuf how
436 Allocate and attach an
440 On success, a non-zero value returned; otherwise, 0.
441 Historically, consumers would check for success by testing the
443 flag on the mbuf, but this is now discouraged to avoid unnecessary awareness
444 of the implementation of external storage in protocol stacks and device
446 .It Fn M_ALIGN mbuf len
449 to place an object of the size
451 at the end of the internal data area of
456 is newly allocated with
460 .It Fn MH_ALIGN mbuf len
461 Serves the same purpose as
473 .It Fn m_align mbuf len
474 Services the same purpose as
476 but handles any type of mbuf.
477 .It Fn M_LEADINGSPACE mbuf
478 Returns the number of bytes available before the beginning
481 .It Fn M_TRAILINGSPACE mbuf
482 Returns the number of bytes available after the end of data in
484 .It Fn M_PREPEND mbuf len how
485 This macro operates on an
487 It is an optimized wrapper for
489 that can make use of possible empty space before data
490 (e.g.\& left after trimming of a link-layer header).
498 .It Fn M_MOVE_PKTHDR to from
499 Using this macro is equivalent to calling
500 .Fn m_move_pkthdr to from .
501 .It Fn M_WRITABLE mbuf
502 This macro will evaluate true if
508 does not contain external storage or,
510 then if the reference count of the storage is not greater than 1.
515 This can be achieved during setup of the external storage,
522 macro, or can be directly set in individual
524 .It Fn MCHTYPE mbuf type
529 This is a relatively expensive operation and should be avoided.
533 .Bl -ohang -offset indent
534 .It Fn m_get how type
535 A function version of
537 for non-critical paths.
538 .It Fn m_get2 size how type flags
541 with enough space to hold specified amount of data.
542 .It Fn m_getm orig len how type
549 if necessary and append the resulting allocated
555 .No non- Ns Dv NULL .
556 If the allocation fails at any point,
557 free whatever was allocated and return
562 .No non- Ns Dv NULL ,
563 it will not be freed.
564 It is possible to use
572 (for example, one which may be sitting in a pre-allocated ring)
573 or to simply perform an all-or-nothing
578 .It Fn m_gethdr how type
579 A function version of
581 for non-critical paths.
582 .It Fn m_getcl how type flags
588 If one of the allocations fails, the entire allocation fails.
589 This routine is the preferred way of fetching both the
593 together, as it avoids having to unlock/relock between allocations.
597 .It Fn m_getjcl how type flags size
600 but it the size of the cluster allocated will be large enough for
603 .It Fn m_getclr how type
606 and zero out the data region.
616 The functions below operate on
618 .Bl -ohang -offset indent
622 including any external storage.
624 .It Fn m_adj mbuf len
627 bytes from the head of an
631 is positive, from the tail otherwise.
633 .It Fn m_append mbuf len cp
640 Extend the mbuf chain if the new data does not fit in
643 .It Fn m_prepend mbuf len how
646 and prepend it to the
652 It does not allocate any
664 .It Fn m_copyup mbuf len dstoff
669 bytes of data into a new mbuf at
674 argument aligns the data and leaves room for a link layer header.
684 The function does not allocate
691 .It Fn m_pullup mbuf len
692 Arrange that the first
696 are contiguous and lay in the data area of
698 so they are accessible with
700 It is important to remember that this may involve
701 reallocating some mbufs and moving data so all pointers
702 referencing data within the old mbuf chain
703 must be recalculated or made invalid.
711 is freed in this case).
713 It does not allocate any
717 must be less than or equal to
720 .It Fn m_pulldown mbuf offset len offsetp
729 are contiguous and lay in the data area of
731 so they are accessible with
734 must be smaller than, or equal to, the size of an
736 Return a pointer to an intermediate
738 in the chain containing the requested region;
739 the offset in the data region of the
741 to the data contained in the returned mbuf is stored in
745 is NULL, the region may be accessed using
749 is non-NULL, the region may be accessed using
750 .Fn mtod mbuf uint8_t
752 The region of the mbuf chain between its beginning and
754 is not modified, therefore it is safe to hold pointers to data within
755 this region before calling
758 .It Fn m_copym mbuf offset len how
763 bytes from the beginning, continuing for
770 copy to the end of the
773 The copy is read-only, because the
775 are not copied, only their reference counts are incremented.
777 .It Fn m_copypacket mbuf how
778 Copy an entire packet including header, which must be present.
779 This is an optimized version of the common case
780 .Fn m_copym mbuf 0 M_COPYALL how .
782 the copy is read-only, because the
784 are not copied, only their reference counts are incremented.
786 .It Fn m_dup mbuf how
789 into a completely new
791 including copying any
795 when you need a writable copy of an
798 .It Fn m_copydata mbuf offset len buf
803 bytes from the beginning, continuing for
805 bytes, into the indicated buffer
808 .It Fn m_copyback mbuf offset len buf
811 bytes from the buffer
813 back into the indicated
817 bytes from the beginning of the
823 It does not allocate any
835 will be allocated to fill the space.
837 .It Fn m_length mbuf last
838 Return the length of the
840 and optionally a pointer to the last
843 .It Fn m_dup_pkthdr to from how
844 Upon the function's completion, the
847 will contain an identical copy of
849 and the per-packet attributes found in the
859 must be empty on entry.
861 .It Fn m_move_pkthdr to from
864 and the per-packet attributes from the
877 must be empty on entry.
878 Upon the function's completion,
882 and the per-packet attributes cleared.
885 Set the packet-header length to the length of the
888 .It Fn m_devget buf len offset ifp copy
889 Copy data from a device local memory pointed to by
893 The copy is done using a specified copy routine
909 must be of the same type.
911 is still valid after the function returned.
917 .It Fn m_split mbuf len how
920 in two pieces, returning the tail:
924 In case of failure, it returns
926 and attempts to restore the
928 to its original state.
930 .It Fn m_apply mbuf off len f arg
931 Apply a function to an
938 Typically used to avoid calls to
940 which would otherwise be unnecessary or undesirable.
942 is a convenience argument which is passed to the callback function
947 is called, it will be passed
951 in the current mbuf, and the length
953 of the data in this mbuf to which the function should be applied.
955 The function should return zero to indicate success;
956 otherwise, if an error is indicated, then
958 will return the error and stop iterating through the
961 .It Fn m_getptr mbuf loc off
962 Return a pointer to the mbuf containing the data located at
964 bytes from the beginning of the
966 The corresponding offset into the mbuf will be stored in
968 .It Fn m_defrag m0 how
969 Defragment an mbuf chain, returning the shortest possible
970 chain of mbufs and clusters.
971 If allocation fails and this can not be completed,
973 will be returned and the original chain will be unchanged.
974 Upon success, the original chain will be freed and the new
975 chain will be returned.
981 depending on the caller's preference.
983 This function is especially useful in network drivers, where
984 certain long mbuf chains must be shortened before being added
985 to TX descriptor lists.
986 .It Fn m_collapse m0 how maxfrags
987 Defragment an mbuf chain, returning a chain of at most
990 If allocation fails or the chain cannot be collapsed as requested,
992 will be returned, with the original chain possibly modified.
1000 .It Fn m_unshare m0 how
1001 Create a version of the specified mbuf chain whose
1002 contents can be safely modified without affecting other users.
1003 If allocation fails and this operation can not be completed,
1006 The original mbuf chain is always reclaimed and the reference
1007 count of any shared mbuf clusters is decremented.
1013 depending on the caller's preference.
1014 As a side-effect of this process the returned
1015 mbuf chain may be compacted.
1017 This function is especially useful in the transmit path of
1018 network code, when data must be encrypted or otherwise
1019 altered prior to transmission.
1021 .Sh HARDWARE-ASSISTED CHECKSUM CALCULATION
1022 This section currently applies to TCP/IP only.
1023 In order to save the host CPU resources, computing checksums is
1024 offloaded to the network interface hardware if possible.
1027 member of the leading
1029 of a packet contains two fields used for that purpose,
1030 .Vt int Va csum_flags
1032 .Vt int Va csum_data .
1033 The meaning of those fields depends on the direction a packet flows in,
1034 and on whether the packet is fragmented.
1040 will denote the corresponding field of the
1042 member of the leading
1046 containing the packet.
1048 On output, checksum offloading is attempted after the outgoing
1049 interface has been determined for a packet.
1050 The interface-specific field
1051 .Va ifnet.if_data.ifi_hwassist
1054 is consulted for the capabilities of the interface to assist in
1055 computing checksums.
1058 field of the packet header is set to indicate which actions the interface
1059 is supposed to perform on it.
1060 The actions unsupported by the network interface are done in the
1061 software prior to passing the packet down to the interface driver;
1062 such actions will never be requested through
1065 The flags demanding a particular action from an interface are as follows:
1066 .Bl -tag -width ".Dv CSUM_TCP" -offset indent
1068 The IP header checksum is to be computed and stored in the
1069 corresponding field of the packet.
1070 The hardware is expected to know the format of an IP header
1071 to determine the offset of the IP checksum field.
1073 The TCP checksum is to be computed.
1076 The UDP checksum is to be computed.
1080 Should a TCP or UDP checksum be offloaded to the hardware,
1083 will contain the byte offset of the checksum field relative to the
1084 end of the IP header.
1085 In this case, the checksum field will be initially
1086 set by the TCP/IP module to the checksum of the pseudo header
1087 defined by the TCP and UDP specifications.
1089 On input, an interface indicates the actions it has performed
1090 on a packet by setting one or more of the following flags in
1092 associated with the packet:
1093 .Bl -tag -width ".Dv CSUM_IP_CHECKED" -offset indent
1094 .It Dv CSUM_IP_CHECKED
1095 The IP header checksum has been computed.
1096 .It Dv CSUM_IP_VALID
1097 The IP header has a valid checksum.
1098 This flag can appear only in combination with
1099 .Dv CSUM_IP_CHECKED .
1100 .It Dv CSUM_DATA_VALID
1101 The checksum of the data portion of the IP packet has been computed
1102 and stored in the field
1104 in network byte order.
1105 .It Dv CSUM_PSEUDO_HDR
1106 Can be set only along with
1108 to indicate that the IP data checksum found in
1110 allows for the pseudo header defined by the TCP and UDP specifications.
1111 Otherwise the checksum of the pseudo header must be calculated by
1112 the host CPU and added to
1114 to obtain the final checksum to be used for TCP or UDP validation purposes.
1117 If a particular network interface just indicates success or
1118 failure of TCP or UDP checksum validation without returning
1119 the exact value of the checksum to the host CPU, its driver can mark
1129 hexadecimal to indicate a valid checksum.
1130 It is a peculiarity of the algorithm used that the Internet checksum
1131 calculated over any valid packet will be
1133 as long as the original checksum field is included.
1135 When running a kernel compiled with the option
1136 .Dv MBUF_STRESS_TEST ,
1139 -controlled options may be used to create
1140 various failure/extreme cases for testing of network drivers
1141 and other parts of the kernel that rely on
1143 .Bl -tag -width ident
1144 .It Va net.inet.ip.mbuf_frag_size
1147 to fragment outgoing
1149 into fragments of the specified size.
1150 Setting this variable to 1 is an excellent way to
1153 handling ability of network drivers.
1154 .It Va kern.ipc.m_defragrandomfailures
1157 to randomly fail, returning
1159 Any piece of code which uses
1161 should be tested with this feature.
1169 .\" Please correct me if I'm wrong
1171 appeared in an early version of
1173 Besides being used for network packets, they were used
1174 to store various dynamic structures, such as routing table
1175 entries, interface addresses, protocol control blocks, etc.
1180 is almost entirely limited to packet storage, with
1182 zones being used directly to store other network-related memory.
1186 allocator has been a special-purpose memory allocator able to run in
1187 interrupt contexts and allocating from a special kernel address space map.
1192 allocator is a wrapper around
1198 + cluster pairs in per-CPU caches, as well as bringing other benefits of
1203 manual page was written by
1208 allocator was written by