1/* 2 * Copyright (c) 2008-2013 Apple Inc. All rights reserved. 3 * 4 * @APPLE_OSREFERENCE_LICENSE_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 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 24 * limitations under the License. 25 * 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 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 retrieved. 55 @constant MBUF_EXT Indicates this mbuf has external data. 56 @constant MBUF_PKTHDR Indicates this mbuf has a packet header. 57 @constant MBUF_EOR Indicates this mbuf is the end of a record. 58 @constant MBUF_LOOP Indicates this packet is looped back. 59 @constant MBUF_BCAST Indicates this packet will be sent or was 60 received as a brodcast. 61 @constant MBUF_MCAST Indicates this packet will be sent or was 62 received as a multicast. 63 @constant MBUF_FRAG Indicates this packet is a fragment of a larger 64 packet. 65 @constant MBUF_FIRSTFRAG Indicates this packet is the first fragment. 66 @constant MBUF_LASTFRAG Indicates this packet is the last fragment. 67 @constant MBUF_PROMISC Indicates this packet was only received 68 because the interface is in promiscuous mode. This should be set 69 by the demux function. These packets will be discarded after 70 being passed to any interface filters. 71*/ 72enum { 73 MBUF_EXT = 0x0001, /* has associated external storage */ 74 MBUF_PKTHDR = 0x0002, /* start of record */ 75 MBUF_EOR = 0x0004, /* end of record */ 76 MBUF_LOOP = 0x0040, /* packet is looped back */ 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 MBUF_HASFCS = 0x4000 /* packet has FCS */ 85}; 86typedef u_int32_t mbuf_flags_t; 87 88/*! 89 @enum mbuf_type_t 90 @abstract Types of mbufs. 91 @discussion Some mbufs represent packets, some represnt data waiting 92 on sockets. Other mbufs store control data or other various 93 structures. The mbuf type is used to store what sort of data the 94 mbuf contains. 95 @constant MBUF_MT_FREE Indicates the mbuf is free and is 96 sitting on the queue of free mbufs. If you find that an mbuf you 97 have a reference to has this type, something has gone terribly 98 wrong. 99 @constant MBUF_MT_DATA Indicates this mbuf is being used to store 100 data. 101 @constant MBUF_MT_HEADER Indicates this mbuf has a packet header, 102 this is probably a packet. 103 @constant MBUF_MT_SOCKET Socket structure. 104 @constant MBUF_MT_PCB Protocol control block. 105 @constant MBUF_MT_RTABLE Routing table entry. 106 @constant MBUF_MT_HTABLE IMP host tables???. 107 @constant MBUF_MT_ATABLE Address resolution table data. 108 @constant MBUF_MT_SONAME Socket name, usually a sockaddr of some 109 sort. 110 @constant MBUF_MT_FTABLE Fragment reassembly header. 111 @constant MBUF_MT_RIGHTS Access rights. 112 @constant MBUF_MT_IFADDR Interface address. 113 @constant MBUF_MT_CONTROL Extra-data protocol message (control 114 message). 115 @constant MBUF_MT_OOBDATA Out of band data. 116*/ 117enum { 118 MBUF_TYPE_FREE = 0, /* should be on free list */ 119 MBUF_TYPE_DATA = 1, /* dynamic (data) allocation */ 120 MBUF_TYPE_HEADER = 2, /* packet header */ 121 MBUF_TYPE_SOCKET = 3, /* socket structure */ 122 MBUF_TYPE_PCB = 4, /* protocol control block */ 123 MBUF_TYPE_RTABLE = 5, /* routing tables */ 124 MBUF_TYPE_HTABLE = 6, /* IMP host tables */ 125 MBUF_TYPE_ATABLE = 7, /* address resolution tables */ 126 MBUF_TYPE_SONAME = 8, /* socket name */ 127 MBUF_TYPE_SOOPTS = 10, /* socket options */ 128 MBUF_TYPE_FTABLE = 11, /* fragment reassembly header */ 129 MBUF_TYPE_RIGHTS = 12, /* access rights */ 130 MBUF_TYPE_IFADDR = 13, /* interface address */ 131 MBUF_TYPE_CONTROL = 14, /* extra-data protocol message */ 132 MBUF_TYPE_OOBDATA = 15 /* expedited data */ 133}; 134typedef u_int32_t mbuf_type_t; 135 136/*! 137 @enum mbuf_csum_request_flags_t 138 @abstract Checksum performed/requested flags. 139 @discussion Mbufs often contain packets. Some hardware supports 140 performing checksums in hardware. The stack uses these flags to 141 indicate to the driver what sort of checksumming should be 142 handled in by the driver/hardware. These flags will only be set 143 if the driver indicates that it supports the corresponding 144 checksums using ifnet_set_offload. 145 @constant MBUF_CSUM_REQ_IP Indicates the IP checksum has not been 146 calculated yet. 147 @constant MBUF_CSUM_REQ_TCP Indicates the TCP checksum has not been 148 calculated yet. 149 @constant MBUF_CSUM_REQ_UDP Indicates the UDP checksum has not been 150 calculated yet. 151 @constant MBUF_CSUM_REQ_TCPIPV6 Indicates the TCP checksum for IPv6 152 has not been calculated yet. 153 @constant MBUF_CSUM_REQ_UDPIPV6 Indicates the UDP checksum for IPv6 154 has not been calculated yet. 155*/ 156enum { 157 MBUF_TSO_IPV4 = 0x100000, 158 MBUF_TSO_IPV6 = 0x200000 159}; 160typedef u_int32_t mbuf_tso_request_flags_t; 161 162enum { 163#ifdef KERNEL_PRIVATE 164 MBUF_CSUM_PARTIAL = 0x1000, /* 16-bit 1's complement sum */ 165 MBUF_CSUM_REQ_SUM16 = MBUF_CSUM_PARTIAL, 166#endif /* KERNEL_PRIVATE */ 167 MBUF_CSUM_REQ_IP = 0x0001, 168 MBUF_CSUM_REQ_TCP = 0x0002, 169 MBUF_CSUM_REQ_UDP = 0x0004, 170 MBUF_CSUM_REQ_TCPIPV6 = 0x0020, 171 MBUF_CSUM_REQ_UDPIPV6 = 0x0040 172}; 173typedef u_int32_t mbuf_csum_request_flags_t; 174 175/*! 176 @enum mbuf_csum_performed_flags_t 177 @abstract Checksum performed/requested flags. 178 @discussion Mbufs often contain packets. Some hardware supports 179 performing checksums in hardware. The driver uses these flags to 180 communicate to the stack the checksums that were calculated in 181 hardware. 182 @constant MBUF_CSUM_DID_IP Indicates that the driver/hardware verified 183 the IP checksum in hardware. 184 @constant MBUF_CSUM_IP_GOOD Indicates whether or not the IP checksum 185 was good or bad. Only valid when MBUF_CSUM_DID_IP is set. 186 @constant MBUF_CSUM_DID_DATA Indicates that the TCP or UDP checksum 187 was calculated. The value for the checksum calculated in 188 hardware should be passed as the second parameter of 189 mbuf_set_csum_performed. The hardware calculated checksum value 190 can be retrieved using the second parameter passed to 191 mbuf_get_csum_performed. This should be done for IPv4 or IPv6. 192 @constant MBUF_CSUM_PSEUDO_HDR If set, this indicates that the 193 checksum value for MBUF_CSUM_DID_DATA includes the pseudo header 194 value. If this is not set, the stack will calculate the pseudo 195 header value and add that to the checksum. The value of this bit 196 is only valid when MBUF_CSUM_DID_DATA is set. 197*/ 198enum { 199#ifdef KERNEL_PRIVATE 200 MBUF_CSUM_TCP_SUM16 = MBUF_CSUM_PARTIAL, 201#endif /* KERNEL_PRIVATE */ 202 MBUF_CSUM_DID_IP = 0x0100, 203 MBUF_CSUM_IP_GOOD = 0x0200, 204 MBUF_CSUM_DID_DATA = 0x0400, 205 MBUF_CSUM_PSEUDO_HDR = 0x0800 206}; 207typedef u_int32_t mbuf_csum_performed_flags_t; 208 209/*! 210 @enum mbuf_how_t 211 @abstract Method of allocating an mbuf. 212 @discussion Blocking will cause the funnel to be dropped. If the 213 funnel is dropped, other threads may make changes to networking 214 data structures. This can lead to very bad things happening. 215 Blocking on the input our output path can also impact 216 performance. There are some cases where making a blocking call 217 is acceptable. When in doubt, use MBUF_DONTWAIT. 218 @constant MBUF_WAITOK Allow a call to allocate an mbuf to block. 219 @constant MBUF_DONTWAIT Don't allow the mbuf allocation call to 220 block, if blocking is necessary fail and return immediately. 221*/ 222enum { 223 MBUF_WAITOK = 0, /* Ok to block to get memory */ 224 MBUF_DONTWAIT = 1 /* Don't block, fail if blocking would be required */ 225}; 226typedef u_int32_t mbuf_how_t; 227 228typedef u_int32_t mbuf_tag_id_t; 229typedef u_int16_t mbuf_tag_type_t; 230 231/*! 232 @struct mbuf_stat 233 @discussion The mbuf_stat contains mbuf statistics. 234 @field mbufs Number of mbufs (free or otherwise). 235 @field clusters Number of clusters (free or otherwise). 236 @field clfree Number of free clusters. 237 @field drops Number of times allocation failed. 238 @field wait Number of times allocation blocked. 239 @field drain Number of times protocol drain functions were called. 240 @field mtypes An array of counts of each type of mbuf allocated. 241 @field mcfail Number of times m_copym failed. 242 @field mpfail Number of times m_pullup failed. 243 @field msize Length of an mbuf. 244 @field mclbytes Length of an mbuf cluster. 245 @field minclsize Minimum length of data to allocate a cluster. 246 Anything smaller than this should be placed in chained mbufs. 247 @field mlen Length of data in an mbuf. 248 @field mhlen Length of data in an mbuf with a packet header. 249 @field bigclusters Number of big clusters. 250 @field bigclfree Number of unused big clusters. 251 @field bigmclbytes Length of a big mbuf cluster. 252*/ 253struct mbuf_stat { 254 u_int32_t mbufs; /* mbufs obtained from page pool */ 255 u_int32_t clusters; /* clusters obtained from page pool */ 256 u_int32_t clfree; /* free clusters */ 257 u_int32_t drops; /* times failed to find space */ 258 u_int32_t wait; /* times waited for space */ 259 u_int32_t drain; /* times drained protocols for space */ 260 u_short mtypes[256]; /* type specific mbuf allocations */ 261 u_int32_t mcfail; /* times m_copym failed */ 262 u_int32_t mpfail; /* times m_pullup failed */ 263 u_int32_t msize; /* length of an mbuf */ 264 u_int32_t mclbytes; /* length of an mbuf cluster */ 265 u_int32_t minclsize; /* min length of data to allocate a cluster */ 266 u_int32_t mlen; /* length of data in an mbuf */ 267 u_int32_t mhlen; /* length of data in a header mbuf */ 268 u_int32_t bigclusters; /* number of big clusters */ 269 u_int32_t bigclfree; /* number of big clustser free */ 270 u_int32_t bigmclbytes; /* length of data in a big cluster */ 271}; 272 273/* Parameter for m_copym to copy all bytes */ 274#define MBUF_COPYALL 1000000000 275 276__BEGIN_DECLS 277/* Data access */ 278/*! 279 @function mbuf_data 280 @discussion Returns a pointer to the start of data in this mbuf. 281 There may be additional data on chained mbufs. The data you're 282 looking for may not be virtually contiguous if it spans more 283 than one mbuf. In addition, data that is virtually contiguous 284 might not be represented by physically contiguous pages; see 285 further comments in mbuf_data_to_physical. Use mbuf_len to 286 determine the lenght of data available in this mbuf. If a data 287 structure you want to access stradles two mbufs in a chain, 288 either use mbuf_pullup to get the data contiguous in one mbuf 289 or copy the pieces of data from each mbuf in to a contiguous 290 buffer. Using mbuf_pullup has the advantage of not having to 291 copy the data. On the other hand, if you don't make sure there 292 is space in the mbuf, mbuf_pullup may fail and free the mbuf. 293 @param mbuf The mbuf. 294 @result A pointer to the data in the mbuf. 295 */ 296extern void *mbuf_data(mbuf_t mbuf); 297 298/*! 299 @function mbuf_datastart 300 @discussion Returns the start of the space set aside for storing 301 data in an mbuf. An mbuf's data may come from a cluster or be 302 embedded in the mbuf structure itself. The data pointer 303 retrieved by mbuf_data may not be at the start of the data 304 (mbuf_leadingspace will be non-zero). This function will return 305 a pointer that matches mbuf_data() - mbuf_leadingspace(). 306 @param mbuf The mbuf. 307 @result A pointer to smallest possible value for data. 308 */ 309extern void *mbuf_datastart(mbuf_t mbuf); 310 311/*! 312 @function mbuf_setdata 313 @discussion Sets the data and length values for an mbuf. The data 314 value must be in a valid range. In the case of an mbuf with a cluster, 315 the data value must point to a location in the cluster and the data 316 value plus the length, must be less than the end of the cluster. For 317 data embedded directly in an mbuf (no cluster), the data value must 318 fall somewhere between the start and end of the data area in the 319 mbuf and the data + length must also be in the same range. 320 @param mbuf The mbuf. 321 @param data The new pointer value for data. 322 @param len The new length of data in the mbuf. 323 @result 0 on success, errno error on failure. 324 */ 325extern errno_t mbuf_setdata(mbuf_t mbuf, void *data, size_t len); 326 327/*! 328 @function mbuf_align_32 329 @discussion mbuf_align_32 is a replacement for M_ALIGN and MH_ALIGN. 330 mbuf_align_32 will set the data pointer to a location aligned on 331 a four byte boundry with at least 'len' bytes between the data 332 pointer and the end of the data block. 333 @param mbuf The mbuf. 334 @param len The minimum length of space that should follow the new 335 data location. 336 @result 0 on success, errno error on failure. 337 */ 338extern errno_t mbuf_align_32(mbuf_t mbuf, size_t len); 339 340/*! 341 @function mbuf_data_to_physical 342 @discussion mbuf_data_to_physical is a replacement for mcl_to_paddr. 343 Given a pointer returned from mbuf_data of mbuf_datastart, 344 mbuf_data_to_physical will return the phyical address for that 345 block of data. Note that even though the data is in virtually 346 contiguous span, the underlying physical pages might not be 347 physically contiguous. Because of this, callers must ensure 348 to call this routine for each page boundary. Device drivers 349 that deal with DMA are strongly encouraged to utilize the 350 IOMbufNaturalMemoryCursor and walk down the list of vectors 351 instead of using this interface to obtain the physical address. 352 Use of this routine is therefore discouraged. 353 @param ptr A pointer to data stored in an mbuf. 354 @result The 64 bit physical address of the mbuf data or NULL if ptr 355 does not point to data stored in an mbuf. 356 */ 357extern addr64_t mbuf_data_to_physical(void *ptr); 358 359 360/* Allocation */ 361 362/*! 363 @function mbuf_get 364 @discussion Allocates an mbuf without a cluster for external data. 365 @param how Blocking or non-blocking. 366 @param type The type of the mbuf. 367 @param mbuf The mbuf. 368 @result 0 on success, errno error on failure. 369 */ 370extern errno_t mbuf_get(mbuf_how_t how, mbuf_type_t type, mbuf_t *mbuf); 371 372/*! 373 @function mbuf_gethdr 374 @discussion Allocates an mbuf without a cluster for external data. 375 Sets a flag to indicate there is a packet header and initializes 376 the packet header. 377 @param how Blocking or non-blocking. 378 @param type The type of the mbuf. 379 @param mbuf The mbuf. 380 @result 0 on success, errno error on failure. 381 */ 382extern errno_t mbuf_gethdr(mbuf_how_t how, mbuf_type_t type, mbuf_t *mbuf); 383 384/*! 385 @function mbuf_attachcluster 386 @discussion Attach an external buffer as a cluster for an mbuf. If mbuf 387 points to a NULL mbuf_t, an mbuf will be allocated for you. If 388 mbuf points to a non-NULL mbuf_t, the user-supplied mbuf will 389 be used instead. The caller is responsible for allocating the 390 external buffer by calling mbuf_alloccluster(). 391 @param how Blocking or non-blocking. 392 @param type The type of the mbuf if mbuf is non-NULL; otherwise ignored. 393 @param mbuf Pointer to the address of the mbuf; if NULL, an mbuf will 394 be allocated, otherwise, it must point to a valid mbuf address. 395 If the user-supplied mbuf is already attached to a cluster, the 396 current cluster will be freed before the mbuf gets attached to 397 the supplied external buffer. Note that this routine may return 398 a different mbuf_t than the one you passed in. 399 @param extbuf Address of the external buffer. 400 @param extfree Free routine for the external buffer; the caller is 401 required to defined a routine that will be invoked when the 402 mbuf is freed. 403 @param extsize Size of the external buffer. 404 @param extarg Private value that will be passed to the free routine 405 when it is called at the time the mbuf is freed. 406 @result 0 on success 407 EINVAL - Invalid parameter 408 ENOMEM - Not enough memory available 409 */ 410extern errno_t mbuf_attachcluster(mbuf_how_t how, mbuf_type_t type, 411 mbuf_t *mbuf, caddr_t extbuf, void (*extfree)(caddr_t , u_int, caddr_t), 412 size_t extsize, caddr_t extarg); 413 414/*! 415 @function mbuf_alloccluster 416 @discussion Allocate a cluster that can be later attached to an 417 mbuf by calling mbuf_attachcluster(). The allocated cluster 418 can also be freed (without being attached to an mbuf) by 419 calling mbuf_freecluster(). At the moment this routine 420 will either return a cluster of 2048, 4096 or 16384 bytes 421 depending on the requested size. Note that clusters greater 422 than 4096 bytes might not be available in all configurations; 423 the caller must additionally check for ENOTSUP (see below). 424 @param how Blocking or non-blocking. 425 @param size Pointer to size of requested cluster. Sizes up to 2048 426 will be rounded up to 2048; sizes greater than 2048 and up 427 to 4096 will be rounded up to 4096. Sizes greater than 4096 428 will be rounded up to 16384. 429 @param addr Pointer to the address of the requested cluster. 430 @result 0 on success or ENOMEM if failure. If the caller requests 431 greater than 4096 bytes and the system is unable to fulfill 432 the request due to the lack of jumbo clusters support based 433 on the configuration, this routine will return ENOTSUP. 434 In this case, the caller is advised to use 4096 bytes or 435 smaller during subseqent requests. 436 */ 437extern errno_t mbuf_alloccluster(mbuf_how_t how, size_t *size, caddr_t *addr); 438 439/*! 440 @function mbuf_freecluster 441 @discussion Free a cluster that was previously allocated by a call 442 to mbuf_alloccluster(). The caller must pass the actual 443 size of the cluster as returned by mbuf_alloccluster(), 444 which at this point must be either 2048, 4096 or 16384 bytes. 445 @param addr The address of the cluster. 446 @param size The actual size of the cluster. 447 */ 448extern void mbuf_freecluster(caddr_t addr, size_t size); 449 450/*! 451 @function mbuf_getcluster 452 @discussion Allocate a cluster of the requested size and attach it to 453 an mbuf for use as external data. If mbuf points to a NULL 454 mbuf_t, an mbuf will be allocated for you. If mbuf points to 455 a non-NULL mbuf_t, mbuf_getcluster may return a different 456 mbuf_t than the one you passed in. 457 @param how Blocking or non-blocking. 458 @param type The type of the mbuf. 459 @param size The size of the cluster to be allocated. Supported sizes 460 for a cluster are be 2048, 4096, or 16384. Any other value 461 with return EINVAL. Note that clusters greater than 4096 462 bytes might not be available in all configurations; the 463 caller must additionally check for ENOTSUP (see below). 464 @param mbuf The mbuf the cluster will be attached to. 465 @result 0 on success, errno error on failure. If you specified NULL 466 for the mbuf, any intermediate mbuf that may have been allocated 467 will be freed. If you specify an mbuf value in *mbuf, 468 mbuf_mclget will not free it. 469 EINVAL - Invalid parameter 470 ENOMEM - Not enough memory available 471 ENOTSUP - The caller had requested greater than 4096 bytes 472 cluster and the system is unable to fulfill it due to the 473 lack of jumbo clusters support based on the configuration. 474 In this case, the caller is advised to use 4096 bytes or 475 smaller during subsequent requests. 476 */ 477extern errno_t mbuf_getcluster(mbuf_how_t how, mbuf_type_t type, size_t size, 478 mbuf_t *mbuf); 479 480/*! 481 @function mbuf_mclget 482 @discussion Allocate a cluster and attach it to an mbuf for use as 483 external data. If mbuf points to a NULL mbuf_t, an mbuf will be 484 allocated for you. If mbuf points to a non-NULL mbuf_t, 485 mbuf_mclget may return a different mbuf_t than the one you 486 passed in. 487 @param how Blocking or non-blocking. 488 @param type The type of the mbuf. 489 @param mbuf The mbuf the cluster will be attached to. 490 @result 0 on success, errno error on failure. If you specified NULL 491 for the mbuf, any intermediate mbuf that may have been allocated 492 will be freed. If you specify an mbuf value in *mbuf, 493 mbuf_mclget will not free it. 494 */ 495extern errno_t mbuf_mclget(mbuf_how_t how, mbuf_type_t type, mbuf_t *mbuf); 496 497/*! 498 @function mbuf_allocpacket 499 @discussion Allocate an mbuf chain to store a single packet of the 500 requested length. According to the requested length, a chain 501 of mbufs will be created. The mbuf type will be set to 502 MBUF_TYPE_DATA. The caller may specify the maximum number of 503 buffer. 504 @param how Blocking or non-blocking 505 @param packetlen The total length of the packet mbuf to be allocated. 506 The length must be greater than zero. 507 @param maxchunks An input/output pointer to the maximum number of mbufs 508 segments making up the chain. On input, if maxchunks is NULL, 509 or the value pointed to by maxchunks is zero, the packet will 510 be made up of as few or as many buffer segments as necessary 511 to fit the length. The allocation will fail with ENOBUFS if 512 the number of segments requested is too small and the sum of 513 the maximum size of each individual segment is less than the 514 packet length. On output, if the allocation succeed and 515 maxchunks is non-NULL, it will point to the actual number 516 of segments allocated. 517 Additional notes for packetlen greater than 4096 bytes: 518 the caller may pass a non-NULL maxchunks and initialize it 519 with zero such that upon success, it can find out whether 520 or not the system configuration allows for larger than 521 4096 bytes cluster allocations, by checking on the value 522 pointed to by maxchunks. E.g. a request for 9018 bytes may 523 result in 1 chunk when jumbo clusters are available, or 524 3 chunks otherwise. 525 @param Upon success, *mbuf will be a reference to the new mbuf. 526 @result Returns 0 upon success or the following error code: 527 EINVAL - Invalid parameter 528 ENOMEM - Not enough memory available 529 ENOBUFS - Buffers not big enough for the maximum number of 530 chunks requested 531*/ 532extern errno_t mbuf_allocpacket(mbuf_how_t how, size_t packetlen, 533 unsigned int * maxchunks, mbuf_t *mbuf); 534 535/*! 536 @function mbuf_allocpacket_list 537 @discussion Allocate a linked list of packets. According to the 538 requested length, each packet will made of a chain of one 539 or more mbufs. The mbuf type will be set to MBUF_TYPE_DATA. 540 The caller may specify the maximum number of element for 541 each mbuf chain making up a packet. 542 @param numpkts Number of packets to allocate 543 @param how Blocking or non-blocking 544 @param packetlen The total length of the packet mbuf to be allocated. 545 The length must be greater than zero. 546 @param maxchunks An input/output pointer to the maximum number of 547 mbufs segments making up the chain. On input, if maxchunks is 548 zero, or the value pointed to by maxchunks is zero, the packet 549 will be made of as few or as many buffer segments as necessary 550 to fit the length. The allocation will fail with ENOBUFS if 551 the number of segments requested is too small and the sum of 552 the maximum size of each individual segment is less than the 553 packet length. On output, if the allocation succeed and 554 maxchunks is non zero, it will point to the actual number 555 of segments allocated. 556 Additional notes for packetlen greater than 4096 bytes: 557 the caller may pass a non-NULL maxchunks and initialize it 558 with zero such that upon success, it can find out whether 559 or not the system configuration allows for larger than 560 4096 bytes cluster allocations, by checking on the value 561 pointed to by maxchunks. E.g. a request for 9018 bytes may 562 result in 1 chunk when jumbo clusters are available, or 563 3 chunks otherwise. 564 @param Upon success, *mbuf will be a reference to the new mbuf. 565 @result Returns 0 upon success or the following error code: 566 EINVAL - Invalid parameter 567 ENOMEM - Not enough memory available 568 ENOBUFS - Buffers not big enough for the maximum number of 569 chunks requested 570*/ 571extern errno_t mbuf_allocpacket_list(unsigned int numpkts, mbuf_how_t how, 572 size_t packetlen, unsigned int * maxchunks, mbuf_t *mbuf); 573 574 575/*! 576 @function mbuf_getpacket 577 @discussion Allocate an mbuf, allocate and attach a cluster, and set 578 the packet header flag. 579 @param how Blocking or non-blocking. 580 @param mbuf Upon success, *mbuf will be a reference to the new mbuf. 581 @result 0 on success, errno error on failure. 582 */ 583extern errno_t mbuf_getpacket(mbuf_how_t how, mbuf_t *mbuf); 584 585/*! 586 @function mbuf_free 587 @discussion Frees a single mbuf. Not commonly used because it 588 doesn't touch the rest of the mbufs on the chain. 589 @param mbuf The mbuf to free. 590 @result The next mbuf in the chain. 591 */ 592extern mbuf_t mbuf_free(mbuf_t mbuf); 593 594/*! 595 @function mbuf_freem 596 @discussion Frees a chain of mbufs link through mnext. 597 @param mbuf The first mbuf in the chain to free. 598 */ 599extern void mbuf_freem(mbuf_t mbuf); 600 601/*! 602 @function mbuf_freem_list 603 @discussion Frees linked list of mbuf chains. Walks through 604 mnextpackt and does the equivalent of mbuf_freem to each. 605 @param mbuf The first mbuf in the linked list to free. 606 @result The number of mbufs freed. 607 */ 608extern int mbuf_freem_list(mbuf_t mbuf); 609 610/*! 611 @function mbuf_leadingspace 612 @discussion Determines the space available in the mbuf proceeding 613 the current data. 614 @param mbuf The mbuf. 615 @result The number of unused bytes at the start of the mbuf. 616 */ 617extern size_t mbuf_leadingspace(const mbuf_t mbuf); 618 619/*! 620 @function mbuf_trailingspace 621 @discussion Determines the space available in the mbuf following 622 the current data. 623 @param mbuf The mbuf. 624 @result The number of unused bytes following the current data. 625 */ 626extern size_t mbuf_trailingspace(const mbuf_t mbuf); 627 628/* Manipulation */ 629 630/*! 631 @function mbuf_copym 632 @discussion Copies len bytes from offset from src to a new mbuf. If 633 the original mbuf contains a packet header, the new mbuf will 634 contain similar packet header except for any tags which may be 635 associated with the original mbuf. mbuf_dup() should be used 636 instead if tags are to be copied to the new mbuf. 637 @param src The source mbuf. 638 @param offset The offset in the mbuf to start copying from. 639 @param len The the number of bytes to copy. 640 @param how To block or not to block, that is a question. 641 @param new_mbuf Upon success, the newly allocated mbuf. 642 @result 0 upon success otherwise the errno error. 643 */ 644extern errno_t mbuf_copym(const mbuf_t src, size_t offset, size_t len, 645 mbuf_how_t how, mbuf_t *new_mbuf); 646 647/*! 648 @function mbuf_dup 649 @discussion Exactly duplicates an mbuf chain. If the original mbuf 650 contains a packet header (including tags), the new mbuf will have 651 the same packet header contents and a copy of each tag associated 652 with the original mbuf. 653 @param src The source mbuf. 654 @param how Blocking or non-blocking. 655 @param new_mbuf Upon success, the newly allocated mbuf. 656 @result 0 upon success otherwise the errno error. 657 */ 658extern errno_t mbuf_dup(const mbuf_t src, mbuf_how_t how, mbuf_t *new_mbuf); 659 660/*! 661 @function mbuf_prepend 662 @discussion Prepend len bytes to an mbuf. If there is space 663 (mbuf_leadingspace >= len), the mbuf's data ptr is changed and 664 the same mbuf is returned. If there is no space, a new mbuf may 665 be allocated and prepended to the mbuf chain. If the operation 666 fails, the mbuf may be freed (*mbuf will be NULL). 667 @param mbuf The mbuf to prepend data to. This may change if a new 668 mbuf must be allocated or may be NULL if the operation fails. 669 @param len The length, in bytes, to be prepended to the mbuf. 670 @param how Blocking or non-blocking. 671 @result 0 upon success otherwise the errno error. 672 */ 673extern errno_t mbuf_prepend(mbuf_t *mbuf, size_t len, mbuf_how_t how); 674 675/*! 676 @function mbuf_split 677 @discussion Split an mbuf chain at a specific offset. 678 @param src The mbuf to be split. 679 @param offset The offset in the buffer where the mbuf should be 680 split. 681 @param how Blocking or non-blocking. 682 @param new_mbuf Upon success, the second half of the split mbuf 683 chain. 684 @result 0 upon success otherwise the errno error. In the case of 685 failure, the original mbuf chain passed in to src will be 686 preserved. 687 */ 688extern errno_t mbuf_split(mbuf_t src, size_t offset, mbuf_how_t how, 689 mbuf_t *new_mbuf); 690 691/*! 692 @function mbuf_pullup 693 @discussion Move the next len bytes in to mbuf from other mbufs in 694 the chain. This is commonly used to get the IP and TCP or UDP 695 header contiguous in the first mbuf. If mbuf_pullup fails, the 696 entire mbuf chain will be freed. 697 @param mbuf The mbuf in the chain the data should be contiguous in. 698 @param len The number of bytes to pull from the next mbuf(s). 699 @result 0 upon success otherwise the errno error. In the case of an 700 error, the mbuf chain has been freed. 701 */ 702extern errno_t mbuf_pullup(mbuf_t *mbuf, size_t len); 703 704/*! 705 @function mbuf_pulldown 706 @discussion Make length bytes at offset in the mbuf chain 707 contiguous. Nothing before offset bytes in the chain will be 708 modified. Upon return, location will be the mbuf the data is 709 contiguous in and offset will be the offset in that mbuf at 710 which the data is located. In the case of a failure, the mbuf 711 chain will be freed. 712 @param src The start of the mbuf chain. 713 @param offset Pass in a pointer to a value with the offset of the 714 data you're interested in making contiguous. Upon success, this 715 will be overwritten with the offset from the mbuf returned in 716 location. 717 @param length The length of data that should be made contiguous. 718 @param location Upon success, *location will be the mbuf the data is 719 in. 720 @result 0 upon success otherwise the errno error. 721 */ 722extern errno_t mbuf_pulldown(mbuf_t src, size_t *offset, size_t length, 723 mbuf_t *location); 724 725/*! 726 @function mbuf_adj 727 @discussion Trims len bytes from the mbuf. If the length is greater 728 than zero, the bytes are trimmed from the front of the mbuf. If 729 the length is less than zero, the bytes are trimmed from the end 730 of the mbuf chain. 731 @param mbuf The mbuf chain to trim. 732 @param len The number of bytes to trim from the mbuf chain. 733 */ 734extern void mbuf_adj(mbuf_t mbuf, int len); 735 736/*! 737 @function mbuf_adjustlen 738 @discussion Adds amount to the mbuf len. Verifies that the new 739 length is valid (greater than or equal to zero and less than 740 maximum amount of data that may be stored in the mbuf). This 741 function will not adjust the packet header length field or 742 affect any other mbufs in a chain. 743 @param mbuf The mbuf to adjust. 744 @param amount The number of bytes increment the length by. 745 @result 0 upon success otherwise the errno error. 746 */ 747extern errno_t mbuf_adjustlen(mbuf_t mbuf, int amount); 748 749/*! 750 @function mbuf_concatenate 751 @discussion Concatenate mbuf chain src to dst using m_next and return 752 a chain which represents the concatenated chain. The routine 753 does not prevent two chains of different mbuf types to be 754 concatenated, nor does it modify any packet header in the 755 destination chain. Therefore, it's the responsibility of the 756 caller to ensure that the resulted concatenated mbuf chain is 757 correct for further usages. 758 @param dst The destination mbuf chain. 759 @param src The source mbuf chain. 760 @result A pointer to the head of the concatenated mbuf chain. This 761 should be treated as the updated destination mbuf chain; the 762 caller must no longer refer to the original src or dst mbuf 763 chain. Otherwise it returns NULL if the original dst mbuf 764 chain is NULL. 765 */ 766extern mbuf_t mbuf_concatenate(mbuf_t dst, mbuf_t src); 767 768/*! 769 @function mbuf_copydata 770 @discussion Copies data out of an mbuf in to a specified buffer. If 771 the data is stored in a chain of mbufs, the data will be copied 772 from each mbuf in the chain until length bytes have been copied. 773 @param mbuf The mbuf chain to copy data out of. 774 @param offset The offset in to the mbuf to start copying. 775 @param length The number of bytes to copy. 776 @param out_data A pointer to the location where the data will be 777 copied. 778 @result 0 upon success otherwise the errno error. 779 */ 780extern errno_t mbuf_copydata(const mbuf_t mbuf, size_t offset, size_t length, 781 void *out_data); 782 783/*! 784 @function mbuf_copyback 785 @discussion Copies data from a buffer to an mbuf chain. 786 mbuf_copyback will grow the chain to fit the specified buffer. 787 788 If mbuf_copydata is unable to allocate enough mbufs to grow the 789 chain, ENOBUFS will be returned. The mbuf chain will be shorter 790 than expected but all of the data up to the end of the mbuf 791 chain will be valid. 792 793 If an offset is specified, mbuf_copyback will skip that many 794 bytes in the mbuf chain before starting to write the buffer in 795 to the chain. If the mbuf chain does not contain this many 796 bytes, mbufs will be allocated to create the space. 797 @param mbuf The first mbuf in the chain to copy the data in to. 798 @param offset Offset in bytes to skip before copying data. 799 @param length The length, in bytes, of the data to copy in to the mbuf 800 chain. 801 @param data A pointer to data in the kernel's address space. 802 @param how Blocking or non-blocking. 803 @result 0 upon success, EINVAL or ENOBUFS upon failure. 804 */ 805extern errno_t mbuf_copyback(mbuf_t mbuf, size_t offset, size_t length, 806 const void *data, mbuf_how_t how); 807 808/*! 809 @function mbuf_mclhasreference 810 @discussion Check if a cluster of an mbuf is referenced by another mbuf. 811 References may be taken, for example, as a result of a call to 812 mbuf_split or mbuf_copym 813 @param mbuf The mbuf with the cluster to test. 814 @result 0 if there is no reference by another mbuf, 1 otherwise. 815 */ 816extern int mbuf_mclhasreference(mbuf_t mbuf); 817 818 819/* mbuf header */ 820 821/*! 822 @function mbuf_next 823 @discussion Returns the next mbuf in the chain. 824 @param mbuf The mbuf. 825 @result The next mbuf in the chain. 826 */ 827extern mbuf_t mbuf_next(const mbuf_t mbuf); 828 829/*! 830 @function mbuf_setnext 831 @discussion Sets the next mbuf in the chain. 832 @param mbuf The mbuf. 833 @param next The new next mbuf. 834 @result 0 upon success otherwise the errno error. 835 */ 836extern errno_t mbuf_setnext(mbuf_t mbuf, mbuf_t next); 837 838/*! 839 @function mbuf_nextpkt 840 @discussion Gets the next packet from the mbuf. 841 @param mbuf The mbuf. 842 @result The nextpkt. 843 */ 844extern mbuf_t mbuf_nextpkt(const mbuf_t mbuf); 845 846/*! 847 @function mbuf_setnextpkt 848 @discussion Sets the next packet attached to this mbuf. 849 @param mbuf The mbuf. 850 @param nextpkt The new next packet. 851 */ 852extern void mbuf_setnextpkt(mbuf_t mbuf, mbuf_t nextpkt); 853 854/*! 855 @function mbuf_len 856 @discussion Gets the length of data in this mbuf. 857 @param mbuf The mbuf. 858 @result The length. 859 */ 860extern size_t mbuf_len(const mbuf_t mbuf); 861 862/*! 863 @function mbuf_setlen 864 @discussion Sets the length of data in this packet. Be careful to 865 not set the length over the space available in the mbuf. 866 @param mbuf The mbuf. 867 @param len The new length. 868 @result 0 upon success otherwise the errno error. 869 */ 870extern void mbuf_setlen(mbuf_t mbuf, size_t len); 871 872/*! 873 @function mbuf_maxlen 874 @discussion Retrieves the maximum length of data that may be stored 875 in this mbuf. This value assumes that the data pointer was set 876 to the start of the possible range for that pointer 877 (mbuf_data_start). 878 @param mbuf The mbuf. 879 @result The maximum lenght of data for this mbuf. 880 */ 881extern size_t mbuf_maxlen(const mbuf_t mbuf); 882 883/*! 884 @function mbuf_type 885 @discussion Gets the type of mbuf. 886 @param mbuf The mbuf. 887 @result The type. 888 */ 889extern mbuf_type_t mbuf_type(const mbuf_t mbuf); 890 891/*! 892 @function mbuf_settype 893 @discussion Sets the type of mbuf. 894 @param mbuf The mbuf. 895 @param new_type The new type. 896 @result 0 upon success otherwise the errno error. 897 */ 898extern errno_t mbuf_settype(mbuf_t mbuf, mbuf_type_t new_type); 899 900/*! 901 @function mbuf_flags 902 @discussion Returns the set flags. 903 @param mbuf The mbuf. 904 @result The flags. 905 */ 906extern mbuf_flags_t mbuf_flags(const mbuf_t mbuf); 907 908/*! 909 @function mbuf_setflags 910 @discussion Sets the set of set flags. 911 @param mbuf The mbuf. 912 @param flags The flags that should be set, all other flags will be 913 cleared. Certain flags such as MBUF_EXT cannot be altered. 914 @result 0 upon success otherwise the errno error. 915 */ 916extern errno_t mbuf_setflags(mbuf_t mbuf, mbuf_flags_t flags); 917 918/*! 919 @function mbuf_setflags_mask 920 @discussion Useful for setting or clearing individual flags. Easier 921 than calling mbuf_setflags(m, mbuf_flags(m) | M_FLAG). 922 @param mbuf The mbuf. 923 @param flags The flags that should be set or cleared. Certain flags 924 such as MBUF_EXT cannot be altered. 925 @param mask The mask controlling which flags will be modified. 926 @result 0 upon success otherwise the errno error. 927 */ 928extern errno_t mbuf_setflags_mask(mbuf_t mbuf, mbuf_flags_t flags, 929 mbuf_flags_t mask); 930 931/*! 932 @function mbuf_copy_pkthdr 933 @discussion Copies the packet header from src to dest. 934 @param src The mbuf from which the packet header will be copied. 935 @param mbuf The mbuf to which the packet header will be copied. 936 @result 0 upon success otherwise the errno error. 937 */ 938extern errno_t mbuf_copy_pkthdr(mbuf_t dest, const mbuf_t src); 939 940/*! 941 @function mbuf_pkthdr_len 942 @discussion Returns the length as reported by the packet header. 943 @param mbuf The mbuf containing the packet header with the length to 944 be changed. 945 @result The length, in bytes, of the packet. 946 */ 947extern size_t mbuf_pkthdr_len(const mbuf_t mbuf); 948 949/*! 950 @function mbuf_pkthdr_setlen 951 @discussion Sets the length of the packet in the packet header. 952 @param mbuf The mbuf containing the packet header. 953 @param len The new length of the packet. 954 */ 955extern void mbuf_pkthdr_setlen(mbuf_t mbuf, size_t len); 956 957/*! 958 @function mbuf_pkthdr_adjustlen 959 @discussion Adjusts the length of the packet in the packet header. 960 @param mbuf The mbuf containing the packet header. 961 @param amount The number of bytes to adjust the packet header length 962 field by. 963 */ 964extern void mbuf_pkthdr_adjustlen(mbuf_t mbuf, int amount); 965 966/*! 967 @function mbuf_pkthdr_rcvif 968 @discussion Returns the interface the packet was received on. This 969 funciton does not modify the reference count of the interface. 970 The interface is only valid for as long as the mbuf is not freed 971 and the rcvif for the mbuf is not changed. Take a reference on 972 the interface that you will release later before doing any of 973 the following: free the mbuf, change the rcvif, pass the mbuf to 974 any function that may free the mbuf or change the rcvif. 975 @param mbuf The mbuf containing the packet header. 976 @result A reference to the interface. 977 */ 978extern ifnet_t mbuf_pkthdr_rcvif(const mbuf_t mbuf); 979 980/*! 981 @function mbuf_pkthdr_setrcvif 982 @discussion Sets the interface the packet was received on. 983 @param mbuf The mbuf containing the packet header. 984 @param ifnet A reference to an interface. 985 @result 0 upon success otherwise the errno error. 986 */ 987extern errno_t mbuf_pkthdr_setrcvif(mbuf_t mbuf, ifnet_t ifp); 988 989/*! 990 @function mbuf_pkthdr_header 991 @discussion Returns a pointer to the packet header. 992 @param mbuf The mbuf containing the packet header. 993 @result A pointer to the packet header. 994 */ 995extern void *mbuf_pkthdr_header(const mbuf_t mbuf); 996 997/*! 998 @function mbuf_pkthdr_setheader 999 @discussion Sets the pointer to the packet header. 1000 @param mbuf The mbuf containing the packet header. 1001 @param ifnet A pointer to the header. 1002 @result 0 upon success otherwise the errno error. 1003 */ 1004extern void mbuf_pkthdr_setheader(mbuf_t mbuf, void *header); 1005 1006/* Checksums */ 1007 1008/*! 1009 @function mbuf_inbound_modified 1010 @discussion This function will clear the checksum flags to indicate 1011 that a hardware checksum should not be used. Any filter 1012 modifying data should call this function on an mbuf before 1013 passing the packet up the stack. If a filter modifies a packet 1014 in a way that affects any checksum, the filter is responsible 1015 for either modifying the checksum to compensate for the changes 1016 or verifying the checksum before making the changes and then 1017 modifying the data and calculating a new checksum only if the 1018 original checksum was valid. 1019 @param mbuf The mbuf that has been modified. 1020 */ 1021extern void mbuf_inbound_modified(mbuf_t mbuf); 1022 1023/*! 1024 @function mbuf_outbound_finalize 1025 @discussion This function will "finalize" the packet allowing your 1026 code to inspect the final packet. 1027 1028 There are a number of operations that are performed in hardware, 1029 such as calculating checksums. This function will perform in 1030 software the various opterations that were scheduled to be done 1031 in hardware. Future operations may include IPSec processing or 1032 vlan support. If you are redirecting a packet to a new interface 1033 which may not have the same hardware support or encapsulating 1034 the packet, you should call this function to force the stack to 1035 calculate and fill out the checksums. This will bypass hardware 1036 checksums but give you a complete packet to work with. If you 1037 need to inspect aspects of the packet which may be generated by 1038 hardware, you must call this function to get an aproximate final 1039 packet. If you plan to modify the packet in any way, you should 1040 call this function. 1041 1042 This function should be called before modifying any outbound 1043 packets. 1044 1045 This function may be called at various levels, in some cases 1046 additional headers may have already been prepended, such as the 1047 case of a packet seen by an interface filter. To handle this, 1048 the caller must pass the protocol family of the packet as well 1049 as the offset from the start of the packet to the protocol 1050 header. 1051 @param mbuf The mbuf that should be finalized. 1052 @param protocol_family The protocol family of the packet in the 1053 mbuf. 1054 @param protocol_offset The offset from the start of the mbuf to the 1055 protocol header. For an IP packet with an ethernet header, this 1056 would be the length of an ethernet header. 1057 */ 1058extern void mbuf_outbound_finalize(mbuf_t mbuf, u_int32_t protocol_family, 1059 size_t protocol_offset); 1060 1061/*! 1062 @function mbuf_set_vlan_tag 1063 @discussion This function is used by interfaces that support vlan 1064 tagging in hardware. This function will set properties in the 1065 mbuf to indicate which vlan the packet was received for. 1066 @param mbuf The mbuf containing the packet. 1067 @param vlan The protocol family of the aux data to add. 1068 @result 0 upon success otherwise the errno error. 1069 */ 1070extern errno_t mbuf_set_vlan_tag(mbuf_t mbuf, u_int16_t vlan); 1071 1072/*! 1073 @function mbuf_get_vlan_tag 1074 @discussion This function is used by drivers that support hardware 1075 vlan tagging to determine which vlan this packet belongs to. To 1076 differentiate between the case where the vlan tag is zero and 1077 the case where there is no vlan tag, this function will return 1078 ENXIO when there is no vlan. 1079 @param mbuf The mbuf containing the packet. 1080 @param vlan The protocol family of the aux data to add. 1081 @result 0 upon success otherwise the errno error. ENXIO indicates 1082 that the vlan tag is not set. 1083 */ 1084extern errno_t mbuf_get_vlan_tag(mbuf_t mbuf, u_int16_t *vlan); 1085 1086/*! 1087 @function mbuf_clear_vlan_tag 1088 @discussion This function will clear any vlan tag associated with 1089 the mbuf. 1090 @param mbuf The mbuf containing the packet. 1091 @result 0 upon success otherwise the errno error. 1092 */ 1093extern errno_t mbuf_clear_vlan_tag(mbuf_t mbuf); 1094 1095#ifdef KERNEL_PRIVATE 1096/* 1097 @function mbuf_set_csum_requested 1098 @discussion This function is used by the stack to indicate which 1099 checksums should be calculated in hardware. The stack normally 1100 sets these flags as the packet is processed in the outbound 1101 direction. Just before send the packe to the interface, the 1102 stack will look at these flags and perform any checksums in 1103 software that are not supported by the interface. 1104 @param mbuf The mbuf containing the packet. 1105 @param request Flags indicating which checksums are being requested 1106 for this packet. 1107 @param value This parameter is currently unsupported. 1108 @result 0 upon success otherwise the errno error. 1109 */ 1110extern errno_t mbuf_set_csum_requested(mbuf_t mbuf, 1111 mbuf_csum_request_flags_t request, u_int32_t value); 1112#endif /* KERNEL_PRIVATE */ 1113 1114/*! 1115 @function mbuf_get_csum_requested 1116 @discussion This function is used by the driver to determine which 1117 checksum operations should be performed in hardware. 1118 @param mbuf The mbuf containing the packet. 1119 @param request Flags indicating which checksums are being requested 1120 for this packet. 1121 @param value This parameter is currently unsupported. 1122 @result 0 upon success otherwise the errno error. 1123 */ 1124extern errno_t mbuf_get_csum_requested(mbuf_t mbuf, 1125 mbuf_csum_request_flags_t *request, u_int32_t *value); 1126 1127/*! 1128 @function mbuf_get_tso_requested 1129 @discussion This function is used by the driver to determine which 1130 checksum operations should be performed in hardware. 1131 @param mbuf The mbuf containing the packet. 1132 @param request Flags indicating which values are being requested 1133 for this packet. 1134 @param value The requested value. 1135 @result 0 upon success otherwise the errno error. 1136 */ 1137extern errno_t mbuf_get_tso_requested(mbuf_t mbuf, 1138 mbuf_tso_request_flags_t *request, u_int32_t *value); 1139 1140/*! 1141 @function mbuf_clear_csum_requested 1142 @discussion This function clears the checksum request flags. 1143 @param mbuf The mbuf containing the packet. 1144 @result 0 upon success otherwise the errno error. 1145 */ 1146extern errno_t mbuf_clear_csum_requested(mbuf_t mbuf); 1147 1148/*! 1149 @function mbuf_set_csum_performed 1150 @discussion This is used by the driver to indicate to the stack which 1151 checksum operations were performed in hardware. 1152 @param mbuf The mbuf containing the packet. 1153 @param flags Flags indicating which hardware checksum operations 1154 were performed. 1155 @param value If the MBUF_CSUM_DID_DATA flag is set, value should be 1156 set to the value of the TCP or UDP header as calculated by the 1157 hardware. 1158 @result 0 upon success otherwise the errno error. 1159 */ 1160extern errno_t mbuf_set_csum_performed(mbuf_t mbuf, 1161 mbuf_csum_performed_flags_t flags, u_int32_t value); 1162 1163#ifdef KERNEL_PRIVATE 1164/* 1165 @function mbuf_get_csum_performed 1166 @discussion This is used by the stack to determine which checksums 1167 were calculated in hardware on the inbound path. 1168 @param mbuf The mbuf containing the packet. 1169 @param flags Flags indicating which hardware checksum operations 1170 were performed. 1171 @param value If the MBUF_CSUM_DID_DATA flag is set, value will be 1172 set to the value of the TCP or UDP header as calculated by the 1173 hardware. 1174 @result 0 upon success otherwise the errno error. 1175 */ 1176extern errno_t mbuf_get_csum_performed(mbuf_t mbuf, 1177 mbuf_csum_performed_flags_t *flags, u_int32_t *value); 1178#endif /* KERNEL_PRIVATE */ 1179 1180/*! 1181 @function mbuf_get_mlen 1182 @discussion This routine returns the number of data bytes in a normal 1183 mbuf, i.e. an mbuf that is not a packet header, nor one with 1184 an external cluster attached to it. This is equivalent to the 1185 legacy MLEN macro. 1186 @result The number of bytes of available data. 1187 */ 1188extern u_int32_t mbuf_get_mlen(void); 1189 1190/*! 1191 @function mbuf_get_mhlen 1192 @discussion This routine returns the number of data bytes in a packet 1193 header mbuf. This is equivalent to the legacy MHLEN macro. 1194 @result The number of bytes of available data. 1195 */ 1196extern u_int32_t mbuf_get_mhlen(void); 1197 1198/*! 1199 @function mbuf_get_minclsize 1200 @discussion This routine returns the minimum number of data bytes 1201 before an external cluster is used. This is equivalent to the 1202 legacy MINCLSIZE macro. 1203 @result The minimum number of bytes before a cluster will be used. 1204 */ 1205extern u_int32_t mbuf_get_minclsize(void); 1206 1207/*! 1208 @function mbuf_clear_csum_performed 1209 @discussion Clears the hardware checksum flags and values. 1210 @param mbuf The mbuf containing the packet. 1211 @result 0 upon success otherwise the errno error. 1212 */ 1213extern errno_t mbuf_clear_csum_performed(mbuf_t mbuf); 1214 1215/*! 1216 @function mbuf_inet_cksum 1217 @discussions Calculates 16-bit 1's complement Internet checksum of the 1218 transport segment with or without the pseudo header checksum 1219 of a given IPv4 packet. If the caller specifies a non-zero 1220 transport protocol, the checksum returned will also include 1221 the pseudo header checksum for the corresponding transport 1222 header. Otherwise, no header parsing will be done and the 1223 caller may use this to calculate the Internet checksum of 1224 an arbitrary span of data. 1225 1226 This routine does not modify the contents of the packet. If 1227 the caller specifies a non-zero protocol and/or offset, the 1228 routine expects the complete protocol header to be present 1229 at the beginning of the first mbuf. 1230 @param mbuf The mbuf (or chain of mbufs) containing the packet. 1231 @param protocol A zero or non-zero value. A non-zero value specifies 1232 the transport protocol used for pseudo header checksum. 1233 @param offset A zero or non-zero value; if the latter, it specifies 1234 the offset of the transport header from the beginning of mbuf. 1235 @param length The total (non-zero) length of the transport segment. 1236 @param csum Pointer to the checksum variable; upon success, this 1237 routine will return the calculated Internet checksum through 1238 this variable. The caller must set it to a non-NULL value. 1239 @result 0 upon success otherwise the errno error. 1240 */ 1241extern errno_t mbuf_inet_cksum(mbuf_t mbuf, int protocol, u_int32_t offset, 1242 u_int32_t length, u_int16_t *csum); 1243 1244/*! 1245 @function mbuf_inet6_cksum 1246 @discussions Calculates 16-bit 1's complement Internet checksum of the 1247 transport segment with or without the pseudo header checksum 1248 of a given IPv6 packet. If the caller specifies a non-zero 1249 transport protocol, the checksum returned will also include 1250 the pseudo header checksum for the corresponding transport 1251 header. Otherwise, no header parsing will be done and the 1252 caller may use this to calculate the Internet checksum of 1253 an arbitrary span of data. 1254 1255 This routine does not modify the contents of the packet. If 1256 the caller specifies a non-zero protocol and/or offset, the 1257 routine expects the complete protocol header(s) to be present 1258 at the beginning of the first mbuf. 1259 @param mbuf The mbuf (or chain of mbufs) containing the packet. 1260 @param protocol A zero or non-zero value. A non-zero value specifies 1261 the transport protocol used for pseudo header checksum. 1262 @param offset A zero or non-zero value; if the latter, it specifies 1263 the offset of the transport header from the beginning of mbuf. 1264 @param length The total (non-zero) length of the transport segment. 1265 @param csum Pointer to the checksum variable; upon success, this 1266 routine will return the calculated Internet checksum through 1267 this variable. The caller must set it to a non-NULL value. 1268 @result 0 upon success otherwise the errno error. 1269 */ 1270extern errno_t mbuf_inet6_cksum(mbuf_t mbuf, int protocol, u_int32_t offset, 1271 u_int32_t length, u_int16_t *csum); 1272 1273/* mbuf tags */ 1274 1275/*! 1276 @function mbuf_tag_id_find 1277 @discussion Lookup the module id for a string. If there is no module 1278 id assigned to this string, a new module id will be assigned. 1279 The string should be the bundle id of the kext. In the case of a 1280 tag that will be shared across multiple kexts, a common bundle 1281 id style string should be used. 1282 1283 The lookup operation is not optimized. A module should call this 1284 function once during startup and chache the module id. The 1285 module id will not be resassigned until the machine reboots. 1286 @param module_string A unique string identifying your module. 1287 Example: com.apple.nke.SharedIP. 1288 @param module_id Upon return, a unique identifier for use with 1289 mbuf_tag_* functions. This identifier is valid until the machine 1290 is rebooted. 1291 @result 0 upon success otherwise the errno error. 1292 */ 1293extern errno_t mbuf_tag_id_find(const char *module_string, 1294 mbuf_tag_id_t *module_id); 1295 1296/*! 1297 @function mbuf_tag_allocate 1298 @discussion Allocate an mbuf tag. Mbuf tags allow various portions 1299 of the stack to tag mbufs with data that will travel with the 1300 mbuf through the stack. 1301 1302 Tags may only be added to mbufs with packet headers 1303 (MBUF_PKTHDR flag is set). Mbuf tags are freed when the mbuf is 1304 freed or when mbuf_tag_free is called. 1305 @param mbuf The mbuf to attach this tag to. 1306 @param module_id A module identifier returned by mbuf_tag_id_find. 1307 @param type A 16 bit type value. For a given module_id, you can use 1308 a number of different tag types. 1309 @param length The length, in bytes, to allocate for storage that 1310 will be associated with this tag on this mbuf. 1311 @param how Indicate whether you want to block and wait for memory if 1312 memory is not immediately available. 1313 @param data_p Upon successful return, *data_p will point to the 1314 buffer allocated for the mtag. 1315 @result 0 upon success otherwise the errno error. 1316 */ 1317extern errno_t mbuf_tag_allocate(mbuf_t mbuf, mbuf_tag_id_t module_id, 1318 mbuf_tag_type_t type, size_t length, mbuf_how_t how, void **data_p); 1319 1320/*! 1321 @function mbuf_tag_find 1322 @discussion Find the data associated with an mbuf tag. 1323 @param mbuf The mbuf the tag is attached to. 1324 @param module_id A module identifier returned by mbuf_tag_id_find. 1325 @param type The 16 bit type of the tag to find. 1326 @param length Upon success, the length of data will be store in 1327 *length. 1328 @param data_p Upon successful return, *data_p will point to the 1329 buffer allocated for the mtag. 1330 @result 0 upon success otherwise the errno error. 1331 */ 1332extern errno_t mbuf_tag_find(mbuf_t mbuf, mbuf_tag_id_t module_id, 1333 mbuf_tag_type_t type, size_t *length, void **data_p); 1334 1335/*! 1336 @function mbuf_tag_free 1337 @discussion Frees a previously allocated mbuf tag. 1338 @param mbuf The mbuf the tag was allocated on. 1339 @param module_id The ID of the tag to free. 1340 @param type The type of the tag to free. 1341 */ 1342extern void mbuf_tag_free(mbuf_t mbuf, mbuf_tag_id_t module_id, 1343 mbuf_tag_type_t type); 1344 1345#ifdef KERNEL_PRIVATE 1346/* 1347 @function mbuf_add_drvaux 1348 @discussion Allocate space for driver auxiliary data and attach it 1349 to the packet (MBUF_PKTHDR is required.) This space is freed 1350 when the mbuf is freed or when mbuf_del_drvaux is called. 1351 Only one instance of driver auxiliary data may be attached to 1352 a packet. Any attempt to add it to a packet already associated 1353 with one will yield an error, and the existing one must first 1354 be removed via mbuf_del_drvaux. The format and length of the 1355 data depend largely on the family and sub-family. The system 1356 makes no attempt to define and/or interpret the contents of 1357 the data, and simply acts as a conduit between its producer 1358 and consumer. 1359 @param mbuf The mbuf to attach the auxiliary data to. 1360 @param how Indicate whether you are willing to block and wait for 1361 memory, if memory is not immediately available. 1362 @param family The interface family as defined in net/kpi_interface.h. 1363 @param subfamily The interface sub-family as defined in 1364 net/kpi_interface.h. 1365 @param length The length of the auxiliary data, must be greater than 0. 1366 @param data_p Upon successful return, *data_p will point to the 1367 space allocated for the data. Caller may set this to NULL. 1368 @result 0 upon success otherwise the errno error. 1369 */ 1370extern errno_t mbuf_add_drvaux(mbuf_t mbuf, mbuf_how_t how, 1371 u_int32_t family, u_int32_t subfamily, size_t length, void **data_p); 1372 1373/* 1374 @function mbuf_find_drvaux 1375 @discussion Find the driver auxiliary data associated with a packet. 1376 @param mbuf The mbuf the auxiliary data is attached to. 1377 @param family_p Upon successful return, *family_p will contain 1378 the interface family associated with the data, as defined 1379 in net/kpi_interface.h. Caller may set this to NULL. 1380 @param subfamily_p Upon successful return, *subfamily_p will contain 1381 the interface family associated with the data, as defined 1382 in net/kpi_interface.h. Caller may set this to NULL. 1383 @param length_p Upon successful return, *length_p will contain 1384 the length of the driver auxiliary data. Caller may 1385 set this to NULL. 1386 @param data_p Upon successful return, *data_p will point to the 1387 space allocated for the data. 1388 @result 0 upon success otherwise the errno error. 1389 */ 1390extern errno_t mbuf_find_drvaux(mbuf_t mbuf, u_int32_t *family_p, 1391 u_int32_t *subfamily_p, u_int32_t *length_p, void **data_p); 1392 1393/* 1394 @function mbuf_del_drvaux 1395 @discussion Remove and free any driver auxility data associated 1396 with the packet. 1397 @param mbuf The mbuf the auxiliary data is attached to. 1398 */ 1399extern void mbuf_del_drvaux(mbuf_t mbuf); 1400#endif /* KERNEL_PRIVATE */ 1401 1402/* mbuf stats */ 1403 1404/*! 1405 @function mbuf_stats 1406 @discussion Get the mbuf statistics. 1407 @param stats Storage to copy the stats in to. 1408 */ 1409extern void mbuf_stats(struct mbuf_stat *stats); 1410 1411 1412/*! 1413 @enum mbuf_traffic_class_t 1414 @abstract Traffic class of a packet 1415 @discussion Property that represent the category of traffic of a packet. 1416 This information may be used by the driver and at the link level. 1417 @constant MBUF_TC_BE Best effort, normal class. 1418 @constant MBUF_TC_BK Background, low priority or bulk traffic. 1419 @constant MBUF_TC_VI Interactive video, constant bit rate, low latency. 1420 @constant MBUF_TC_VO Interactive voice, constant bit rate, lowest latency. 1421*/ 1422typedef enum { 1423#ifdef XNU_KERNEL_PRIVATE 1424 MBUF_TC_UNSPEC = -1, /* Internal: not specified */ 1425#endif 1426 MBUF_TC_BE = 0, 1427 MBUF_TC_BK = 1, 1428 MBUF_TC_VI = 2, 1429 MBUF_TC_VO = 3 1430#ifdef XNU_KERNEL_PRIVATE 1431 , 1432 MBUF_TC_MAX = 4 /* Internal: traffic class count */ 1433#endif 1434} mbuf_traffic_class_t; 1435 1436/*! 1437 @function mbuf_get_traffic_class 1438 @discussion Get the traffic class of an mbuf packet 1439 @param mbuf The mbuf to get the traffic class of. 1440 @result The traffic class 1441*/ 1442extern mbuf_traffic_class_t mbuf_get_traffic_class(mbuf_t mbuf); 1443 1444/*! 1445 @function mbuf_set_traffic_class 1446 @discussion Set the traffic class of an mbuf packet. 1447 @param mbuf The mbuf to set the traffic class on. 1448 @tc The traffic class 1449 @result 0 on success, EINVAL if bad parameter is passed 1450*/ 1451extern errno_t mbuf_set_traffic_class(mbuf_t mbuf, mbuf_traffic_class_t tc); 1452 1453/*! 1454 @function mbuf_is_traffic_class_privileged 1455 @discussion Returns the privileged status of the traffic class 1456 of the packet specified by the mbuf. 1457 @param mbuf The mbuf to retrieve the status from. 1458 @result Non-zero if privileged, 0 otherwise. 1459 */ 1460extern int mbuf_is_traffic_class_privileged(mbuf_t mbuf); 1461 1462#ifdef KERNEL_PRIVATE 1463 1464/*! 1465 @function mbuf_get_traffic_class_max_count 1466 @discussion Returns the maximum number of mbuf traffic class types 1467 @result The total count of mbuf traffic classes 1468 */ 1469extern u_int32_t mbuf_get_traffic_class_max_count(void); 1470 1471/*! 1472 @function mbuf_get_traffic_class_index 1473 @discussion Returns the zero-based index of an mbuf traffic class value 1474 @param tc The traffic class 1475 @param index Pointer to the index value 1476 @result 0 on success, EINVAL if bad parameter is passed 1477 */ 1478extern errno_t mbuf_get_traffic_class_index(mbuf_traffic_class_t tc, 1479 u_int32_t *index); 1480 1481/*! 1482 @enum mbuf_svc_class_t 1483 @abstract Service class of a packet 1484 @discussion Property that represents the category of service 1485 of a packet. This information may be used by the driver 1486 and at the link level. 1487 @constant MBUF_SC_BK_SYS "Background System-Initiated", high delay 1488 tolerant, high loss tolerant, elastic flow, variable size & 1489 long-lived. 1490 @constant MBUF_SC_BK "Background", user-initiated, high delay tolerant, 1491 high loss tolerant, elastic flow, variable size. This level 1492 corresponds to WMM access class "BG", or MBUF_TC_BK. 1493 @constant MBUF_SC_BE "Best Effort", unclassified/standard. This is 1494 the default service class; pretty much a mix of everything. 1495 This level corresponds to WMM access class "BE" or MBUF_TC_BE. 1496 @constant MBUF_SC_RD 1497 "Responsive Data", a notch higher than "Best Effort", medium 1498 delay tolerant, medium loss tolerant, elastic flow, bursty, 1499 long-lived. 1500 @constant MBUF_SC_OAM "Operations, Administration, and Management", 1501 medium delay tolerant, low-medium loss tolerant, elastic & 1502 inelastic flows, variable size. 1503 @constant MBUF_SC_AV "Multimedia Audio/Video Streaming", medium delay 1504 tolerant, low-medium loss tolerant, elastic flow, constant 1505 packet interval, variable rate & size. 1506 @constant MBUF_SC_RV "Responsive Multimedia Audio/Video", low delay 1507 tolerant, low-medium loss tolerant, elastic flow, variable 1508 packet interval, rate and size. 1509 @constant MBUF_SC_VI "Interactive Video", low delay tolerant, low- 1510 medium loss tolerant, elastic flow, constant packet interval, 1511 variable rate & size. This level corresponds to WMM access 1512 class "VI" or MBUF_TC_VI. 1513 @constant MBUF_SC_VO "Interactive Voice", low delay tolerant, low loss 1514 tolerant, inelastic flow, constant packet rate, somewhat fixed 1515 size. This level corresponds to WMM access class "VO" or 1516 MBUF_TC_VO. 1517 @constant MBUF_SC_CTL "Network Control", low delay tolerant, low loss 1518 tolerant, inelastic flow, rate is short & burst, variable size. 1519*/ 1520typedef enum { 1521#ifdef XNU_KERNEL_PRIVATE 1522 MBUF_SC_UNSPEC = -1, /* Internal: not specified */ 1523#endif 1524 MBUF_SC_BK_SYS = 0x00080090, /* lowest class */ 1525 MBUF_SC_BK = 0x00100080, 1526 1527 MBUF_SC_BE = 0x00000000, 1528 MBUF_SC_RD = 0x00180010, 1529 MBUF_SC_OAM = 0x00200020, 1530 1531 MBUF_SC_AV = 0x00280120, 1532 MBUF_SC_RV = 0x00300110, 1533 MBUF_SC_VI = 0x00380100, 1534 1535 MBUF_SC_VO = 0x00400180, 1536 MBUF_SC_CTL = 0x00480190, /* highest class */ 1537} mbuf_svc_class_t; 1538 1539/*! 1540 @function mbuf_get_service_class_max_count 1541 @discussion Returns the maximum number of mbuf service class types. 1542 @result The total count of mbuf service classes. 1543 */ 1544extern u_int32_t mbuf_get_service_class_max_count(void); 1545 1546/*! 1547 @function mbuf_get_service_class_index 1548 @discussion Returns the zero-based index of an mbuf service class value 1549 @param sc The service class 1550 @param index Pointer to the index value 1551 @result 0 on success, EINVAL if bad parameter is passed 1552 */ 1553extern errno_t mbuf_get_service_class_index(mbuf_svc_class_t sc, 1554 u_int32_t *index); 1555 1556/*! 1557 @function mbuf_get_service_class 1558 @discussion Get the service class of an mbuf packet 1559 @param mbuf The mbuf to get the service class of. 1560 @result The service class 1561*/ 1562extern mbuf_svc_class_t mbuf_get_service_class(mbuf_t mbuf); 1563 1564/*! 1565 @function mbuf_set_servicec_class 1566 @discussion Set the service class of an mbuf packet. 1567 @param mbuf The mbuf to set the service class on. 1568 @sc The service class 1569 @result 0 on success, EINVAL if bad parameter is passed 1570*/ 1571extern errno_t mbuf_set_service_class(mbuf_t mbuf, mbuf_svc_class_t sc); 1572 1573/*! 1574 @function mbuf_is_service_class_privileged 1575 @discussion Returns the privileged status of the service class 1576 of the packet specified by the mbuf. 1577 @param mbuf The mbuf to retrieve the status from. 1578 @result Non-zero if privileged, 0 otherwise. 1579 */ 1580extern int mbuf_is_service_class_privileged(mbuf_t mbuf); 1581 1582/* 1583 @enum mbuf_pkthdr_aux_flags_t 1584 @abstract Constants defining mbuf auxiliary flags. Only the flags 1585 listed below can be retrieved. 1586 @constant MBUF_PKTAUXF_INET_RESOLVE_RTR Indicates this is an ARP 1587 request packet, whose target is the address of the default 1588 IPv4 router. 1589 @constant MBUF_PKTAUXF_INET6_RESOLVE_RTR Indicates this is an ICMPv6 1590 Neighbor Solicitation packet, whose target is the address of 1591 the default IPv6 router. 1592 */ 1593enum { 1594 MBUF_PKTAUXF_INET_RESOLVE_RTR = 0x0004, 1595 MBUF_PKTAUXF_INET6_RESOLVE_RTR = 0x0008, 1596}; 1597typedef u_int32_t mbuf_pkthdr_aux_flags_t; 1598 1599/* 1600 @function mbuf_pkthdr_aux_flags 1601 @discussion Returns the auxiliary flags of a packet. 1602 @param mbuf The mbuf containing the packet header. 1603 @param paux_flags Pointer to mbuf_pkthdr_aux_flags_t variable. 1604 @result 0 upon success otherwise the errno error. 1605*/ 1606extern errno_t mbuf_pkthdr_aux_flags(mbuf_t mbuf, 1607 mbuf_pkthdr_aux_flags_t *paux_flags); 1608 1609/* 1610 @function mbuf_get_driver_scratch 1611 @discussion Returns a pointer to a driver specific area in the mbuf 1612 @param m The mbuf whose driver scratch space is to be returned 1613 @param area A pointer to a location to store the address of the 1614 driver scratch space. This value is guaranteed to be 32-bit 1615 aligned. 1616 @param area_ln A pointer to a location to store the total length of 1617 the memory location. 1618*/ 1619extern errno_t mbuf_get_driver_scratch(mbuf_t m, u_int8_t **area, 1620 size_t *area_ln); 1621#endif /* KERNEL_PRIVATE */ 1622 1623/* IF_QUEUE interaction */ 1624 1625#define IF_ENQUEUE_MBUF(ifq, m) { \ 1626 mbuf_setnextpkt((m), 0); \ 1627 if ((ifq)->ifq_tail == 0) \ 1628 (ifq)->ifq_head = (m); \ 1629 else \ 1630 mbuf_setnextpkt((mbuf_t)(ifq)->ifq_tail, (m)); \ 1631 (ifq)->ifq_tail = (m); \ 1632 (ifq)->ifq_len++; \ 1633} 1634 1635#define IF_PREPEND_MBUF(ifq, m) { \ 1636 mbuf_setnextpkt((m), (ifq)->ifq_head); \ 1637 if ((ifq)->ifq_tail == 0) \ 1638 (ifq)->ifq_tail = (m); \ 1639 (ifq)->ifq_head = (m); \ 1640 (ifq)->ifq_len++; \ 1641} 1642 1643#define IF_DEQUEUE_MBUF(ifq, m) { \ 1644 (m) = (ifq)->ifq_head; \ 1645 if (m) { \ 1646 if (((ifq)->ifq_head = mbuf_nextpkt((m))) == 0) \ 1647 (ifq)->ifq_tail = 0; \ 1648 mbuf_setnextpkt((m), 0); \ 1649 (ifq)->ifq_len--; \ 1650 } \ 1651} 1652 1653__END_DECLS 1654#endif /* __KPI_MBUF__ */ 1655