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