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" "u_int type"
69 .Fn M_WRITABLE "struct mbuf *mbuf"
71 .Ss Mbuf allocation functions
73 .Fn m_get "int how" "int type"
75 .Fn m_getm "struct mbuf *orig" "int len" "int how" "int type"
77 .Fn m_getcl "int how" "short type" "int flags"
79 .Fn m_getclr "int how" "int type"
81 .Fn m_gethdr "int how" "int type"
83 .Fn m_free "struct mbuf *mbuf"
85 .Fn m_freem "struct mbuf *mbuf"
87 .Ss Mbuf utility functions
89 .Fn m_adj "struct mbuf *mbuf" "int len"
91 .Fn m_align "struct mbuf *mbuf" "int len"
93 .Fn m_append "struct mbuf *mbuf" "int len" "c_caddr_t cp"
95 .Fn m_prepend "struct mbuf *mbuf" "int len" "int how"
97 .Fn m_copyup "struct mbuf *mbuf" "int len" "int dstoff"
99 .Fn m_pullup "struct mbuf *mbuf" "int len"
101 .Fn m_pulldown "struct mbuf *mbuf" "int offset" "int len" "int *offsetp"
103 .Fn m_copym "struct mbuf *mbuf" "int offset" "int len" "int how"
105 .Fn m_copypacket "struct mbuf *mbuf" "int how"
107 .Fn m_dup "struct mbuf *mbuf" "int how"
109 .Fn m_copydata "const struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
111 .Fn m_copyback "struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
117 .Fa "struct ifnet *ifp"
118 .Fa "void (*copy)(char *from, caddr_t to, u_int len)"
121 .Fn m_cat "struct mbuf *m" "struct mbuf *n"
123 .Fn m_fixhdr "struct mbuf *mbuf"
125 .Fn m_dup_pkthdr "struct mbuf *to" "struct mbuf *from"
127 .Fn m_move_pkthdr "struct mbuf *to" "struct mbuf *from"
129 .Fn m_length "struct mbuf *mbuf" "struct mbuf **last"
131 .Fn m_split "struct mbuf *mbuf" "int len" "int how"
133 .Fn m_apply "struct mbuf *mbuf" "int off" "int len" "int (*f)(void *arg, void *data, u_int len)" "void *arg"
135 .Fn m_getptr "struct mbuf *mbuf" "int loc" "int *off"
137 .Fn m_defrag "struct mbuf *m0" "int how"
139 .Fn m_unshare "struct mbuf *m0" "int how"
144 is a basic unit of memory management in the kernel IPC subsystem.
145 Network packets and socket buffers are stored in
147 A network packet may span multiple
152 which allows adding or trimming
153 network headers with little overhead.
155 While a developer should not bother with
157 internals without serious
158 reason in order to avoid incompatibilities with future changes, it
159 is useful to understand the general structure of an
164 consists of a variable-sized header and a small internal
169 is a constant defined in
175 .Bl -tag -width "m_nextpkt" -offset indent
178 A pointer to the next
184 A pointer to the next
189 A pointer to data attached to this
193 The length of the data.
196 The type of the data.
206 flag bits are defined as follows:
209 #define M_EXT 0x0001 /* has associated external storage */
210 #define M_PKTHDR 0x0002 /* start of record */
211 #define M_EOR 0x0004 /* end of record */
212 #define M_RDONLY 0x0008 /* associated data marked read-only */
213 #define M_PROTO1 0x0010 /* protocol-specific */
214 #define M_PROTO2 0x0020 /* protocol-specific */
215 #define M_PROTO3 0x0040 /* protocol-specific */
216 #define M_PROTO4 0x0080 /* protocol-specific */
217 #define M_PROTO5 0x0100 /* protocol-specific */
218 #define M_PROTO6 0x4000 /* protocol-specific (avoid M_BCAST conflict) */
219 #define M_FREELIST 0x8000 /* mbuf is on the free list */
221 /* mbuf pkthdr flags (also stored in m_flags) */
222 #define M_BCAST 0x0200 /* send/received as link-level broadcast */
223 #define M_MCAST 0x0400 /* send/received as link-level multicast */
224 #define M_FRAG 0x0800 /* packet is fragment of larger packet */
225 #define M_FIRSTFRAG 0x1000 /* packet is first fragment */
226 #define M_LASTFRAG 0x2000 /* packet is last fragment */
231 types are defined as follows:
234 #define MT_DATA 1 /* dynamic (data) allocation */
235 #define MT_HEADER MT_DATA /* packet header */
236 #define MT_SONAME 8 /* socket name */
237 #define MT_CONTROL 14 /* extra-data protocol message */
238 #define MT_OOBDATA 15 /* expedited data */
244 .Vt struct pkthdr Va m_pkthdr
248 It contains a pointer to the interface
249 the packet has been received from
250 .Pq Vt struct ifnet Va *rcvif ,
251 and the total packet length
253 Optionally, it may also contain an attached list of packet tags
254 .Pq Vt "struct m_tag" .
258 Fields used in offloading checksum calculation to the hardware are kept in
262 .Sx HARDWARE-ASSISTED CHECKSUM CALCULATION
265 If small enough, data is stored in the internal data buffer of an
267 If the data is sufficiently large, another
271 or external storage may be associated with the
274 bytes of data can fit into an
282 If external storage is being associated with an
286 header is added at the cost of losing the internal data buffer.
287 It includes a pointer to external storage, the size of the storage,
288 a pointer to a function used for freeing the storage,
289 a pointer to an optional argument that can be passed to the function,
290 and a pointer to a reference counter.
293 using external storage has the
297 The system supplies a macro for allocating the desired external storage
301 The allocation and management of the reference counter is handled by the
304 The system also supplies a default type of external storage buffer called an
307 can be allocated and configured with the use of the
314 in size, where MCLBYTES is a machine-dependent constant.
315 The system defines an advisory macro
317 which is the smallest amount of data to put into an
319 It is equal to the sum of
323 It is typically preferable to store data into the data region of an
325 if size permits, as opposed to allocating a separate
327 to hold the same data.
329 .Ss Macros and Functions
330 There are numerous predefined macros and functions that provide the
331 developer with common utilities.
333 .Bl -ohang -offset indent
334 .It Fn mtod mbuf type
337 pointer to a data pointer.
338 The macro expands to the data pointer cast to the pointer of the specified
341 It is advisable to ensure that there is enough contiguous data in
346 .It Fn MGET mbuf how type
349 and initialize it to contain internal data.
351 will point to the allocated
353 on success, or be set to
358 argument is to be set to
362 It specifies whether the caller is willing to block if necessary.
363 A number of other functions and macros related to
365 have the same argument because they may
366 at some point need to allocate new
373 section) used allocation flags
377 These constants are kept for compatibility
378 and their use in new code is discouraged.
379 .It Fn MGETHDR mbuf how type
382 and initialize it to contain a packet header
387 .It Fn MCLGET mbuf how
388 Allocate and attach an
392 If the macro fails, the
394 flag will not be set in
396 .It Fn M_ALIGN mbuf len
399 to place an object of the size
401 at the end of the internal data area of
406 is newly allocated with
410 .It Fn MH_ALIGN mbuf len
411 Serves the same purpose as
423 .It Fn m_align mbuf len
424 Services the same purpose as
426 but handles any type of mbuf.
427 .It Fn M_LEADINGSPACE mbuf
428 Returns the number of bytes available before the beginning
431 .It Fn M_TRAILINGSPACE mbuf
432 Returns the number of bytes available after the end of data in
434 .It Fn M_PREPEND mbuf len how
435 This macro operates on an
437 It is an optimized wrapper for
439 that can make use of possible empty space before data
440 (e.g.\& left after trimming of a link-layer header).
448 .It Fn M_MOVE_PKTHDR to from
449 Using this macro is equivalent to calling
450 .Fn m_move_pkthdr to from .
451 .It Fn M_WRITABLE mbuf
452 This macro will evaluate true if
458 does not contain external storage or,
460 then if the reference count of the storage is not greater than 1.
465 This can be achieved during setup of the external storage,
472 macro, or can be directly set in individual
474 .It Fn MCHTYPE mbuf type
479 This is a relatively expensive operation and should be avoided.
483 .Bl -ohang -offset indent
484 .It Fn m_get how type
485 A function version of
487 for non-critical paths.
488 .It Fn m_getm orig len how type
495 if necessary and append the resulting allocated
501 .No non- Ns Dv NULL .
502 If the allocation fails at any point,
503 free whatever was allocated and return
508 .No non- Ns Dv NULL ,
509 it will not be freed.
510 It is possible to use
518 (for example, one which may be sitting in a pre-allocated ring)
519 or to simply perform an all-or-nothing
524 .It Fn m_gethdr how type
525 A function version of
527 for non-critical paths.
528 .It Fn m_getcl how type flags
534 If one of the allocations fails, the entire allocation fails.
535 This routine is the preferred way of fetching both the
539 together, as it avoids having to unlock/relock between allocations.
543 .It Fn m_getclr how type
546 and zero out the data region.
556 The functions below operate on
558 .Bl -ohang -offset indent
562 including any external storage.
564 .It Fn m_adj mbuf len
567 bytes from the head of an
571 is positive, from the tail otherwise.
573 .It Fn m_append mbuf len cp
580 Extend the mbuf chain if the new data does not fit in
583 .It Fn m_prepend mbuf len how
586 and prepend it to the
592 It does not allocate any
604 .It Fn m_copyup mbuf len dstoff
609 bytes of data into a new mbuf at
614 argument aligns the data and leaves room for a link layer header.
624 The function does not allocate
631 .It Fn m_pullup mbuf len
632 Arrange that the first
636 are contiguous and lay in the data area of
638 so they are accessible with
640 It is important to remember that this may involve
641 reallocating some mbufs and moving data so all pointers
642 referencing data within the old mbuf chain
643 must be recalculated or made invalid.
651 is freed in this case).
653 It does not allocate any
660 .It Fn m_pulldown mbuf offset len offsetp
669 are contiguous and lay in the data area of
671 so they are accessible with
674 must be smaller than, or equal to, the size of an
676 Return a pointer to an intermediate
678 in the chain containing the requested region;
679 the offset in the data region of the
681 to the data contained in the returned mbuf is stored in
685 is NULL, the region may be accessed using
689 is non-NULL, the region may be accessed using
690 .Fn mtod mbuf uint8_t + *offsetp .
691 The region of the mbuf chain between its beginning and
693 is not modified, therefore it is safe to hold pointers to data within
694 this region before calling
697 .It Fn m_copym mbuf offset len how
702 bytes from the beginning, continuing for
709 copy to the end of the
712 The copy is read-only, because the
714 are not copied, only their reference counts are incremented.
716 .It Fn m_copypacket mbuf how
717 Copy an entire packet including header, which must be present.
718 This is an optimized version of the common case
719 .Fn m_copym mbuf 0 M_COPYALL how .
721 the copy is read-only, because the
723 are not copied, only their reference counts are incremented.
725 .It Fn m_dup mbuf how
728 into a completely new
730 including copying any
734 when you need a writable copy of an
737 .It Fn m_copydata mbuf offset len buf
742 bytes from the beginning, continuing for
744 bytes, into the indicated buffer
747 .It Fn m_copyback mbuf offset len buf
750 bytes from the buffer
752 back into the indicated
756 bytes from the beginning of the
762 It does not allocate any
774 will be allocated to fill the space.
776 .It Fn m_length mbuf last
777 Return the length of the
779 and optionally a pointer to the last
782 .It Fn m_dup_pkthdr to from how
783 Upon the function's completion, the
786 will contain an identical copy of
788 and the per-packet attributes found in the
798 must be empty on entry.
800 .It Fn m_move_pkthdr to from
803 and the per-packet attributes from the
816 must be empty on entry.
817 Upon the function's completion,
821 and the per-packet attributes cleared.
824 Set the packet-header length to the length of the
827 .It Fn m_devget buf len offset ifp copy
828 Copy data from a device local memory pointed to by
832 The copy is done using a specified copy routine
848 must be of the same type.
850 is still valid after the function returned.
856 .It Fn m_split mbuf len how
859 in two pieces, returning the tail:
863 In case of failure, it returns
865 and attempts to restore the
867 to its original state.
869 .It Fn m_apply mbuf off len f arg
870 Apply a function to an
877 Typically used to avoid calls to
879 which would otherwise be unnecessary or undesirable.
881 is a convenience argument which is passed to the callback function
886 is called, it will be passed
890 in the current mbuf, and the length
892 of the data in this mbuf to which the function should be applied.
894 The function should return zero to indicate success;
895 otherwise, if an error is indicated, then
897 will return the error and stop iterating through the
900 .It Fn m_getptr mbuf loc off
901 Return a pointer to the mbuf containing the data located at
903 bytes from the beginning of the
905 The corresponding offset into the mbuf will be stored in
907 .It Fn m_defrag m0 how
908 Defragment an mbuf chain, returning the shortest possible
909 chain of mbufs and clusters.
910 If allocation fails and this can not be completed,
912 will be returned and the original chain will be unchanged.
913 Upon success, the original chain will be freed and the new
914 chain will be returned.
920 depending on the caller's preference.
922 This function is especially useful in network drivers, where
923 certain long mbuf chains must be shortened before being added
924 to TX descriptor lists.
925 .It Fn m_unshare m0 how
926 Create a version of the specified mbuf chain whose
927 contents can be safely modified without affecting other users.
928 If allocation fails and this operation can not be completed,
931 The original mbuf chain is always reclaimed and the reference
932 count of any shared mbuf clusters is decremented.
938 depending on the caller's preference.
939 As a side-effect of this process the returned
940 mbuf chain may be compacted.
942 This function is especially useful in the transmit path of
943 network code, when data must be encrypted or otherwise
944 altered prior to transmission.
946 .Sh HARDWARE-ASSISTED CHECKSUM CALCULATION
947 This section currently applies to TCP/IP only.
948 In order to save the host CPU resources, computing checksums is
949 offloaded to the network interface hardware if possible.
952 member of the leading
954 of a packet contains two fields used for that purpose,
955 .Vt int Va csum_flags
957 .Vt int Va csum_data .
958 The meaning of those fields depends on the direction a packet flows in,
959 and on whether the packet is fragmented.
965 will denote the corresponding field of the
967 member of the leading
971 containing the packet.
973 On output, checksum offloading is attempted after the outgoing
974 interface has been determined for a packet.
975 The interface-specific field
976 .Va ifnet.if_data.ifi_hwassist
979 is consulted for the capabilities of the interface to assist in
983 field of the packet header is set to indicate which actions the interface
984 is supposed to perform on it.
985 The actions unsupported by the network interface are done in the
986 software prior to passing the packet down to the interface driver;
987 such actions will never be requested through
990 The flags demanding a particular action from an interface are as follows:
991 .Bl -tag -width ".Dv CSUM_TCP" -offset indent
993 The IP header checksum is to be computed and stored in the
994 corresponding field of the packet.
995 The hardware is expected to know the format of an IP header
996 to determine the offset of the IP checksum field.
998 The TCP checksum is to be computed.
1001 The UDP checksum is to be computed.
1005 Should a TCP or UDP checksum be offloaded to the hardware,
1008 will contain the byte offset of the checksum field relative to the
1009 end of the IP header.
1010 In this case, the checksum field will be initially
1011 set by the TCP/IP module to the checksum of the pseudo header
1012 defined by the TCP and UDP specifications.
1014 For outbound packets which have been fragmented
1015 by the host CPU, the following will also be true,
1016 regardless of the checksum flag settings:
1017 .Bl -bullet -offset indent
1019 all fragments will have the flag
1025 the first and the last fragments in the chain will have
1033 the first fragment in the chain will have the total number
1034 of fragments contained in its
1039 The last rule for fragmented packets takes precedence over the one
1040 for a TCP or UDP checksum.
1041 Nevertheless, offloading a TCP or UDP checksum is possible for a
1042 fragmented packet if the flag
1045 .Va ifnet.if_data.ifi_hwassist
1046 associated with the network interface.
1047 However, in this case the interface is expected to figure out
1048 the location of the checksum field within the sequence of fragments
1051 contains a fragment count instead of a checksum offset value.
1053 On input, an interface indicates the actions it has performed
1054 on a packet by setting one or more of the following flags in
1056 associated with the packet:
1057 .Bl -tag -width ".Dv CSUM_IP_CHECKED" -offset indent
1058 .It Dv CSUM_IP_CHECKED
1059 The IP header checksum has been computed.
1060 .It Dv CSUM_IP_VALID
1061 The IP header has a valid checksum.
1062 This flag can appear only in combination with
1063 .Dv CSUM_IP_CHECKED .
1064 .It Dv CSUM_DATA_VALID
1065 The checksum of the data portion of the IP packet has been computed
1066 and stored in the field
1068 in network byte order.
1069 .It Dv CSUM_PSEUDO_HDR
1070 Can be set only along with
1072 to indicate that the IP data checksum found in
1074 allows for the pseudo header defined by the TCP and UDP specifications.
1075 Otherwise the checksum of the pseudo header must be calculated by
1076 the host CPU and added to
1078 to obtain the final checksum to be used for TCP or UDP validation purposes.
1081 If a particular network interface just indicates success or
1082 failure of TCP or UDP checksum validation without returning
1083 the exact value of the checksum to the host CPU, its driver can mark
1093 hexadecimal to indicate a valid checksum.
1094 It is a peculiarity of the algorithm used that the Internet checksum
1095 calculated over any valid packet will be
1097 as long as the original checksum field is included.
1099 For inbound packets which are IP fragments, all
1101 fields will be summed during reassembly to obtain the final checksum
1102 value passed to an upper layer in the
1104 field of the reassembled packet.
1107 fields of all fragments will be consolidated using logical AND
1108 to obtain the final value for
1110 Thus, in order to successfully
1111 offload checksum computation for fragmented data,
1112 all fragments should have the same value of
1115 When running a kernel compiled with the option
1116 .Dv MBUF_STRESS_TEST ,
1119 -controlled options may be used to create
1120 various failure/extreme cases for testing of network drivers
1121 and other parts of the kernel that rely on
1123 .Bl -tag -width ident
1124 .It Va net.inet.ip.mbuf_frag_size
1127 to fragment outgoing
1129 into fragments of the specified size.
1130 Setting this variable to 1 is an excellent way to
1133 handling ability of network drivers.
1134 .It Va kern.ipc.m_defragrandomfailures
1137 to randomly fail, returning
1139 Any piece of code which uses
1141 should be tested with this feature.
1149 .\" Please correct me if I'm wrong
1151 appeared in an early version of
1153 Besides being used for network packets, they were used
1154 to store various dynamic structures, such as routing table
1155 entries, interface addresses, protocol control blocks, etc.
1160 is almost entirely limited to packet storage, with
1162 zones being used directly to store other network-related memory.
1166 allocator has been a special-purpose memory allocator able to run in
1167 interrupt contexts and allocating from a special kernel address space map.
1172 allocator is a wrapper around
1178 + cluster pairs in per-CPU caches, as well as bringing other benefits of
1183 manual page was written by Yar Tikhiy.
1187 allocator was written by Bosko Milekic.