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