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"
43 .Fn MCLGET "struct mbuf *mbuf" "int how"
45 .Fa "struct mbuf *mbuf"
48 .Fa "void (*free)(void *opt_arg1, void *opt_arg2)"
54 .Fn MEXTFREE "struct mbuf *mbuf"
55 .Fn MFREE "struct mbuf *mbuf" "struct mbuf *successor"
57 .Ss Mbuf utility macros
58 .Fn mtod "struct mbuf *mbuf" "type"
59 .Fn M_ALIGN "struct mbuf *mbuf" "u_int len"
60 .Fn MH_ALIGN "struct mbuf *mbuf" "u_int len"
62 .Fn M_LEADINGSPACE "struct mbuf *mbuf"
64 .Fn M_TRAILINGSPACE "struct mbuf *mbuf"
65 .Fn M_MOVE_PKTHDR "struct mbuf *to" "struct mbuf *from"
66 .Fn M_PREPEND "struct mbuf *mbuf" "int len" "int how"
67 .Fn MCHTYPE "struct mbuf *mbuf" "short type"
69 .Fn M_WRITABLE "struct mbuf *mbuf"
71 .Ss Mbuf allocation functions
73 .Fn m_get "int how" "short type"
75 .Fn m_get2 "int size" "int how" "short type" "int flags"
77 .Fn m_getm "struct mbuf *orig" "int len" "int how" "short type"
79 .Fn m_getjcl "int how" "short type" "int flags" "int size"
81 .Fn m_getcl "int how" "short type" "int flags"
83 .Fn m_getclr "int how" "short type"
85 .Fn m_gethdr "int how" "short type"
87 .Fn m_free "struct mbuf *mbuf"
89 .Fn m_freem "struct mbuf *mbuf"
91 .Ss Mbuf utility functions
93 .Fn m_adj "struct mbuf *mbuf" "int len"
95 .Fn m_align "struct mbuf *mbuf" "int len"
97 .Fn m_append "struct mbuf *mbuf" "int len" "c_caddr_t cp"
99 .Fn m_prepend "struct mbuf *mbuf" "int len" "int how"
101 .Fn m_copyup "struct mbuf *mbuf" "int len" "int dstoff"
103 .Fn m_pullup "struct mbuf *mbuf" "int len"
105 .Fn m_pulldown "struct mbuf *mbuf" "int offset" "int len" "int *offsetp"
107 .Fn m_copym "struct mbuf *mbuf" "int offset" "int len" "int how"
109 .Fn m_copypacket "struct mbuf *mbuf" "int how"
111 .Fn m_dup "struct mbuf *mbuf" "int how"
113 .Fn m_copydata "const struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
115 .Fn m_copyback "struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
121 .Fa "struct ifnet *ifp"
122 .Fa "void (*copy)(char *from, caddr_t to, u_int len)"
125 .Fn m_cat "struct mbuf *m" "struct mbuf *n"
127 .Fn m_fixhdr "struct mbuf *mbuf"
129 .Fn m_dup_pkthdr "struct mbuf *to" "struct mbuf *from"
131 .Fn m_move_pkthdr "struct mbuf *to" "struct mbuf *from"
133 .Fn m_length "struct mbuf *mbuf" "struct mbuf **last"
135 .Fn m_split "struct mbuf *mbuf" "int len" "int how"
137 .Fn m_apply "struct mbuf *mbuf" "int off" "int len" "int (*f)(void *arg, void *data, u_int len)" "void *arg"
139 .Fn m_getptr "struct mbuf *mbuf" "int loc" "int *off"
141 .Fn m_defrag "struct mbuf *m0" "int how"
143 .Fn m_unshare "struct mbuf *m0" "int how"
148 is a basic unit of memory management in the kernel IPC subsystem.
149 Network packets and socket buffers are stored in
151 A network packet may span multiple
156 which allows adding or trimming
157 network headers with little overhead.
159 While a developer should not bother with
161 internals without serious
162 reason in order to avoid incompatibilities with future changes, it
163 is useful to understand the general structure of an
168 consists of a variable-sized header and a small internal
173 is a constant defined in
178 .Bl -tag -width "m_nextpkt" -offset indent
181 A pointer to the next
187 A pointer to the next
192 A pointer to data attached to this
196 The length of the data.
199 The type of the data.
209 flag bits are defined as follows:
212 #define M_EXT 0x00000001 /* has associated external storage */
213 #define M_PKTHDR 0x00000002 /* start of record */
214 #define M_EOR 0x00000004 /* end of record */
215 #define M_RDONLY 0x00000008 /* associated data marked read-only */
216 #define M_PROTO1 0x00001000 /* protocol-specific */
217 #define M_PROTO2 0x00002000 /* protocol-specific */
218 #define M_PROTO3 0x00004000 /* protocol-specific */
219 #define M_PROTO4 0x00008000 /* protocol-specific */
220 #define M_PROTO5 0x00010000 /* protocol-specific */
221 #define M_PROTO6 0x00020000 /* protocol-specific */
222 #define M_PROTO7 0x00040000 /* protocol-specific */
223 #define M_PROTO8 0x00080000 /* protocol-specific */
224 #define M_PROTO9 0x00100000 /* protocol-specific */
225 #define M_PROTO10 0x00200000 /* protocol-specific */
226 #define M_PROTO11 0x00400000 /* protocol-specific */
227 #define M_PROTO12 0x00800000 /* protocol-specific */
229 /* mbuf pkthdr flags (also stored in m_flags) */
230 #define M_BCAST 0x00000010 /* send/received as link-level broadcast */
231 #define M_MCAST 0x00000020 /* send/received as link-level multicast */
236 types are defined as follows:
239 #define MT_DATA 1 /* dynamic (data) allocation */
240 #define MT_HEADER MT_DATA /* packet header */
241 #define MT_SONAME 8 /* socket name */
242 #define MT_CONTROL 14 /* extra-data protocol message */
243 #define MT_OOBDATA 15 /* expedited data */
246 The available external buffer types are defined as follows:
248 /* external buffer types */
249 #define EXT_CLUSTER 1 /* mbuf cluster */
250 #define EXT_SFBUF 2 /* sendfile(2)'s sf_bufs */
251 #define EXT_JUMBOP 3 /* jumbo cluster 4096 bytes */
252 #define EXT_JUMBO9 4 /* jumbo cluster 9216 bytes */
253 #define EXT_JUMBO16 5 /* jumbo cluster 16184 bytes */
254 #define EXT_PACKET 6 /* mbuf+cluster from packet zone */
255 #define EXT_MBUF 7 /* external mbuf reference (M_IOVEC) */
256 #define EXT_NET_DRV 252 /* custom ext_buf provided by net driver(s) */
257 #define EXT_MOD_TYPE 253 /* custom module's ext_buf type */
258 #define EXT_DISPOSABLE 254 /* can throw this buffer away w/page flipping */
259 #define EXT_EXTREF 255 /* has externally maintained ref_cnt ptr */
265 .Vt struct pkthdr Va m_pkthdr
269 It contains a pointer to the interface
270 the packet has been received from
271 .Pq Vt struct ifnet Va *rcvif ,
272 and the total packet length
274 Optionally, it may also contain an attached list of packet tags
275 .Pq Vt "struct m_tag" .
279 Fields used in offloading checksum calculation to the hardware are kept in
283 .Sx HARDWARE-ASSISTED CHECKSUM CALCULATION
286 If small enough, data is stored in the internal data buffer of an
288 If the data is sufficiently large, another
292 or external storage may be associated with the
295 bytes of data can fit into an
303 If external storage is being associated with an
307 header is added at the cost of losing the internal data buffer.
308 It includes a pointer to external storage, the size of the storage,
309 a pointer to a function used for freeing the storage,
310 a pointer to an optional argument that can be passed to the function,
311 and a pointer to a reference counter.
314 using external storage has the
318 The system supplies a macro for allocating the desired external storage
322 The allocation and management of the reference counter is handled by the
325 The system also supplies a default type of external storage buffer called an
328 can be allocated and configured with the use of the
335 in size, where MCLBYTES is a machine-dependent constant.
336 The system defines an advisory macro
338 which is the smallest amount of data to put into an
343 It is typically preferable to store data into the data region of an
345 if size permits, as opposed to allocating a separate
347 to hold the same data.
349 .Ss Macros and Functions
350 There are numerous predefined macros and functions that provide the
351 developer with common utilities.
353 .Bl -ohang -offset indent
354 .It Fn mtod mbuf type
357 pointer to a data pointer.
358 The macro expands to the data pointer cast to the pointer of the specified
361 It is advisable to ensure that there is enough contiguous data in
366 .It Fn MGET mbuf how type
369 and initialize it to contain internal data.
371 will point to the allocated
373 on success, or be set to
378 argument is to be set to
382 It specifies whether the caller is willing to block if necessary.
383 A number of other functions and macros related to
385 have the same argument because they may
386 at some point need to allocate new
388 .It Fn MGETHDR mbuf how type
391 and initialize it to contain a packet header
396 .It Fn MEXTADD mbuf buf size free opt_arg1 opt_arg2 flags type
397 Associate externally managed data with
399 Any internal data contained in the mbuf will be discarded, and the
406 arguments are the address and length, respectively, of the data.
409 argument points to a function which will be called to free the data
410 when the mbuf is freed; it is only used if
418 arguments will be passed unmodified to
422 argument specifies additional
424 flags; it is not necessary to specify
428 argument specifies the type of external data, which controls how it
429 will be disposed of when the
432 In most cases, the correct value is
434 .It Fn MCLGET mbuf how
435 Allocate and attach an
439 If the macro fails, the
441 flag will not be set in
443 .It Fn M_ALIGN mbuf len
446 to place an object of the size
448 at the end of the internal data area of
453 is newly allocated with
457 .It Fn MH_ALIGN mbuf len
458 Serves the same purpose as
470 .It Fn m_align mbuf len
471 Services the same purpose as
473 but handles any type of mbuf.
474 .It Fn M_LEADINGSPACE mbuf
475 Returns the number of bytes available before the beginning
478 .It Fn M_TRAILINGSPACE mbuf
479 Returns the number of bytes available after the end of data in
481 .It Fn M_PREPEND mbuf len how
482 This macro operates on an
484 It is an optimized wrapper for
486 that can make use of possible empty space before data
487 (e.g.\& left after trimming of a link-layer header).
495 .It Fn M_MOVE_PKTHDR to from
496 Using this macro is equivalent to calling
497 .Fn m_move_pkthdr to from .
498 .It Fn M_WRITABLE mbuf
499 This macro will evaluate true if
505 does not contain external storage or,
507 then if the reference count of the storage is not greater than 1.
512 This can be achieved during setup of the external storage,
519 macro, or can be directly set in individual
521 .It Fn MCHTYPE mbuf type
526 This is a relatively expensive operation and should be avoided.
530 .Bl -ohang -offset indent
531 .It Fn m_get how type
532 A function version of
534 for non-critical paths.
535 .It Fn m_get2 size how type flags
538 with enough space to hold specified amount of data.
539 .It Fn m_getm orig len how type
546 if necessary and append the resulting allocated
552 .No non- Ns Dv NULL .
553 If the allocation fails at any point,
554 free whatever was allocated and return
559 .No non- Ns Dv NULL ,
560 it will not be freed.
561 It is possible to use
569 (for example, one which may be sitting in a pre-allocated ring)
570 or to simply perform an all-or-nothing
575 .It Fn m_gethdr how type
576 A function version of
578 for non-critical paths.
579 .It Fn m_getcl how type flags
585 If one of the allocations fails, the entire allocation fails.
586 This routine is the preferred way of fetching both the
590 together, as it avoids having to unlock/relock between allocations.
594 .It Fn m_getjcl how type flags size
597 but it the size of the cluster allocated will be large enough for
600 .It Fn m_getclr how type
603 and zero out the data region.
613 The functions below operate on
615 .Bl -ohang -offset indent
619 including any external storage.
621 .It Fn m_adj mbuf len
624 bytes from the head of an
628 is positive, from the tail otherwise.
630 .It Fn m_append mbuf len cp
637 Extend the mbuf chain if the new data does not fit in
640 .It Fn m_prepend mbuf len how
643 and prepend it to the
649 It does not allocate any
661 .It Fn m_copyup mbuf len dstoff
666 bytes of data into a new mbuf at
671 argument aligns the data and leaves room for a link layer header.
681 The function does not allocate
688 .It Fn m_pullup mbuf len
689 Arrange that the first
693 are contiguous and lay in the data area of
695 so they are accessible with
697 It is important to remember that this may involve
698 reallocating some mbufs and moving data so all pointers
699 referencing data within the old mbuf chain
700 must be recalculated or made invalid.
708 is freed in this case).
710 It does not allocate any
714 must be less than or equal to
717 .It Fn m_pulldown mbuf offset len offsetp
726 are contiguous and lay in the data area of
728 so they are accessible with
731 must be smaller than, or equal to, the size of an
733 Return a pointer to an intermediate
735 in the chain containing the requested region;
736 the offset in the data region of the
738 to the data contained in the returned mbuf is stored in
742 is NULL, the region may be accessed using
746 is non-NULL, the region may be accessed using
747 .Fn mtod mbuf uint8_t + *offsetp .
748 The region of the mbuf chain between its beginning and
750 is not modified, therefore it is safe to hold pointers to data within
751 this region before calling
754 .It Fn m_copym mbuf offset len how
759 bytes from the beginning, continuing for
766 copy to the end of the
769 The copy is read-only, because the
771 are not copied, only their reference counts are incremented.
773 .It Fn m_copypacket mbuf how
774 Copy an entire packet including header, which must be present.
775 This is an optimized version of the common case
776 .Fn m_copym mbuf 0 M_COPYALL how .
778 the copy is read-only, because the
780 are not copied, only their reference counts are incremented.
782 .It Fn m_dup mbuf how
785 into a completely new
787 including copying any
791 when you need a writable copy of an
794 .It Fn m_copydata mbuf offset len buf
799 bytes from the beginning, continuing for
801 bytes, into the indicated buffer
804 .It Fn m_copyback mbuf offset len buf
807 bytes from the buffer
809 back into the indicated
813 bytes from the beginning of the
819 It does not allocate any
831 will be allocated to fill the space.
833 .It Fn m_length mbuf last
834 Return the length of the
836 and optionally a pointer to the last
839 .It Fn m_dup_pkthdr to from how
840 Upon the function's completion, the
843 will contain an identical copy of
845 and the per-packet attributes found in the
855 must be empty on entry.
857 .It Fn m_move_pkthdr to from
860 and the per-packet attributes from the
873 must be empty on entry.
874 Upon the function's completion,
878 and the per-packet attributes cleared.
881 Set the packet-header length to the length of the
884 .It Fn m_devget buf len offset ifp copy
885 Copy data from a device local memory pointed to by
889 The copy is done using a specified copy routine
905 must be of the same type.
907 is still valid after the function returned.
913 .It Fn m_split mbuf len how
916 in two pieces, returning the tail:
920 In case of failure, it returns
922 and attempts to restore the
924 to its original state.
926 .It Fn m_apply mbuf off len f arg
927 Apply a function to an
934 Typically used to avoid calls to
936 which would otherwise be unnecessary or undesirable.
938 is a convenience argument which is passed to the callback function
943 is called, it will be passed
947 in the current mbuf, and the length
949 of the data in this mbuf to which the function should be applied.
951 The function should return zero to indicate success;
952 otherwise, if an error is indicated, then
954 will return the error and stop iterating through the
957 .It Fn m_getptr mbuf loc off
958 Return a pointer to the mbuf containing the data located at
960 bytes from the beginning of the
962 The corresponding offset into the mbuf will be stored in
964 .It Fn m_defrag m0 how
965 Defragment an mbuf chain, returning the shortest possible
966 chain of mbufs and clusters.
967 If allocation fails and this can not be completed,
969 will be returned and the original chain will be unchanged.
970 Upon success, the original chain will be freed and the new
971 chain will be returned.
977 depending on the caller's preference.
979 This function is especially useful in network drivers, where
980 certain long mbuf chains must be shortened before being added
981 to TX descriptor lists.
982 .It Fn m_unshare m0 how
983 Create a version of the specified mbuf chain whose
984 contents can be safely modified without affecting other users.
985 If allocation fails and this operation can not be completed,
988 The original mbuf chain is always reclaimed and the reference
989 count of any shared mbuf clusters is decremented.
995 depending on the caller's preference.
996 As a side-effect of this process the returned
997 mbuf chain may be compacted.
999 This function is especially useful in the transmit path of
1000 network code, when data must be encrypted or otherwise
1001 altered prior to transmission.
1003 .Sh HARDWARE-ASSISTED CHECKSUM CALCULATION
1004 This section currently applies to TCP/IP only.
1005 In order to save the host CPU resources, computing checksums is
1006 offloaded to the network interface hardware if possible.
1009 member of the leading
1011 of a packet contains two fields used for that purpose,
1012 .Vt int Va csum_flags
1014 .Vt int Va csum_data .
1015 The meaning of those fields depends on the direction a packet flows in,
1016 and on whether the packet is fragmented.
1022 will denote the corresponding field of the
1024 member of the leading
1028 containing the packet.
1030 On output, checksum offloading is attempted after the outgoing
1031 interface has been determined for a packet.
1032 The interface-specific field
1033 .Va ifnet.if_data.ifi_hwassist
1036 is consulted for the capabilities of the interface to assist in
1037 computing checksums.
1040 field of the packet header is set to indicate which actions the interface
1041 is supposed to perform on it.
1042 The actions unsupported by the network interface are done in the
1043 software prior to passing the packet down to the interface driver;
1044 such actions will never be requested through
1047 The flags demanding a particular action from an interface are as follows:
1048 .Bl -tag -width ".Dv CSUM_TCP" -offset indent
1050 The IP header checksum is to be computed and stored in the
1051 corresponding field of the packet.
1052 The hardware is expected to know the format of an IP header
1053 to determine the offset of the IP checksum field.
1055 The TCP checksum is to be computed.
1058 The UDP checksum is to be computed.
1062 Should a TCP or UDP checksum be offloaded to the hardware,
1065 will contain the byte offset of the checksum field relative to the
1066 end of the IP header.
1067 In this case, the checksum field will be initially
1068 set by the TCP/IP module to the checksum of the pseudo header
1069 defined by the TCP and UDP specifications.
1071 On input, an interface indicates the actions it has performed
1072 on a packet by setting one or more of the following flags in
1074 associated with the packet:
1075 .Bl -tag -width ".Dv CSUM_IP_CHECKED" -offset indent
1076 .It Dv CSUM_IP_CHECKED
1077 The IP header checksum has been computed.
1078 .It Dv CSUM_IP_VALID
1079 The IP header has a valid checksum.
1080 This flag can appear only in combination with
1081 .Dv CSUM_IP_CHECKED .
1082 .It Dv CSUM_DATA_VALID
1083 The checksum of the data portion of the IP packet has been computed
1084 and stored in the field
1086 in network byte order.
1087 .It Dv CSUM_PSEUDO_HDR
1088 Can be set only along with
1090 to indicate that the IP data checksum found in
1092 allows for the pseudo header defined by the TCP and UDP specifications.
1093 Otherwise the checksum of the pseudo header must be calculated by
1094 the host CPU and added to
1096 to obtain the final checksum to be used for TCP or UDP validation purposes.
1099 If a particular network interface just indicates success or
1100 failure of TCP or UDP checksum validation without returning
1101 the exact value of the checksum to the host CPU, its driver can mark
1111 hexadecimal to indicate a valid checksum.
1112 It is a peculiarity of the algorithm used that the Internet checksum
1113 calculated over any valid packet will be
1115 as long as the original checksum field is included.
1117 When running a kernel compiled with the option
1118 .Dv MBUF_STRESS_TEST ,
1121 -controlled options may be used to create
1122 various failure/extreme cases for testing of network drivers
1123 and other parts of the kernel that rely on
1125 .Bl -tag -width ident
1126 .It Va net.inet.ip.mbuf_frag_size
1129 to fragment outgoing
1131 into fragments of the specified size.
1132 Setting this variable to 1 is an excellent way to
1135 handling ability of network drivers.
1136 .It Va kern.ipc.m_defragrandomfailures
1139 to randomly fail, returning
1141 Any piece of code which uses
1143 should be tested with this feature.
1151 .\" Please correct me if I'm wrong
1153 appeared in an early version of
1155 Besides being used for network packets, they were used
1156 to store various dynamic structures, such as routing table
1157 entries, interface addresses, protocol control blocks, etc.
1162 is almost entirely limited to packet storage, with
1164 zones being used directly to store other network-related memory.
1168 allocator has been a special-purpose memory allocator able to run in
1169 interrupt contexts and allocating from a special kernel address space map.
1174 allocator is a wrapper around
1180 + cluster pairs in per-CPU caches, as well as bringing other benefits of
1185 manual page was written by Yar Tikhiy.
1189 allocator was written by Bosko Milekic.