1 /* 2 * Copyright (c) 2004 The DragonFly Project. All rights reserved. 3 * 4 * Copyright (c) 1982, 1986, 1988, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)mbuf.h 8.5 (Berkeley) 2/19/95 36 * $FreeBSD: src/sys/sys/mbuf.h,v 1.44.2.17 2003/04/15 06:15:02 silby Exp $ 37 * $DragonFly: src/sys/sys/mbuf.h,v 1.54 2008/10/19 08:39:55 sephe Exp $ 38 */ 39 40 #ifndef _SYS_MBUF_H_ 41 #define _SYS_MBUF_H_ 42 43 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES) 44 45 #ifndef _SYS_TYPES_H_ 46 #include <sys/types.h> 47 #endif 48 #ifndef _SYS_PARAM_H_ 49 #include <sys/param.h> 50 #endif 51 #ifndef _SYS_QUEUE_H_ 52 #include <sys/queue.h> 53 #endif 54 #ifndef _NET_NETISR_H_ 55 #include <net/netisr.h> 56 #endif 57 58 /* 59 * Mbufs are of a single size, MSIZE (machine/param.h), which 60 * includes overhead. An mbuf may add a single "mbuf cluster" of size 61 * MCLBYTES (also in machine/param.h), which has no additional overhead 62 * and is used instead of the internal data area; this is done when 63 * at least MINCLSIZE of data must be stored. 64 */ 65 #define MLEN (MSIZE - sizeof(struct m_hdr)) /* normal data len */ 66 #define MHLEN (MLEN - sizeof(struct pkthdr)) /* data len w/pkthdr */ 67 #define MINCLSIZE (MHLEN + 1) /* smallest amount to put in cluster */ 68 #define M_MAXCOMPRESS (MHLEN / 2) /* max amount to copy for compression */ 69 70 /* 71 * Macros for type conversion: 72 * mtod(m, t) -- Convert mbuf pointer to data pointer of correct type. 73 * mtocl(x) - convert pointer within cluster to cluster index # 74 * cltom(x) - convert cluster # to ptr to beginning of cluster 75 */ 76 #define mtod(m, t) ((t)((m)->m_data)) 77 #define mtodoff(m, t, off) ((t)((m)->m_data + (off))) 78 79 /* 80 * Header present at the beginning of every mbuf. 81 */ 82 struct m_hdr { 83 struct mbuf *mh_next; /* next buffer in chain */ 84 struct mbuf *mh_nextpkt; /* next chain in queue/record */ 85 caddr_t mh_data; /* location of data */ 86 int mh_len; /* amount of data in this mbuf */ 87 int mh_flags; /* flags; see below */ 88 short mh_type; /* type of data in this mbuf */ 89 short mh_pad; /* padding */ 90 #ifdef MBUF_DEBUG 91 const char *mh_lastfunc; 92 #endif 93 struct netmsg_packet mh_netmsg; /* hardware->proto stack msg */ 94 }; 95 96 /* pf stuff */ 97 struct pkthdr_pf { 98 void *hdr; /* saved hdr pos in mbuf, for ECN */ 99 u_int rtableid; /* alternate routing table id */ 100 u_int32_t qid; /* queue id */ 101 u_int16_t tag; /* tag id */ 102 u_int8_t flags; 103 u_int8_t routed; 104 uint32_t state_hash; /* identifies 'connections' */ 105 uint8_t ecn_af; /* for altq_red */ 106 uint8_t unused01; 107 uint8_t unused02; 108 uint8_t unused03; 109 }; 110 111 /* 112 * Packet tag structure (see below for details). 113 */ 114 struct m_tag { 115 SLIST_ENTRY(m_tag) m_tag_link; /* List of packet tags */ 116 u_int16_t m_tag_id; /* Tag ID */ 117 u_int16_t m_tag_len; /* Length of data */ 118 u_int32_t m_tag_cookie; /* ABI/Module ID */ 119 }; 120 121 SLIST_HEAD(packet_tags, m_tag); 122 123 /* 124 * Record/packet header in first mbuf of chain; valid only if M_PKTHDR is set. 125 * 126 * Be careful: The fields have been carefully ordered to avoid hidden padding. 127 * Keep this in mind, when adding or removing fields! 128 */ 129 struct pkthdr { 130 struct ifnet *rcvif; /* rcv interface */ 131 int len; /* total packet length */ 132 struct packet_tags tags; /* list of packet tags */ 133 134 /* variables for ip and tcp reassembly */ 135 void *header; /* pointer to packet header */ 136 137 /* variables for hardware checksum */ 138 int csum_flags; /* flags regarding checksum */ 139 int csum_data; /* data field used by csum routines */ 140 141 /* firewall flags */ 142 uint32_t fw_flags; /* flags for PF */ 143 144 /* variables for PF processing */ 145 struct pkthdr_pf pf; /* structure for PF */ 146 147 uint16_t ether_vlantag; /* ethernet 802.1p+q vlan tag */ 148 uint16_t hash; /* packet hash */ 149 150 uint16_t wlan_seqno; /* IEEE 802.11 seq no. */ 151 }; 152 153 /* 154 * Description of external storage mapped into mbuf; valid only if M_EXT is set. 155 */ 156 struct m_ext { 157 caddr_t ext_buf; /* start of buffer */ 158 void (*ext_free)(void *); 159 u_int ext_size; /* size of buffer, for ext_free */ 160 void (*ext_ref)(void *); 161 void *ext_arg; 162 }; 163 164 /* 165 * The core of the mbuf object along with some shortcut defines for 166 * practical purposes. 167 */ 168 struct mbuf { 169 struct m_hdr m_hdr; 170 union { 171 struct { 172 struct pkthdr MH_pkthdr; /* M_PKTHDR set */ 173 union { 174 struct m_ext MH_ext; /* M_EXT set */ 175 char MH_databuf[MHLEN]; 176 } MH_dat; 177 } MH; 178 char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */ 179 } M_dat; 180 }; 181 #define m_next m_hdr.mh_next 182 #define m_len m_hdr.mh_len 183 #define m_data m_hdr.mh_data 184 #define m_type m_hdr.mh_type 185 #define m_flags m_hdr.mh_flags 186 #define m_nextpkt m_hdr.mh_nextpkt 187 #define m_pkthdr M_dat.MH.MH_pkthdr 188 #define m_ext M_dat.MH.MH_dat.MH_ext 189 #define m_pktdat M_dat.MH.MH_dat.MH_databuf 190 #define m_dat M_dat.M_databuf 191 192 /* 193 * Code that uses m_act should be converted to use m_nextpkt 194 * instead; m_act is historical and deprecated. 195 */ 196 #define m_act m_nextpkt 197 198 /* 199 * mbuf flags. 200 */ 201 #define M_EXT 0x0001 /* has associated external storage */ 202 #define M_PKTHDR 0x0002 /* start of record */ 203 #define M_EOR 0x0004 /* end of record */ 204 #define M_PROTO1 0x0008 /* protocol-specific */ 205 #define M_PROTO2 0x0010 /* protocol-specific */ 206 #define M_PROTO3 0x0020 /* protocol-specific */ 207 #define M_PROTO4 0x0040 /* protocol-specific */ 208 #define M_PROTO5 0x0080 /* protocol-specific */ 209 210 /* 211 * mbuf pkthdr flags (also stored in m_flags). 212 */ 213 #define M_BCAST 0x0100 /* send/received as link-level broadcast */ 214 #define M_MCAST 0x0200 /* send/received as link-level multicast */ 215 #define M_FRAG 0x0400 /* packet is a fragment of a larger packet */ 216 #define M_FIRSTFRAG 0x0800 /* packet is first fragment */ 217 #define M_LASTFRAG 0x1000 /* packet is last fragment */ 218 #define M_CLCACHE 0x2000 /* mbuf allocated from the cluster cache */ 219 #define M_EXT_CLUSTER 0x4000 /* standard cluster else special */ 220 #define M_PHCACHE 0x8000 /* mbuf allocated from the pkt header cache */ 221 #define M_NOTIFICATION 0x10000 /* notification event */ 222 #define M_VLANTAG 0x20000 /* ether_vlantag is valid */ 223 #define M_MPLSLABELED 0x40000 /* packet is mpls labeled */ 224 #define M_LENCHECKED 0x80000 /* packet proto lengths are checked */ 225 #define M_HASH 0x100000/* hash field in pkthdr is valid */ 226 #define M_PROTO6 0x200000/* protocol-specific */ 227 #define M_PROTO7 0x400000/* protocol-specific */ 228 #define M_PROTO8 0x800000/* protocol-specific */ 229 230 /* 231 * Flags copied when copying m_pkthdr. 232 */ 233 #define M_COPYFLAGS (M_PKTHDR|M_EOR|M_PROTO1|M_PROTO2|M_PROTO3 | \ 234 M_PROTO4|M_PROTO5|M_PROTO6|M_PROTO7|M_PROTO8 | \ 235 M_BCAST|M_MCAST|M_FRAG|M_FIRSTFRAG|M_LASTFRAG | \ 236 M_VLANTAG|M_MPLSLABELED | \ 237 M_LENCHECKED|M_HASH) 238 239 /* 240 * Flags indicating hw checksum support and sw checksum requirements. 241 */ 242 #define CSUM_IP 0x0001 /* will csum IP */ 243 #define CSUM_TCP 0x0002 /* will csum TCP */ 244 #define CSUM_UDP 0x0004 /* will csum UDP */ 245 #define CSUM_IP_FRAGS 0x0008 /* will csum IP fragments */ 246 #define CSUM_FRAGMENT 0x0010 /* will do IP fragmentation */ 247 248 #define CSUM_IP_CHECKED 0x0100 /* did csum IP */ 249 #define CSUM_IP_VALID 0x0200 /* ... the csum is valid */ 250 #define CSUM_DATA_VALID 0x0400 /* csum_data field is valid */ 251 #define CSUM_PSEUDO_HDR 0x0800 /* csum_data has pseudo hdr */ 252 #define CSUM_FRAG_NOT_CHECKED 0x1000 /* did _not_ csum fragment 253 * NB: This flag is only used 254 * by IP defragmenter. 255 */ 256 257 #define CSUM_DELAY_DATA (CSUM_TCP | CSUM_UDP) 258 #define CSUM_DELAY_IP (CSUM_IP) /* XXX add ipv6 here too? */ 259 260 /* 261 * Flags indicating PF processing status 262 */ 263 #define FW_MBUF_GENERATED 0x00000001 264 #define PF_MBUF_STRUCTURE 0x00000002 /* m_pkthdr.pf valid */ 265 #define PF_MBUF_ROUTED 0x00000004 /* pf_routed field is valid */ 266 #define PF_MBUF_TAGGED 0x00000008 267 #define XX_MBUF_UNUSED10 0x00000010 268 #define XX_MBUF_UNUSED20 0x00000020 269 #define IPFORWARD_MBUF_TAGGED 0x00000040 270 #define DUMMYNET_MBUF_TAGGED 0x00000080 271 #define XX_MBUF_UNUSED100 0x00000100 272 #define FW_MBUF_REDISPATCH 0x00000200 273 #define IPFW_MBUF_GENERATED FW_MBUF_GENERATED 274 /* 275 * mbuf types. 276 */ 277 #define MT_FREE 0 /* should be on free list */ 278 #define MT_DATA 1 /* dynamic (data) allocation */ 279 #define MT_HEADER 2 /* packet header */ 280 #define MT_SONAME 3 /* socket name */ 281 /* 4 was MT_TAG */ 282 #define MT_CONTROL 5 /* extra-data protocol message */ 283 #define MT_OOBDATA 6 /* expedited data */ 284 #define MT_NTYPES 7 /* number of mbuf types for mbtypes[] */ 285 286 struct mbuf_chain { 287 struct mbuf *mc_head; 288 struct mbuf *mc_tail; 289 }; 290 291 /* 292 * General mbuf allocator statistics structure. 293 */ 294 struct mbstat { 295 u_long m_mbufs; /* mbufs obtained from page pool */ 296 u_long m_clusters; /* clusters obtained from page pool */ 297 u_long m_spare; /* spare field */ 298 u_long m_clfree; /* free clusters */ 299 u_long m_drops; /* times failed to find space */ 300 u_long m_wait; /* times waited for space */ 301 u_long m_drain; /* times drained protocols for space */ 302 u_long m_mcfail; /* times m_copym failed */ 303 u_long m_mpfail; /* times m_pullup failed */ 304 u_long m_msize; /* length of an mbuf */ 305 u_long m_mclbytes; /* length of an mbuf cluster */ 306 u_long m_minclsize; /* min length of data to allocate a cluster */ 307 u_long m_mlen; /* length of data in an mbuf */ 308 u_long m_mhlen; /* length of data in a header mbuf */ 309 }; 310 311 /* 312 * Flags specifying how an allocation should be made. 313 */ 314 315 #define MB_DONTWAIT 0x4 316 #define MB_TRYWAIT 0x8 317 #define MB_WAIT MB_TRYWAIT 318 319 /* 320 * Mbuf to Malloc Flag Conversion. 321 */ 322 #define MBTOM(how) ((how) & MB_TRYWAIT ? M_WAITOK : M_NOWAIT) 323 324 /* 325 * These are identifying numbers passed to the m_mballoc_wait function, 326 * allowing us to determine whether the call came from an MGETHDR or 327 * an MGET. 328 */ 329 #define MGETHDR_C 1 330 #define MGET_C 2 331 332 /* 333 * mbuf allocation/deallocation macros (YYY deprecated, too big): 334 * 335 * MGET(struct mbuf *m, int how, int type) 336 * allocates an mbuf and initializes it to contain internal data. 337 * 338 * MGETHDR(struct mbuf *m, int how, int type) 339 * allocates an mbuf and initializes it to contain a packet header 340 * and internal data. 341 */ 342 #define MGET(m, how, type) do { \ 343 (m) = m_get((how), (type)); \ 344 } while (0) 345 346 #define MGETHDR(m, how, type) do { \ 347 (m) = m_gethdr((how), (type)); \ 348 } while (0) 349 350 /* 351 * MCLGET adds such clusters to a normal mbuf. The flag M_EXT is set upon 352 * success. 353 * Deprecated. Use m_getcl() or m_getl() instead. 354 */ 355 #define MCLGET(m, how) do { \ 356 m_mclget((m), (how)); \ 357 } while (0) 358 359 /* 360 * NB: M_COPY_PKTHDR is deprecated; use either M_MOVE_PKTHDR 361 * or m_dup_pkthdr. 362 */ 363 /* 364 * Move mbuf pkthdr from "from" to "to". 365 * from should have M_PKTHDR set, and to must be empty. 366 * from no longer has a pkthdr after this operation. 367 */ 368 #define M_MOVE_PKTHDR(_to, _from) m_move_pkthdr((_to), (_from)) 369 370 /* 371 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place 372 * an object of the specified size at the end of the mbuf, longword aligned. 373 */ 374 #define M_ALIGN(m, len) do { \ 375 (m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1); \ 376 } while (0) 377 378 /* 379 * As above, for mbufs allocated with m_gethdr/MGETHDR 380 * or initialized by M_COPY_PKTHDR. 381 */ 382 #define MH_ALIGN(m, len) do { \ 383 (m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1); \ 384 } while (0) 385 386 /* 387 * Check if we can write to an mbuf. 388 */ 389 #define M_EXT_WRITABLE(m) (m_sharecount(m) == 1) 390 #define M_WRITABLE(m) (!((m)->m_flags & M_EXT) || M_EXT_WRITABLE(m)) 391 392 /* 393 * Check if the supplied mbuf has a packet header, or else panic. 394 */ 395 #define M_ASSERTPKTHDR(m) \ 396 KASSERT(m != NULL && m->m_flags & M_PKTHDR, \ 397 ("%s: invalid mbuf or no mbuf packet header!", __func__)) 398 399 /* 400 * Compute the amount of space available before the current start of data. 401 * The M_EXT_WRITABLE() is a temporary, conservative safety measure: the burden 402 * of checking writability of the mbuf data area rests solely with the caller. 403 */ 404 #define M_LEADINGSPACE(m) \ 405 ((m)->m_flags & M_EXT ? \ 406 (M_EXT_WRITABLE(m) ? (m)->m_data - (m)->m_ext.ext_buf : 0): \ 407 (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \ 408 (m)->m_data - (m)->m_dat) 409 410 /* 411 * Compute the amount of space available after the end of data in an mbuf. 412 * The M_WRITABLE() is a temporary, conservative safety measure: the burden 413 * of checking writability of the mbuf data area rests solely with the caller. 414 */ 415 #define M_TRAILINGSPACE(m) \ 416 ((m)->m_flags & M_EXT ? \ 417 (M_WRITABLE(m) ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size \ 418 - ((m)->m_data + (m)->m_len) : 0) : \ 419 &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len)) 420 421 /* 422 * Arrange to prepend space of size plen to mbuf m. 423 * If a new mbuf must be allocated, how specifies whether to wait. 424 * If how is MB_DONTWAIT and allocation fails, the original mbuf chain 425 * is freed and m is set to NULL. 426 */ 427 #define M_PREPEND(m, plen, how) do { \ 428 struct mbuf **_mmp = &(m); \ 429 struct mbuf *_mm = *_mmp; \ 430 int _mplen = (plen); \ 431 int __mhow = (how); \ 432 \ 433 if (M_LEADINGSPACE(_mm) >= _mplen) { \ 434 _mm->m_data -= _mplen; \ 435 _mm->m_len += _mplen; \ 436 } else \ 437 _mm = m_prepend(_mm, _mplen, __mhow); \ 438 if (_mm != NULL && (_mm->m_flags & M_PKTHDR)) \ 439 _mm->m_pkthdr.len += _mplen; \ 440 *_mmp = _mm; \ 441 } while (0) 442 443 /* Length to m_copy to copy all. */ 444 #define M_COPYALL 1000000000 445 446 /* Compatibility with 4.3 */ 447 #define m_copy(m, o, l) m_copym((m), (o), (l), MB_DONTWAIT) 448 449 #ifdef _KERNEL 450 extern u_int m_clalloc_wid; /* mbuf cluster wait count */ 451 extern u_int m_mballoc_wid; /* mbuf wait count */ 452 extern int max_linkhdr; /* largest link-level header */ 453 extern int max_protohdr; /* largest protocol header */ 454 extern int max_hdr; /* largest link+protocol header */ 455 extern int max_datalen; /* MHLEN - max_hdr */ 456 extern int mbuf_wait; /* mbuf sleep time */ 457 extern int nmbclusters; 458 extern int nmbufs; 459 460 struct uio; 461 462 void m_adj(struct mbuf *, int); 463 void m_align(struct mbuf *, int); 464 int m_apply(struct mbuf *, int, int, 465 int (*)(void *, void *, u_int), void *); 466 int m_append(struct mbuf *, int, c_caddr_t); 467 void m_cat(struct mbuf *, struct mbuf *); 468 u_int m_countm(struct mbuf *m, struct mbuf **lastm, u_int *mbcnt); 469 void m_copyback(struct mbuf *, int, int, caddr_t); 470 void m_copydata(const struct mbuf *, int, int, caddr_t); 471 struct mbuf *m_copym(const struct mbuf *, int, int, int); 472 struct mbuf *m_copypacket(struct mbuf *, int); 473 struct mbuf *m_defrag(struct mbuf *, int); 474 struct mbuf *m_defrag_nofree(struct mbuf *, int); 475 struct mbuf *m_devget(char *, int, int, struct ifnet *, 476 void (*copy)(volatile const void *, volatile void *, size_t)); 477 struct mbuf *m_dup(struct mbuf *, int); 478 struct mbuf *m_dup_data(struct mbuf *, int); 479 int m_dup_pkthdr(struct mbuf *, const struct mbuf *, int); 480 #ifdef MBUF_DEBUG 481 struct mbuf *_m_free(struct mbuf *, const char *name); 482 void _m_freem(struct mbuf *, const char *name); 483 #else 484 struct mbuf *m_free(struct mbuf *); 485 void m_freem(struct mbuf *); 486 #endif 487 struct mbuf *m_get(int, int); 488 struct mbuf *m_getc(int len, int how, int type); 489 struct mbuf *m_getcl(int how, short type, int flags); 490 struct mbuf *m_getclr(int, int); 491 struct mbuf *m_gethdr(int, int); 492 struct mbuf *m_getm(struct mbuf *, int, int, int); 493 struct mbuf *m_getptr(struct mbuf *, int, int *); 494 struct mbuf *m_last(struct mbuf *m); 495 u_int m_lengthm(struct mbuf *m, struct mbuf **lastm); 496 void m_move_pkthdr(struct mbuf *, struct mbuf *); 497 struct mbuf *m_prepend(struct mbuf *, int, int); 498 void m_print(const struct mbuf *m); 499 struct mbuf *m_pulldown(struct mbuf *, int, int, int *); 500 struct mbuf *m_pullup(struct mbuf *, int); 501 struct mbuf *m_split(struct mbuf *, int, int); 502 struct mbuf *m_uiomove(struct uio *); 503 void m_mclget(struct mbuf *m, int how); 504 int m_sharecount(struct mbuf *m); 505 void m_chtype(struct mbuf *m, int type); 506 int m_devpad(struct mbuf *m, int padto); 507 508 #ifdef MBUF_DEBUG 509 510 void mbuftrackid(struct mbuf *, int); 511 512 #define m_free(m) _m_free(m, __func__) 513 #define m_freem(m) _m_freem(m, __func__) 514 515 #else 516 517 #define mbuftrackid(m, id) /* empty */ 518 519 #endif 520 521 /* 522 * Allocate the right type of mbuf for the desired total length. 523 * The mbuf returned does not necessarily cover the entire requested length. 524 * This function follows mbuf chaining policy of allowing MINCLSIZE 525 * amount of chained mbufs. 526 */ 527 static __inline struct mbuf * 528 m_getl(int len, int how, int type, int flags, int *psize) 529 { 530 struct mbuf *m; 531 int size; 532 533 if (len >= MINCLSIZE) { 534 m = m_getcl(how, type, flags); 535 size = MCLBYTES; 536 } else if (flags & M_PKTHDR) { 537 m = m_gethdr(how, type); 538 size = MHLEN; 539 } else { 540 m = m_get(how, type); 541 size = MLEN; 542 } 543 if (psize != NULL) 544 *psize = size; 545 return (m); 546 } 547 548 /* 549 * Get a single mbuf that covers the requested number of bytes. 550 * This function does not create mbuf chains. It explicitly marks 551 * places in the code that abuse mbufs for contiguous data buffers. 552 */ 553 static __inline struct mbuf * 554 m_getb(int len, int how, int type, int flags) 555 { 556 struct mbuf *m; 557 int mbufsize = (flags & M_PKTHDR) ? MHLEN : MLEN; 558 559 if (len > mbufsize) 560 m = m_getcl(how, type, flags); 561 else if (flags & M_PKTHDR) 562 m = m_gethdr(how, type); 563 else 564 m = m_get(how, type); 565 return (m); 566 } 567 568 /* 569 * Packets may have annotations attached by affixing a list 570 * of "packet tags" to the pkthdr structure. Packet tags are 571 * dynamically allocated semi-opaque data structures that have 572 * a fixed header (struct m_tag) that specifies the size of the 573 * memory block and a <cookie,type> pair that identifies it. 574 * The cookie is a 32-bit unique unsigned value used to identify 575 * a module or ABI. By convention this value is chose as the 576 * date+time that the module is created, expressed as the number of 577 * seconds since the epoch (e.g. using date -u +'%s'). The type value 578 * is an ABI/module-specific value that identifies a particular annotation 579 * and is private to the module. For compatibility with systems 580 * like openbsd that define packet tags w/o an ABI/module cookie, 581 * the value PACKET_ABI_COMPAT is used to implement m_tag_get and 582 * m_tag_find compatibility shim functions and several tag types are 583 * defined below. Users that do not require compatibility should use 584 * a private cookie value so that packet tag-related definitions 585 * can be maintained privately. 586 * 587 * Note that the packet tag returned by m_tag_alloc has the default 588 * memory alignment implemented by kmalloc. To reference private data 589 * one can use a construct like: 590 * 591 * struct m_tag *mtag = m_tag_alloc(...); 592 * struct foo *p = m_tag_data(mtag); 593 * 594 * if the alignment of struct m_tag is sufficient for referencing members 595 * of struct foo. Otherwise it is necessary to embed struct m_tag within 596 * the private data structure to insure proper alignment; e.g. 597 * 598 * struct foo { 599 * struct m_tag tag; 600 * ... 601 * }; 602 * struct foo *p = (struct foo *)m_tag_alloc(...); 603 * struct m_tag *mtag = &p->tag; 604 */ 605 606 #define PACKET_TAG_NONE 0 /* Nadda */ 607 608 /* Packet tag for use with PACKET_ABI_COMPAT */ 609 #define PACKET_TAG_IPSEC_IN_DONE 1 /* IPsec applied, in */ 610 /* struct tdb_indent */ 611 #define PACKET_TAG_IPSEC_OUT_DONE 2 /* IPsec applied, out */ 612 /* struct tdb_indent */ 613 #define PACKET_TAG_IPSEC_IN_CRYPTO_DONE 3 /* NIC IPsec crypto done */ 614 /* struct tdb_indent, never added */ 615 #define PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED 4 /* NIC IPsec crypto req'ed */ 616 /* struct tdb_indent, never added */ 617 #define PACKET_TAG_IPSEC_PENDING_TDB 5 /* Reminder to do IPsec */ 618 /* struct tdb_indent, never added */ 619 #define PACKET_TAG_ENCAP 6 /* Encap. processing */ 620 /* struct ifnet *, the GIF interface */ 621 #define PACKET_TAG_IPSEC_HISTORY 7 /* IPSEC history */ 622 /* struct ipsec_history */ 623 #define PACKET_TAG_IPV6_INPUT 8 /* IPV6 input processing */ 624 /* struct ip6aux */ 625 #define PACKET_TAG_IPFW_DIVERT 9 /* divert info */ 626 /* struct divert_info */ 627 #define PACKET_TAG_DUMMYNET 15 /* dummynet info */ 628 /* struct dn_pkt */ 629 #define PACKET_TAG_IPFORWARD 18 /* ipforward info */ 630 /* struct sockaddr_in */ 631 #define PACKET_TAG_IPSRCRT 27 /* IP srcrt opts */ 632 /* struct ip_srcrt_opt */ 633 #define PACKET_TAG_CARP 28 /* CARP info */ 634 /* struct pf_mtag */ 635 #define PACKET_TAG_PF 29 /* PF info */ 636 637 /* Packet tag routines */ 638 struct m_tag *m_tag_alloc(u_int32_t, int, int, int); 639 void m_tag_free(struct m_tag *); 640 void m_tag_prepend(struct mbuf *, struct m_tag *); 641 void m_tag_unlink(struct mbuf *, struct m_tag *); 642 void m_tag_delete(struct mbuf *, struct m_tag *); 643 void m_tag_delete_chain(struct mbuf *); 644 struct m_tag *m_tag_locate(struct mbuf *, u_int32_t, int, struct m_tag *); 645 struct m_tag *m_tag_copy(struct m_tag *, int); 646 int m_tag_copy_chain(struct mbuf *, const struct mbuf *, int); 647 void m_tag_init(struct mbuf *); 648 struct m_tag *m_tag_first(struct mbuf *); 649 struct m_tag *m_tag_next(struct mbuf *, struct m_tag *); 650 651 /* these are for openbsd compatibility */ 652 #define MTAG_ABI_COMPAT 0 /* compatibility ABI */ 653 654 static __inline void * 655 m_tag_data(struct m_tag *tag) 656 { 657 return ((void *)(tag + 1)); 658 } 659 660 static __inline struct m_tag * 661 m_tag_get(int type, int length, int wait) 662 { 663 return m_tag_alloc(MTAG_ABI_COMPAT, type, length, wait); 664 } 665 666 static __inline struct m_tag * 667 m_tag_find(struct mbuf *m, int type, struct m_tag *start) 668 { 669 return m_tag_locate(m, MTAG_ABI_COMPAT, type, start); 670 } 671 672 #endif /* _KERNEL */ 673 674 #endif /* _KERNEL || _KERNEL_STRUCTURES */ 675 #endif /* !_SYS_MBUF_H_ */ 676