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_args)"
53 .Fn MEXTFREE "struct mbuf *mbuf"
54 .Fn MEXT_ADD_REF "struct mbuf *mbuf"
55 .Fn MEXT_REM_REF "struct mbuf *mbuf"
56 .Fn MFREE "struct mbuf *mbuf" "struct mbuf *successor"
58 .Ss Mbuf utility macros
59 .Fn mtod "struct mbuf *mbuf" "type"
61 .Fn MEXT_IS_REF "struct mbuf *mbuf"
62 .Fn M_ALIGN "struct mbuf *mbuf" "u_int len"
63 .Fn MH_ALIGN "struct mbuf *mbuf" "u_int len"
65 .Fn M_LEADINGSPACE "struct mbuf *mbuf"
67 .Fn M_TRAILINGSPACE "struct mbuf *mbuf"
68 .Fn M_MOVE_PKTHDR "struct mbuf *to" "struct mbuf *from"
69 .Fn M_PREPEND "struct mbuf *mbuf" "int len" "int how"
70 .Fn MCHTYPE "struct mbuf *mbuf" "u_int type"
72 .Fn M_WRITABLE "struct mbuf *mbuf"
74 .Ss Mbuf allocation functions
76 .Fn m_get "int how" "int type"
78 .Fn m_getm "struct mbuf *orig" "int len" "int how" "int type"
80 .Fn m_getcl "int how" "short type" "int flags"
82 .Fn m_getclr "int how" "int type"
84 .Fn m_gethdr "int how" "int 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_append "struct mbuf *mbuf" "int len" "c_caddr_t cp"
96 .Fn m_prepend "struct mbuf *mbuf" "int len" "int how"
98 .Fn m_copyup "struct mbuf *mbuf" "int len" "int dstoff"
100 .Fn m_pullup "struct mbuf *mbuf" "int len"
102 .Fn m_copym "struct mbuf *mbuf" "int offset" "int len" "int how"
104 .Fn m_copypacket "struct mbuf *mbuf" "int how"
106 .Fn m_dup "struct mbuf *mbuf" "int how"
108 .Fn m_copydata "const struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
110 .Fn m_copyback "struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
116 .Fa "struct ifnet *ifp"
117 .Fa "void (*copy)(char *from, caddr_t to, u_int len)"
120 .Fn m_cat "struct mbuf *m" "struct mbuf *n"
122 .Fn m_fixhdr "struct mbuf *mbuf"
124 .Fn m_dup_pkthdr "struct mbuf *to" "struct mbuf *from"
126 .Fn m_move_pkthdr "struct mbuf *to" "struct mbuf *from"
128 .Fn m_length "struct mbuf *mbuf" "struct mbuf **last"
130 .Fn m_split "struct mbuf *mbuf" "int len" "int how"
132 .Fn m_apply "struct mbuf *mbuf" "int off" "int len" "int (*f)(void *arg, void *data, u_int len)" "void *arg"
134 .Fn m_getptr "struct mbuf *mbuf" "int loc" "int *off"
136 .Fn m_defrag "struct mbuf *m0" "int how"
141 is a basic unit of memory management in the kernel IPC subsystem.
142 Network packets and socket buffers are stored in
144 A network packet may span multiple
149 which allows adding or trimming
150 network headers with little overhead.
152 While a developer should not bother with
154 internals without serious
155 reason in order to avoid incompatibilities with future changes, it
156 is useful to understand the general structure of an
161 consists of a variable-sized header and a small internal
166 is a constant defined in
172 .Bl -tag -width "m_nextpkt" -offset indent
175 A pointer to the next
181 A pointer to the next
186 A pointer to data attached to this
190 The length of the data.
193 The type of the data.
203 flag bits are defined as follows:
206 #define M_EXT 0x0001 /* has associated external storage */
207 #define M_PKTHDR 0x0002 /* start of record */
208 #define M_EOR 0x0004 /* end of record */
209 #define M_RDONLY 0x0008 /* associated data marked read-only */
210 #define M_PROTO1 0x0010 /* protocol-specific */
211 #define M_PROTO2 0x0020 /* protocol-specific */
212 #define M_PROTO3 0x0040 /* protocol-specific */
213 #define M_PROTO4 0x0080 /* protocol-specific */
214 #define M_PROTO5 0x0100 /* protocol-specific */
215 #define M_PROTO6 0x4000 /* protocol-specific (avoid M_BCAST conflict) */
216 #define M_FREELIST 0x8000 /* mbuf is on the free list */
218 /* mbuf pkthdr flags (also stored in m_flags) */
219 #define M_BCAST 0x0200 /* send/received as link-level broadcast */
220 #define M_MCAST 0x0400 /* send/received as link-level multicast */
221 #define M_FRAG 0x0800 /* packet is fragment of larger packet */
222 #define M_FIRSTFRAG 0x1000 /* packet is first fragment */
223 #define M_LASTFRAG 0x2000 /* packet is last fragment */
228 types are defined as follows:
231 #define MT_DATA 1 /* dynamic (data) allocation */
232 #define MT_HEADER 2 /* packet header */
233 #define MT_SONAME 8 /* socket name */
234 #define MT_FTABLE 11 /* fragment reassembly header */
235 #define MT_CONTROL 14 /* extra-data protocol message */
236 #define MT_OOBDATA 15 /* expedited data */
242 .Vt struct pkthdr Va m_pkthdr
246 It contains a pointer to the interface
247 the packet has been received from
248 .Pq Vt struct ifnet Va *rcvif ,
249 and the total packet length
251 Optionally, it may also contain an attached list of packet tags
252 .Pq Vt "struct m_tag" .
256 Fields used in offloading checksum calculation to the hardware are kept in
260 .Sx HARDWARE-ASSISTED CHECKSUM CALCULATION
263 If small enough, data is stored in the internal data buffer of an
265 If the data is sufficiently large, another
269 or external storage may be associated with the
272 bytes of data can fit into an
280 If external storage is being associated with an
284 header is added at the cost of losing the internal data buffer.
285 It includes a pointer to external storage, the size of the storage,
286 a pointer to a function used for freeing the storage,
287 a pointer to an optional argument that can be passed to the function,
288 and a pointer to a reference counter.
291 using external storage has the
295 The system supplies a macro for allocating the desired external storage
299 The allocation and management of the reference counter is handled by the
301 The developer can check whether the reference count for the
302 external storage of a given
304 is greater than 1 with the
307 Similarly, the developer can directly add and remove references,
308 if absolutely necessary, with the use of the
314 The system also supplies a default type of external storage buffer called an
317 can be allocated and configured with the use of the
324 in size, where MCLBYTES is a machine-dependent constant.
325 The system defines an advisory macro
327 which is the smallest amount of data to put into an
329 It is equal to the sum of
333 It is typically preferable to store data into the data region of an
335 if size permits, as opposed to allocating a separate
337 to hold the same data.
339 .Ss Macros and Functions
340 There are numerous predefined macros and functions that provide the
341 developer with common utilities.
343 .Bl -ohang -offset indent
344 .It Fn mtod mbuf type
347 pointer to a data pointer.
348 The macro expands to the data pointer cast to the pointer of the specified
351 It is advisable to ensure that there is enough contiguous data in
356 .It Fn MGET mbuf how type
359 and initialize it to contain internal data.
361 will point to the allocated
363 on success, or be set to
368 argument is to be set to
372 It specifies whether the caller is willing to block if necessary.
377 a failed allocation will result in the caller being put
378 to sleep for a designated
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
389 Programmers should be careful not to confuse the
397 They are not the same.
398 .It Fn MGETHDR mbuf how type
401 and initialize it to contain a packet header
406 .It Fn MCLGET mbuf how
407 Allocate and attach an
411 If the macro fails, the
413 flag will not be set in
415 .It Fn M_ALIGN mbuf len
418 to place an object of the size
420 at the end of the internal data area of
425 is newly allocated with
429 .It Fn MH_ALIGN mbuf len
430 Serves the same purpose as
442 .It Fn M_LEADINGSPACE mbuf
443 Returns the number of bytes available before the beginning
446 .It Fn M_TRAILINGSPACE mbuf
447 Returns the number of bytes available after the end of data in
449 .It Fn M_PREPEND mbuf len how
450 This macro operates on an
452 It is an optimized wrapper for
454 that can make use of possible empty space before data
455 (e.g.\& left after trimming of a link-layer header).
463 .It Fn M_MOVE_PKTHDR to from
464 Using this macro is equivalent to calling
465 .Fn m_move_pkthdr to from .
466 .It Fn M_WRITABLE mbuf
467 This macro will evaluate true if
473 does not contain external storage or,
475 then if the reference count of the storage is not greater than 1.
480 This can be achieved during setup of the external storage,
487 macro, or can be directly set in individual
489 .It Fn MCHTYPE mbuf type
494 This is a relatively expensive operation and should be avoided.
498 .Bl -ohang -offset indent
499 .It Fn m_get how type
500 A function version of
502 for non-critical paths.
503 .It Fn m_getm orig len how type
510 if necessary and append the resulting allocated
516 .No non- Ns Dv NULL .
517 If the allocation fails at any point,
518 free whatever was allocated and return
523 .No non- Ns Dv NULL ,
524 it will not be freed.
525 It is possible to use
533 (for example, one which may be sitting in a pre-allocated ring)
534 or to simply perform an all-or-nothing
539 .It Fn m_gethdr how type
540 A function version of
542 for non-critical paths.
543 .It Fn m_getcl how type flags
549 If one of the allocations fails, the entire allocation fails.
550 This routine is the preferred way of fetching both the
554 together, as it avoids having to unlock/relock between allocations.
558 .It Fn m_getclr how type
561 and zero out the data region.
571 The functions below operate on
573 .Bl -ohang -offset indent
577 including any external storage.
579 .It Fn m_adj mbuf len
582 bytes from the head of an
586 is positive, from the tail otherwise.
588 .It Fn m_append mbuf len cp
595 Extend the mbuf chain if the new data does not fit in
598 .It Fn m_prepend mbuf len how
601 and prepend it to the
607 It does not allocate any
619 .It Fn m_copyup mbuf len dstoff
624 bytes of data into a new mbuf at
629 argument aligns the data and leaves room for a link layer header.
639 The function does not allocate
646 .It Fn m_pullup mbuf len
647 Arrange that the first
651 are contiguous and lay in the data area of
653 so they are accessible with
662 is freed in this case).
664 It does not allocate any
671 .It Fn m_copym mbuf offset len how
676 bytes from the beginning, continuing for
683 copy to the end of the
686 The copy is read-only, because the
688 are not copied, only their reference counts are incremented.
690 .It Fn m_copypacket mbuf how
691 Copy an entire packet including header, which must be present.
692 This is an optimized version of the common case
693 .Fn m_copym mbuf 0 M_COPYALL how .
695 the copy is read-only, because the
697 are not copied, only their reference counts are incremented.
699 .It Fn m_dup mbuf how
702 into a completely new
704 including copying any
708 when you need a writable copy of an
711 .It Fn m_copydata mbuf offset len buf
716 bytes from the beginning, continuing for
718 bytes, into the indicated buffer
721 .It Fn m_copyback mbuf offset len buf
724 bytes from the buffer
726 back into the indicated
730 bytes from the beginning of the
736 It does not allocate any
748 will be allocated to fill the space.
750 .It Fn m_length mbuf last
751 Return the length of the
753 and optionally a pointer to the last
756 .It Fn m_dup_pkthdr to from how
757 Upon the function's completion, the
760 will contain an identical copy of
762 and the per-packet attributes found in the
772 must be empty on entry.
774 .It Fn m_move_pkthdr to from
777 and the per-packet attributes from the
790 must be empty on entry.
791 Upon the function's completion,
795 and the per-packet attributes cleared.
798 Set the packet-header length to the length of the
801 .It Fn m_devget buf len offset ifp copy
802 Copy data from a device local memory pointed to by
806 The copy is done using a specified copy routine
822 must be of the same type.
824 is still valid after the function returned.
830 .It Fn m_split mbuf len how
833 in two pieces, returning the tail:
837 In case of failure, it returns
839 and attempts to restore the
841 to its original state.
843 .It Fn m_apply mbuf off len f arg
844 Apply a function to an
851 Typically used to avoid calls to
853 which would otherwise be unnecessary or undesirable.
855 is a convenience argument which is passed to the callback function
860 is called, it will be passed
864 in the current mbuf, and the length
866 of the data in this mbuf to which the function should be applied.
868 The function should return zero to indicate success;
869 otherwise, if an error is indicated, then
871 will return the error and stop iterating through the
874 .It Fn m_getptr mbuf loc off
875 Return a pointer to the mbuf containing the data located at
877 bytes from the beginning of the
879 The corresponding offset into the mbuf will be stored in
881 .It Fn m_defrag m0 how
882 Defragment an mbuf chain, returning the shortest possible
883 chain of mbufs and clusters.
884 If allocation fails and this can not be completed,
886 will be returned and the original chain will be unchanged.
887 Upon success, the original chain will be freed and the new
888 chain will be returned.
894 depending on the caller's preference.
896 This function is especially useful in network drivers, where
897 certain long mbuf chains must be shortened before being added
898 to TX descriptor lists.
900 .Sh HARDWARE-ASSISTED CHECKSUM CALCULATION
901 This section currently applies to TCP/IP only.
902 In order to save the host CPU resources, computing checksums is
903 offloaded to the network interface hardware if possible.
906 member of the leading
908 of a packet contains two fields used for that purpose,
909 .Vt int Va csum_flags
911 .Vt int Va csum_data .
912 The meaning of those fields depends on the direction a packet flows in,
913 and on whether the packet is fragmented.
919 will denote the corresponding field of the
921 member of the leading
925 containing the packet.
927 On output, checksum offloading is attempted after the outgoing
928 interface has been determined for a packet.
929 The interface-specific field
930 .Va ifnet.if_data.ifi_hwassist
933 is consulted for the capabilities of the interface to assist in
937 field of the packet header is set to indicate which actions the interface
938 is supposed to perform on it.
939 The actions unsupported by the network interface are done in the
940 software prior to passing the packet down to the interface driver;
941 such actions will never be requested through
944 The flags demanding a particular action from an interface are as follows:
945 .Bl -tag -width ".Dv CSUM_TCP" -offset indent
947 The IP header checksum is to be computed and stored in the
948 corresponding field of the packet.
949 The hardware is expected to know the format of an IP header
950 to determine the offset of the IP checksum field.
952 The TCP checksum is to be computed.
955 The UDP checksum is to be computed.
959 Should a TCP or UDP checksum be offloaded to the hardware,
962 will contain the byte offset of the checksum field relative to the
963 end of the IP header.
964 In this case, the checksum field will be initially
965 set by the TCP/IP module to the checksum of the pseudo header
966 defined by the TCP and UDP specifications.
968 For outbound packets which have been fragmented
969 by the host CPU, the following will also be true,
970 regardless of the checksum flag settings:
971 .Bl -bullet -offset indent
973 all fragments will have the flag
979 the first and the last fragments in the chain will have
987 the first fragment in the chain will have the total number
988 of fragments contained in its
993 The last rule for fragmented packets takes precedence over the one
994 for a TCP or UDP checksum.
995 Nevertheless, offloading a TCP or UDP checksum is possible for a
996 fragmented packet if the flag
999 .Va ifnet.if_data.ifi_hwassist
1000 associated with the network interface.
1001 However, in this case the interface is expected to figure out
1002 the location of the checksum field within the sequence of fragments
1005 contains a fragment count instead of a checksum offset value.
1007 On input, an interface indicates the actions it has performed
1008 on a packet by setting one or more of the following flags in
1010 associated with the packet:
1011 .Bl -tag -width ".Dv CSUM_IP_CHECKED" -offset indent
1012 .It Dv CSUM_IP_CHECKED
1013 The IP header checksum has been computed.
1014 .It Dv CSUM_IP_VALID
1015 The IP header has a valid checksum.
1016 This flag can appear only in combination with
1017 .Dv CSUM_IP_CHECKED .
1018 .It Dv CSUM_DATA_VALID
1019 The checksum of the data portion of the IP packet has been computed
1020 and stored in the field
1022 in network byte order.
1023 .It Dv CSUM_PSEUDO_HDR
1024 Can be set only along with
1026 to indicate that the IP data checksum found in
1028 allows for the pseudo header defined by the TCP and UDP specifications.
1029 Otherwise the checksum of the pseudo header must be calculated by
1030 the host CPU and added to
1032 to obtain the final checksum to be used for TCP or UDP validation purposes.
1035 If a particular network interface just indicates success or
1036 failure of TCP or UDP checksum validation without returning
1037 the exact value of the checksum to the host CPU, its driver can mark
1047 hexadecimal to indicate a valid checksum.
1048 It is a peculiarity of the algorithm used that the Internet checksum
1049 calculated over any valid packet will be
1051 as long as the original checksum field is included.
1053 For inbound packets which are IP fragments, all
1055 fields will be summed during reassembly to obtain the final checksum
1056 value passed to an upper layer in the
1058 field of the reassembled packet.
1061 fields of all fragments will be consolidated using logical AND
1062 to obtain the final value for
1064 Thus, in order to successfully
1065 offload checksum computation for fragmented data,
1066 all fragments should have the same value of
1069 When running a kernel compiled with the option
1070 .Dv MBUF_STRESS_TEST ,
1073 -controlled options may be used to create
1074 various failure/extreme cases for testing of network drivers
1075 and other parts of the kernel that rely on
1077 .Bl -tag -width ident
1078 .It Va net.inet.ip.mbuf_frag_size
1081 to fragment outgoing
1083 into fragments of the specified size.
1084 Setting this variable to 1 is an excellent way to
1087 handling ability of network drivers.
1088 .It Va kern.ipc.m_defragrandomfailures
1091 to randomly fail, returning
1093 Any piece of code which uses
1095 should be tested with this feature.
1103 .\" Please correct me if I'm wrong
1105 appeared in an early version of
1107 Besides being used for network packets, they were used
1108 to store various dynamic structures, such as routing table
1109 entries, interface addresses, protocol control blocks, etc.
1113 man page was written by Yar Tikhiy.