1 /*- 2 * Copyright (c) 1982, 1986, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)mbuf.h 8.5 (Berkeley) 2/19/95 30 * $FreeBSD$ 31 */ 32 33 #ifndef _SYS_MBUF_H_ 34 #define _SYS_MBUF_H_ 35 36 /* XXX: These includes suck. Sorry! */ 37 #include <sys/queue.h> 38 #ifdef _KERNEL 39 #include <sys/systm.h> 40 #include <vm/uma.h> 41 #ifdef WITNESS 42 #include <sys/lock.h> 43 #endif 44 #endif 45 46 /* 47 * Mbufs are of a single size, MSIZE (sys/param.h), which 48 * includes overhead. An mbuf may add a single "mbuf cluster" of size 49 * MCLBYTES (also in sys/param.h), which has no additional overhead 50 * and is used instead of the internal data area; this is done when 51 * at least MINCLSIZE of data must be stored. Additionally, it is possible 52 * to allocate a separate buffer externally and attach it to the mbuf in 53 * a way similar to that of mbuf clusters. 54 */ 55 #define MLEN (MSIZE - sizeof(struct m_hdr)) /* normal data len */ 56 #define MHLEN (MLEN - sizeof(struct pkthdr)) /* data len w/pkthdr */ 57 #define MINCLSIZE (MHLEN + 1) /* smallest amount to put in cluster */ 58 #define M_MAXCOMPRESS (MHLEN / 2) /* max amount to copy for compression */ 59 60 #ifdef _KERNEL 61 /*- 62 * Macros for type conversion: 63 * mtod(m, t) -- Convert mbuf pointer to data pointer of correct type. 64 * dtom(x) -- Convert data pointer within mbuf to mbuf pointer (XXX). 65 */ 66 #define mtod(m, t) ((t)((m)->m_data)) 67 #define dtom(x) ((struct mbuf *)((intptr_t)(x) & ~(MSIZE-1))) 68 69 /* 70 * Argument structure passed to UMA routines during mbuf and packet 71 * allocations. 72 */ 73 struct mb_args { 74 int flags; /* Flags for mbuf being allocated */ 75 short type; /* Type of mbuf being allocated */ 76 }; 77 #endif /* _KERNEL */ 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 }; 90 91 /* 92 * Packet tag structure (see below for details). 93 */ 94 struct m_tag { 95 SLIST_ENTRY(m_tag) m_tag_link; /* List of packet tags */ 96 u_int16_t m_tag_id; /* Tag ID */ 97 u_int16_t m_tag_len; /* Length of data */ 98 u_int32_t m_tag_cookie; /* ABI/Module ID */ 99 void (*m_tag_free)(struct m_tag *); 100 }; 101 102 /* 103 * Record/packet header in first mbuf of chain; valid only if M_PKTHDR is set. 104 */ 105 struct pkthdr { 106 struct ifnet *rcvif; /* rcv interface */ 107 int len; /* total packet length */ 108 /* variables for ip and tcp reassembly */ 109 void *header; /* pointer to packet header */ 110 /* variables for hardware checksum */ 111 int csum_flags; /* flags regarding checksum */ 112 int csum_data; /* data field used by csum routines */ 113 SLIST_HEAD(packet_tags, m_tag) tags; /* list of packet tags */ 114 }; 115 116 /* 117 * Description of external storage mapped into mbuf; valid only if M_EXT is set. 118 */ 119 struct m_ext { 120 caddr_t ext_buf; /* start of buffer */ 121 void (*ext_free) /* free routine if not the usual */ 122 (void *, void *); 123 void *ext_args; /* optional argument pointer */ 124 u_int ext_size; /* size of buffer, for ext_free */ 125 volatile u_int *ref_cnt; /* pointer to ref count info */ 126 int ext_type; /* type of external storage */ 127 }; 128 129 /* 130 * The core of the mbuf object along with some shortcut defines for 131 * practical purposes. 132 */ 133 struct mbuf { 134 struct m_hdr m_hdr; 135 union { 136 struct { 137 struct pkthdr MH_pkthdr; /* M_PKTHDR set */ 138 union { 139 struct m_ext MH_ext; /* M_EXT set */ 140 char MH_databuf[MHLEN]; 141 } MH_dat; 142 } MH; 143 char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */ 144 } M_dat; 145 }; 146 #define m_next m_hdr.mh_next 147 #define m_len m_hdr.mh_len 148 #define m_data m_hdr.mh_data 149 #define m_type m_hdr.mh_type 150 #define m_flags m_hdr.mh_flags 151 #define m_nextpkt m_hdr.mh_nextpkt 152 #define m_act m_nextpkt 153 #define m_pkthdr M_dat.MH.MH_pkthdr 154 #define m_ext M_dat.MH.MH_dat.MH_ext 155 #define m_pktdat M_dat.MH.MH_dat.MH_databuf 156 #define m_dat M_dat.M_databuf 157 158 /* 159 * mbuf flags. 160 */ 161 #define M_EXT 0x0001 /* has associated external storage */ 162 #define M_PKTHDR 0x0002 /* start of record */ 163 #define M_EOR 0x0004 /* end of record */ 164 #define M_RDONLY 0x0008 /* associated data is marked read-only */ 165 #define M_PROTO1 0x0010 /* protocol-specific */ 166 #define M_PROTO2 0x0020 /* protocol-specific */ 167 #define M_PROTO3 0x0040 /* protocol-specific */ 168 #define M_PROTO4 0x0080 /* protocol-specific */ 169 #define M_PROTO5 0x0100 /* protocol-specific */ 170 #define M_SKIP_FIREWALL 0x4000 /* skip firewall processing */ 171 #define M_FREELIST 0x8000 /* mbuf is on the free list */ 172 173 /* 174 * mbuf pkthdr flags (also stored in m_flags). 175 */ 176 #define M_BCAST 0x0200 /* send/received as link-level broadcast */ 177 #define M_MCAST 0x0400 /* send/received as link-level multicast */ 178 #define M_FRAG 0x0800 /* packet is a fragment of a larger packet */ 179 #define M_FIRSTFRAG 0x1000 /* packet is first fragment */ 180 #define M_LASTFRAG 0x2000 /* packet is last fragment */ 181 #define M_VLANTAG 0x10000 /* packet has VLAN tag attached */ 182 183 /* 184 * External buffer types: identify ext_buf type. 185 */ 186 #define EXT_CLUSTER 1 /* mbuf cluster */ 187 #define EXT_SFBUF 2 /* sendfile(2)'s sf_bufs */ 188 #define EXT_JUMBO4 3 /* jumbo cluster 4096 bytes */ 189 #define EXT_JUMBO9 4 /* jumbo cluster 9216 bytes */ 190 #define EXT_JUMBO16 5 /* jumbo cluster 16184 bytes */ 191 #define EXT_PACKET 6 /* mbuf+cluster from packet zone */ 192 #define EXT_NET_DRV 100 /* custom ext_buf provided by net driver(s) */ 193 #define EXT_MOD_TYPE 200 /* custom module's ext_buf type */ 194 #define EXT_DISPOSABLE 300 /* can throw this buffer away w/page flipping */ 195 #define EXT_EXTREF 400 /* has externally maintained ref_cnt ptr */ 196 197 /* 198 * Flags copied when copying m_pkthdr. 199 */ 200 #define M_COPYFLAGS (M_PKTHDR|M_EOR|M_RDONLY|M_PROTO1|M_PROTO1|M_PROTO2|\ 201 M_PROTO3|M_PROTO4|M_PROTO5|M_SKIP_FIREWALL|\ 202 M_BCAST|M_MCAST|M_FRAG|M_FIRSTFRAG|M_LASTFRAG|\ 203 M_VLANTAG) 204 205 /* 206 * Flags to purge when crossing layers. 207 */ 208 #define M_PROTOFLAGS (M_PROTO1|M_PROTO2|M_PROTO3|M_PROTO4|M_PROTO5) 209 210 /* 211 * Flags indicating hw checksum support and sw checksum requirements. 212 */ 213 #define CSUM_IP 0x0001 /* will csum IP */ 214 #define CSUM_TCP 0x0002 /* will csum TCP */ 215 #define CSUM_UDP 0x0004 /* will csum UDP */ 216 #define CSUM_IP_FRAGS 0x0008 /* will csum IP fragments */ 217 #define CSUM_FRAGMENT 0x0010 /* will do IP fragmentation */ 218 219 #define CSUM_IP_CHECKED 0x0100 /* did csum IP */ 220 #define CSUM_IP_VALID 0x0200 /* ... the csum is valid */ 221 #define CSUM_DATA_VALID 0x0400 /* csum_data field is valid */ 222 #define CSUM_PSEUDO_HDR 0x0800 /* csum_data has pseudo hdr */ 223 224 #define CSUM_DELAY_DATA (CSUM_TCP | CSUM_UDP) 225 #define CSUM_DELAY_IP (CSUM_IP) /* XXX add ipv6 here too? */ 226 227 /* 228 * mbuf types. 229 */ 230 #define MT_NOTMBUF 0 /* USED INTERNALLY ONLY! Object is not mbuf */ 231 #define MT_DATA 1 /* dynamic (data) allocation */ 232 #define MT_HEADER MT_DATA /* packet header, use M_PKTHDR instead */ 233 #if 0 234 #define MT_SOCKET 3 /* socket structure */ 235 #define MT_PCB 4 /* protocol control block */ 236 #define MT_RTABLE 5 /* routing tables */ 237 #define MT_HTABLE 6 /* IMP host tables */ 238 #define MT_ATABLE 7 /* address resolution tables */ 239 #endif 240 #define MT_SONAME 8 /* socket name */ 241 #if 0 242 #define MT_SOOPTS 10 /* socket options */ 243 #endif 244 #define MT_FTABLE 11 /* fragment reassembly header */ 245 #if 0 246 #define MT_RIGHTS 12 /* access rights */ 247 #define MT_IFADDR 13 /* interface address */ 248 #endif 249 #define MT_CONTROL 14 /* extra-data protocol message */ 250 #define MT_OOBDATA 15 /* expedited data */ 251 #define MT_NTYPES 16 /* number of mbuf types for mbtypes[] */ 252 253 #define MT_NOINIT 255 /* Not a type but a flag to allocate 254 a non-initialized mbuf */ 255 256 /* 257 * General mbuf allocator statistics structure. 258 */ 259 struct mbstat { 260 u_long m_mbufs; /* XXX */ 261 u_long m_mclusts; /* XXX */ 262 263 u_long m_drain; /* times drained protocols for space */ 264 u_long m_mcfail; /* XXX: times m_copym failed */ 265 u_long m_mpfail; /* XXX: times m_pullup failed */ 266 u_long m_msize; /* length of an mbuf */ 267 u_long m_mclbytes; /* length of an mbuf cluster */ 268 u_long m_minclsize; /* min length of data to allocate a cluster */ 269 u_long m_mlen; /* length of data in an mbuf */ 270 u_long m_mhlen; /* length of data in a header mbuf */ 271 272 /* Number of mbtypes (gives # elems in mbtypes[] array: */ 273 short m_numtypes; 274 275 /* XXX: Sendfile stats should eventually move to their own struct */ 276 u_long sf_iocnt; /* times sendfile had to do disk I/O */ 277 u_long sf_allocfail; /* times sfbuf allocation failed */ 278 u_long sf_allocwait; /* times sfbuf allocation had to wait */ 279 }; 280 281 /* 282 * Flags specifying how an allocation should be made. 283 * 284 * The flag to use is as follows: 285 * - M_DONTWAIT or M_NOWAIT from an interrupt handler to not block allocation. 286 * - M_WAIT or M_WAITOK or M_TRYWAIT from wherever it is safe to block. 287 * 288 * M_DONTWAIT/M_NOWAIT means that we will not block the thread explicitly 289 * and if we cannot allocate immediately we may return NULL, 290 * whereas M_WAIT/M_WAITOK/M_TRYWAIT means that if we cannot allocate 291 * resources we will block until they are available, and thus never 292 * return NULL. 293 * 294 * XXX Eventually just phase this out to use M_WAITOK/M_NOWAIT. 295 */ 296 #define MBTOM(how) (how) 297 #define M_DONTWAIT M_NOWAIT 298 #define M_TRYWAIT M_WAITOK 299 #define M_WAIT M_WAITOK 300 301 /* 302 * String names of mbuf-related UMA(9) and malloc(9) types. Exposed to 303 * !_KERNEL so that monitoring tools can look up the zones with 304 * libmemstat(3). 305 */ 306 #define MBUF_MEM_NAME "mbuf" 307 #define MBUF_CLUSTER_MEM_NAME "mbuf_cluster" 308 #define MBUF_PACKET_MEM_NAME "mbuf_packet" 309 #define MBUF_JUMBO4_MEM_NAME "mbuf_jumbo_4k" 310 #define MBUF_JUMBO9_MEM_NAME "mbuf_jumbo_9k" 311 #define MBUF_JUMBO16_MEM_NAME "mbuf_jumbo_16k" 312 #define MBUF_TAG_MEM_NAME "mbuf_tag" 313 #define MBUF_EXTREFCNT_MEM_NAME "mbuf_ext_refcnt" 314 315 #ifdef _KERNEL 316 317 #ifdef WITNESS 318 #define MBUF_CHECKSLEEP(how) do { \ 319 if (how == M_WAITOK) \ 320 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, \ 321 "Sleeping in \"%s\"", __func__); \ 322 } while (0) 323 #else 324 #define MBUF_CHECKSLEEP(how) 325 #endif 326 327 /* 328 * Network buffer allocation API 329 * 330 * The rest of it is defined in kern/kern_mbuf.c 331 */ 332 333 extern uma_zone_t zone_mbuf; 334 extern uma_zone_t zone_clust; 335 extern uma_zone_t zone_pack; 336 extern uma_zone_t zone_jumbo4; 337 extern uma_zone_t zone_jumbo9; 338 extern uma_zone_t zone_jumbo16; 339 extern uma_zone_t zone_ext_refcnt; 340 341 static __inline struct mbuf *m_get(int how, short type); 342 static __inline struct mbuf *m_gethdr(int how, short type); 343 static __inline struct mbuf *m_getcl(int how, short type, int flags); 344 static __inline struct mbuf *m_getjcl(int how, short type, int flags, int size); 345 static __inline struct mbuf *m_getclr(int how, short type); /* XXX */ 346 static __inline struct mbuf *m_free(struct mbuf *m); 347 static __inline void m_clget(struct mbuf *m, int how); 348 static __inline void *m_cljget(struct mbuf *m, int how, int size); 349 static __inline void m_chtype(struct mbuf *m, short new_type); 350 void mb_free_ext(struct mbuf *); 351 352 static __inline 353 struct mbuf * 354 m_get(int how, short type) 355 { 356 struct mb_args args; 357 358 args.flags = 0; 359 args.type = type; 360 return (uma_zalloc_arg(zone_mbuf, &args, how)); 361 } 362 363 /* XXX This should be depracated, very little use */ 364 static __inline 365 struct mbuf * 366 m_getclr(int how, short type) 367 { 368 struct mbuf *m; 369 struct mb_args args; 370 371 args.flags = 0; 372 args.type = type; 373 m = uma_zalloc_arg(zone_mbuf, &args, how); 374 if (m != NULL) 375 bzero(m->m_data, MLEN); 376 return m; 377 } 378 379 static __inline 380 struct mbuf * 381 m_gethdr(int how, short type) 382 { 383 struct mb_args args; 384 385 args.flags = M_PKTHDR; 386 args.type = type; 387 return (uma_zalloc_arg(zone_mbuf, &args, how)); 388 } 389 390 static __inline 391 struct mbuf * 392 m_getcl(int how, short type, int flags) 393 { 394 struct mb_args args; 395 396 args.flags = flags; 397 args.type = type; 398 return (uma_zalloc_arg(zone_pack, &args, how)); 399 } 400 401 /* 402 * m_getjcl() returns an mbuf with a cluster of the specified size attached. 403 * For size it takes MCLBYTES, MJUM4BYTES, MJUM9BYTES, MJUM16BYTES. 404 */ 405 static __inline /* XXX: This is rather large, should be real function maybe. */ 406 struct mbuf * 407 m_getjcl(int how, short type, int flags, int size) 408 { 409 struct mb_args args; 410 struct mbuf *m, *n; 411 uma_zone_t zone; 412 413 args.flags = flags; 414 args.type = type; 415 416 m = uma_zalloc_arg(zone_mbuf, &args, how); 417 if (m == NULL) 418 return NULL; 419 420 switch (size) { 421 case MCLBYTES: 422 zone = zone_clust; 423 break; 424 case MJUM4BYTES: 425 zone = zone_jumbo4; 426 break; 427 case MJUM9BYTES: 428 zone = zone_jumbo9; 429 break; 430 case MJUM16BYTES: 431 zone = zone_jumbo16; 432 break; 433 default: 434 panic("%s: m_getjcl: invalid cluster type", __func__); 435 } 436 n = uma_zalloc_arg(zone, m, how); 437 if (n == NULL) 438 uma_zfree(zone_mbuf, m); 439 return n; 440 } 441 442 static __inline 443 struct mbuf * 444 m_free(struct mbuf *m) 445 { 446 struct mbuf *n = m->m_next; 447 448 if (m->m_flags & M_EXT) 449 mb_free_ext(m); 450 else 451 uma_zfree(zone_mbuf, m); 452 return n; 453 } 454 455 static __inline 456 void 457 m_clget(struct mbuf *m, int how) 458 { 459 if (m->m_flags & M_EXT) 460 printf("%s: %p mbuf already has cluster\n", __func__, m); 461 m->m_ext.ext_buf = NULL; 462 uma_zalloc_arg(zone_clust, m, how); 463 } 464 465 /* 466 * m_cljget() is different from m_clget() as it can allocate clusters 467 * without attaching them to an mbuf. In that case the return value 468 * is the pointer to the cluster of the requested size. If an mbuf was 469 * specified, it gets the cluster attached to it and the return value 470 * can be safely ignored. 471 * For size it takes MCLBYTES, MJUM4BYTES, MJUM9BYTES, MJUM16BYTES. 472 */ 473 static __inline 474 void * 475 m_cljget(struct mbuf *m, int how, int size) 476 { 477 uma_zone_t zone; 478 479 if (m && m->m_flags & M_EXT) 480 printf("%s: %p mbuf already has cluster\n", __func__, m); 481 if (m != NULL) 482 m->m_ext.ext_buf = NULL; 483 484 switch (size) { 485 case MCLBYTES: 486 zone = zone_clust; 487 break; 488 case MJUM4BYTES: 489 zone = zone_jumbo4; 490 break; 491 case MJUM9BYTES: 492 zone = zone_jumbo9; 493 break; 494 case MJUM16BYTES: 495 zone = zone_jumbo16; 496 break; 497 default: 498 panic("%s: m_getjcl: invalid cluster type", __func__); 499 } 500 501 return (uma_zalloc_arg(zone, m, how)); 502 } 503 504 static __inline 505 void 506 m_chtype(struct mbuf *m, short new_type) 507 { 508 m->m_type = new_type; 509 } 510 511 /* 512 * mbuf, cluster, and external object allocation macros 513 * (for compatibility purposes). 514 */ 515 /* NB: M_COPY_PKTHDR is deprecated. Use M_MOVE_PKTHDR or m_dup_pktdr. */ 516 #define M_MOVE_PKTHDR(to, from) m_move_pkthdr((to), (from)) 517 #define MGET(m, how, type) ((m) = m_get((how), (type))) 518 #define MGETHDR(m, how, type) ((m) = m_gethdr((how), (type))) 519 #define MCLGET(m, how) m_clget((m), (how)) 520 #define MEXTADD(m, buf, size, free, args, flags, type) \ 521 m_extadd((m), (caddr_t)(buf), (size), (free), (args), (flags), (type)) 522 523 /* 524 * Evaluate TRUE if it's safe to write to the mbuf m's data region (this 525 * can be both the local data payload, or an external buffer area, 526 * depending on whether M_EXT is set). 527 */ 528 #define M_WRITABLE(m) (!((m)->m_flags & M_RDONLY) && \ 529 (!(((m)->m_flags & M_EXT)) || \ 530 (*((m)->m_ext.ref_cnt) == 1)) ) \ 531 532 /* Check if the supplied mbuf has a packet header, or else panic. */ 533 #define M_ASSERTPKTHDR(m) \ 534 KASSERT(m != NULL && m->m_flags & M_PKTHDR, \ 535 ("%s: no mbuf packet header!", __func__)) 536 537 /* Ensure that the supplied mbuf is a valid, non-free mbuf. */ 538 /* XXX: Broken at the moment. Need some UMA magic to make it work again. */ 539 #define M_ASSERTVALID(m) \ 540 KASSERT((((struct mbuf *)m)->m_flags & 0) == 0, \ 541 ("%s: attempted use of a free mbuf!", __func__)) 542 543 /* 544 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place 545 * an object of the specified size at the end of the mbuf, longword aligned. 546 */ 547 #define M_ALIGN(m, len) do { \ 548 KASSERT(!((m)->m_flags & (M_PKTHDR|M_EXT)), \ 549 ("%s: M_ALIGN not normal mbuf", __func__)); \ 550 KASSERT((m)->m_data == (m)->m_dat, \ 551 ("%s: M_ALIGN not a virgin mbuf", __func__)); \ 552 (m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1); \ 553 } while (0) 554 555 /* 556 * As above, for mbufs allocated with m_gethdr/MGETHDR 557 * or initialized by M_COPY_PKTHDR. 558 */ 559 #define MH_ALIGN(m, len) do { \ 560 KASSERT((m)->m_flags & M_PKTHDR && !((m)->m_flags & M_EXT), \ 561 ("%s: MH_ALIGN not PKTHDR mbuf", __func__)); \ 562 KASSERT((m)->m_data == (m)->m_pktdat, \ 563 ("%s: MH_ALIGN not a virgin mbuf", __func__)); \ 564 (m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1); \ 565 } while (0) 566 567 /* 568 * Compute the amount of space available 569 * before the current start of data in an mbuf. 570 * 571 * The M_WRITABLE() is a temporary, conservative safety measure: the burden 572 * of checking writability of the mbuf data area rests solely with the caller. 573 */ 574 #define M_LEADINGSPACE(m) \ 575 ((m)->m_flags & M_EXT ? \ 576 (M_WRITABLE(m) ? (m)->m_data - (m)->m_ext.ext_buf : 0): \ 577 (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \ 578 (m)->m_data - (m)->m_dat) 579 580 /* 581 * Compute the amount of space available 582 * after the end of data in an mbuf. 583 * 584 * The M_WRITABLE() is a temporary, conservative safety measure: the burden 585 * of checking writability of the mbuf data area rests solely with the caller. 586 */ 587 #define M_TRAILINGSPACE(m) \ 588 ((m)->m_flags & M_EXT ? \ 589 (M_WRITABLE(m) ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size \ 590 - ((m)->m_data + (m)->m_len) : 0) : \ 591 &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len)) 592 593 /* 594 * Arrange to prepend space of size plen to mbuf m. 595 * If a new mbuf must be allocated, how specifies whether to wait. 596 * If the allocation fails, the original mbuf chain is freed and m is 597 * set to NULL. 598 */ 599 #define M_PREPEND(m, plen, how) do { \ 600 struct mbuf **_mmp = &(m); \ 601 struct mbuf *_mm = *_mmp; \ 602 int _mplen = (plen); \ 603 int __mhow = (how); \ 604 \ 605 MBUF_CHECKSLEEP(how); \ 606 if (M_LEADINGSPACE(_mm) >= _mplen) { \ 607 _mm->m_data -= _mplen; \ 608 _mm->m_len += _mplen; \ 609 } else \ 610 _mm = m_prepend(_mm, _mplen, __mhow); \ 611 if (_mm != NULL && _mm->m_flags & M_PKTHDR) \ 612 _mm->m_pkthdr.len += _mplen; \ 613 *_mmp = _mm; \ 614 } while (0) 615 616 /* 617 * Change mbuf to new type. 618 * This is a relatively expensive operation and should be avoided. 619 */ 620 #define MCHTYPE(m, t) m_chtype((m), (t)) 621 622 /* Length to m_copy to copy all. */ 623 #define M_COPYALL 1000000000 624 625 /* Compatibility with 4.3. */ 626 #define m_copy(m, o, l) m_copym((m), (o), (l), M_DONTWAIT) 627 628 extern int max_datalen; /* MHLEN - max_hdr */ 629 extern int max_hdr; /* Largest link + protocol header */ 630 extern int max_linkhdr; /* Largest link-level header */ 631 extern int max_protohdr; /* Largest protocol header */ 632 extern struct mbstat mbstat; /* General mbuf stats/infos */ 633 extern int nmbclusters; /* Maximum number of clusters */ 634 635 struct uio; 636 637 void m_adj(struct mbuf *, int); 638 void m_align(struct mbuf *, int); 639 int m_apply(struct mbuf *, int, int, 640 int (*)(void *, void *, u_int), void *); 641 int m_append(struct mbuf *, int, c_caddr_t); 642 void m_cat(struct mbuf *, struct mbuf *); 643 void m_extadd(struct mbuf *, caddr_t, u_int, 644 void (*)(void *, void *), void *, int, int); 645 void m_copyback(struct mbuf *, int, int, c_caddr_t); 646 void m_copydata(const struct mbuf *, int, int, caddr_t); 647 struct mbuf *m_copym(struct mbuf *, int, int, int); 648 struct mbuf *m_copymdata(struct mbuf *, struct mbuf *, 649 int, int, int, int); 650 struct mbuf *m_copypacket(struct mbuf *, int); 651 void m_copy_pkthdr(struct mbuf *, struct mbuf *); 652 struct mbuf *m_copyup(struct mbuf *n, int len, int dstoff); 653 struct mbuf *m_defrag(struct mbuf *, int); 654 void m_demote(struct mbuf *, int); 655 struct mbuf *m_devget(char *, int, int, struct ifnet *, 656 void (*)(char *, caddr_t, u_int)); 657 struct mbuf *m_dup(struct mbuf *, int); 658 int m_dup_pkthdr(struct mbuf *, struct mbuf *, int); 659 u_int m_fixhdr(struct mbuf *); 660 struct mbuf *m_fragment(struct mbuf *, int, int); 661 void m_freem(struct mbuf *); 662 struct mbuf *m_getm(struct mbuf *, int, int, short); 663 struct mbuf *m_getptr(struct mbuf *, int, int *); 664 u_int m_length(struct mbuf *, struct mbuf **); 665 void m_move_pkthdr(struct mbuf *, struct mbuf *); 666 struct mbuf *m_prepend(struct mbuf *, int, int); 667 void m_print(const struct mbuf *, int); 668 struct mbuf *m_pulldown(struct mbuf *, int, int, int *); 669 struct mbuf *m_pullup(struct mbuf *, int); 670 int m_sanity(struct mbuf *, int); 671 struct mbuf *m_split(struct mbuf *, int, int); 672 struct mbuf *m_uiotombuf(struct uio *, int, int, int); 673 674 /*- 675 * Network packets may have annotations attached by affixing a list 676 * of "packet tags" to the pkthdr structure. Packet tags are 677 * dynamically allocated semi-opaque data structures that have 678 * a fixed header (struct m_tag) that specifies the size of the 679 * memory block and a <cookie,type> pair that identifies it. 680 * The cookie is a 32-bit unique unsigned value used to identify 681 * a module or ABI. By convention this value is chose as the 682 * date+time that the module is created, expressed as the number of 683 * seconds since the epoch (e.g., using date -u +'%s'). The type value 684 * is an ABI/module-specific value that identifies a particular annotation 685 * and is private to the module. For compatibility with systems 686 * like OpenBSD that define packet tags w/o an ABI/module cookie, 687 * the value PACKET_ABI_COMPAT is used to implement m_tag_get and 688 * m_tag_find compatibility shim functions and several tag types are 689 * defined below. Users that do not require compatibility should use 690 * a private cookie value so that packet tag-related definitions 691 * can be maintained privately. 692 * 693 * Note that the packet tag returned by m_tag_alloc has the default 694 * memory alignment implemented by malloc. To reference private data 695 * one can use a construct like: 696 * 697 * struct m_tag *mtag = m_tag_alloc(...); 698 * struct foo *p = (struct foo *)(mtag+1); 699 * 700 * if the alignment of struct m_tag is sufficient for referencing members 701 * of struct foo. Otherwise it is necessary to embed struct m_tag within 702 * the private data structure to insure proper alignment; e.g., 703 * 704 * struct foo { 705 * struct m_tag tag; 706 * ... 707 * }; 708 * struct foo *p = (struct foo *) m_tag_alloc(...); 709 * struct m_tag *mtag = &p->tag; 710 */ 711 712 /* 713 * Persistent tags stay with an mbuf until the mbuf is reclaimed. 714 * Otherwise tags are expected to ``vanish'' when they pass through 715 * a network interface. For most interfaces this happens normally 716 * as the tags are reclaimed when the mbuf is free'd. However in 717 * some special cases reclaiming must be done manually. An example 718 * is packets that pass through the loopback interface. Also, one 719 * must be careful to do this when ``turning around'' packets (e.g., 720 * icmp_reflect). 721 * 722 * To mark a tag persistent bit-or this flag in when defining the 723 * tag id. The tag will then be treated as described above. 724 */ 725 #define MTAG_PERSISTENT 0x800 726 727 #define PACKET_TAG_NONE 0 /* Nadda */ 728 729 /* Packet tags for use with PACKET_ABI_COMPAT. */ 730 #define PACKET_TAG_IPSEC_IN_DONE 1 /* IPsec applied, in */ 731 #define PACKET_TAG_IPSEC_OUT_DONE 2 /* IPsec applied, out */ 732 #define PACKET_TAG_IPSEC_IN_CRYPTO_DONE 3 /* NIC IPsec crypto done */ 733 #define PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED 4 /* NIC IPsec crypto req'ed */ 734 #define PACKET_TAG_IPSEC_IN_COULD_DO_CRYPTO 5 /* NIC notifies IPsec */ 735 #define PACKET_TAG_IPSEC_PENDING_TDB 6 /* Reminder to do IPsec */ 736 #define PACKET_TAG_BRIDGE 7 /* Bridge processing done */ 737 #define PACKET_TAG_GIF 8 /* GIF processing done */ 738 #define PACKET_TAG_GRE 9 /* GRE processing done */ 739 #define PACKET_TAG_IN_PACKET_CHECKSUM 10 /* NIC checksumming done */ 740 #define PACKET_TAG_ENCAP 11 /* Encap. processing */ 741 #define PACKET_TAG_IPSEC_SOCKET 12 /* IPSEC socket ref */ 742 #define PACKET_TAG_IPSEC_HISTORY 13 /* IPSEC history */ 743 #define PACKET_TAG_IPV6_INPUT 14 /* IPV6 input processing */ 744 #define PACKET_TAG_DUMMYNET 15 /* dummynet info */ 745 #define PACKET_TAG_DIVERT 17 /* divert info */ 746 #define PACKET_TAG_IPFORWARD 18 /* ipforward info */ 747 #define PACKET_TAG_MACLABEL (19 | MTAG_PERSISTENT) /* MAC label */ 748 #define PACKET_TAG_PF_ROUTED 21 /* PF routed, avoid loops */ 749 #define PACKET_TAG_PF_FRAGCACHE 22 /* PF fragment cached */ 750 #define PACKET_TAG_PF_QID 23 /* PF ALTQ queue id */ 751 #define PACKET_TAG_PF_TAG 24 /* PF tagged */ 752 #define PACKET_TAG_RTSOCKFAM 25 /* rtsock sa family */ 753 #define PACKET_TAG_PF_TRANSLATE_LOCALHOST 26 /* PF translate localhost */ 754 #define PACKET_TAG_IPOPTIONS 27 /* Saved IP options */ 755 #define PACKET_TAG_CARP 28 /* CARP info */ 756 757 /* Packet tag routines. */ 758 struct m_tag *m_tag_alloc(u_int32_t, int, int, int); 759 void m_tag_delete(struct mbuf *, struct m_tag *); 760 void m_tag_delete_chain(struct mbuf *, struct m_tag *); 761 void m_tag_free_default(struct m_tag *); 762 struct m_tag *m_tag_locate(struct mbuf *, u_int32_t, int, struct m_tag *); 763 struct m_tag *m_tag_copy(struct m_tag *, int); 764 int m_tag_copy_chain(struct mbuf *, struct mbuf *, int); 765 void m_tag_delete_nonpersistent(struct mbuf *); 766 767 /* 768 * Initialize the list of tags associated with an mbuf. 769 */ 770 static __inline void 771 m_tag_init(struct mbuf *m) 772 { 773 SLIST_INIT(&m->m_pkthdr.tags); 774 } 775 776 /* 777 * Set up the contents of a tag. Note that this does not 778 * fill in the free method; the caller is expected to do that. 779 * 780 * XXX probably should be called m_tag_init, but that was 781 * already taken. 782 */ 783 static __inline void 784 m_tag_setup(struct m_tag *t, u_int32_t cookie, int type, int len) 785 { 786 t->m_tag_id = type; 787 t->m_tag_len = len; 788 t->m_tag_cookie = cookie; 789 } 790 791 /* 792 * Reclaim resources associated with a tag. 793 */ 794 static __inline void 795 m_tag_free(struct m_tag *t) 796 { 797 (*t->m_tag_free)(t); 798 } 799 800 /* 801 * Return the first tag associated with an mbuf. 802 */ 803 static __inline struct m_tag * 804 m_tag_first(struct mbuf *m) 805 { 806 return (SLIST_FIRST(&m->m_pkthdr.tags)); 807 } 808 809 /* 810 * Return the next tag in the list of tags associated with an mbuf. 811 */ 812 static __inline struct m_tag * 813 m_tag_next(struct mbuf *m, struct m_tag *t) 814 { 815 return (SLIST_NEXT(t, m_tag_link)); 816 } 817 818 /* 819 * Prepend a tag to the list of tags associated with an mbuf. 820 */ 821 static __inline void 822 m_tag_prepend(struct mbuf *m, struct m_tag *t) 823 { 824 SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link); 825 } 826 827 /* 828 * Unlink a tag from the list of tags associated with an mbuf. 829 */ 830 static __inline void 831 m_tag_unlink(struct mbuf *m, struct m_tag *t) 832 { 833 SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link); 834 } 835 836 /* These are for OpenBSD compatibility. */ 837 #define MTAG_ABI_COMPAT 0 /* compatibility ABI */ 838 839 static __inline struct m_tag * 840 m_tag_get(int type, int length, int wait) 841 { 842 return (m_tag_alloc(MTAG_ABI_COMPAT, type, length, wait)); 843 } 844 845 static __inline struct m_tag * 846 m_tag_find(struct mbuf *m, int type, struct m_tag *start) 847 { 848 return (SLIST_EMPTY(&m->m_pkthdr.tags) ? 849 NULL : m_tag_locate(m, MTAG_ABI_COMPAT, type, start)); 850 } 851 852 #endif /* _KERNEL */ 853 854 #endif /* !_SYS_MBUF_H_ */ 855