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
174 .Bl -tag -width "m_nextpkt" -offset indent
177 A pointer to the next
183 A pointer to the next
188 A pointer to data attached to this
192 The length of the data.
195 The type of the data.
205 flag bits are defined as follows:
208 #define M_EXT 0x0001 /* has associated external storage */
209 #define M_PKTHDR 0x0002 /* start of record */
210 #define M_EOR 0x0004 /* end of record */
211 #define M_RDONLY 0x0008 /* associated data marked read-only */
212 #define M_PROTO1 0x0010 /* protocol-specific */
213 #define M_PROTO2 0x0020 /* protocol-specific */
214 #define M_PROTO3 0x0040 /* protocol-specific */
215 #define M_PROTO4 0x0080 /* protocol-specific */
216 #define M_PROTO5 0x0100 /* protocol-specific */
217 #define M_PROTO6 0x4000 /* protocol-specific (avoid M_BCAST conflict) */
218 #define M_FREELIST 0x8000 /* mbuf is on the free list */
220 /* mbuf pkthdr flags (also stored in m_flags) */
221 #define M_BCAST 0x0200 /* send/received as link-level broadcast */
222 #define M_MCAST 0x0400 /* send/received as link-level multicast */
223 #define M_FRAG 0x0800 /* packet is fragment of larger packet */
224 #define M_FIRSTFRAG 0x1000 /* packet is first fragment */
225 #define M_LASTFRAG 0x2000 /* packet is last fragment */
230 types are defined as follows:
233 #define MT_DATA 1 /* dynamic (data) allocation */
234 #define MT_HEADER MT_DATA /* packet header */
235 #define MT_SONAME 8 /* socket name */
236 #define MT_CONTROL 14 /* extra-data protocol message */
237 #define MT_OOBDATA 15 /* expedited data */
240 The available external buffer types are defined as follows:
242 /* external buffer types */
243 #define EXT_CLUSTER 1 /* mbuf cluster */
244 #define EXT_SFBUF 2 /* sendfile(2)'s sf_bufs */
245 #define EXT_JUMBOP 3 /* jumbo cluster 4096 bytes */
246 #define EXT_JUMBO9 4 /* jumbo cluster 9216 bytes */
247 #define EXT_JUMBO16 5 /* jumbo cluster 16184 bytes */
248 #define EXT_PACKET 6 /* mbuf+cluster from packet zone */
249 #define EXT_MBUF 7 /* external mbuf reference (M_IOVEC) */
250 #define EXT_NET_DRV 100 /* custom ext_buf provided by net driver(s) */
251 #define EXT_MOD_TYPE 200 /* custom module's ext_buf type */
252 #define EXT_DISPOSABLE 300 /* can throw this buffer away w/page flipping */
253 #define EXT_EXTREF 400 /* has externally maintained ref_cnt ptr */
259 .Vt struct pkthdr Va m_pkthdr
263 It contains a pointer to the interface
264 the packet has been received from
265 .Pq Vt struct ifnet Va *rcvif ,
266 and the total packet length
268 Optionally, it may also contain an attached list of packet tags
269 .Pq Vt "struct m_tag" .
273 Fields used in offloading checksum calculation to the hardware are kept in
277 .Sx HARDWARE-ASSISTED CHECKSUM CALCULATION
280 If small enough, data is stored in the internal data buffer of an
282 If the data is sufficiently large, another
286 or external storage may be associated with the
289 bytes of data can fit into an
297 If external storage is being associated with an
301 header is added at the cost of losing the internal data buffer.
302 It includes a pointer to external storage, the size of the storage,
303 a pointer to a function used for freeing the storage,
304 a pointer to an optional argument that can be passed to the function,
305 and a pointer to a reference counter.
308 using external storage has the
312 The system supplies a macro for allocating the desired external storage
316 The allocation and management of the reference counter is handled by the
319 The system also supplies a default type of external storage buffer called an
322 can be allocated and configured with the use of the
329 in size, where MCLBYTES is a machine-dependent constant.
330 The system defines an advisory macro
332 which is the smallest amount of data to put into an
334 It is equal to the sum of
338 It is typically preferable to store data into the data region of an
340 if size permits, as opposed to allocating a separate
342 to hold the same data.
344 .Ss Macros and Functions
345 There are numerous predefined macros and functions that provide the
346 developer with common utilities.
348 .Bl -ohang -offset indent
349 .It Fn mtod mbuf type
352 pointer to a data pointer.
353 The macro expands to the data pointer cast to the pointer of the specified
356 It is advisable to ensure that there is enough contiguous data in
361 .It Fn MGET mbuf how type
364 and initialize it to contain internal data.
366 will point to the allocated
368 on success, or be set to
373 argument is to be set to
377 It specifies whether the caller is willing to block if necessary.
378 A number of other functions and macros related to
380 have the same argument because they may
381 at some point need to allocate new
388 section) used allocation flags
392 These constants are kept for compatibility
393 and their use in new code is discouraged.
394 .It Fn MGETHDR mbuf how type
397 and initialize it to contain a packet header
402 .It Fn MEXTADD mbuf buf size free opt_arg1 opt_arg2 flags type
403 Associate externally managed data with
405 Any internal data contained in the mbuf will be discarded, and the
412 arguments are the address and length, respectively, of the data.
415 argument points to a function which will be called to free the data
416 when the mbuf is freed; it is only used if
424 arguments will be passed unmodified to
428 argument specifies additional
430 flags; it is not necessary to specify
434 argument specifies the type of external data, which controls how it
435 will be disposed of when the
438 In most cases, the correct value is
440 .It Fn MCLGET mbuf how
441 Allocate and attach an
445 If the macro fails, the
447 flag will not be set in
449 .It Fn M_ALIGN mbuf len
452 to place an object of the size
454 at the end of the internal data area of
459 is newly allocated with
463 .It Fn MH_ALIGN mbuf len
464 Serves the same purpose as
476 .It Fn m_align mbuf len
477 Services the same purpose as
479 but handles any type of mbuf.
480 .It Fn M_LEADINGSPACE mbuf
481 Returns the number of bytes available before the beginning
484 .It Fn M_TRAILINGSPACE mbuf
485 Returns the number of bytes available after the end of data in
487 .It Fn M_PREPEND mbuf len how
488 This macro operates on an
490 It is an optimized wrapper for
492 that can make use of possible empty space before data
493 (e.g.\& left after trimming of a link-layer header).
501 .It Fn M_MOVE_PKTHDR to from
502 Using this macro is equivalent to calling
503 .Fn m_move_pkthdr to from .
504 .It Fn M_WRITABLE mbuf
505 This macro will evaluate true if
511 does not contain external storage or,
513 then if the reference count of the storage is not greater than 1.
518 This can be achieved during setup of the external storage,
525 macro, or can be directly set in individual
527 .It Fn MCHTYPE mbuf type
532 This is a relatively expensive operation and should be avoided.
536 .Bl -ohang -offset indent
537 .It Fn m_get how type
538 A function version of
540 for non-critical paths.
541 .It Fn m_getm orig len how type
548 if necessary and append the resulting allocated
554 .No non- Ns Dv NULL .
555 If the allocation fails at any point,
556 free whatever was allocated and return
561 .No non- Ns Dv NULL ,
562 it will not be freed.
563 It is possible to use
571 (for example, one which may be sitting in a pre-allocated ring)
572 or to simply perform an all-or-nothing
577 .It Fn m_gethdr how type
578 A function version of
580 for non-critical paths.
581 .It Fn m_getcl how type flags
587 If one of the allocations fails, the entire allocation fails.
588 This routine is the preferred way of fetching both the
592 together, as it avoids having to unlock/relock between allocations.
596 .It Fn m_getclr how type
599 and zero out the data region.
609 The functions below operate on
611 .Bl -ohang -offset indent
615 including any external storage.
617 .It Fn m_adj mbuf len
620 bytes from the head of an
624 is positive, from the tail otherwise.
626 .It Fn m_append mbuf len cp
633 Extend the mbuf chain if the new data does not fit in
636 .It Fn m_prepend mbuf len how
639 and prepend it to the
645 It does not allocate any
657 .It Fn m_copyup mbuf len dstoff
662 bytes of data into a new mbuf at
667 argument aligns the data and leaves room for a link layer header.
677 The function does not allocate
684 .It Fn m_pullup mbuf len
685 Arrange that the first
689 are contiguous and lay in the data area of
691 so they are accessible with
693 It is important to remember that this may involve
694 reallocating some mbufs and moving data so all pointers
695 referencing data within the old mbuf chain
696 must be recalculated or made invalid.
704 is freed in this case).
706 It does not allocate any
713 .It Fn m_pulldown mbuf offset len offsetp
722 are contiguous and lay in the data area of
724 so they are accessible with
727 must be smaller than, or equal to, the size of an
729 Return a pointer to an intermediate
731 in the chain containing the requested region;
732 the offset in the data region of the
734 to the data contained in the returned mbuf is stored in
738 is NULL, the region may be accessed using
742 is non-NULL, the region may be accessed using
743 .Fn mtod mbuf uint8_t + *offsetp .
744 The region of the mbuf chain between its beginning and
746 is not modified, therefore it is safe to hold pointers to data within
747 this region before calling
750 .It Fn m_copym mbuf offset len how
755 bytes from the beginning, continuing for
762 copy to the end of the
765 The copy is read-only, because the
767 are not copied, only their reference counts are incremented.
769 .It Fn m_copypacket mbuf how
770 Copy an entire packet including header, which must be present.
771 This is an optimized version of the common case
772 .Fn m_copym mbuf 0 M_COPYALL how .
774 the copy is read-only, because the
776 are not copied, only their reference counts are incremented.
778 .It Fn m_dup mbuf how
781 into a completely new
783 including copying any
787 when you need a writable copy of an
790 .It Fn m_copydata mbuf offset len buf
795 bytes from the beginning, continuing for
797 bytes, into the indicated buffer
800 .It Fn m_copyback mbuf offset len buf
803 bytes from the buffer
805 back into the indicated
809 bytes from the beginning of the
815 It does not allocate any
827 will be allocated to fill the space.
829 .It Fn m_length mbuf last
830 Return the length of the
832 and optionally a pointer to the last
835 .It Fn m_dup_pkthdr to from how
836 Upon the function's completion, the
839 will contain an identical copy of
841 and the per-packet attributes found in the
851 must be empty on entry.
853 .It Fn m_move_pkthdr to from
856 and the per-packet attributes from the
869 must be empty on entry.
870 Upon the function's completion,
874 and the per-packet attributes cleared.
877 Set the packet-header length to the length of the
880 .It Fn m_devget buf len offset ifp copy
881 Copy data from a device local memory pointed to by
885 The copy is done using a specified copy routine
901 must be of the same type.
903 is still valid after the function returned.
909 .It Fn m_split mbuf len how
912 in two pieces, returning the tail:
916 In case of failure, it returns
918 and attempts to restore the
920 to its original state.
922 .It Fn m_apply mbuf off len f arg
923 Apply a function to an
930 Typically used to avoid calls to
932 which would otherwise be unnecessary or undesirable.
934 is a convenience argument which is passed to the callback function
939 is called, it will be passed
943 in the current mbuf, and the length
945 of the data in this mbuf to which the function should be applied.
947 The function should return zero to indicate success;
948 otherwise, if an error is indicated, then
950 will return the error and stop iterating through the
953 .It Fn m_getptr mbuf loc off
954 Return a pointer to the mbuf containing the data located at
956 bytes from the beginning of the
958 The corresponding offset into the mbuf will be stored in
960 .It Fn m_defrag m0 how
961 Defragment an mbuf chain, returning the shortest possible
962 chain of mbufs and clusters.
963 If allocation fails and this can not be completed,
965 will be returned and the original chain will be unchanged.
966 Upon success, the original chain will be freed and the new
967 chain will be returned.
973 depending on the caller's preference.
975 This function is especially useful in network drivers, where
976 certain long mbuf chains must be shortened before being added
977 to TX descriptor lists.
978 .It Fn m_unshare m0 how
979 Create a version of the specified mbuf chain whose
980 contents can be safely modified without affecting other users.
981 If allocation fails and this operation can not be completed,
984 The original mbuf chain is always reclaimed and the reference
985 count of any shared mbuf clusters is decremented.
991 depending on the caller's preference.
992 As a side-effect of this process the returned
993 mbuf chain may be compacted.
995 This function is especially useful in the transmit path of
996 network code, when data must be encrypted or otherwise
997 altered prior to transmission.
999 .Sh HARDWARE-ASSISTED CHECKSUM CALCULATION
1000 This section currently applies to TCP/IP only.
1001 In order to save the host CPU resources, computing checksums is
1002 offloaded to the network interface hardware if possible.
1005 member of the leading
1007 of a packet contains two fields used for that purpose,
1008 .Vt int Va csum_flags
1010 .Vt int Va csum_data .
1011 The meaning of those fields depends on the direction a packet flows in,
1012 and on whether the packet is fragmented.
1018 will denote the corresponding field of the
1020 member of the leading
1024 containing the packet.
1026 On output, checksum offloading is attempted after the outgoing
1027 interface has been determined for a packet.
1028 The interface-specific field
1029 .Va ifnet.if_data.ifi_hwassist
1032 is consulted for the capabilities of the interface to assist in
1033 computing checksums.
1036 field of the packet header is set to indicate which actions the interface
1037 is supposed to perform on it.
1038 The actions unsupported by the network interface are done in the
1039 software prior to passing the packet down to the interface driver;
1040 such actions will never be requested through
1043 The flags demanding a particular action from an interface are as follows:
1044 .Bl -tag -width ".Dv CSUM_TCP" -offset indent
1046 The IP header checksum is to be computed and stored in the
1047 corresponding field of the packet.
1048 The hardware is expected to know the format of an IP header
1049 to determine the offset of the IP checksum field.
1051 The TCP checksum is to be computed.
1054 The UDP checksum is to be computed.
1058 Should a TCP or UDP checksum be offloaded to the hardware,
1061 will contain the byte offset of the checksum field relative to the
1062 end of the IP header.
1063 In this case, the checksum field will be initially
1064 set by the TCP/IP module to the checksum of the pseudo header
1065 defined by the TCP and UDP specifications.
1067 On input, an interface indicates the actions it has performed
1068 on a packet by setting one or more of the following flags in
1070 associated with the packet:
1071 .Bl -tag -width ".Dv CSUM_IP_CHECKED" -offset indent
1072 .It Dv CSUM_IP_CHECKED
1073 The IP header checksum has been computed.
1074 .It Dv CSUM_IP_VALID
1075 The IP header has a valid checksum.
1076 This flag can appear only in combination with
1077 .Dv CSUM_IP_CHECKED .
1078 .It Dv CSUM_DATA_VALID
1079 The checksum of the data portion of the IP packet has been computed
1080 and stored in the field
1082 in network byte order.
1083 .It Dv CSUM_PSEUDO_HDR
1084 Can be set only along with
1086 to indicate that the IP data checksum found in
1088 allows for the pseudo header defined by the TCP and UDP specifications.
1089 Otherwise the checksum of the pseudo header must be calculated by
1090 the host CPU and added to
1092 to obtain the final checksum to be used for TCP or UDP validation purposes.
1095 If a particular network interface just indicates success or
1096 failure of TCP or UDP checksum validation without returning
1097 the exact value of the checksum to the host CPU, its driver can mark
1107 hexadecimal to indicate a valid checksum.
1108 It is a peculiarity of the algorithm used that the Internet checksum
1109 calculated over any valid packet will be
1111 as long as the original checksum field is included.
1113 When running a kernel compiled with the option
1114 .Dv MBUF_STRESS_TEST ,
1117 -controlled options may be used to create
1118 various failure/extreme cases for testing of network drivers
1119 and other parts of the kernel that rely on
1121 .Bl -tag -width ident
1122 .It Va net.inet.ip.mbuf_frag_size
1125 to fragment outgoing
1127 into fragments of the specified size.
1128 Setting this variable to 1 is an excellent way to
1131 handling ability of network drivers.
1132 .It Va kern.ipc.m_defragrandomfailures
1135 to randomly fail, returning
1137 Any piece of code which uses
1139 should be tested with this feature.
1147 .\" Please correct me if I'm wrong
1149 appeared in an early version of
1151 Besides being used for network packets, they were used
1152 to store various dynamic structures, such as routing table
1153 entries, interface addresses, protocol control blocks, etc.
1158 is almost entirely limited to packet storage, with
1160 zones being used directly to store other network-related memory.
1164 allocator has been a special-purpose memory allocator able to run in
1165 interrupt contexts and allocating from a special kernel address space map.
1170 allocator is a wrapper around
1176 + cluster pairs in per-CPU caches, as well as bringing other benefits of
1181 manual page was written by Yar Tikhiy.
1185 allocator was written by Bosko Milekic.