/share/man/man9/mbuf.9

https://bitbucket.org/freebsd/freebsd-head/ · Unknown · 1240 lines · 1239 code · 1 blank · 0 comment · 0 complexity · 023ab3966d620c49ab93df7ede1db028 MD5 · raw file

  1. .\" Copyright (c) 2000 FreeBSD Inc.
  2. .\" All rights reserved.
  3. .\"
  4. .\" Redistribution and use in source and binary forms, with or without
  5. .\" modification, are permitted provided that the following conditions
  6. .\" are met:
  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.
  12. .\"
  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
  23. .\" SUCH DAMAGE.
  24. .\"
  25. .\" $FreeBSD$
  26. .\"
  27. .Dd April 18, 2011
  28. .Dt MBUF 9
  29. .Os
  30. .\"
  31. .Sh NAME
  32. .Nm mbuf
  33. .Nd "memory management in the kernel IPC subsystem"
  34. .\"
  35. .Sh SYNOPSIS
  36. .In sys/param.h
  37. .In sys/systm.h
  38. .In sys/mbuf.h
  39. .\"
  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"
  44. .Fo MEXTADD
  45. .Fa "struct mbuf *mbuf"
  46. .Fa "caddr_t buf"
  47. .Fa "u_int size"
  48. .Fa "void (*free)(void *opt_arg1, void *opt_arg2)"
  49. .Fa "void *opt_arg1"
  50. .Fa "void *opt_arg2"
  51. .Fa "short flags"
  52. .Fa "int type"
  53. .Fc
  54. .Fn MEXTFREE "struct mbuf *mbuf"
  55. .Fn MFREE "struct mbuf *mbuf" "struct mbuf *successor"
  56. .\"
  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"
  61. .Ft int
  62. .Fn M_LEADINGSPACE "struct mbuf *mbuf"
  63. .Ft int
  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"
  68. .Ft int
  69. .Fn M_WRITABLE "struct mbuf *mbuf"
  70. .\"
  71. .Ss Mbuf allocation functions
  72. .Ft struct mbuf *
  73. .Fn m_get "int how" "int type"
  74. .Ft struct mbuf *
  75. .Fn m_getm "struct mbuf *orig" "int len" "int how" "int type"
  76. .Ft struct mbuf *
  77. .Fn m_getcl "int how" "short type" "int flags"
  78. .Ft struct mbuf *
  79. .Fn m_getclr "int how" "int type"
  80. .Ft struct mbuf *
  81. .Fn m_gethdr "int how" "int type"
  82. .Ft struct mbuf *
  83. .Fn m_free "struct mbuf *mbuf"
  84. .Ft void
  85. .Fn m_freem "struct mbuf *mbuf"
  86. .\"
  87. .Ss Mbuf utility functions
  88. .Ft void
  89. .Fn m_adj "struct mbuf *mbuf" "int len"
  90. .Ft void
  91. .Fn m_align "struct mbuf *mbuf" "int len"
  92. .Ft int
  93. .Fn m_append "struct mbuf *mbuf" "int len" "c_caddr_t cp"
  94. .Ft struct mbuf *
  95. .Fn m_prepend "struct mbuf *mbuf" "int len" "int how"
  96. .Ft struct mbuf *
  97. .Fn m_copyup "struct mbuf *mbuf" "int len" "int dstoff"
  98. .Ft struct mbuf *
  99. .Fn m_pullup "struct mbuf *mbuf" "int len"
  100. .Ft struct mbuf *
  101. .Fn m_pulldown "struct mbuf *mbuf" "int offset" "int len" "int *offsetp"
  102. .Ft struct mbuf *
  103. .Fn m_copym "struct mbuf *mbuf" "int offset" "int len" "int how"
  104. .Ft struct mbuf *
  105. .Fn m_copypacket "struct mbuf *mbuf" "int how"
  106. .Ft struct mbuf *
  107. .Fn m_dup "struct mbuf *mbuf" "int how"
  108. .Ft void
  109. .Fn m_copydata "const struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
  110. .Ft void
  111. .Fn m_copyback "struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
  112. .Ft struct mbuf *
  113. .Fo m_devget
  114. .Fa "char *buf"
  115. .Fa "int len"
  116. .Fa "int offset"
  117. .Fa "struct ifnet *ifp"
  118. .Fa "void (*copy)(char *from, caddr_t to, u_int len)"
  119. .Fc
  120. .Ft void
  121. .Fn m_cat "struct mbuf *m" "struct mbuf *n"
  122. .Ft u_int
  123. .Fn m_fixhdr "struct mbuf *mbuf"
  124. .Ft void
  125. .Fn m_dup_pkthdr "struct mbuf *to" "struct mbuf *from"
  126. .Ft void
  127. .Fn m_move_pkthdr "struct mbuf *to" "struct mbuf *from"
  128. .Ft u_int
  129. .Fn m_length "struct mbuf *mbuf" "struct mbuf **last"
  130. .Ft struct mbuf *
  131. .Fn m_split "struct mbuf *mbuf" "int len" "int how"
  132. .Ft int
  133. .Fn m_apply "struct mbuf *mbuf" "int off" "int len" "int (*f)(void *arg, void *data, u_int len)" "void *arg"
  134. .Ft struct mbuf *
  135. .Fn m_getptr "struct mbuf *mbuf" "int loc" "int *off"
  136. .Ft struct mbuf *
  137. .Fn m_defrag "struct mbuf *m0" "int how"
  138. .Ft struct mbuf *
  139. .Fn m_unshare "struct mbuf *m0" "int how"
  140. .\"
  141. .Sh DESCRIPTION
  142. An
  143. .Vt mbuf
  144. is a basic unit of memory management in the kernel IPC subsystem.
  145. Network packets and socket buffers are stored in
  146. .Vt mbufs .
  147. A network packet may span multiple
  148. .Vt mbufs
  149. arranged into a
  150. .Vt mbuf chain
  151. (linked list),
  152. which allows adding or trimming
  153. network headers with little overhead.
  154. .Pp
  155. While a developer should not bother with
  156. .Vt mbuf
  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
  160. .Vt mbuf .
  161. .Pp
  162. An
  163. .Vt mbuf
  164. consists of a variable-sized header and a small internal
  165. buffer for data.
  166. The total size of an
  167. .Vt mbuf ,
  168. .Dv MSIZE ,
  169. is a constant defined in
  170. .In sys/param.h .
  171. The
  172. .Vt mbuf
  173. header includes:
  174. .Bl -tag -width "m_nextpkt" -offset indent
  175. .It Va m_next
  176. .Pq Vt struct mbuf *
  177. A pointer to the next
  178. .Vt mbuf
  179. in the
  180. .Vt mbuf chain .
  181. .It Va m_nextpkt
  182. .Pq Vt struct mbuf *
  183. A pointer to the next
  184. .Vt mbuf chain
  185. in the queue.
  186. .It Va m_data
  187. .Pq Vt caddr_t
  188. A pointer to data attached to this
  189. .Vt mbuf .
  190. .It Va m_len
  191. .Pq Vt int
  192. The length of the data.
  193. .It Va m_type
  194. .Pq Vt short
  195. The type of the data.
  196. .It Va m_flags
  197. .Pq Vt int
  198. The
  199. .Vt mbuf
  200. flags.
  201. .El
  202. .Pp
  203. The
  204. .Vt mbuf
  205. flag bits are defined as follows:
  206. .Bd -literal
  207. /* mbuf flags */
  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 */
  219. /* mbuf pkthdr flags (also stored in m_flags) */
  220. #define M_BCAST 0x0200 /* send/received as link-level broadcast */
  221. #define M_MCAST 0x0400 /* send/received as link-level multicast */
  222. #define M_FRAG 0x0800 /* packet is fragment of larger packet */
  223. #define M_FIRSTFRAG 0x1000 /* packet is first fragment */
  224. #define M_LASTFRAG 0x2000 /* packet is last fragment */
  225. .Ed
  226. .Pp
  227. The available
  228. .Vt mbuf
  229. types are defined as follows:
  230. .Bd -literal
  231. /* mbuf types */
  232. #define MT_DATA 1 /* dynamic (data) allocation */
  233. #define MT_HEADER MT_DATA /* packet header */
  234. #define MT_SONAME 8 /* socket name */
  235. #define MT_CONTROL 14 /* extra-data protocol message */
  236. #define MT_OOBDATA 15 /* expedited data */
  237. .Ed
  238. .Pp
  239. The available external buffer types are defined as follows:
  240. .Bd -literal
  241. /* external buffer types */
  242. #define EXT_CLUSTER 1 /* mbuf cluster */
  243. #define EXT_SFBUF 2 /* sendfile(2)'s sf_bufs */
  244. #define EXT_JUMBOP 3 /* jumbo cluster 4096 bytes */
  245. #define EXT_JUMBO9 4 /* jumbo cluster 9216 bytes */
  246. #define EXT_JUMBO16 5 /* jumbo cluster 16184 bytes */
  247. #define EXT_PACKET 6 /* mbuf+cluster from packet zone */
  248. #define EXT_MBUF 7 /* external mbuf reference (M_IOVEC) */
  249. #define EXT_NET_DRV 100 /* custom ext_buf provided by net driver(s) */
  250. #define EXT_MOD_TYPE 200 /* custom module's ext_buf type */
  251. #define EXT_DISPOSABLE 300 /* can throw this buffer away w/page flipping */
  252. #define EXT_EXTREF 400 /* has externally maintained ref_cnt ptr */
  253. .Ed
  254. .Pp
  255. If the
  256. .Dv M_PKTHDR
  257. flag is set, a
  258. .Vt struct pkthdr Va m_pkthdr
  259. is added to the
  260. .Vt mbuf
  261. header.
  262. It contains a pointer to the interface
  263. the packet has been received from
  264. .Pq Vt struct ifnet Va *rcvif ,
  265. and the total packet length
  266. .Pq Vt int Va len .
  267. Optionally, it may also contain an attached list of packet tags
  268. .Pq Vt "struct m_tag" .
  269. See
  270. .Xr mbuf_tags 9
  271. for details.
  272. Fields used in offloading checksum calculation to the hardware are kept in
  273. .Va m_pkthdr
  274. as well.
  275. See
  276. .Sx HARDWARE-ASSISTED CHECKSUM CALCULATION
  277. for details.
  278. .Pp
  279. If small enough, data is stored in the internal data buffer of an
  280. .Vt mbuf .
  281. If the data is sufficiently large, another
  282. .Vt mbuf
  283. may be added to the
  284. .Vt mbuf chain ,
  285. or external storage may be associated with the
  286. .Vt mbuf .
  287. .Dv MHLEN
  288. bytes of data can fit into an
  289. .Vt mbuf
  290. with the
  291. .Dv M_PKTHDR
  292. flag set,
  293. .Dv MLEN
  294. bytes can otherwise.
  295. .Pp
  296. If external storage is being associated with an
  297. .Vt mbuf ,
  298. the
  299. .Va m_ext
  300. header is added at the cost of losing the internal data buffer.
  301. It includes a pointer to external storage, the size of the storage,
  302. a pointer to a function used for freeing the storage,
  303. a pointer to an optional argument that can be passed to the function,
  304. and a pointer to a reference counter.
  305. An
  306. .Vt mbuf
  307. using external storage has the
  308. .Dv M_EXT
  309. flag set.
  310. .Pp
  311. The system supplies a macro for allocating the desired external storage
  312. buffer,
  313. .Dv MEXTADD .
  314. .Pp
  315. The allocation and management of the reference counter is handled by the
  316. subsystem.
  317. .Pp
  318. The system also supplies a default type of external storage buffer called an
  319. .Vt mbuf cluster .
  320. .Vt Mbuf clusters
  321. can be allocated and configured with the use of the
  322. .Dv MCLGET
  323. macro.
  324. Each
  325. .Vt mbuf cluster
  326. is
  327. .Dv MCLBYTES
  328. in size, where MCLBYTES is a machine-dependent constant.
  329. The system defines an advisory macro
  330. .Dv MINCLSIZE ,
  331. which is the smallest amount of data to put into an
  332. .Vt mbuf cluster .
  333. It is equal to the sum of
  334. .Dv MLEN
  335. and
  336. .Dv MHLEN .
  337. It is typically preferable to store data into the data region of an
  338. .Vt mbuf ,
  339. if size permits, as opposed to allocating a separate
  340. .Vt mbuf cluster
  341. to hold the same data.
  342. .\"
  343. .Ss Macros and Functions
  344. There are numerous predefined macros and functions that provide the
  345. developer with common utilities.
  346. .\"
  347. .Bl -ohang -offset indent
  348. .It Fn mtod mbuf type
  349. Convert an
  350. .Fa mbuf
  351. pointer to a data pointer.
  352. The macro expands to the data pointer cast to the pointer of the specified
  353. .Fa type .
  354. .Sy Note :
  355. It is advisable to ensure that there is enough contiguous data in
  356. .Fa mbuf .
  357. See
  358. .Fn m_pullup
  359. for details.
  360. .It Fn MGET mbuf how type
  361. Allocate an
  362. .Vt mbuf
  363. and initialize it to contain internal data.
  364. .Fa mbuf
  365. will point to the allocated
  366. .Vt mbuf
  367. on success, or be set to
  368. .Dv NULL
  369. on failure.
  370. The
  371. .Fa how
  372. argument is to be set to
  373. .Dv M_WAITOK
  374. or
  375. .Dv M_NOWAIT .
  376. It specifies whether the caller is willing to block if necessary.
  377. A number of other functions and macros related to
  378. .Vt mbufs
  379. have the same argument because they may
  380. at some point need to allocate new
  381. .Vt mbufs .
  382. .Pp
  383. Historical
  384. .Vt mbuf
  385. allocator (See
  386. .Sx HISTORY
  387. section) used allocation flags
  388. .Dv M_WAIT
  389. and
  390. .Dv M_DONTWAIT .
  391. These constants are kept for compatibility
  392. and their use in new code is discouraged.
  393. .It Fn MGETHDR mbuf how type
  394. Allocate an
  395. .Vt mbuf
  396. and initialize it to contain a packet header
  397. and internal data.
  398. See
  399. .Fn MGET
  400. for details.
  401. .It Fn MEXTADD mbuf buf size free opt_arg1 opt_arg2 flags type
  402. Associate externally managed data with
  403. .Fa mbuf .
  404. Any internal data contained in the mbuf will be discarded, and the
  405. .Dv M_EXT
  406. flag will be set.
  407. The
  408. .Fa buf
  409. and
  410. .Fa size
  411. arguments are the address and length, respectively, of the data.
  412. The
  413. .Fa free
  414. argument points to a function which will be called to free the data
  415. when the mbuf is freed; it is only used if
  416. .Fa type
  417. is
  418. .Dv EXT_EXTREF .
  419. The
  420. .Fa opt_arg1
  421. and
  422. .Fa opt_arg2
  423. arguments will be passed unmodified to
  424. .Fa free .
  425. The
  426. .Fa flags
  427. argument specifies additional
  428. .Vt mbuf
  429. flags; it is not necessary to specify
  430. .Dv M_EXT .
  431. Finally, the
  432. .Fa type
  433. argument specifies the type of external data, which controls how it
  434. will be disposed of when the
  435. .Vt mbuf
  436. is freed.
  437. In most cases, the correct value is
  438. .Dv EXT_EXTREF .
  439. .It Fn MCLGET mbuf how
  440. Allocate and attach an
  441. .Vt mbuf cluster
  442. to
  443. .Fa mbuf .
  444. If the macro fails, the
  445. .Dv M_EXT
  446. flag will not be set in
  447. .Fa mbuf .
  448. .It Fn M_ALIGN mbuf len
  449. Set the pointer
  450. .Fa mbuf->m_data
  451. to place an object of the size
  452. .Fa len
  453. at the end of the internal data area of
  454. .Fa mbuf ,
  455. long word aligned.
  456. Applicable only if
  457. .Fa mbuf
  458. is newly allocated with
  459. .Fn MGET
  460. or
  461. .Fn m_get .
  462. .It Fn MH_ALIGN mbuf len
  463. Serves the same purpose as
  464. .Fn M_ALIGN
  465. does, but only for
  466. .Fa mbuf
  467. newly allocated with
  468. .Fn MGETHDR
  469. or
  470. .Fn m_gethdr ,
  471. or initialized by
  472. .Fn m_dup_pkthdr
  473. or
  474. .Fn m_move_pkthdr .
  475. .It Fn m_align mbuf len
  476. Services the same purpose as
  477. .Fn M_ALIGN
  478. but handles any type of mbuf.
  479. .It Fn M_LEADINGSPACE mbuf
  480. Returns the number of bytes available before the beginning
  481. of data in
  482. .Fa mbuf .
  483. .It Fn M_TRAILINGSPACE mbuf
  484. Returns the number of bytes available after the end of data in
  485. .Fa mbuf .
  486. .It Fn M_PREPEND mbuf len how
  487. This macro operates on an
  488. .Vt mbuf chain .
  489. It is an optimized wrapper for
  490. .Fn m_prepend
  491. that can make use of possible empty space before data
  492. (e.g.\& left after trimming of a link-layer header).
  493. The new
  494. .Vt mbuf chain
  495. pointer or
  496. .Dv NULL
  497. is in
  498. .Fa mbuf
  499. after the call.
  500. .It Fn M_MOVE_PKTHDR to from
  501. Using this macro is equivalent to calling
  502. .Fn m_move_pkthdr to from .
  503. .It Fn M_WRITABLE mbuf
  504. This macro will evaluate true if
  505. .Fa mbuf
  506. is not marked
  507. .Dv M_RDONLY
  508. and if either
  509. .Fa mbuf
  510. does not contain external storage or,
  511. if it does,
  512. then if the reference count of the storage is not greater than 1.
  513. The
  514. .Dv M_RDONLY
  515. flag can be set in
  516. .Fa mbuf->m_flags .
  517. This can be achieved during setup of the external storage,
  518. by passing the
  519. .Dv M_RDONLY
  520. bit as a
  521. .Fa flags
  522. argument to the
  523. .Fn MEXTADD
  524. macro, or can be directly set in individual
  525. .Vt mbufs .
  526. .It Fn MCHTYPE mbuf type
  527. Change the type of
  528. .Fa mbuf
  529. to
  530. .Fa type .
  531. This is a relatively expensive operation and should be avoided.
  532. .El
  533. .Pp
  534. The functions are:
  535. .Bl -ohang -offset indent
  536. .It Fn m_get how type
  537. A function version of
  538. .Fn MGET
  539. for non-critical paths.
  540. .It Fn m_getm orig len how type
  541. Allocate
  542. .Fa len
  543. bytes worth of
  544. .Vt mbufs
  545. and
  546. .Vt mbuf clusters
  547. if necessary and append the resulting allocated
  548. .Vt mbuf chain
  549. to the
  550. .Vt mbuf chain
  551. .Fa orig ,
  552. if it is
  553. .No non- Ns Dv NULL .
  554. If the allocation fails at any point,
  555. free whatever was allocated and return
  556. .Dv NULL .
  557. If
  558. .Fa orig
  559. is
  560. .No non- Ns Dv NULL ,
  561. it will not be freed.
  562. It is possible to use
  563. .Fn m_getm
  564. to either append
  565. .Fa len
  566. bytes to an existing
  567. .Vt mbuf
  568. or
  569. .Vt mbuf chain
  570. (for example, one which may be sitting in a pre-allocated ring)
  571. or to simply perform an all-or-nothing
  572. .Vt mbuf
  573. and
  574. .Vt mbuf cluster
  575. allocation.
  576. .It Fn m_gethdr how type
  577. A function version of
  578. .Fn MGETHDR
  579. for non-critical paths.
  580. .It Fn m_getcl how type flags
  581. Fetch an
  582. .Vt mbuf
  583. with a
  584. .Vt mbuf cluster
  585. attached to it.
  586. If one of the allocations fails, the entire allocation fails.
  587. This routine is the preferred way of fetching both the
  588. .Vt mbuf
  589. and
  590. .Vt mbuf cluster
  591. together, as it avoids having to unlock/relock between allocations.
  592. Returns
  593. .Dv NULL
  594. on failure.
  595. .It Fn m_getclr how type
  596. Allocate an
  597. .Vt mbuf
  598. and zero out the data region.
  599. .It Fn m_free mbuf
  600. Frees
  601. .Vt mbuf .
  602. Returns
  603. .Va m_next
  604. of the freed
  605. .Vt mbuf .
  606. .El
  607. .Pp
  608. The functions below operate on
  609. .Vt mbuf chains .
  610. .Bl -ohang -offset indent
  611. .It Fn m_freem mbuf
  612. Free an entire
  613. .Vt mbuf chain ,
  614. including any external storage.
  615. .\"
  616. .It Fn m_adj mbuf len
  617. Trim
  618. .Fa len
  619. bytes from the head of an
  620. .Vt mbuf chain
  621. if
  622. .Fa len
  623. is positive, from the tail otherwise.
  624. .\"
  625. .It Fn m_append mbuf len cp
  626. Append
  627. .Vt len
  628. bytes of data
  629. .Vt cp
  630. to the
  631. .Vt mbuf chain .
  632. Extend the mbuf chain if the new data does not fit in
  633. existing space.
  634. .\"
  635. .It Fn m_prepend mbuf len how
  636. Allocate a new
  637. .Vt mbuf
  638. and prepend it to the
  639. .Vt mbuf chain ,
  640. handle
  641. .Dv M_PKTHDR
  642. properly.
  643. .Sy Note :
  644. It does not allocate any
  645. .Vt mbuf clusters ,
  646. so
  647. .Fa len
  648. must be less than
  649. .Dv MLEN
  650. or
  651. .Dv MHLEN ,
  652. depending on the
  653. .Dv M_PKTHDR
  654. flag setting.
  655. .\"
  656. .It Fn m_copyup mbuf len dstoff
  657. Similar to
  658. .Fn m_pullup
  659. but copies
  660. .Fa len
  661. bytes of data into a new mbuf at
  662. .Fa dstoff
  663. bytes into the mbuf.
  664. The
  665. .Fa dstoff
  666. argument aligns the data and leaves room for a link layer header.
  667. Returns the new
  668. .Vt mbuf chain
  669. on success,
  670. and frees the
  671. .Vt mbuf chain
  672. and returns
  673. .Dv NULL
  674. on failure.
  675. .Sy Note :
  676. The function does not allocate
  677. .Vt mbuf clusters ,
  678. so
  679. .Fa len + dstoff
  680. must be less than
  681. .Dv MHLEN .
  682. .\"
  683. .It Fn m_pullup mbuf len
  684. Arrange that the first
  685. .Fa len
  686. bytes of an
  687. .Vt mbuf chain
  688. are contiguous and lay in the data area of
  689. .Fa mbuf ,
  690. so they are accessible with
  691. .Fn mtod mbuf type .
  692. It is important to remember that this may involve
  693. reallocating some mbufs and moving data so all pointers
  694. referencing data within the old mbuf chain
  695. must be recalculated or made invalid.
  696. Return the new
  697. .Vt mbuf chain
  698. on success,
  699. .Dv NULL
  700. on failure
  701. (the
  702. .Vt mbuf chain
  703. is freed in this case).
  704. .Sy Note :
  705. It does not allocate any
  706. .Vt mbuf clusters ,
  707. so
  708. .Fa len
  709. must be less than
  710. .Dv MHLEN .
  711. .\"
  712. .It Fn m_pulldown mbuf offset len offsetp
  713. Arrange that
  714. .Fa len
  715. bytes between
  716. .Fa offset
  717. and
  718. .Fa offset + len
  719. in the
  720. .Vt mbuf chain
  721. are contiguous and lay in the data area of
  722. .Fa mbuf ,
  723. so they are accessible with
  724. .Fn mtod mbuf type .
  725. .Fa len
  726. must be smaller than, or equal to, the size of an
  727. .Vt mbuf cluster .
  728. Return a pointer to an intermediate
  729. .Vt mbuf
  730. in the chain containing the requested region;
  731. the offset in the data region of the
  732. .Vt mbuf chain
  733. to the data contained in the returned mbuf is stored in
  734. .Fa *offsetp .
  735. If
  736. .Fa offp
  737. is NULL, the region may be accessed using
  738. .Fn mtod mbuf type .
  739. If
  740. .Fa offp
  741. is non-NULL, the region may be accessed using
  742. .Fn mtod mbuf uint8_t + *offsetp .
  743. The region of the mbuf chain between its beginning and
  744. .Fa off
  745. is not modified, therefore it is safe to hold pointers to data within
  746. this region before calling
  747. .Fn m_pulldown .
  748. .\"
  749. .It Fn m_copym mbuf offset len how
  750. Make a copy of an
  751. .Vt mbuf chain
  752. starting
  753. .Fa offset
  754. bytes from the beginning, continuing for
  755. .Fa len
  756. bytes.
  757. If
  758. .Fa len
  759. is
  760. .Dv M_COPYALL ,
  761. copy to the end of the
  762. .Vt mbuf chain .
  763. .Sy Note :
  764. The copy is read-only, because the
  765. .Vt mbuf clusters
  766. are not copied, only their reference counts are incremented.
  767. .\"
  768. .It Fn m_copypacket mbuf how
  769. Copy an entire packet including header, which must be present.
  770. This is an optimized version of the common case
  771. .Fn m_copym mbuf 0 M_COPYALL how .
  772. .Sy Note :
  773. the copy is read-only, because the
  774. .Vt mbuf clusters
  775. are not copied, only their reference counts are incremented.
  776. .\"
  777. .It Fn m_dup mbuf how
  778. Copy a packet header
  779. .Vt mbuf chain
  780. into a completely new
  781. .Vt mbuf chain ,
  782. including copying any
  783. .Vt mbuf clusters .
  784. Use this instead of
  785. .Fn m_copypacket
  786. when you need a writable copy of an
  787. .Vt mbuf chain .
  788. .\"
  789. .It Fn m_copydata mbuf offset len buf
  790. Copy data from an
  791. .Vt mbuf chain
  792. starting
  793. .Fa off
  794. bytes from the beginning, continuing for
  795. .Fa len
  796. bytes, into the indicated buffer
  797. .Fa buf .
  798. .\"
  799. .It Fn m_copyback mbuf offset len buf
  800. Copy
  801. .Fa len
  802. bytes from the buffer
  803. .Fa buf
  804. back into the indicated
  805. .Vt mbuf chain ,
  806. starting at
  807. .Fa offset
  808. bytes from the beginning of the
  809. .Vt mbuf chain ,
  810. extending the
  811. .Vt mbuf chain
  812. if necessary.
  813. .Sy Note :
  814. It does not allocate any
  815. .Vt mbuf clusters ,
  816. just adds
  817. .Vt mbufs
  818. to the
  819. .Vt mbuf chain .
  820. It is safe to set
  821. .Fa offset
  822. beyond the current
  823. .Vt mbuf chain
  824. end: zeroed
  825. .Vt mbufs
  826. will be allocated to fill the space.
  827. .\"
  828. .It Fn m_length mbuf last
  829. Return the length of the
  830. .Vt mbuf chain ,
  831. and optionally a pointer to the last
  832. .Vt mbuf .
  833. .\"
  834. .It Fn m_dup_pkthdr to from how
  835. Upon the function's completion, the
  836. .Vt mbuf
  837. .Fa to
  838. will contain an identical copy of
  839. .Fa from->m_pkthdr
  840. and the per-packet attributes found in the
  841. .Vt mbuf chain
  842. .Fa from .
  843. The
  844. .Vt mbuf
  845. .Fa from
  846. must have the flag
  847. .Dv M_PKTHDR
  848. initially set, and
  849. .Fa to
  850. must be empty on entry.
  851. .\"
  852. .It Fn m_move_pkthdr to from
  853. Move
  854. .Va m_pkthdr
  855. and the per-packet attributes from the
  856. .Vt mbuf chain
  857. .Fa from
  858. to the
  859. .Vt mbuf
  860. .Fa to .
  861. The
  862. .Vt mbuf
  863. .Fa from
  864. must have the flag
  865. .Dv M_PKTHDR
  866. initially set, and
  867. .Fa to
  868. must be empty on entry.
  869. Upon the function's completion,
  870. .Fa from
  871. will have the flag
  872. .Dv M_PKTHDR
  873. and the per-packet attributes cleared.
  874. .\"
  875. .It Fn m_fixhdr mbuf
  876. Set the packet-header length to the length of the
  877. .Vt mbuf chain .
  878. .\"
  879. .It Fn m_devget buf len offset ifp copy
  880. Copy data from a device local memory pointed to by
  881. .Fa buf
  882. to an
  883. .Vt mbuf chain .
  884. The copy is done using a specified copy routine
  885. .Fa copy ,
  886. or
  887. .Fn bcopy
  888. if
  889. .Fa copy
  890. is
  891. .Dv NULL .
  892. .\"
  893. .It Fn m_cat m n
  894. Concatenate
  895. .Fa n
  896. to
  897. .Fa m .
  898. Both
  899. .Vt mbuf chains
  900. must be of the same type.
  901. .Fa N
  902. is still valid after the function returned.
  903. .Sy Note :
  904. It does not handle
  905. .Dv M_PKTHDR
  906. and friends.
  907. .\"
  908. .It Fn m_split mbuf len how
  909. Partition an
  910. .Vt mbuf chain
  911. in two pieces, returning the tail:
  912. all but the first
  913. .Fa len
  914. bytes.
  915. In case of failure, it returns
  916. .Dv NULL
  917. and attempts to restore the
  918. .Vt mbuf chain
  919. to its original state.
  920. .\"
  921. .It Fn m_apply mbuf off len f arg
  922. Apply a function to an
  923. .Vt mbuf chain ,
  924. at offset
  925. .Fa off ,
  926. for length
  927. .Fa len
  928. bytes.
  929. Typically used to avoid calls to
  930. .Fn m_pullup
  931. which would otherwise be unnecessary or undesirable.
  932. .Fa arg
  933. is a convenience argument which is passed to the callback function
  934. .Fa f .
  935. .Pp
  936. Each time
  937. .Fn f
  938. is called, it will be passed
  939. .Fa arg ,
  940. a pointer to the
  941. .Fa data
  942. in the current mbuf, and the length
  943. .Fa len
  944. of the data in this mbuf to which the function should be applied.
  945. .Pp
  946. The function should return zero to indicate success;
  947. otherwise, if an error is indicated, then
  948. .Fn m_apply
  949. will return the error and stop iterating through the
  950. .Vt mbuf chain .
  951. .\"
  952. .It Fn m_getptr mbuf loc off
  953. Return a pointer to the mbuf containing the data located at
  954. .Fa loc
  955. bytes from the beginning of the
  956. .Vt mbuf chain .
  957. The corresponding offset into the mbuf will be stored in
  958. .Fa *off .
  959. .It Fn m_defrag m0 how
  960. Defragment an mbuf chain, returning the shortest possible
  961. chain of mbufs and clusters.
  962. If allocation fails and this can not be completed,
  963. .Dv NULL
  964. will be returned and the original chain will be unchanged.
  965. Upon success, the original chain will be freed and the new
  966. chain will be returned.
  967. .Fa how
  968. should be either
  969. .Dv M_WAITOK
  970. or
  971. .Dv M_NOWAIT ,
  972. depending on the caller's preference.
  973. .Pp
  974. This function is especially useful in network drivers, where
  975. certain long mbuf chains must be shortened before being added
  976. to TX descriptor lists.
  977. .It Fn m_unshare m0 how
  978. Create a version of the specified mbuf chain whose
  979. contents can be safely modified without affecting other users.
  980. If allocation fails and this operation can not be completed,
  981. .Dv NULL
  982. will be returned.
  983. The original mbuf chain is always reclaimed and the reference
  984. count of any shared mbuf clusters is decremented.
  985. .Fa how
  986. should be either
  987. .Dv M_WAITOK
  988. or
  989. .Dv M_NOWAIT ,
  990. depending on the caller's preference.
  991. As a side-effect of this process the returned
  992. mbuf chain may be compacted.
  993. .Pp
  994. This function is especially useful in the transmit path of
  995. network code, when data must be encrypted or otherwise
  996. altered prior to transmission.
  997. .El
  998. .Sh HARDWARE-ASSISTED CHECKSUM CALCULATION
  999. This section currently applies to TCP/IP only.
  1000. In order to save the host CPU resources, computing checksums is
  1001. offloaded to the network interface hardware if possible.
  1002. The
  1003. .Va m_pkthdr
  1004. member of the leading
  1005. .Vt mbuf
  1006. of a packet contains two fields used for that purpose,
  1007. .Vt int Va csum_flags
  1008. and
  1009. .Vt int Va csum_data .
  1010. The meaning of those fields depends on the direction a packet flows in,
  1011. and on whether the packet is fragmented.
  1012. Henceforth,
  1013. .Va csum_flags
  1014. or
  1015. .Va csum_data
  1016. of a packet
  1017. will denote the corresponding field of the
  1018. .Va m_pkthdr
  1019. member of the leading
  1020. .Vt mbuf
  1021. in the
  1022. .Vt mbuf chain
  1023. containing the packet.
  1024. .Pp
  1025. On output, checksum offloading is attempted after the outgoing
  1026. interface has been determined for a packet.
  1027. The interface-specific field
  1028. .Va ifnet.if_data.ifi_hwassist
  1029. (see
  1030. .Xr ifnet 9 )
  1031. is consulted for the capabilities of the interface to assist in
  1032. computing checksums.
  1033. The
  1034. .Va csum_flags
  1035. field of the packet header is set to indicate which actions the interface
  1036. is supposed to perform on it.
  1037. The actions unsupported by the network interface are done in the
  1038. software prior to passing the packet down to the interface driver;
  1039. such actions will never be requested through
  1040. .Va csum_flags .
  1041. .Pp
  1042. The flags demanding a particular action from an interface are as follows:
  1043. .Bl -tag -width ".Dv CSUM_TCP" -offset indent
  1044. .It Dv CSUM_IP
  1045. The IP header checksum is to be computed and stored in the
  1046. corresponding field of the packet.
  1047. The hardware is expected to know the format of an IP header
  1048. to determine the offset of the IP checksum field.
  1049. .It Dv CSUM_TCP
  1050. The TCP checksum is to be computed.
  1051. (See below.)
  1052. .It Dv CSUM_UDP
  1053. The UDP checksum is to be computed.
  1054. (See below.)
  1055. .El
  1056. .Pp
  1057. Should a TCP or UDP checksum be offloaded to the hardware,
  1058. the field
  1059. .Va csum_data
  1060. will contain the byte offset of the checksum field relative to the
  1061. end of the IP header.
  1062. In this case, the checksum field will be initially
  1063. set by the TCP/IP module to the checksum of the pseudo header
  1064. defined by the TCP and UDP specifications.
  1065. .Pp
  1066. For outbound packets which have been fragmented
  1067. by the host CPU, the following will also be true,
  1068. regardless of the checksum flag settings:
  1069. .Bl -bullet -offset indent
  1070. .It
  1071. all fragments will have the flag
  1072. .Dv M_FRAG
  1073. set in their
  1074. .Va m_flags
  1075. field;
  1076. .It
  1077. the first and the last fragments in the chain will have
  1078. .Dv M_FIRSTFRAG
  1079. or
  1080. .Dv M_LASTFRAG
  1081. set in their
  1082. .Va m_flags ,
  1083. correspondingly;
  1084. .It
  1085. the first fragment in the chain will have the total number
  1086. of fragments contained in its
  1087. .Va csum_data
  1088. field.
  1089. .El
  1090. .Pp
  1091. The last rule for fragmented packets takes precedence over the one
  1092. for a TCP or UDP checksum.
  1093. Nevertheless, offloading a TCP or UDP checksum is possible for a
  1094. fragmented packet if the flag
  1095. .Dv CSUM_IP_FRAGS
  1096. is set in the field
  1097. .Va ifnet.if_data.ifi_hwassist
  1098. associated with the network interface.
  1099. However, in this case the interface is expected to figure out
  1100. the location of the checksum field within the sequence of fragments
  1101. by itself because
  1102. .Va csum_data
  1103. contains a fragment count instead of a checksum offset value.
  1104. .Pp
  1105. On input, an interface indicates the actions it has performed
  1106. on a packet by setting one or more of the following flags in
  1107. .Va csum_flags
  1108. associated with the packet:
  1109. .Bl -tag -width ".Dv CSUM_IP_CHECKED" -offset indent
  1110. .It Dv CSUM_IP_CHECKED
  1111. The IP header checksum has been computed.
  1112. .It Dv CSUM_IP_VALID
  1113. The IP header has a valid checksum.
  1114. This flag can appear only in combination with
  1115. .Dv CSUM_IP_CHECKED .
  1116. .It Dv CSUM_DATA_VALID
  1117. The checksum of the data portion of the IP packet has been computed
  1118. and stored in the field
  1119. .Va csum_data
  1120. in network byte order.
  1121. .It Dv CSUM_PSEUDO_HDR
  1122. Can be set only along with
  1123. .Dv CSUM_DATA_VALID
  1124. to indicate that the IP data checksum found in
  1125. .Va csum_data
  1126. allows for the pseudo header defined by the TCP and UDP specifications.
  1127. Otherwise the checksum of the pseudo header must be calculated by
  1128. the host CPU and added to
  1129. .Va csum_data
  1130. to obtain the final checksum to be used for TCP or UDP validation purposes.
  1131. .El
  1132. .Pp
  1133. If a particular network interface just indicates success or
  1134. failure of TCP or UDP checksum validation without returning
  1135. the exact value of the checksum to the host CPU, its driver can mark
  1136. .Dv CSUM_DATA_VALID
  1137. and
  1138. .Dv CSUM_PSEUDO_HDR
  1139. in
  1140. .Va csum_flags ,
  1141. and set
  1142. .Va csum_data
  1143. to
  1144. .Li 0xFFFF
  1145. hexadecimal to indicate a valid checksum.
  1146. It is a peculiarity of the algorithm used that the Internet checksum
  1147. calculated over any valid packet will be
  1148. .Li 0xFFFF
  1149. as long as the original checksum field is included.
  1150. .Pp
  1151. For inbound packets which are IP fragments, all
  1152. .Va csum_data
  1153. fields will be summed during reassembly to obtain the final checksum
  1154. value passed to an upper layer in the
  1155. .Va csum_data
  1156. field of the reassembled packet.
  1157. The
  1158. .Va csum_flags
  1159. fields of all fragments will be consolidated using logical AND
  1160. to obtain the final value for
  1161. .Va csum_flags .
  1162. Thus, in order to successfully
  1163. offload checksum computation for fragmented data,
  1164. all fragments should have the same value of
  1165. .Va csum_flags .
  1166. .Sh STRESS TESTING
  1167. When running a kernel compiled with the option
  1168. .Dv MBUF_STRESS_TEST ,
  1169. the following
  1170. .Xr sysctl 8 Ns
  1171. -controlled options may be used to create
  1172. various failure/extreme cases for testing of network drivers
  1173. and other parts of the kernel that rely on
  1174. .Vt mbufs .
  1175. .Bl -tag -width ident
  1176. .It Va net.inet.ip.mbuf_frag_size
  1177. Causes
  1178. .Fn ip_output
  1179. to fragment outgoing
  1180. .Vt mbuf chains
  1181. into fragments of the specified size.
  1182. Setting this variable to 1 is an excellent way to
  1183. test the long
  1184. .Vt mbuf chain
  1185. handling ability of network drivers.
  1186. .It Va kern.ipc.m_defragrandomfailures
  1187. Causes the function
  1188. .Fn m_defrag
  1189. to randomly fail, returning
  1190. .Dv NULL .
  1191. Any piece of code which uses
  1192. .Fn m_defrag
  1193. should be tested with this feature.
  1194. .El
  1195. .Sh RETURN VALUES
  1196. See above.
  1197. .Sh SEE ALSO
  1198. .Xr ifnet 9 ,
  1199. .Xr mbuf_tags 9
  1200. .Sh HISTORY
  1201. .\" Please correct me if I'm wrong
  1202. .Vt Mbufs
  1203. appeared in an early version of
  1204. .Bx .
  1205. Besides being used for network packets, they were used
  1206. to store various dynamic structures, such as routing table
  1207. entries, interface addresses, protocol control blocks, etc.
  1208. In more recent
  1209. .Fx
  1210. use of
  1211. .Vt mbufs
  1212. is almost entirely limited to packet storage, with
  1213. .Xr uma 9
  1214. zones being used directly to store other network-related memory.
  1215. .Pp
  1216. Historically, the
  1217. .Vt mbuf
  1218. allocator has been a special-purpose memory allocator able to run in
  1219. interrupt contexts and allocating from a special kernel address space map.
  1220. As of
  1221. .Fx 5.3 ,
  1222. the
  1223. .Vt mbuf
  1224. allocator is a wrapper around
  1225. .Xr uma 9 ,
  1226. allowing caching of
  1227. .Vt mbufs ,
  1228. clusters, and
  1229. .Vt mbuf
  1230. + cluster pairs in per-CPU caches, as well as bringing other benefits of
  1231. slab allocation.
  1232. .Sh AUTHORS
  1233. The original
  1234. .Nm
  1235. manual page was written by Yar Tikhiy.
  1236. The
  1237. .Xr uma 9
  1238. .Vt mbuf
  1239. allocator was written by Bosko Milekic.