2 * Copyright (c) 2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 This header defines an API for interacting with mbufs. mbufs are the
31 primary method of storing packets in the networking stack.
33 mbufs are used to store various items in the networking stack. The
34 most common usage of an mbuf is to store a packet or data on a
35 socket waiting to be sent or received. The mbuf is a contiguous
36 structure with some header followed by some data. To store more data
37 than would fit in an mbuf, external data is used. Most mbufs with
38 external data use clusters to store the external data.
40 mbufs can be chained, contiguous data in a packet can be found by
41 following the m_next chain. Packets may be bundled together using
42 m_nextpacket. Many parts of the stack do not properly handle chains
43 of packets. When in doubt, don't chain packets.
48 #include <sys/kernel_types.h>
49 #include <mach/vm_types.h>
53 @abstract Constants defining mbuf flags. Only the flags listed below
54 can be set or retrieved.
55 @constant MBUF_EXT Indicates this mbuf has external data.
56 @constant MBUF_PKTHDR Indicates this mbuf has a packet header.
57 @constant MBUF_EOR Indicates this mbuf is the end of a record.
58 @constant MBUF_BCAST Indicates this packet will be sent or was
59 received as a brodcast.
60 @constant MBUF_MCAST Indicates this packet will be sent or was
61 received as a multicast.
62 @constant MBUF_FRAG Indicates this packet is a fragment of a larger
64 @constant MBUF_FIRSTFRAG Indicates this packet is the first fragment.
65 @constant MBUF_LASTFRAG Indicates this packet is the last fragment.
66 @constant MBUF_PROMISC Indicates this packet was only received
67 because the interface is in promiscuous mode. This should be set
68 by the demux function. These packets will be discarded after
69 being passed to any interface filters.
72 MBUF_EXT
= 0x0001, /* has associated external storage */
73 MBUF_PKTHDR
= 0x0002, /* start of record */
74 MBUF_EOR
= 0x0004, /* end of record */
76 MBUF_BCAST
= 0x0100, /* send/received as link-level broadcast */
77 MBUF_MCAST
= 0x0200, /* send/received as link-level multicast */
78 MBUF_FRAG
= 0x0400, /* packet is a fragment of a larger packet */
79 MBUF_FIRSTFRAG
= 0x0800, /* packet is first fragment */
80 MBUF_LASTFRAG
= 0x1000, /* packet is last fragment */
81 MBUF_PROMISC
= 0x2000 /* packet is promiscuous */
83 typedef u_int32_t mbuf_flags_t
;
87 @abstract Types of mbufs.
88 @discussion Some mbufs represent packets, some represnt data waiting
89 on sockets. Other mbufs store control data or other various
90 structures. The mbuf type is used to store what sort of data the
92 @constant MBUF_MT_FREE Indicates the mbuf is free and is
93 sitting on the queue of free mbufs. If you find that an mbuf you
94 have a reference to has this type, something has gone terribly
96 @constant MBUF_MT_DATA Indicates this mbuf is being used to store
98 @constant MBUF_MT_HEADER Indicates this mbuf has a packet header,
99 this is probably a packet.
100 @constant MBUF_MT_SOCKET Socket structure.
101 @constant MBUF_MT_PCB Protocol control block.
102 @constant MBUF_MT_RTABLE Routing table entry.
103 @constant MBUF_MT_HTABLE IMP host tables???.
104 @constant MBUF_MT_ATABLE Address resolution table data.
105 @constant MBUF_MT_SONAME Socket name, usually a sockaddr of some
107 @constant MBUF_MT_FTABLE Fragment reassembly header.
108 @constant MBUF_MT_RIGHTS Access rights.
109 @constant MBUF_MT_IFADDR Interface address.
110 @constant MBUF_MT_CONTROL Extra-data protocol message (control
112 @constant MBUF_MT_OOBDATA Out of band data.
115 MBUF_TYPE_FREE
= 0, /* should be on free list */
116 MBUF_TYPE_DATA
= 1, /* dynamic (data) allocation */
117 MBUF_TYPE_HEADER
= 2, /* packet header */
118 MBUF_TYPE_SOCKET
= 3, /* socket structure */
119 MBUF_TYPE_PCB
= 4, /* protocol control block */
120 MBUF_TYPE_RTABLE
= 5, /* routing tables */
121 MBUF_TYPE_HTABLE
= 6, /* IMP host tables */
122 MBUF_TYPE_ATABLE
= 7, /* address resolution tables */
123 MBUF_TYPE_SONAME
= 8, /* socket name */
124 MBUF_TYPE_SOOPTS
= 10, /* socket options */
125 MBUF_TYPE_FTABLE
= 11, /* fragment reassembly header */
126 MBUF_TYPE_RIGHTS
= 12, /* access rights */
127 MBUF_TYPE_IFADDR
= 13, /* interface address */
128 MBUF_TYPE_CONTROL
= 14, /* extra-data protocol message */
129 MBUF_TYPE_OOBDATA
= 15 /* expedited data */
131 typedef u_int32_t mbuf_type_t
;
134 @enum mbuf_csum_request_flags_t
135 @abstract Checksum performed/requested flags.
136 @discussion Mbufs often contain packets. Some hardware supports
137 performing checksums in hardware. The stack uses these flags to
138 indicate to the driver what sort of checksumming should be
139 handled in by the driver/hardware. These flags will only be set
140 if the driver indicates that it supports the corresponding
141 checksums using ifnet_set_offload.
142 @constant MBUF_CSUM_REQ_IP Indicates the IP checksum has not been
144 @constant MBUF_CSUM_REQ_TCP Indicates the TCP checksum has not been
146 @constant MBUF_CSUM_REQ_UDP Indicates the UDP checksum has not been
150 MBUF_TSO_IPV4
= 0x100000,
151 MBUF_TSO_IPV6
= 0x200000
153 typedef u_int32_t mbuf_tso_request_flags_t
;
156 #ifdef KERNEL_PRIVATE
157 MBUF_CSUM_REQ_SUM16
= 0x1000, /* Weird apple hardware checksum */
158 #endif /* KERNEL_PRIVATE */
159 MBUF_CSUM_REQ_IP
= 0x0001,
160 MBUF_CSUM_REQ_TCP
= 0x0002,
161 MBUF_CSUM_REQ_UDP
= 0x0004
163 typedef u_int32_t mbuf_csum_request_flags_t
;
166 @enum mbuf_csum_performed_flags_t
167 @abstract Checksum performed/requested flags.
168 @discussion Mbufs often contain packets. Some hardware supports
169 performing checksums in hardware. The driver uses these flags to
170 communicate to the stack the checksums that were calculated in
172 @constant MBUF_CSUM_DID_IP Indicates that the driver/hardware verified
173 the IP checksum in hardware.
174 @constant MBUF_CSUM_IP_GOOD Indicates whether or not the IP checksum
175 was good or bad. Only valid when MBUF_CSUM_DID_IP is set.
176 @constant MBUF_CSUM_DID_DATA Indicates that the TCP or UDP checksum
177 was calculated. The value for the checksum calculated in
178 hardware should be passed as the second parameter of
179 mbuf_set_csum_performed. The hardware calculated checksum value
180 can be retrieved using the second parameter passed to
181 mbuf_get_csum_performed.
182 @constant MBUF_CSUM_PSEUDO_HDR If set, this indicates that the
183 checksum value for MBUF_CSUM_DID_DATA includes the pseudo header
184 value. If this is not set, the stack will calculate the pseudo
185 header value and add that to the checksum. The value of this bit
186 is only valid when MBUF_CSUM_DID_DATA is set.
189 #ifdef KERNEL_PRIVATE
190 MBUF_CSUM_TCP_SUM16
= MBUF_CSUM_REQ_SUM16
, /* Weird apple hardware checksum */
191 #endif /* KERNEL_PRIVATE */
192 MBUF_CSUM_DID_IP
= 0x0100,
193 MBUF_CSUM_IP_GOOD
= 0x0200,
194 MBUF_CSUM_DID_DATA
= 0x0400,
195 MBUF_CSUM_PSEUDO_HDR
= 0x0800
197 typedef u_int32_t mbuf_csum_performed_flags_t
;
201 @abstract Method of allocating an mbuf.
202 @discussion Blocking will cause the funnel to be dropped. If the
203 funnel is dropped, other threads may make changes to networking
204 data structures. This can lead to very bad things happening.
205 Blocking on the input our output path can also impact
206 performance. There are some cases where making a blocking call
207 is acceptable. When in doubt, use MBUF_DONTWAIT.
208 @constant MBUF_WAITOK Allow a call to allocate an mbuf to block.
209 @constant MBUF_DONTWAIT Don't allow the mbuf allocation call to
210 block, if blocking is necessary fail and return immediately.
213 MBUF_WAITOK
= 0, /* Ok to block to get memory */
214 MBUF_DONTWAIT
= 1 /* Don't block, fail if blocking would be required */
216 typedef u_int32_t mbuf_how_t
;
218 typedef u_int32_t mbuf_tag_id_t
;
219 typedef u_int16_t mbuf_tag_type_t
;
223 @discussion The mbuf_stat contains mbuf statistics.
224 @field mbufs Number of mbufs (free or otherwise).
225 @field clusters Number of clusters (free or otherwise).
226 @field clfree Number of free clusters.
227 @field drops Number of times allocation failed.
228 @field wait Number of times allocation blocked.
229 @field drain Number of times protocol drain functions were called.
230 @field mtypes An array of counts of each type of mbuf allocated.
231 @field mcfail Number of times m_copym failed.
232 @field mpfail Number of times m_pullup failed.
233 @field msize Length of an mbuf.
234 @field mclbytes Length of an mbuf cluster.
235 @field minclsize Minimum length of data to allocate a cluster.
236 Anything smaller than this should be placed in chained mbufs.
237 @field mlen Length of data in an mbuf.
238 @field mhlen Length of data in an mbuf with a packet header.
239 @field bigclusters Number of big clusters.
240 @field bigclfree Number of unused big clusters.
241 @field bigmclbytes Length of a big mbuf cluster.
244 u_int32_t mbufs
; /* mbufs obtained from page pool */
245 u_int32_t clusters
; /* clusters obtained from page pool */
246 u_int32_t clfree
; /* free clusters */
247 u_int32_t drops
; /* times failed to find space */
248 u_int32_t wait
; /* times waited for space */
249 u_int32_t drain
; /* times drained protocols for space */
250 u_short mtypes
[256]; /* type specific mbuf allocations */
251 u_int32_t mcfail
; /* times m_copym failed */
252 u_int32_t mpfail
; /* times m_pullup failed */
253 u_int32_t msize
; /* length of an mbuf */
254 u_int32_t mclbytes
; /* length of an mbuf cluster */
255 u_int32_t minclsize
; /* min length of data to allocate a cluster */
256 u_int32_t mlen
; /* length of data in an mbuf */
257 u_int32_t mhlen
; /* length of data in a header mbuf */
258 u_int32_t bigclusters
; /* number of big clusters */
259 u_int32_t bigclfree
; /* number of big clustser free */
260 u_int32_t bigmclbytes
; /* length of data in a big cluster */
263 /* Parameter for m_copym to copy all bytes */
264 #define MBUF_COPYALL 1000000000
270 @discussion Returns a pointer to the start of data in this mbuf.
271 There may be additional data on chained mbufs. The data you're
272 looking for may not be virtually contiguous if it spans more
273 than one mbuf. In addition, data that is virtually contiguous
274 might not be represented by physically contiguous pages; see
275 further comments in mbuf_data_to_physical. Use mbuf_len to
276 determine the lenght of data available in this mbuf. If a data
277 structure you want to access stradles two mbufs in a chain,
278 either use mbuf_pullup to get the data contiguous in one mbuf
279 or copy the pieces of data from each mbuf in to a contiguous
280 buffer. Using mbuf_pullup has the advantage of not having to
281 copy the data. On the other hand, if you don't make sure there
282 is space in the mbuf, mbuf_pullup may fail and free the mbuf.
283 @param mbuf The mbuf.
284 @result A pointer to the data in the mbuf.
286 extern void *mbuf_data(mbuf_t mbuf
);
289 @function mbuf_datastart
290 @discussion Returns the start of the space set aside for storing
291 data in an mbuf. An mbuf's data may come from a cluster or be
292 embedded in the mbuf structure itself. The data pointer
293 retrieved by mbuf_data may not be at the start of the data
294 (mbuf_leadingspace will be non-zero). This function will return
295 a pointer that matches mbuf_data() - mbuf_leadingspace().
296 @param mbuf The mbuf.
297 @result A pointer to smallest possible value for data.
299 extern void *mbuf_datastart(mbuf_t mbuf
);
302 @function mbuf_setdata
303 @discussion Sets the data and length values for an mbuf. The data
304 value must be in a valid range. In the case of an mbuf with a cluster,
305 the data value must point to a location in the cluster and the data
306 value plus the length, must be less than the end of the cluster. For
307 data embedded directly in an mbuf (no cluster), the data value must
308 fall somewhere between the start and end of the data area in the
309 mbuf and the data + length must also be in the same range.
310 @param mbuf The mbuf.
311 @param data The new pointer value for data.
312 @param len The new length of data in the mbuf.
313 @result 0 on success, errno error on failure.
315 extern errno_t
mbuf_setdata(mbuf_t mbuf
, void *data
, size_t len
);
318 @function mbuf_align_32
319 @discussion mbuf_align_32 is a replacement for M_ALIGN and MH_ALIGN.
320 mbuf_align_32 will set the data pointer to a location aligned on
321 a four byte boundry with at least 'len' bytes between the data
322 pointer and the end of the data block.
323 @param mbuf The mbuf.
324 @param len The minimum length of space that should follow the new
326 @result 0 on success, errno error on failure.
328 extern errno_t
mbuf_align_32(mbuf_t mbuf
, size_t len
);
331 @function mbuf_data_to_physical
332 @discussion mbuf_data_to_physical is a replacement for mcl_to_paddr.
333 Given a pointer returned from mbuf_data of mbuf_datastart,
334 mbuf_data_to_physical will return the phyical address for that
335 block of data. Note that even though the data is in virtually
336 contiguous span, the underlying physical pages might not be
337 physically contiguous. Because of this, callers must ensure
338 to call this routine for each page boundary. Device drivers
339 that deal with DMA are strongly encouraged to utilize the
340 IOMbufNaturalMemoryCursor and walk down the list of vectors
341 instead of using this interface to obtain the physical address.
342 Use of this routine is therefore discouraged.
343 @param ptr A pointer to data stored in an mbuf.
344 @result The 64 bit physical address of the mbuf data or NULL if ptr
345 does not point to data stored in an mbuf.
347 extern addr64_t
mbuf_data_to_physical(void *ptr
);
354 @discussion Allocates an mbuf without a cluster for external data.
355 @param how Blocking or non-blocking.
356 @param type The type of the mbuf.
357 @param mbuf The mbuf.
358 @result 0 on success, errno error on failure.
360 extern errno_t
mbuf_get(mbuf_how_t how
, mbuf_type_t type
, mbuf_t
*mbuf
);
363 @function mbuf_gethdr
364 @discussion Allocates an mbuf without a cluster for external data.
365 Sets a flag to indicate there is a packet header and initializes
367 @param how Blocking or non-blocking.
368 @param type The type of the mbuf.
369 @param mbuf The mbuf.
370 @result 0 on success, errno error on failure.
372 extern errno_t
mbuf_gethdr(mbuf_how_t how
, mbuf_type_t type
, mbuf_t
*mbuf
);
375 @function mbuf_attachcluster
376 @discussion Attach an external buffer as a cluster for an mbuf. If mbuf
377 points to a NULL mbuf_t, an mbuf will be allocated for you. If
378 mbuf points to a non-NULL mbuf_t, the user-supplied mbuf will
379 be used instead. The caller is responsible for allocating the
380 external buffer by calling mbuf_alloccluster().
381 @param how Blocking or non-blocking.
382 @param type The type of the mbuf if mbuf is non-NULL; otherwise ignored.
383 @param mbuf Pointer to the address of the mbuf; if NULL, an mbuf will
384 be allocated, otherwise, it must point to a valid mbuf address.
385 If the user-supplied mbuf is already attached to a cluster, the
386 current cluster will be freed before the mbuf gets attached to
387 the supplied external buffer. Note that this routine may return
388 a different mbuf_t than the one you passed in.
389 @param extbuf Address of the external buffer.
390 @param extfree Free routine for the external buffer; the caller is
391 required to defined a routine that will be invoked when the
393 @param extsize Size of the external buffer.
394 @param extarg Private value that will be passed to the free routine
395 when it is called at the time the mbuf is freed.
397 EINVAL - Invalid parameter
398 ENOMEM - Not enough memory available
400 extern errno_t
mbuf_attachcluster(mbuf_how_t how
, mbuf_type_t type
,
401 mbuf_t
*mbuf
, caddr_t extbuf
, void (*extfree
)(caddr_t
, u_int
, caddr_t
),
402 size_t extsize
, caddr_t extarg
);
405 @function mbuf_alloccluster
406 @discussion Allocate a cluster that can be later attached to an
407 mbuf by calling mbuf_attachcluster(). The allocated cluster
408 can also be freed (without being attached to an mbuf) by
409 calling mbuf_freecluster(). At the moment this routine
410 will either return a cluster of 2048, 4096 or 16384 bytes
411 depending on the requested size. Note that clusters greater
412 than 4096 bytes might not be available in all configurations;
413 the caller must additionally check for ENOTSUP (see below).
414 @param how Blocking or non-blocking.
415 @param size Pointer to size of requested cluster. Sizes up to 2048
416 will be rounded up to 2048; sizes greater than 2048 and up
417 to 4096 will be rounded up to 4096. Sizes greater than 4096
418 will be rounded up to 16384.
419 @param addr Pointer to the address of the requested cluster.
420 @result 0 on success or ENOMEM if failure. If the caller requests
421 greater than 4096 bytes and the system is unable to fulfill
422 the request due to the lack of jumbo clusters support based
423 on the configuration, this routine will return ENOTSUP.
424 In this case, the caller is advised to use 4096 bytes or
425 smaller during subseqent requests.
427 extern errno_t
mbuf_alloccluster(mbuf_how_t how
, size_t *size
, caddr_t
*addr
);
430 @function mbuf_freecluster
431 @discussion Free a cluster that was previously allocated by a call
432 to mbuf_alloccluster(). The caller must pass the actual
433 size of the cluster as returned by mbuf_alloccluster(),
434 which at this point must be either 2048, 4096 or 16384 bytes.
435 @param addr The address of the cluster.
436 @param size The actual size of the cluster.
438 extern void mbuf_freecluster(caddr_t addr
, size_t size
);
441 @function mbuf_getcluster
442 @discussion Allocate a cluster of the requested size and attach it to
443 an mbuf for use as external data. If mbuf points to a NULL
444 mbuf_t, an mbuf will be allocated for you. If mbuf points to
445 a non-NULL mbuf_t, mbuf_getcluster may return a different
446 mbuf_t than the one you passed in.
447 @param how Blocking or non-blocking.
448 @param type The type of the mbuf.
449 @param size The size of the cluster to be allocated. Supported sizes
450 for a cluster are be 2048, 4096, or 16384. Any other value
451 with return EINVAL. Note that clusters greater than 4096
452 bytes might not be available in all configurations; the
453 caller must additionally check for ENOTSUP (see below).
454 @param mbuf The mbuf the cluster will be attached to.
455 @result 0 on success, errno error on failure. If you specified NULL
456 for the mbuf, any intermediate mbuf that may have been allocated
457 will be freed. If you specify an mbuf value in *mbuf,
458 mbuf_mclget will not free it.
459 EINVAL - Invalid parameter
460 ENOMEM - Not enough memory available
461 ENOTSUP - The caller had requested greater than 4096 bytes
462 cluster and the system is unable to fulfill it due to the
463 lack of jumbo clusters support based on the configuration.
464 In this case, the caller is advised to use 4096 bytes or
465 smaller during subsequent requests.
467 extern errno_t
mbuf_getcluster(mbuf_how_t how
, mbuf_type_t type
, size_t size
,
471 @function mbuf_mclget
472 @discussion Allocate a cluster and attach it to an mbuf for use as
473 external data. If mbuf points to a NULL mbuf_t, an mbuf will be
474 allocated for you. If mbuf points to a non-NULL mbuf_t,
475 mbuf_mclget may return a different mbuf_t than the one you
477 @param how Blocking or non-blocking.
478 @param type The type of the mbuf.
479 @param mbuf The mbuf the cluster will be attached to.
480 @result 0 on success, errno error on failure. If you specified NULL
481 for the mbuf, any intermediate mbuf that may have been allocated
482 will be freed. If you specify an mbuf value in *mbuf,
483 mbuf_mclget will not free it.
485 extern errno_t
mbuf_mclget(mbuf_how_t how
, mbuf_type_t type
, mbuf_t
*mbuf
);
488 @function mbuf_allocpacket
489 @discussion Allocate an mbuf chain to store a single packet of the
490 requested length. According to the requested length, a chain
491 of mbufs will be created. The mbuf type will be set to
492 MBUF_TYPE_DATA. The caller may specify the maximum number of
494 @param how Blocking or non-blocking
495 @param packetlen The total length of the packet mbuf to be allocated.
496 The length must be greater than zero.
497 @param maxchunks An input/output pointer to the maximum number of mbufs
498 segments making up the chain. On input, if maxchunks is NULL,
499 or the value pointed to by maxchunks is zero, the packet will
500 be made up of as few or as many buffer segments as necessary
501 to fit the length. The allocation will fail with ENOBUFS if
502 the number of segments requested is too small and the sum of
503 the maximum size of each individual segment is less than the
504 packet length. On output, if the allocation succeed and
505 maxchunks is non-NULL, it will point to the actual number
506 of segments allocated.
507 Additional notes for packetlen greater than 4096 bytes:
508 the caller may pass a non-NULL maxchunks and initialize it
509 with zero such that upon success, it can find out whether
510 or not the system configuration allows for larger than
511 4096 bytes cluster allocations, by checking on the value
512 pointed to by maxchunks. E.g. a request for 9018 bytes may
513 result in 1 chunk when jumbo clusters are available, or
515 @param Upon success, *mbuf will be a reference to the new mbuf.
516 @result Returns 0 upon success or the following error code:
517 EINVAL - Invalid parameter
518 ENOMEM - Not enough memory available
519 ENOBUFS - Buffers not big enough for the maximum number of
522 extern errno_t
mbuf_allocpacket(mbuf_how_t how
, size_t packetlen
,
523 unsigned int * maxchunks
, mbuf_t
*mbuf
);
526 @function mbuf_allocpacket_list
527 @discussion Allocate a linked list of packets. According to the
528 requested length, each packet will made of a chain of one
529 or more mbufs. The mbuf type will be set to MBUF_TYPE_DATA.
530 The caller may specify the maximum number of element for
531 each mbuf chain making up a packet.
532 @param numpkts Number of packets to allocate
533 @param how Blocking or non-blocking
534 @param packetlen The total length of the packet mbuf to be allocated.
535 The length must be greater than zero.
536 @param maxchunks An input/output pointer to the maximum number of
537 mbufs segments making up the chain. On input, if maxchunks is
538 zero, or the value pointed to by maxchunks is zero, the packet
539 will be made of as few or as many buffer segments as necessary
540 to fit the length. The allocation will fail with ENOBUFS if
541 the number of segments requested is too small and the sum of
542 the maximum size of each individual segment is less than the
543 packet length. On output, if the allocation succeed and
544 maxchunks is non zero, it will point to the actual number
545 of segments allocated.
546 Additional notes for packetlen greater than 4096 bytes:
547 the caller may pass a non-NULL maxchunks and initialize it
548 with zero such that upon success, it can find out whether
549 or not the system configuration allows for larger than
550 4096 bytes cluster allocations, by checking on the value
551 pointed to by maxchunks. E.g. a request for 9018 bytes may
552 result in 1 chunk when jumbo clusters are available, or
554 @param Upon success, *mbuf will be a reference to the new mbuf.
555 @result Returns 0 upon success or the following error code:
556 EINVAL - Invalid parameter
557 ENOMEM - Not enough memory available
558 ENOBUFS - Buffers not big enough for the maximum number of
561 extern errno_t
mbuf_allocpacket_list(unsigned int numpkts
, mbuf_how_t how
,
562 size_t packetlen
, unsigned int * maxchunks
, mbuf_t
*mbuf
);
566 @function mbuf_getpacket
567 @discussion Allocate an mbuf, allocate and attach a cluster, and set
568 the packet header flag.
569 @param how Blocking or non-blocking.
570 @param mbuf Upon success, *mbuf will be a reference to the new mbuf.
571 @result 0 on success, errno error on failure.
573 extern errno_t
mbuf_getpacket(mbuf_how_t how
, mbuf_t
*mbuf
);
577 @discussion Frees a single mbuf. Not commonly used because it
578 doesn't touch the rest of the mbufs on the chain.
579 @param mbuf The mbuf to free.
580 @result The next mbuf in the chain.
582 extern mbuf_t
mbuf_free(mbuf_t mbuf
);
586 @discussion Frees a chain of mbufs link through mnext.
587 @param mbuf The first mbuf in the chain to free.
589 extern void mbuf_freem(mbuf_t mbuf
);
592 @function mbuf_freem_list
593 @discussion Frees linked list of mbuf chains. Walks through
594 mnextpackt and does the equivalent of mbuf_freem to each.
595 @param mbuf The first mbuf in the linked list to free.
596 @result The number of mbufs freed.
598 extern int mbuf_freem_list(mbuf_t mbuf
);
601 @function mbuf_leadingspace
602 @discussion Determines the space available in the mbuf proceeding
604 @param mbuf The mbuf.
605 @result The number of unused bytes at the start of the mbuf.
607 extern size_t mbuf_leadingspace(const mbuf_t mbuf
);
610 @function mbuf_trailingspace
611 @discussion Determines the space available in the mbuf following
613 @param mbuf The mbuf.
614 @result The number of unused bytes following the current data.
616 extern size_t mbuf_trailingspace(const mbuf_t mbuf
);
622 @discussion Copies len bytes from offset from src to a new mbuf. If
623 the original mbuf contains a packet header, the new mbuf will
624 contain similar packet header except for any tags which may be
625 associated with the original mbuf. mbuf_dup() should be used
626 instead if tags are to be copied to the new mbuf.
627 @param src The source mbuf.
628 @param offset The offset in the mbuf to start copying from.
629 @param len The the number of bytes to copy.
630 @param how To block or not to block, that is a question.
631 @param new_mbuf Upon success, the newly allocated mbuf.
632 @result 0 upon success otherwise the errno error.
634 extern errno_t
mbuf_copym(const mbuf_t src
, size_t offset
, size_t len
,
635 mbuf_how_t how
, mbuf_t
*new_mbuf
);
639 @discussion Exactly duplicates an mbuf chain. If the original mbuf
640 contains a packet header (including tags), the new mbuf will have
641 the same packet header contents and a copy of each tag associated
642 with the original mbuf.
643 @param src The source mbuf.
644 @param how Blocking or non-blocking.
645 @param new_mbuf Upon success, the newly allocated mbuf.
646 @result 0 upon success otherwise the errno error.
648 extern errno_t
mbuf_dup(const mbuf_t src
, mbuf_how_t how
, mbuf_t
*new_mbuf
);
651 @function mbuf_prepend
652 @discussion Prepend len bytes to an mbuf. If there is space
653 (mbuf_leadingspace >= len), the mbuf's data ptr is changed and
654 the same mbuf is returned. If there is no space, a new mbuf may
655 be allocated and prepended to the mbuf chain. If the operation
656 fails, the mbuf may be freed (*mbuf will be NULL).
657 @param mbuf The mbuf to prepend data to. This may change if a new
658 mbuf must be allocated or may be NULL if the operation fails.
659 @param len The length, in bytes, to be prepended to the mbuf.
660 @param how Blocking or non-blocking.
661 @result 0 upon success otherwise the errno error.
663 extern errno_t
mbuf_prepend(mbuf_t
*mbuf
, size_t len
, mbuf_how_t how
);
667 @discussion Split an mbuf chain at a specific offset.
668 @param src The mbuf to be split.
669 @param offset The offset in the buffer where the mbuf should be
671 @param how Blocking or non-blocking.
672 @param new_mbuf Upon success, the second half of the split mbuf
674 @result 0 upon success otherwise the errno error. In the case of
675 failure, the original mbuf chain passed in to src will be
678 extern errno_t
mbuf_split(mbuf_t src
, size_t offset
, mbuf_how_t how
,
682 @function mbuf_pullup
683 @discussion Move the next len bytes in to mbuf from other mbufs in
684 the chain. This is commonly used to get the IP and TCP or UDP
685 header contiguous in the first mbuf. If mbuf_pullup fails, the
686 entire mbuf chain will be freed.
687 @param mbuf The mbuf in the chain the data should be contiguous in.
688 @param len The number of bytes to pull from the next mbuf(s).
689 @result 0 upon success otherwise the errno error. In the case of an
690 error, the mbuf chain has been freed.
692 extern errno_t
mbuf_pullup(mbuf_t
*mbuf
, size_t len
);
695 @function mbuf_pulldown
696 @discussion Make length bytes at offset in the mbuf chain
697 contiguous. Nothing before offset bytes in the chain will be
698 modified. Upon return, location will be the mbuf the data is
699 contiguous in and offset will be the offset in that mbuf at
700 which the data is located. In the case of a failure, the mbuf
702 @param src The start of the mbuf chain.
703 @param offset Pass in a pointer to a value with the offset of the
704 data you're interested in making contiguous. Upon success, this
705 will be overwritten with the offset from the mbuf returned in
707 @param length The length of data that should be made contiguous.
708 @param location Upon success, *location will be the mbuf the data is
710 @result 0 upon success otherwise the errno error.
712 extern errno_t
mbuf_pulldown(mbuf_t src
, size_t *offset
, size_t length
,
717 @discussion Trims len bytes from the mbuf. If the length is greater
718 than zero, the bytes are trimmed from the front of the mbuf. If
719 the length is less than zero, the bytes are trimmed from the end
721 @param mbuf The mbuf chain to trim.
722 @param len The number of bytes to trim from the mbuf chain.
724 extern void mbuf_adj(mbuf_t mbuf
, int len
);
727 @function mbuf_adjustlen
728 @discussion Adds amount to the mbuf len. Verifies that the new
729 length is valid (greater than or equal to zero and less than
730 maximum amount of data that may be stored in the mbuf). This
731 function will not adjust the packet header length field or
732 affect any other mbufs in a chain.
733 @param mbuf The mbuf to adjust.
734 @param amount The number of bytes increment the length by.
735 @result 0 upon success otherwise the errno error.
737 extern errno_t
mbuf_adjustlen(mbuf_t mbuf
, int amount
);
740 @function mbuf_concatenate
741 @discussion Concatenate mbuf chain src to dst using m_next and return
742 a chain which represents the concatenated chain. The routine
743 does not prevent two chains of different mbuf types to be
744 concatenated, nor does it modify any packet header in the
745 destination chain. Therefore, it's the responsibility of the
746 caller to ensure that the resulted concatenated mbuf chain is
747 correct for further usages.
748 @param dst The destination mbuf chain.
749 @param src The source mbuf chain.
750 @result A pointer to the head of the concatenated mbuf chain. This
751 should be treated as the updated destination mbuf chain; the
752 caller must no longer refer to the original src or dst mbuf
753 chain. Otherwise it returns NULL if the original dst mbuf
756 extern mbuf_t
mbuf_concatenate(mbuf_t dst
, mbuf_t src
);
759 @function mbuf_copydata
760 @discussion Copies data out of an mbuf in to a specified buffer. If
761 the data is stored in a chain of mbufs, the data will be copied
762 from each mbuf in the chain until length bytes have been copied.
763 @param mbuf The mbuf chain to copy data out of.
764 @param offset The offset in to the mbuf to start copying.
765 @param length The number of bytes to copy.
766 @param out_data A pointer to the location where the data will be
768 @result 0 upon success otherwise the errno error.
770 extern errno_t
mbuf_copydata(const mbuf_t mbuf
, size_t offset
, size_t length
,
774 @function mbuf_copyback
775 @discussion Copies data from a buffer to an mbuf chain.
776 mbuf_copyback will grow the chain to fit the specified buffer.
778 If mbuf_copydata is unable to allocate enough mbufs to grow the
779 chain, ENOBUFS will be returned. The mbuf chain will be shorter
780 than expected but all of the data up to the end of the mbuf
783 If an offset is specified, mbuf_copyback will skip that many
784 bytes in the mbuf chain before starting to write the buffer in
785 to the chain. If the mbuf chain does not contain this many
786 bytes, mbufs will be allocated to create the space.
787 @param mbuf The first mbuf in the chain to copy the data in to.
788 @param offset Offset in bytes to skip before copying data.
789 @param length The length, in bytes, of the data to copy in to the mbuf
791 @param data A pointer to data in the kernel's address space.
792 @param how Blocking or non-blocking.
793 @result 0 upon success, EINVAL or ENOBUFS upon failure.
795 extern errno_t
mbuf_copyback(mbuf_t mbuf
, size_t offset
, size_t length
,
796 const void *data
, mbuf_how_t how
);
799 @function mbuf_mclhasreference
800 @discussion Check if a cluster of an mbuf is referenced by another mbuf.
801 References may be taken, for example, as a result of a call to
802 mbuf_split or mbuf_copym
803 @param mbuf The mbuf with the cluster to test.
804 @result 0 if there is no reference by another mbuf, 1 otherwise.
806 extern int mbuf_mclhasreference(mbuf_t mbuf
);
813 @discussion Returns the next mbuf in the chain.
814 @param mbuf The mbuf.
815 @result The next mbuf in the chain.
817 extern mbuf_t
mbuf_next(const mbuf_t mbuf
);
820 @function mbuf_setnext
821 @discussion Sets the next mbuf in the chain.
822 @param mbuf The mbuf.
823 @param next The new next mbuf.
824 @result 0 upon success otherwise the errno error.
826 extern errno_t
mbuf_setnext(mbuf_t mbuf
, mbuf_t next
);
829 @function mbuf_nextpkt
830 @discussion Gets the next packet from the mbuf.
831 @param mbuf The mbuf.
834 extern mbuf_t
mbuf_nextpkt(const mbuf_t mbuf
);
837 @function mbuf_setnextpkt
838 @discussion Sets the next packet attached to this mbuf.
839 @param mbuf The mbuf.
840 @param nextpkt The new next packet.
842 extern void mbuf_setnextpkt(mbuf_t mbuf
, mbuf_t nextpkt
);
846 @discussion Gets the length of data in this mbuf.
847 @param mbuf The mbuf.
850 extern size_t mbuf_len(const mbuf_t mbuf
);
853 @function mbuf_setlen
854 @discussion Sets the length of data in this packet. Be careful to
855 not set the length over the space available in the mbuf.
856 @param mbuf The mbuf.
857 @param len The new length.
858 @result 0 upon success otherwise the errno error.
860 extern void mbuf_setlen(mbuf_t mbuf
, size_t len
);
863 @function mbuf_maxlen
864 @discussion Retrieves the maximum length of data that may be stored
865 in this mbuf. This value assumes that the data pointer was set
866 to the start of the possible range for that pointer
868 @param mbuf The mbuf.
869 @result The maximum lenght of data for this mbuf.
871 extern size_t mbuf_maxlen(const mbuf_t mbuf
);
875 @discussion Gets the type of mbuf.
876 @param mbuf The mbuf.
879 extern mbuf_type_t
mbuf_type(const mbuf_t mbuf
);
882 @function mbuf_settype
883 @discussion Sets the type of mbuf.
884 @param mbuf The mbuf.
885 @param new_type The new type.
886 @result 0 upon success otherwise the errno error.
888 extern errno_t
mbuf_settype(mbuf_t mbuf
, mbuf_type_t new_type
);
892 @discussion Returns the set flags.
893 @param mbuf The mbuf.
896 extern mbuf_flags_t
mbuf_flags(const mbuf_t mbuf
);
899 @function mbuf_setflags
900 @discussion Sets the set of set flags.
901 @param mbuf The mbuf.
902 @param flags The flags that should be set, all other flags will be cleared.
903 @result 0 upon success otherwise the errno error.
905 extern errno_t
mbuf_setflags(mbuf_t mbuf
, mbuf_flags_t flags
);
908 @function mbuf_setflags_mask
909 @discussion Useful for setting or clearing individual flags. Easier
910 than calling mbuf_setflags(m, mbuf_flags(m) | M_FLAG).
911 @param mbuf The mbuf.
912 @param flags The flags that should be set or cleared.
913 @param mask The mask controlling which flags will be modified.
914 @result 0 upon success otherwise the errno error.
916 extern errno_t
mbuf_setflags_mask(mbuf_t mbuf
, mbuf_flags_t flags
,
920 @function mbuf_copy_pkthdr
921 @discussion Copies the packet header from src to dest.
922 @param src The mbuf from which the packet header will be copied.
923 @param mbuf The mbuf to which the packet header will be copied.
924 @result 0 upon success otherwise the errno error.
926 extern errno_t
mbuf_copy_pkthdr(mbuf_t dest
, const mbuf_t src
);
929 @function mbuf_pkthdr_len
930 @discussion Returns the length as reported by the packet header.
931 @param mbuf The mbuf containing the packet header with the length to
933 @result The length, in bytes, of the packet.
935 extern size_t mbuf_pkthdr_len(const mbuf_t mbuf
);
938 @function mbuf_pkthdr_setlen
939 @discussion Sets the length of the packet in the packet header.
940 @param mbuf The mbuf containing the packet header.
941 @param len The new length of the packet.
943 extern void mbuf_pkthdr_setlen(mbuf_t mbuf
, size_t len
);
946 @function mbuf_pkthdr_adjustlen
947 @discussion Adjusts the length of the packet in the packet header.
948 @param mbuf The mbuf containing the packet header.
949 @param amount The number of bytes to adjust the packet header length
952 extern void mbuf_pkthdr_adjustlen(mbuf_t mbuf
, int amount
);
955 @function mbuf_pkthdr_rcvif
956 @discussion Returns the interface the packet was received on. This
957 funciton does not modify the reference count of the interface.
958 The interface is only valid for as long as the mbuf is not freed
959 and the rcvif for the mbuf is not changed. Take a reference on
960 the interface that you will release later before doing any of
961 the following: free the mbuf, change the rcvif, pass the mbuf to
962 any function that may free the mbuf or change the rcvif.
963 @param mbuf The mbuf containing the packet header.
964 @result A reference to the interface.
966 extern ifnet_t
mbuf_pkthdr_rcvif(const mbuf_t mbuf
);
969 @function mbuf_pkthdr_setrcvif
970 @discussion Sets the interface the packet was received on.
971 @param mbuf The mbuf containing the packet header.
972 @param ifnet A reference to an interface.
973 @result 0 upon success otherwise the errno error.
975 extern errno_t
mbuf_pkthdr_setrcvif(mbuf_t mbuf
, ifnet_t ifp
);
978 @function mbuf_pkthdr_header
979 @discussion Returns a pointer to the packet header.
980 @param mbuf The mbuf containing the packet header.
981 @result A pointer to the packet header.
983 extern void *mbuf_pkthdr_header(const mbuf_t mbuf
);
986 @function mbuf_pkthdr_setheader
987 @discussion Sets the pointer to the packet header.
988 @param mbuf The mbuf containing the packet header.
989 @param ifnet A pointer to the header.
990 @result 0 upon success otherwise the errno error.
992 extern void mbuf_pkthdr_setheader(mbuf_t mbuf
, void *header
);
997 @function mbuf_inbound_modified
998 @discussion This function will clear the checksum flags to indicate
999 that a hardware checksum should not be used. Any filter
1000 modifying data should call this function on an mbuf before
1001 passing the packet up the stack. If a filter modifies a packet
1002 in a way that affects any checksum, the filter is responsible
1003 for either modifying the checksum to compensate for the changes
1004 or verifying the checksum before making the changes and then
1005 modifying the data and calculating a new checksum only if the
1006 original checksum was valid.
1007 @param mbuf The mbuf that has been modified.
1009 extern void mbuf_inbound_modified(mbuf_t mbuf
);
1012 @function mbuf_outbound_finalize
1013 @discussion This function will "finalize" the packet allowing your
1014 code to inspect the final packet.
1016 There are a number of operations that are performed in hardware,
1017 such as calculating checksums. This function will perform in
1018 software the various opterations that were scheduled to be done
1019 in hardware. Future operations may include IPSec processing or
1020 vlan support. If you are redirecting a packet to a new interface
1021 which may not have the same hardware support or encapsulating
1022 the packet, you should call this function to force the stack to
1023 calculate and fill out the checksums. This will bypass hardware
1024 checksums but give you a complete packet to work with. If you
1025 need to inspect aspects of the packet which may be generated by
1026 hardware, you must call this function to get an aproximate final
1027 packet. If you plan to modify the packet in any way, you should
1030 This function should be called before modifying any outbound
1033 This function may be called at various levels, in some cases
1034 additional headers may have already been prepended, such as the
1035 case of a packet seen by an interface filter. To handle this,
1036 the caller must pass the protocol family of the packet as well
1037 as the offset from the start of the packet to the protocol
1039 @param mbuf The mbuf that should be finalized.
1040 @param protocol_family The protocol family of the packet in the
1042 @param protocol_offset The offset from the start of the mbuf to the
1043 protocol header. For an IP packet with an ethernet header, this
1044 would be the length of an ethernet header.
1046 extern void mbuf_outbound_finalize(mbuf_t mbuf
, u_int32_t protocol_family
,
1047 size_t protocol_offset
);
1050 @function mbuf_set_vlan_tag
1051 @discussion This function is used by interfaces that support vlan
1052 tagging in hardware. This function will set properties in the
1053 mbuf to indicate which vlan the packet was received for.
1054 @param mbuf The mbuf containing the packet.
1055 @param vlan The protocol family of the aux data to add.
1056 @result 0 upon success otherwise the errno error.
1058 extern errno_t
mbuf_set_vlan_tag(mbuf_t mbuf
, u_int16_t vlan
);
1061 @function mbuf_get_vlan_tag
1062 @discussion This function is used by drivers that support hardware
1063 vlan tagging to determine which vlan this packet belongs to. To
1064 differentiate between the case where the vlan tag is zero and
1065 the case where there is no vlan tag, this function will return
1066 ENXIO when there is no vlan.
1067 @param mbuf The mbuf containing the packet.
1068 @param vlan The protocol family of the aux data to add.
1069 @result 0 upon success otherwise the errno error. ENXIO indicates
1070 that the vlan tag is not set.
1072 extern errno_t
mbuf_get_vlan_tag(mbuf_t mbuf
, u_int16_t
*vlan
);
1075 @function mbuf_clear_vlan_tag
1076 @discussion This function will clear any vlan tag associated with
1078 @param mbuf The mbuf containing the packet.
1079 @result 0 upon success otherwise the errno error.
1081 extern errno_t
mbuf_clear_vlan_tag(mbuf_t mbuf
);
1083 #ifdef KERNEL_PRIVATE
1085 @function mbuf_set_csum_requested
1086 @discussion This function is used by the stack to indicate which
1087 checksums should be calculated in hardware. The stack normally
1088 sets these flags as the packet is processed in the outbound
1089 direction. Just before send the packe to the interface, the
1090 stack will look at these flags and perform any checksums in
1091 software that are not supported by the interface.
1092 @param mbuf The mbuf containing the packet.
1093 @param request Flags indicating which checksums are being requested
1095 @param value This parameter is currently unsupported.
1096 @result 0 upon success otherwise the errno error.
1098 extern errno_t
mbuf_set_csum_requested(mbuf_t mbuf
,
1099 mbuf_csum_request_flags_t request
, u_int32_t value
);
1100 #endif /* KERNEL_PRIVATE */
1103 @function mbuf_get_csum_requested
1104 @discussion This function is used by the driver to determine which
1105 checksum operations should be performed in hardware.
1106 @param mbuf The mbuf containing the packet.
1107 @param request Flags indicating which checksums are being requested
1109 @param value This parameter is currently unsupported.
1110 @result 0 upon success otherwise the errno error.
1112 extern errno_t
mbuf_get_csum_requested(mbuf_t mbuf
,
1113 mbuf_csum_request_flags_t
*request
, u_int32_t
*value
);
1116 @function mbuf_get_tso_requested
1117 @discussion This function is used by the driver to determine which
1118 checksum operations should be performed in hardware.
1119 @param mbuf The mbuf containing the packet.
1120 @param request Flags indicating which values are being requested
1122 @param value The requested value.
1123 @result 0 upon success otherwise the errno error.
1125 extern errno_t
mbuf_get_tso_requested(mbuf_t mbuf
,
1126 mbuf_tso_request_flags_t
*request
, u_int32_t
*value
);
1129 @function mbuf_clear_csum_requested
1130 @discussion This function clears the checksum request flags.
1131 @param mbuf The mbuf containing the packet.
1132 @result 0 upon success otherwise the errno error.
1134 extern errno_t
mbuf_clear_csum_requested(mbuf_t mbuf
);
1137 @function mbuf_set_csum_performed
1138 @discussion This is used by the driver to indicate to the stack which
1139 checksum operations were performed in hardware.
1140 @param mbuf The mbuf containing the packet.
1141 @param flags Flags indicating which hardware checksum operations
1143 @param value If the MBUF_CSUM_DID_DATA flag is set, value should be
1144 set to the value of the TCP or UDP header as calculated by the
1146 @result 0 upon success otherwise the errno error.
1148 extern errno_t
mbuf_set_csum_performed(mbuf_t mbuf
,
1149 mbuf_csum_performed_flags_t flags
, u_int32_t value
);
1151 #ifdef KERNEL_PRIVATE
1153 @function mbuf_get_csum_performed
1154 @discussion This is used by the stack to determine which checksums
1155 were calculated in hardware on the inbound path.
1156 @param mbuf The mbuf containing the packet.
1157 @param flags Flags indicating which hardware checksum operations
1159 @param value If the MBUF_CSUM_DID_DATA flag is set, value will be
1160 set to the value of the TCP or UDP header as calculated by the
1162 @result 0 upon success otherwise the errno error.
1164 extern errno_t
mbuf_get_csum_performed(mbuf_t mbuf
,
1165 mbuf_csum_performed_flags_t
*flags
, u_int32_t
*value
);
1166 #endif /* KERNEL_PRIVATE */
1169 @function mbuf_get_mlen
1170 @discussion This routine returns the number of data bytes in a normal
1171 mbuf, i.e. an mbuf that is not a packet header, nor one with
1172 an external cluster attached to it. This is equivalent to the
1174 @result The number of bytes of available data.
1176 extern u_int32_t
mbuf_get_mlen(void);
1179 @function mbuf_get_mhlen
1180 @discussion This routine returns the number of data bytes in a packet
1181 header mbuf. This is equivalent to the legacy MHLEN macro.
1182 @result The number of bytes of available data.
1184 extern u_int32_t
mbuf_get_mhlen(void);
1187 @function mbuf_clear_csum_performed
1188 @discussion Clears the hardware checksum flags and values.
1189 @param mbuf The mbuf containing the packet.
1190 @result 0 upon success otherwise the errno error.
1192 extern errno_t
mbuf_clear_csum_performed(mbuf_t mbuf
);
1195 @function mbuf_inet_cksum
1196 @discussions Calculates 16-bit 1's complement Internet checksum of the
1197 transport segment with or without the pseudo header checksum
1198 of a given IPv4 packet. If the caller specifies a non-zero
1199 transport protocol, the checksum returned will also include
1200 the pseudo header checksum for the corresponding transport
1201 header. Otherwise, no header parsing will be done and the
1202 caller may use this to calculate the Internet checksum of
1203 an arbitrary span of data.
1205 This routine does not modify the contents of the packet. If
1206 the caller specifies a non-zero protocol and/or offset, the
1207 routine expects the complete protocol header to be present
1208 at the beginning of the first mbuf.
1209 @param mbuf The mbuf (or chain of mbufs) containing the packet.
1210 @param protocol A zero or non-zero value. A non-zero value specifies
1211 the transport protocol used for pseudo header checksum.
1212 @param offset A zero or non-zero value; if the latter, it specifies
1213 the offset of the transport header from the beginning of mbuf.
1214 @param length The total (non-zero) length of the transport segment.
1215 @param csum Pointer to the checksum variable; upon success, this
1216 routine will return the calculated Internet checksum through
1217 this variable. The caller must set it to a non-NULL value.
1218 @result 0 upon success otherwise the errno error.
1220 extern errno_t
mbuf_inet_cksum(mbuf_t mbuf
, int protocol
, u_int32_t offset
,
1221 u_int32_t length
, u_int16_t
*csum
);
1224 @function mbuf_inet6_cksum
1225 @discussions Calculates 16-bit 1's complement Internet checksum of the
1226 transport segment with or without the pseudo header checksum
1227 of a given IPv6 packet. If the caller specifies a non-zero
1228 transport protocol, the checksum returned will also include
1229 the pseudo header checksum for the corresponding transport
1230 header. Otherwise, no header parsing will be done and the
1231 caller may use this to calculate the Internet checksum of
1232 an arbitrary span of data.
1234 This routine does not modify the contents of the packet. If
1235 the caller specifies a non-zero protocol and/or offset, the
1236 routine expects the complete protocol header(s) to be present
1237 at the beginning of the first mbuf.
1238 @param mbuf The mbuf (or chain of mbufs) containing the packet.
1239 @param protocol A zero or non-zero value. A non-zero value specifies
1240 the transport protocol used for pseudo header checksum.
1241 @param offset A zero or non-zero value; if the latter, it specifies
1242 the offset of the transport header from the beginning of mbuf.
1243 @param length The total (non-zero) length of the transport segment.
1244 @param csum Pointer to the checksum variable; upon success, this
1245 routine will return the calculated Internet checksum through
1246 this variable. The caller must set it to a non-NULL value.
1247 @result 0 upon success otherwise the errno error.
1249 extern errno_t
mbuf_inet6_cksum(mbuf_t mbuf
, int protocol
, u_int32_t offset
,
1250 u_int32_t length
, u_int16_t
*csum
);
1255 @function mbuf_tag_id_find
1256 @discussion Lookup the module id for a string. If there is no module
1257 id assigned to this string, a new module id will be assigned.
1258 The string should be the bundle id of the kext. In the case of a
1259 tag that will be shared across multiple kexts, a common bundle
1260 id style string should be used.
1262 The lookup operation is not optimized. A module should call this
1263 function once during startup and chache the module id. The
1264 module id will not be resassigned until the machine reboots.
1265 @param module_string A unique string identifying your module.
1266 Example: com.apple.nke.SharedIP.
1267 @param module_id Upon return, a unique identifier for use with
1268 mbuf_tag_* functions. This identifier is valid until the machine
1270 @result 0 upon success otherwise the errno error.
1272 extern errno_t
mbuf_tag_id_find(const char *module_string
,
1273 mbuf_tag_id_t
*module_id
);
1276 @function mbuf_tag_allocate
1277 @discussion Allocate an mbuf tag. Mbuf tags allow various portions
1278 of the stack to tag mbufs with data that will travel with the
1279 mbuf through the stack.
1281 Tags may only be added to mbufs with packet headers
1282 (MBUF_PKTHDR flag is set). Mbuf tags are freed when the mbuf is
1283 freed or when mbuf_tag_free is called.
1284 @param mbuf The mbuf to attach this tag to.
1285 @param module_id A module identifier returned by mbuf_tag_id_find.
1286 @param type A 16 bit type value. For a given module_id, you can use
1287 a number of different tag types.
1288 @param length The length, in bytes, to allocate for storage that
1289 will be associated with this tag on this mbuf.
1290 @param how Indicate whether you want to block and wait for memory if
1291 memory is not immediately available.
1292 @param data_p Upon successful return, *data_p will point to the
1293 buffer allocated for the mtag.
1294 @result 0 upon success otherwise the errno error.
1296 extern errno_t
mbuf_tag_allocate(mbuf_t mbuf
, mbuf_tag_id_t module_id
,
1297 mbuf_tag_type_t type
, size_t length
, mbuf_how_t how
, void **data_p
);
1300 @function mbuf_tag_find
1301 @discussion Find the data associated with an mbuf tag.
1302 @param mbuf The mbuf the tag is attached to.
1303 @param module_id A module identifier returned by mbuf_tag_id_find.
1304 @param type The 16 bit type of the tag to find.
1305 @param length Upon success, the length of data will be store in
1307 @param data_p Upon successful return, *data_p will point to the
1308 buffer allocated for the mtag.
1309 @result 0 upon success otherwise the errno error.
1311 extern errno_t
mbuf_tag_find(mbuf_t mbuf
, mbuf_tag_id_t module_id
,
1312 mbuf_tag_type_t type
, size_t *length
, void **data_p
);
1315 @function mbuf_tag_free
1316 @discussion Frees a previously allocated mbuf tag.
1317 @param mbuf The mbuf the tag was allocated on.
1318 @param module_id The ID of the tag to free.
1319 @param type The type of the tag to free.
1321 extern void mbuf_tag_free(mbuf_t mbuf
, mbuf_tag_id_t module_id
,
1322 mbuf_tag_type_t type
);
1327 @function mbuf_stats
1328 @discussion Get the mbuf statistics.
1329 @param stats Storage to copy the stats in to.
1331 extern void mbuf_stats(struct mbuf_stat
*stats
);
1334 /* IF_QUEUE interaction */
1336 #define IF_ENQUEUE_MBUF(ifq, m) { \
1337 mbuf_setnextpkt((m), 0); \
1338 if ((ifq)->ifq_tail == 0) \
1339 (ifq)->ifq_head = (m); \
1341 mbuf_setnextpkt((mbuf_t)(ifq)->ifq_tail, (m)); \
1342 (ifq)->ifq_tail = (m); \
1346 #define IF_PREPEND_MBUF(ifq, m) { \
1347 mbuf_setnextpkt((m), (ifq)->ifq_head); \
1348 if ((ifq)->ifq_tail == 0) \
1349 (ifq)->ifq_tail = (m); \
1350 (ifq)->ifq_head = (m); \
1354 #define IF_DEQUEUE_MBUF(ifq, m) { \
1355 (m) = (ifq)->ifq_head; \
1357 if (((ifq)->ifq_head = mbuf_nextpkt((m))) == 0) \
1358 (ifq)->ifq_tail = 0; \
1359 mbuf_setnextpkt((m), 0); \
1365 #endif /* __KPI_MBUF__ */