]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/kpi_mbuf.h
xnu-792.17.14.tar.gz
[apple/xnu.git] / bsd / sys / kpi_mbuf.h
CommitLineData
91447636 1/*
5d5c5d0d
A
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
3 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
91447636 5 *
8f6c56a5
A
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
91447636
A
27 */
28/*!
29 @header kpi_mbuf.h
30 This header defines an API for interacting with mbufs. mbufs are the
31 primary method of storing packets in the networking stack.
32
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.
39
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.
44 */
45
46#ifndef __KPI_MBUF__
47#define __KPI_MBUF__
48#include <sys/kernel_types.h>
49#include <mach/vm_types.h>
50
51/*!
52 @enum mbuf_flags_t
53 @abstract Constants defining mbuf flags. Only the flags listed below
54 can be set or retreieved.
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
63 packet.
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.
70*/
71enum {
72 MBUF_EXT = 0x0001, /* has associated external storage */
73 MBUF_PKTHDR = 0x0002, /* start of record */
74 MBUF_EOR = 0x0004, /* end of record */
75
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 */
82};
83typedef u_int32_t mbuf_flags_t;
84
85/*!
86 @enum mbuf_type_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
91 mbuf contains.
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
95 wrong.
96 @constant MBUF_MT_DATA Indicates this mbuf is being used to store
97 data.
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
106 sort.
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
111 message).
112 @constant MBUF_MT_OOBDATA Out of band data.
113*/
114enum {
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 */
130};
131typedef u_int32_t mbuf_type_t;
132
133/*!
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
143 calculated yet.
144 @constant MBUF_CSUM_REQ_TCP Indicates the TCP checksum has not been
145 calculated yet.
146 @constant MBUF_CSUM_REQ_UDP Indicates the UDP checksum has not been
147 calculated yet.
148*/
149enum {
150#ifdef KERNEL_PRIVATE
151 MBUF_CSUM_REQ_SUM16 = 0x1000, /* Weird apple hardware checksum */
152#endif KERNEL_PRIVATE
153 MBUF_CSUM_REQ_IP = 0x0001,
154 MBUF_CSUM_REQ_TCP = 0x0002,
155 MBUF_CSUM_REQ_UDP = 0x0004
156};
157typedef u_int32_t mbuf_csum_request_flags_t;
158
159/*!
160 @enum mbuf_csum_performed_flags_t
161 @abstract Checksum performed/requested flags.
162 @discussion Mbufs often contain packets. Some hardware supports
163 performing checksums in hardware. The driver uses these flags to
164 communicate to the stack the checksums that were calculated in
165 hardware.
166 @constant MBUF_CSUM_DID_IP Indicates that the driver/hardware verified
167 the IP checksum in hardware.
168 @constant MBUF_CSUM_IP_GOOD Indicates whether or not the IP checksum
169 was good or bad. Only valid when MBUF_CSUM_DID_IP is set.
170 @constant MBUF_CSUM_DID_DATA Indicates that the TCP or UDP checksum
171 was calculated. The value for the checksum calculated in
172 hardware should be passed as the second parameter of
173 mbuf_set_csum_performed. The hardware calculated checksum value
174 can be retrieved using the second parameter passed to
175 mbuf_get_csum_performed.
176 @constant MBUF_CSUM_PSEUDO_HDR If set, this indicates that the
177 checksum value for MBUF_CSUM_DID_DATA includes the pseudo header
178 value. If this is not set, the stack will calculate the pseudo
179 header value and add that to the checksum. The value of this bit
180 is only valid when MBUF_CSUM_DID_DATA is set.
181*/
182enum {
183#ifdef KERNEL_PRIVATE
184 MBUF_CSUM_TCP_SUM16 = MBUF_CSUM_REQ_SUM16, /* Weird apple hardware checksum */
185#endif
186 MBUF_CSUM_DID_IP = 0x0100,
187 MBUF_CSUM_IP_GOOD = 0x0200,
188 MBUF_CSUM_DID_DATA = 0x0400,
189 MBUF_CSUM_PSEUDO_HDR = 0x0800
190};
191typedef u_int32_t mbuf_csum_performed_flags_t;
192
193/*!
194 @enum mbuf_how_t
195 @abstract Method of allocating an mbuf.
196 @discussion Blocking will cause the funnel to be dropped. If the
197 funnel is dropped, other threads may make changes to networking
198 data structures. This can lead to very bad things happening.
199 Blocking on the input our output path can also impact
200 performance. There are some cases where making a blocking call
201 is acceptable. When in doubt, use MBUF_DONTWAIT.
202 @constant MBUF_WAITOK Allow a call to allocate an mbuf to block.
203 @constant MBUF_DONTWAIT Don't allow the mbuf allocation call to
204 block, if blocking is necessary fail and return immediately.
205*/
206enum {
207 MBUF_WAITOK = 0, /* Ok to block to get memory */
208 MBUF_DONTWAIT = 1 /* Don't block, fail if blocking would be required */
209};
210typedef u_int32_t mbuf_how_t;
211
212typedef u_int32_t mbuf_tag_id_t;
213typedef u_int16_t mbuf_tag_type_t;
214
215/*!
216 @struct mbuf_stat
217 @discussion The mbuf_stat contains mbuf statistics.
218 @field mbufs Number of mbufs (free or otherwise).
219 @field clusters Number of clusters (free or otherwise).
220 @field clfree Number of free clusters.
221 @field drops Number of times allocation failed.
222 @field wait Number of times allocation blocked.
223 @field drain Number of times protocol drain functions were called.
224 @field mtypes An array of counts of each type of mbuf allocated.
225 @field mcfail Number of times m_copym failed.
226 @field mpfail Number of times m_pullup failed.
227 @field msize Length of an mbuf.
228 @field mclbytes Length of an mbuf cluster.
229 @field minclsize Minimum length of data to allocate a cluster.
230 Anything smaller than this should be placed in chained mbufs.
231 @field mlen Length of data in an mbuf.
232 @field mhlen Length of data in an mbuf with a packet header.
233 @field bigclusters Number of big clusters.
234 @field bigclfree Number of unused big clusters.
235 @field bigmclbytes Length of a big mbuf cluster.
236*/
237struct mbuf_stat {
238 u_long mbufs; /* mbufs obtained from page pool */
239 u_long clusters; /* clusters obtained from page pool */
240 u_long clfree; /* free clusters */
241 u_long drops; /* times failed to find space */
242 u_long wait; /* times waited for space */
243 u_long drain; /* times drained protocols for space */
244 u_short mtypes[256]; /* type specific mbuf allocations */
245 u_long mcfail; /* times m_copym failed */
246 u_long mpfail; /* times m_pullup failed */
247 u_long msize; /* length of an mbuf */
248 u_long mclbytes; /* length of an mbuf cluster */
249 u_long minclsize; /* min length of data to allocate a cluster */
250 u_long mlen; /* length of data in an mbuf */
251 u_long mhlen; /* length of data in a header mbuf */
252 u_long bigclusters; /* number of big clusters */
253 u_long bigclfree; /* number of big clustser free */
254 u_long bigmclbytes; /* length of data in a big cluster */
255};
256
257/* Parameter for m_copym to copy all bytes */
258#define MBUF_COPYALL 1000000000
259
260/* Data access */
261/*!
262 @function mbuf_data
263 @discussion Returns a pointer to the start of data in this mbuf.
264 There may be additional data on chained mbufs. The data you're
265 looking for may not be contiguous if it spans more than one
266 mbuf. Use mbuf_len to determine the lenght of data available in
267 this mbuf. If a data structure you want to access stradles two
268 mbufs in a chain, either use mbuf_pullup to get the data
269 contiguous in one mbuf or copy the pieces of data from each mbuf
270 in to a contiguous buffer. Using mbuf_pullup has the advantage
271 of not having to copy the data. On the other hand, if you don't
272 make sure there is space in the mbuf, mbuf_pullup may fail and
273 free the mbuf.
274 @param mbuf The mbuf.
275 @result A pointer to the data in the mbuf.
276 */
277void* mbuf_data(mbuf_t mbuf);
278
279/*!
280 @function mbuf_datastart
281 @discussion Returns the start of the space set aside for storing
282 data in an mbuf. An mbuf's data may come from a cluster or be
283 embedded in the mbuf structure itself. The data pointer
284 retrieved by mbuf_data may not be at the start of the data
285 (mbuf_leadingspace will be non-zero). This function will return to
286 you a pointer that matches mbuf_data() - mbuf_leadingspace().
287 @param mbuf The mbuf.
288 @result A pointer to smallest possible value for data.
289 */
290void* mbuf_datastart(mbuf_t mbuf);
291
292/*!
293 @function mbuf_setdata
294 @discussion Sets the data and length values for an mbuf. The data
295 value must be in a valid range. In the case of an mbuf with a cluster,
296 the data value must point to a location in the cluster and the data
297 value plus the length, must be less than the end of the cluster. For
298 data embedded directly in an mbuf (no cluster), the data value must
299 fall somewhere between the start and end of the data area in the
300 mbuf and the data + length must also be in the same range.
301 @param mbuf The mbuf.
302 @param data The new pointer value for data.
303 @param len The new length of data in the mbuf.
304 @result 0 on success, errno error on failure.
305 */
306errno_t mbuf_setdata(mbuf_t mbuf, void *data, size_t len);
307
308/*!
309 @function mbuf_align_32
310 @discussion mbuf_align_32 is a replacement for M_ALIGN and MH_ALIGN.
311 mbuf_align_32 will set the data pointer to a location aligned on
312 a four byte boundry with at least 'len' bytes between the data
313 pointer and the end of the data block.
314 @param mbuf The mbuf.
315 @param len The minimum length of space that should follow the new
316 data location.
317 @result 0 on success, errno error on failure.
318 */
319errno_t mbuf_align_32(mbuf_t mbuf, size_t len);
320
321/*!
322 @function mbuf_data_to_physical
323 @discussion mbuf_data_to_physical is a replacement for mcl_to_paddr.
324 Given a pointer returned from mbuf_data of mbuf_datastart,
325 mbuf_data_to_physical will return the phyical address for that
326 block of data.
327 @param ptr A pointer to data stored in an mbuf.
328 @result The 64 bit physical address of the mbuf data or NULL if ptr
329 does not point to data stored in an mbuf.
330 */
331addr64_t mbuf_data_to_physical(void* ptr);
332
333
334/* Allocation */
335
336/*!
337 @function mbuf_get
338 @discussion Allocates an mbuf without a cluster for external data.
339 @param how Blocking or non-blocking.
340 @param type The type of the mbuf.
341 @param mbuf The mbuf.
342 @result 0 on success, errno error on failure.
343 */
344errno_t mbuf_get(mbuf_how_t how, mbuf_type_t type, mbuf_t* mbuf);
345
346/*!
347 @function mbuf_gethdr
348 @discussion Allocates an mbuf without a cluster for external data.
349 Sets a flag to indicate there is a packet header and initializes
350 the packet header.
351 @param how Blocking or non-blocking.
352 @param type The type of the mbuf.
353 @param mbuf The mbuf.
354 @result 0 on success, errno error on failure.
355 */
356errno_t mbuf_gethdr(mbuf_how_t how, mbuf_type_t type, mbuf_t* mbuf);
357
358
359/*!
360 @function mbuf_getcluster
361 @discussion Allocate a cluster of the requested size and attach it to
362 an mbuf for use as external data. If mbuf points to a NULL mbuf_t,
363 an mbuf will be allocated for you. If mbuf points to a non-NULL mbuf_t,
364 mbuf_getcluster may return a different mbuf_t than the one you
365 passed in.
366 @param how Blocking or non-blocking.
367 @param type The type of the mbuf.
368 @param size The size of the cluster to be allocated. Supported sizes for a
369 cluster are be 2048 or 4096. Any other value with return EINVAL.
370 @param mbuf The mbuf the cluster will be attached to.
371 @result 0 on success, errno error on failure. If you specified NULL
372 for the mbuf, any intermediate mbuf that may have been allocated
373 will be freed. If you specify an mbuf value in *mbuf,
374 mbuf_mclget will not free it.
375 EINVAL - Invalid parameter
376 ENOMEM - Not enough memory available
377 */
378errno_t mbuf_getcluster(mbuf_how_t how, mbuf_type_t type, size_t size, mbuf_t* mbuf);
379
380/*!
381 @function mbuf_mclget
382 @discussion Allocate a cluster and attach it to an mbuf for use as
383 external data. If mbuf points to a NULL mbuf_t, an mbuf will be
384 allocated for you. If mbuf points to a non-NULL mbuf_t,
385 mbuf_mclget may return a different mbuf_t than the one you
386 passed in.
387 @param how Blocking or non-blocking.
388 @param type The type of the mbuf.
389 @param mbuf The mbuf the cluster will be attached to.
390 @result 0 on success, errno error on failure. If you specified NULL
391 for the mbuf, any intermediate mbuf that may have been allocated
392 will be freed. If you specify an mbuf value in *mbuf,
393 mbuf_mclget will not free it.
394 */
395errno_t mbuf_mclget(mbuf_how_t how, mbuf_type_t type, mbuf_t* mbuf);
396
397/*!
398 @function mbuf_allocpacket
399 @discussion Allocate an mbuf chain to store a single packet of the requested length.
400 According to the requested length, a chain of mbufs will be created. The mbuf type
401 will be set to MBUF_TYPE_DATA. The caller may specify the maximum number of
402 buffer
403 @param how Blocking or non-blocking
404 @param packetlen The total length of the packet mbuf to be allocated.
405 The length must be greater than zero.
406 @param maxchunks An input/output pointer to the maximum number of mbufs segments making up the chain.
407 On input if maxchunks is zero, or the value pointed to by maxchunks is zero,
408 the packet will be made of as many buffer segments as necessary to fit the length.
409 The allocation will fail with ENOBUFS if the number of segments requested is too small and
410 the sum of the maximum size of each individual segment is less than the packet length.
411 On output, if the allocation succeed and maxchunks is non zero, it will point to
412 the actual number of segments allocated.
413 @param Upon success, *mbuf will be a reference to the new mbuf.
414 @result Returns 0 upon success or the following error code:
415 EINVAL - Invalid parameter
416 ENOMEM - Not enough memory available
417 ENOBUFS - Buffers not big enough for the maximum number of chunks requested
418*/
419errno_t mbuf_allocpacket(mbuf_how_t how, size_t packetlen, unsigned int * maxchunks, mbuf_t *mbuf);
420
421/*!
422 @function mbuf_getpacket
423 @discussion Allocate an mbuf, allocate and attach a cluster, and set
424 the packet header flag.
425 @param how Blocking or non-blocking.
426 @param mbuf Upon success, *mbuf will be a reference to the new mbuf.
427 @result 0 on success, errno error on failure.
428 */
429errno_t mbuf_getpacket(mbuf_how_t how, mbuf_t* mbuf);
430
431/*!
432 @function mbuf_free
433 @discussion Frees a single mbuf. Not commonly used because it
434 doesn't touch the rest of the mbufs on the chain.
435 @param mbuf The mbuf to free.
436 @result The next mbuf in the chain.
437 */
438mbuf_t mbuf_free(mbuf_t mbuf);
439
440/*!
441 @function mbuf_freem
442 @discussion Frees a chain of mbufs link through mnext.
443 @param mbuf The first mbuf in the chain to free.
444 */
445void mbuf_freem(mbuf_t mbuf);
446
447/*!
448 @function mbuf_freem_list
449 @discussion Frees linked list of mbuf chains. Walks through
450 mnextpackt and does the equivalent of mbuf_mfreem to each.
451 @param mbuf The first mbuf in the linked list to free.
452 @result The number of mbufs freed.
453 */
454int mbuf_freem_list(mbuf_t mbuf);
455
456/*!
457 @function mbuf_leadingspace
458 @discussion Determines the space available in the mbuf proceeding
459 the current data.
460 @param mbuf The mbuf.
461 @result The number of unused bytes at the start of the mbuf.
462 */
463size_t mbuf_leadingspace(mbuf_t mbuf);
464
465/*!
466 @function mbuf_trailingspace
467 @discussion Determines the space available in the mbuf following
468 the current data.
469 @param mbuf The mbuf.
470 @result The number of unused bytes following the current data.
471 */
472size_t mbuf_trailingspace(mbuf_t mbuf);
473
474/* Manipulation */
475
476/*!
477 @function mbuf_copym
478 @discussion Copies len bytes from offset from src to a new mbuf.
479 @param src The source mbuf.
480 @param offset The offset in the mbuf to start copying from.
481 @param len The the number of bytes to copy.
482 @param how To block or not to block, that is a question.
483 @param new_mbuf Upon success, the newly allocated mbuf.
484 @result 0 upon success otherwise the errno error.
485 */
486errno_t mbuf_copym(mbuf_t src, size_t offset, size_t len,
487 mbuf_how_t how, mbuf_t* new_mbuf);
488
489/*!
490 @function mbuf_dup
491 @discussion Exactly duplicates an mbuf chain.
492 @param src The source mbuf.
493 @param how Blocking or non-blocking.
494 @param new_mbuf Upon success, the newly allocated mbuf.
495 @result 0 upon success otherwise the errno error.
496 */
497errno_t mbuf_dup(mbuf_t src, mbuf_how_t how, mbuf_t* new_mbuf);
498
499/*!
500 @function mbuf_prepend
501 @discussion Prepend len bytes to an mbuf. If there is space
502 (mbuf_leadingspace >= len), the mbuf's data ptr is changed and
503 the same mbuf is returned. If there is no space, a new mbuf may
504 be allocated and prepended to the mbuf chain. If the operation
505 fails, the mbuf may be freed (*mbuf will be NULL).
506 @param mbuf The mbuf to prepend data to. This may change if a new
507 mbuf must be allocated or may be NULL if the operation fails.
508 @param len The length, in bytes, to be prepended to the mbuf.
509 @param how Blocking or non-blocking.
510 @result 0 upon success otherwise the errno error.
511 */
512errno_t mbuf_prepend(mbuf_t* mbuf, size_t len, mbuf_how_t how);
513
514/*!
515 @function mbuf_split
516 @discussion Split an mbuf chain at a specific offset.
517 @param src The mbuf to be split.
518 @param offset The offset in the buffer where the mbuf should be
519 split.
520 @param how Blocking or non-blocking.
521 @param new_mbuf Upon success, the second half of the split mbuf
522 chain.
523 @result 0 upon success otherwise the errno error. In the case of
524 failure, the original mbuf chain passed in to src will be
525 preserved.
526 */
527errno_t mbuf_split(mbuf_t src, size_t offset,
528 mbuf_how_t how, mbuf_t* new_mbuf);
529
530/*!
531 @function mbuf_pullup
532 @discussion Move the next len bytes in to mbuf from other mbufs in
533 the chain. This is commonly used to get the IP and TCP or UDP
534 header contiguous in the first mbuf. If mbuf_pullup fails, the
535 entire mbuf chain will be freed.
536 @param mbuf The mbuf in the chain the data should be contiguous in.
537 @param len The number of bytes to pull from the next mbuf(s).
538 @result 0 upon success otherwise the errno error. In the case of an
539 error, the mbuf chain has been freed.
540 */
541errno_t mbuf_pullup(mbuf_t* mbuf, size_t len);
542
543/*!
544 @function mbuf_pulldown
545 @discussion Make length bytes at offset in the mbuf chain
546 contiguous. Nothing before offset bytes in the chain will be
547 modified. Upon return, location will be the mbuf the data is
548 contiguous in and offset will be the offset in that mbuf at
549 which the data is located. In the case of a failure, the mbuf
550 chain will be freed.
551 @param src The start of the mbuf chain.
552 @param offset Pass in a pointer to a value with the offset of the
553 data you're interested in making contiguous. Upon success, this
554 will be overwritten with the offset from the mbuf returned in
555 location.
556 @param length The length of data that should be made contiguous.
557 @param location Upon success, *location will be the mbuf the data is
558 in.
559 @result 0 upon success otherwise the errno error.
560 */
561errno_t mbuf_pulldown(mbuf_t src, size_t *offset, size_t length, mbuf_t *location);
562
563/*!
564 @function mbuf_adj
565 @discussion Trims len bytes from the mbuf. If the length is greater
566 than zero, the bytes are trimmed from the front of the mbuf. If
567 the length is less than zero, the bytes are trimmed from the end
568 of the mbuf chain.
569 @param mbuf The mbuf chain to trim.
570 @param len The number of bytes to trim from the mbuf chain.
571 @result 0 upon success otherwise the errno error.
572 */
573void mbuf_adj(mbuf_t mbuf, int len);
574
575/*!
576 @function mbuf_copydata
577 @discussion Copies data out of an mbuf in to a specified buffer. If
578 the data is stored in a chain of mbufs, the data will be copied
579 from each mbuf in the chain until length bytes have been copied.
580 @param mbuf The mbuf chain to copy data out of.
581 @param offset The offset in to the mbuf to start copying.
582 @param length The number of bytes to copy.
583 @param out_data A pointer to the location where the data will be
584 copied.
585 @result 0 upon success otherwise the errno error.
586 */
587errno_t mbuf_copydata(mbuf_t mbuf, size_t offset, size_t length, void* out_data);
588
589/*!
590 @function mbuf_copyback
591 @discussion Copies data from a buffer to an mbuf chain.
592 mbuf_copyback will grow the chain to fit the specified buffer.
593
594 If mbuf_copydata is unable to allocate enough mbufs to grow the
595 chain, ENOBUFS will be returned. The mbuf chain will be shorter
596 than expected but all of the data up to the end of the mbuf
597 chain will be valid.
598
599 If an offset is specified, mbuf_copyback will skip that many
600 bytes in the mbuf chain before starting to write the buffer in
601 to the chain. If the mbuf chain does not contain this many
602 bytes, mbufs will be allocated to create the space.
603 @param mbuf The first mbuf in the chain to copy the data in to.
604 @param offset Offset in bytes to skip before copying data.
605 @param length The length, in bytes, of the data to copy in to the mbuf
606 chain.
607 @param data A pointer to data in the kernel's address space.
608 @param how Blocking or non-blocking.
609 @result 0 upon success, EINVAL or ENOBUFS upon failure.
610 */
611errno_t mbuf_copyback(mbuf_t mbuf, size_t offset, size_t length,
612 const void *data, mbuf_how_t how);
613
614#ifdef KERNEL_PRIVATE
615/*!
616 @function mbuf_mclref
617 @discussion Incrememnt the reference count of the cluster.
618 @param mbuf The mbuf with the cluster to increment the refcount of.
619 @result 0 upon success otherwise the errno error.
620 */
621int mbuf_mclref(mbuf_t mbuf);
622
623/*!
624 @function mbuf_mclunref
625 @discussion Decrement the reference count of the cluster.
626 @param mbuf The mbuf with the cluster to decrement the refcount of.
627 @result 0 upon success otherwise the errno error.
628 */
629int mbuf_mclunref(mbuf_t mbuf);
630#endif
631
632/*!
633 @function mbuf_mclhasreference
634 @discussion Check if a cluster of an mbuf is referenced by another mbuf.
635 References may be taken, for example, as a result of a call to
636 mbuf_split or mbuf_copym
637 @param mbuf The mbuf with the cluster to test.
638 @result 0 if there is no reference by another mbuf, 1 otherwise.
639 */
640int mbuf_mclhasreference(mbuf_t mbuf);
641
642
643/* mbuf header */
644
645/*!
646 @function mbuf_next
647 @discussion Returns the next mbuf in the chain.
648 @param mbuf The mbuf.
649 @result The next mbuf in the chain.
650 */
651mbuf_t mbuf_next(mbuf_t mbuf);
652
653/*!
654 @function mbuf_setnext
655 @discussion Sets the next mbuf in the chain.
656 @param mbuf The mbuf.
657 @param next The new next mbuf.
658 @result 0 upon success otherwise the errno error.
659 */
660errno_t mbuf_setnext(mbuf_t mbuf, mbuf_t next);
661
662/*!
663 @function mbuf_nextpkt
664 @discussion Gets the next packet from the mbuf.
665 @param mbuf The mbuf.
666 @result The nextpkt.
667 */
668mbuf_t mbuf_nextpkt(mbuf_t mbuf);
669
670/*!
671 @function mbuf_setnextpkt
672 @discussion Sets the next packet attached to this mbuf.
673 @param mbuf The mbuf.
674 @param nextpkt The new next packet.
675 */
676void mbuf_setnextpkt(mbuf_t mbuf, mbuf_t nextpkt);
677
678/*!
679 @function mbuf_len
680 @discussion Gets the length of data in this mbuf.
681 @param mbuf The mbuf.
682 @result The length.
683 */
684size_t mbuf_len(mbuf_t mbuf);
685
686/*!
687 @function mbuf_setlen
688 @discussion Sets the length of data in this packet. Be careful to
689 not set the length over the space available in the mbuf.
690 @param mbuf The mbuf.
691 @param len The new length.
692 @result 0 upon success otherwise the errno error.
693 */
694void mbuf_setlen(mbuf_t mbuf, size_t len);
695
696/*!
697 @function mbuf_maxlen
698 @discussion Retrieves the maximum length of data that may be stored
699 in this mbuf. This value assumes that the data pointer was set
700 to the start of the possible range for that pointer
701 (mbuf_data_start).
702 @param mbuf The mbuf.
703 @result The maximum lenght of data for this mbuf.
704 */
705size_t mbuf_maxlen(mbuf_t mbuf);
706
707/*!
708 @function mbuf_type
709 @discussion Gets the type of mbuf.
710 @param mbuf The mbuf.
711 @result The type.
712 */
713mbuf_type_t mbuf_type(mbuf_t mbuf);
714
715/*!
716 @function mbuf_settype
717 @discussion Sets the type of mbuf.
718 @param mbuf The mbuf.
719 @param new_type The new type.
720 @result 0 upon success otherwise the errno error.
721 */
722errno_t mbuf_settype(mbuf_t mbuf, mbuf_type_t new_type);
723
724/*!
725 @function mbuf_flags
726 @discussion Returns the set flags.
727 @param mbuf The mbuf.
728 @result The flags.
729 */
730mbuf_flags_t mbuf_flags(mbuf_t mbuf);
731
732/*!
733 @function mbuf_setflags
734 @discussion Sets the set of set flags.
735 @param mbuf The mbuf.
736 @param flags The flags that should be set, all other flags will be cleared.
737 @result 0 upon success otherwise the errno error.
738 */
739errno_t mbuf_setflags(mbuf_t mbuf, mbuf_flags_t flags);
740
741/*!
742 @function mbuf_setflags_mask
743 @discussion Useful for setting or clearing individual flags. Easier
744 than calling mbuf_setflags(m, mbuf_flags(m) | M_FLAG).
745 @param mbuf The mbuf.
746 @param flags The flags that should be set or cleared.
747 @param mask The mask controlling which flags will be modified.
748 @result 0 upon success otherwise the errno error.
749 */
750errno_t mbuf_setflags_mask(mbuf_t mbuf, mbuf_flags_t flags,
751 mbuf_flags_t mask);
752
753/*!
754 @function mbuf_copy_pkthdr
755 @discussion Copies the packet header from src to dest.
756 @param src The mbuf from which the packet header will be copied.
757 @param mbuf The mbuf to which the packet header will be copied.
758 @result 0 upon success otherwise the errno error.
759 */
760errno_t mbuf_copy_pkthdr(mbuf_t dest, mbuf_t src);
761
762/*!
763 @function mbuf_pkthdr_len
764 @discussion Returns the length as reported by the packet header.
765 @param mbuf The mbuf containing the packet header with the length to
766 be changed.
767 @result The length, in bytes, of the packet.
768 */
769size_t mbuf_pkthdr_len(mbuf_t mbuf);
770
771/*!
772 @function mbuf_pkthdr_setlen
773 @discussion Sets the length of the packet in the packet header.
774 @param mbuf The mbuf containing the packet header.
775 @param len The new length of the packet.
776 @result 0 upon success otherwise the errno error.
777 */
778void mbuf_pkthdr_setlen(mbuf_t mbuf, size_t len);
779
780/*!
781 @function mbuf_pkthdr_rcvif
782 @discussion Returns a reference to the interface the packet was
783 received on. Increments the reference count of the interface
784 before returning. Caller is responsible for releasing
785 the reference by calling ifnet_release.
786 @param mbuf The mbuf containing the packet header.
787 @result A reference to the interface.
788 */
789ifnet_t mbuf_pkthdr_rcvif(mbuf_t mbuf);
790
791/*!
792 @function mbuf_pkthdr_setrcvif
793 @discussion Sets the interface the packet was received on.
794 @param mbuf The mbuf containing the packet header.
795 @param ifnet A reference to an interface.
796 @result 0 upon success otherwise the errno error.
797 */
798errno_t mbuf_pkthdr_setrcvif(mbuf_t mbuf, ifnet_t ifnet);
799
800/*!
801 @function mbuf_pkthdr_header
802 @discussion Returns a pointer to the packet header.
803 @param mbuf The mbuf containing the packet header.
804 @result A pointer to the packet header.
805 */
806void* mbuf_pkthdr_header(mbuf_t mbuf);
807
808/*!
809 @function mbuf_pkthdr_setheader
810 @discussion Sets the pointer to the packet header.
811 @param mbuf The mbuf containing the packet header.
812 @param ifnet A pointer to the header.
813 @result 0 upon success otherwise the errno error.
814 */
815void mbuf_pkthdr_setheader(mbuf_t mbuf, void* header);
816#ifdef KERNEL_PRIVATE
817
818/* mbuf aux data */
819
820/*!
821 @function mbuf_aux_add
822 @discussion Adds auxiliary data in the form of an mbuf.
823 @param mbuf The mbuf to add aux data to.
824 @param family The protocol family of the aux data to add.
825 @param type The mbuf type of the aux data to add.
826 @param aux_mbuf The aux mbuf allocated for you.
827 @result 0 upon success otherwise the errno error.
828 */
829errno_t mbuf_aux_add(mbuf_t mbuf, int family, mbuf_type_t type, mbuf_t *aux_mbuf);
830
831/*!
832 @function mbuf_aux_find
833 @discussion Finds auxiliary data attached to an mbuf.
834 @param mbuf The mbuf to find aux data on.
835 @param family The protocol family of the aux data to add.
836 @param type The mbuf type of the aux data to add.
837 @result The aux data mbuf or NULL if there isn't one.
838 */
839mbuf_t mbuf_aux_find(mbuf_t mbuf, int family, mbuf_type_t type);
840
841/*!
842 @function mbuf_aux_delete
843 @discussion Free an mbuf used as aux data and disassosciate it from
844 the mbuf.
845 @param mbuf The mbuf to find aux data on.
846 @param aux The aux data to free.
847 */
848void mbuf_aux_delete(mbuf_t mbuf, mbuf_t aux);
849#endif /* KERNEL_PRIVATE */
850
851/* Checksums */
852
853/*!
854 @function mbuf_inbound_modified
855 @discussion This function will clear the checksum flags to indicate
856 that a hardware checksum should not be used. Any filter
857 modifying data should call this function on an mbuf before
858 passing the packet up the stack. If a filter modifies a packet
859 in a way that affects any checksum, the filter is responsible
860 for either modifying the checksum to compensate for the changes
861 or verifying the checksum before making the changes and then
862 modifying the data and calculating a new checksum only if the
863 original checksum was valid.
864 @param mbuf The mbuf that has been modified.
865 */
866void mbuf_inbound_modified(mbuf_t mbuf);
867
868/*!
869 @function mbuf_outbound_finalize
870 @discussion This function will "finalize" the packet allowing your
871 code to inspect the final packet.
872
873 There are a number of operations that are performed in hardware,
874 such as calculating checksums. This function will perform in
875 software the various opterations that were scheduled to be done
876 in hardware. Future operations may include IPSec processing or
877 vlan support. If you are redirecting a packet to a new interface
878 which may not have the same hardware support or encapsulating
879 the packet, you should call this function to force the stack to
880 calculate and fill out the checksums. This will bypass hardware
881 checksums but give you a complete packet to work with. If you
882 need to inspect aspects of the packet which may be generated by
883 hardware, you must call this function to get an aproximate final
884 packet. If you plan to modify the packet in any way, you should
885 call this function.
886
887 This function should be called before modifying any outbound
888 packets.
889
890 This function may be called at various levels, in some cases
891 additional headers may have already been prepended, such as the
892 case of a packet seen by an interface filter. To handle this,
893 the caller must pass the protocol family of the packet as well
894 as the offset from the start of the packet to the protocol
895 header.
896 @param mbuf The mbuf that should be finalized.
897 @param protocol_family The protocol family of the packet in the
898 mbuf.
899 @param protocol_offset The offset from the start of the mbuf to the
900 protocol header. For an IP packet with an ethernet header, this
901 would be the length of an ethernet header.
902 */
903void mbuf_outbound_finalize(mbuf_t mbuf, u_long protocol_family,
904 size_t protocol_offset);
905
906/*!
907 @function mbuf_set_vlan_tag
908 @discussion This function is used by interfaces that support vlan
909 tagging in hardware. This function will set properties in the
910 mbuf to indicate which vlan the packet was received for.
911 @param mbuf The mbuf containing the packet.
912 @param vlan The protocol family of the aux data to add.
913 @result 0 upon success otherwise the errno error.
914 */
915errno_t mbuf_set_vlan_tag(mbuf_t mbuf, u_int16_t vlan);
916
917/*!
918 @function mbuf_get_vlan_tag
919 @discussion This function is used by drivers that support hardware
920 vlan tagging to determine which vlan this packet belongs to. To
921 differentiate between the case where the vlan tag is zero and
922 the case where there is no vlan tag, this function will return
923 ENXIO when there is no vlan.
924 @param mbuf The mbuf containing the packet.
925 @param vlan The protocol family of the aux data to add.
926 @result 0 upon success otherwise the errno error. ENXIO indicates
927 that the vlan tag is not set.
928 */
929errno_t mbuf_get_vlan_tag(mbuf_t mbuf, u_int16_t *vlan);
930
931/*!
932 @function mbuf_clear_vlan_tag
933 @discussion This function will clear any vlan tag associated with
934 the mbuf.
935 @param mbuf The mbuf containing the packet.
936 @result 0 upon success otherwise the errno error.
937 */
938errno_t mbuf_clear_vlan_tag(mbuf_t mbuf);
939
940#ifdef KERNEL_PRIVATE
941/*!
942 @function mbuf_set_csum_requested
943 @discussion This function is used by the stack to indicate which
944 checksums should be calculated in hardware. The stack normally
945 sets these flags as the packet is processed in the outbound
946 direction. Just before send the packe to the interface, the
947 stack will look at these flags and perform any checksums in
948 software that are not supported by the interface.
949 @param mbuf The mbuf containing the packet.
950 @param request Flags indicating which checksums are being requested
951 for this packet.
952 @param value This parameter is currently unsupported.
953 @result 0 upon success otherwise the errno error.
954 */
955errno_t mbuf_set_csum_requested(mbuf_t mbuf,
956 mbuf_csum_request_flags_t request, u_int32_t value);
957#endif
958
959/*!
960 @function mbuf_get_csum_requested
961 @discussion This function is used by the driver to determine which
962 checksum operations should be performed in hardware.
963 @param mbuf The mbuf containing the packet.
964 @param request Flags indicating which checksums are being requested
965 for this packet.
966 @param value This parameter is currently unsupported.
967 @result 0 upon success otherwise the errno error.
968 */
969errno_t mbuf_get_csum_requested(mbuf_t mbuf,
970 mbuf_csum_request_flags_t *request, u_int32_t *value);
971
972/*!
973 @function mbuf_clear_csum_requested
974 @discussion This function clears the checksum request flags.
975 @param mbuf The mbuf containing the packet.
976 @result 0 upon success otherwise the errno error.
977 */
978errno_t mbuf_clear_csum_requested(mbuf_t mbuf);
979
980/*!
981 @function mbuf_set_csum_performed
982 @discussion This is used by the driver to indicate to the stack which
983 checksum operations were performed in hardware.
984 @param mbuf The mbuf containing the packet.
985 @param flags Flags indicating which hardware checksum operations
986 were performed.
987 @param value If the MBUF_CSUM_DID_DATA flag is set, value should be
988 set to the value of the TCP or UDP header as calculated by the
989 hardware.
990 @result 0 upon success otherwise the errno error.
991 */
992errno_t mbuf_set_csum_performed(mbuf_t mbuf,
993 mbuf_csum_performed_flags_t flags, u_int32_t value);
994
995#ifdef KERNEL_PRIVATE
996/*!
997 @function mbuf_get_csum_performed
998 @discussion This is used by the stack to determine which checksums
999 were calculated in hardware on the inbound path.
1000 @param mbuf The mbuf containing the packet.
1001 @param flags Flags indicating which hardware checksum operations
1002 were performed.
1003 @param value If the MBUF_CSUM_DID_DATA flag is set, value will be
1004 set to the value of the TCP or UDP header as calculated by the
1005 hardware.
1006 @result 0 upon success otherwise the errno error.
1007 */
1008errno_t mbuf_get_csum_performed(mbuf_t mbuf,
1009 mbuf_csum_performed_flags_t *flags, u_int32_t *value);
1010#endif
1011
1012/*!
1013 @function mbuf_clear_csum_performed
1014 @discussion Clears the hardware checksum flags and values.
1015 @param mbuf The mbuf containing the packet.
1016 @result 0 upon success otherwise the errno error.
1017 */
1018errno_t mbuf_clear_csum_performed(mbuf_t mbuf);
1019
1020/* mbuf tags */
1021
1022/*!
1023 @function mbuf_tag_id_find
1024 @discussion Lookup the module id for a string. If there is no module
1025 id assigned to this string, a new module id will be assigned.
1026 The string should be the bundle id of the kext. In the case of a
1027 tag that will be shared across multiple kexts, a common bundle id
1028 style string should be used.
1029
1030 The lookup operation is not optimized. A module should call this
1031 function once during startup and chache the module id. The module id
1032 will not be resassigned until the machine reboots.
1033 @param module_string A unique string identifying your module.
1034 Example: com.apple.nke.SharedIP.
1035 @param module_id Upon return, a unique identifier for use with
1036 mbuf_tag_* functions. This identifier is valid until the machine
1037 is rebooted.
1038 @result 0 upon success otherwise the errno error.
1039 */
1040errno_t mbuf_tag_id_find(const char *module_string,
1041 mbuf_tag_id_t *module_id);
1042
1043/*!
1044 @function mbuf_tag_allocate
1045 @discussion Allocate an mbuf tag. Mbuf tags allow various portions
1046 of the stack to tag mbufs with data that will travel with the
1047 mbuf through the stack.
1048
1049 Tags may only be added to mbufs with packet headers
1050 (MBUF_PKTHDR flag is set). Mbuf tags are freed when the mbuf is
1051 freed or when mbuf_tag_free is called.
1052 @param mbuf The mbuf to attach this tag to.
1053 @param module_id A module identifier returned by mbuf_tag_id_find.
1054 @param type A 16 bit type value. For a given module_id, you can use
1055 a number of different tag types.
1056 @param length The length, in bytes, to allocate for storage that
1057 will be associated with this tag on this mbuf.
1058 @param how Indicate whether you want to block and wait for memory if
1059 memory is not immediately available.
1060 @param data_p Upon successful return, *data_p will point to the
1061 buffer allocated for the mtag.
1062 @result 0 upon success otherwise the errno error.
1063 */
1064errno_t mbuf_tag_allocate(mbuf_t mbuf, mbuf_tag_id_t module_id,
1065 mbuf_tag_type_t type, size_t length,
1066 mbuf_how_t how, void** data_p);
1067
1068/*!
1069 @function mbuf_tag_find
1070 @discussion Find the data associated with an mbuf tag.
1071 @param mbuf The mbuf the tag is attached to.
1072 @param module_id A module identifier returned by mbuf_tag_id_find.
1073 @param type The 16 bit type of the tag to find.
1074 @param length Upon success, the length of data will be store in
1075 *length.
1076 @param data_p Upon successful return, *data_p will point to the
1077 buffer allocated for the mtag.
1078 @result 0 upon success otherwise the errno error.
1079 */
1080errno_t mbuf_tag_find(mbuf_t mbuf, mbuf_tag_id_t module_id,
1081 mbuf_tag_type_t type, size_t *length, void** data_p);
1082
1083/*!
1084 @function mbuf_tag_free
1085 @discussion Frees a previously allocated mbuf tag.
1086 @param mbuf The mbuf the tag was allocated on.
1087 @param module_id The ID of the tag to free.
1088 @param type The type of the tag to free.
1089 */
1090void mbuf_tag_free(mbuf_t mbuf, mbuf_tag_id_t module_id,
1091 mbuf_tag_type_t type);
1092
1093/* mbuf stats */
1094
1095/*!
1096 @function mbuf_stats
1097 @discussion Get the mbuf statistics.
1098 @param stats Storage to copy the stats in to.
1099 */
1100void mbuf_stats(struct mbuf_stat* stats);
1101
1102
1103
1104/* IF_QUEUE interaction */
1105
1106#define IF_ENQUEUE_MBUF(ifq, m) { \
1107 mbuf_setnextpkt((m), 0); \
1108 if ((ifq)->ifq_tail == 0) \
1109 (ifq)->ifq_head = (m); \
1110 else \
1111 mbuf_setnextpkt((mbuf_t)(ifq)->ifq_tail, (m)); \
1112 (ifq)->ifq_tail = (m); \
1113 (ifq)->ifq_len++; \
1114}
1115#define IF_PREPEND_MBUF(ifq, m) { \
1116 mbuf_setnextpkt((m), (ifq)->ifq_head); \
1117 if ((ifq)->ifq_tail == 0) \
1118 (ifq)->ifq_tail = (m); \
1119 (ifq)->ifq_head = (m); \
1120 (ifq)->ifq_len++; \
1121}
1122#define IF_DEQUEUE_MBUF(ifq, m) { \
1123 (m) = (ifq)->ifq_head; \
1124 if (m) { \
1125 if (((ifq)->ifq_head = mbuf_nextpkt((m))) == 0) \
1126 (ifq)->ifq_tail = 0; \
1127 mbuf_setnextpkt((m), 0); \
1128 (ifq)->ifq_len--; \
1129 } \
1130}
1131
1132
1133#endif