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. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)mbuf.h 8.5 (Berkeley) 2/19/95 34 * $FreeBSD$ 35 */ 36 37 #ifndef _SYS_MBUF_H_ 38 #define _SYS_MBUF_H_ 39 40 #include <sys/queue.h> 41 42 /* 43 * Mbufs are of a single size, MSIZE (sys/param.h), which 44 * includes overhead. An mbuf may add a single "mbuf cluster" of size 45 * MCLBYTES (also in sys/param.h), which has no additional overhead 46 * and is used instead of the internal data area; this is done when 47 * at least MINCLSIZE of data must be stored. Additionally, it is possible 48 * to allocate a separate buffer externally and attach it to the mbuf in 49 * a way similar to that of mbuf clusters. 50 */ 51 #define MLEN (MSIZE - sizeof(struct m_hdr)) /* normal data len */ 52 #define MHLEN (MLEN - sizeof(struct pkthdr)) /* data len w/pkthdr */ 53 #define MINCLSIZE (MHLEN + 1) /* smallest amount to put in cluster */ 54 #define M_MAXCOMPRESS (MHLEN / 2) /* max amount to copy for compression */ 55 56 #ifdef _KERNEL 57 /*- 58 * Macros for type conversion: 59 * mtod(m, t) -- Convert mbuf pointer to data pointer of correct type. 60 * dtom(x) -- Convert data pointer within mbuf to mbuf pointer (XXX). 61 */ 62 #define mtod(m, t) ((t)((m)->m_data)) 63 #define dtom(x) ((struct mbuf *)((intptr_t)(x) & ~(MSIZE-1))) 64 #endif /* _KERNEL */ 65 66 /* 67 * Header present at the beginning of every mbuf. 68 */ 69 struct m_hdr { 70 struct mbuf *mh_next; /* next buffer in chain */ 71 struct mbuf *mh_nextpkt; /* next chain in queue/record */ 72 caddr_t mh_data; /* location of data */ 73 int mh_len; /* amount of data in this mbuf */ 74 int mh_flags; /* flags; see below */ 75 short mh_type; /* type of data in this mbuf */ 76 }; 77 78 /* 79 * Packet tag structure (see below for details). 80 */ 81 struct m_tag { 82 SLIST_ENTRY(m_tag) m_tag_link; /* List of packet tags */ 83 u_int16_t m_tag_id; /* Tag ID */ 84 u_int16_t m_tag_len; /* Length of data */ 85 u_int32_t m_tag_cookie; /* ABI/Module ID */ 86 void (*m_tag_free)(struct m_tag *); 87 }; 88 89 /* 90 * Record/packet header in first mbuf of chain; valid only if M_PKTHDR is set. 91 */ 92 struct pkthdr { 93 struct ifnet *rcvif; /* rcv interface */ 94 int len; /* total packet length */ 95 /* variables for ip and tcp reassembly */ 96 void *header; /* pointer to packet header */ 97 /* variables for hardware checksum */ 98 int csum_flags; /* flags regarding checksum */ 99 int csum_data; /* data field used by csum routines */ 100 SLIST_HEAD(packet_tags, m_tag) tags; /* list of packet tags */ 101 }; 102 103 /* 104 * Description of external storage mapped into mbuf; valid only if M_EXT is set. 105 */ 106 struct m_ext { 107 caddr_t ext_buf; /* start of buffer */ 108 void (*ext_free) /* free routine if not the usual */ 109 (void *, void *); 110 void *ext_args; /* optional argument pointer */ 111 u_int ext_size; /* size of buffer, for ext_free */ 112 u_int *ref_cnt; /* pointer to ref count info */ 113 int ext_type; /* type of external storage */ 114 }; 115 116 /* 117 * The core of the mbuf object along with some shortcut defines for 118 * practical purposes. 119 */ 120 struct mbuf { 121 struct m_hdr m_hdr; 122 union { 123 struct { 124 struct pkthdr MH_pkthdr; /* M_PKTHDR set */ 125 union { 126 struct m_ext MH_ext; /* M_EXT set */ 127 char MH_databuf[MHLEN]; 128 } MH_dat; 129 } MH; 130 char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */ 131 } M_dat; 132 }; 133 #define m_next m_hdr.mh_next 134 #define m_len m_hdr.mh_len 135 #define m_data m_hdr.mh_data 136 #define m_type m_hdr.mh_type 137 #define m_flags m_hdr.mh_flags 138 #define m_nextpkt m_hdr.mh_nextpkt 139 #define m_act m_nextpkt 140 #define m_pkthdr M_dat.MH.MH_pkthdr 141 #define m_ext M_dat.MH.MH_dat.MH_ext 142 #define m_pktdat M_dat.MH.MH_dat.MH_databuf 143 #define m_dat M_dat.M_databuf 144 145 /* 146 * mbuf flags. 147 */ 148 #define M_EXT 0x0001 /* has associated external storage */ 149 #define M_PKTHDR 0x0002 /* start of record */ 150 #define M_EOR 0x0004 /* end of record */ 151 #define M_RDONLY 0x0008 /* associated data is marked read-only */ 152 #define M_PROTO1 0x0010 /* protocol-specific */ 153 #define M_PROTO2 0x0020 /* protocol-specific */ 154 #define M_PROTO3 0x0040 /* protocol-specific */ 155 #define M_PROTO4 0x0080 /* protocol-specific */ 156 #define M_PROTO5 0x0100 /* protocol-specific */ 157 #define M_PROTO6 0x4000 /* protocol-specific (avoid M_BCAST conflict) */ 158 #define M_FREELIST 0x8000 /* mbuf is on the free list */ 159 160 /* 161 * mbuf pkthdr flags (also stored in m_flags). 162 */ 163 #define M_BCAST 0x0200 /* send/received as link-level broadcast */ 164 #define M_MCAST 0x0400 /* send/received as link-level multicast */ 165 #define M_FRAG 0x0800 /* packet is a fragment of a larger packet */ 166 #define M_FIRSTFRAG 0x1000 /* packet is first fragment */ 167 #define M_LASTFRAG 0x2000 /* packet is last fragment */ 168 169 /* 170 * External buffer types: identify ext_buf type. 171 */ 172 #define EXT_CLUSTER 1 /* mbuf cluster */ 173 #define EXT_SFBUF 2 /* sendfile(2)'s sf_bufs */ 174 #define EXT_NET_DRV 100 /* custom ext_buf provided by net driver(s) */ 175 #define EXT_MOD_TYPE 200 /* custom module's ext_buf type */ 176 #define EXT_DISPOSABLE 300 /* can throw this buffer away w/page flipping */ 177 #define EXT_EXTREF 400 /* has externally maintained ref_cnt ptr*/ 178 179 /* 180 * Flags copied when copying m_pkthdr. 181 */ 182 #define M_COPYFLAGS (M_PKTHDR|M_EOR|M_RDONLY|M_PROTO1|M_PROTO1|M_PROTO2|\ 183 M_PROTO3|M_PROTO4|M_PROTO5|M_PROTO6|\ 184 M_BCAST|M_MCAST|M_FRAG|M_FIRSTFRAG|M_LASTFRAG) 185 186 /* 187 * Flags indicating hw checksum support and sw checksum requirements. 188 */ 189 #define CSUM_IP 0x0001 /* will csum IP */ 190 #define CSUM_TCP 0x0002 /* will csum TCP */ 191 #define CSUM_UDP 0x0004 /* will csum UDP */ 192 #define CSUM_IP_FRAGS 0x0008 /* will csum IP fragments */ 193 #define CSUM_FRAGMENT 0x0010 /* will do IP fragmentation */ 194 195 #define CSUM_IP_CHECKED 0x0100 /* did csum IP */ 196 #define CSUM_IP_VALID 0x0200 /* ... the csum is valid */ 197 #define CSUM_DATA_VALID 0x0400 /* csum_data field is valid */ 198 #define CSUM_PSEUDO_HDR 0x0800 /* csum_data has pseudo hdr */ 199 200 #define CSUM_DELAY_DATA (CSUM_TCP | CSUM_UDP) 201 #define CSUM_DELAY_IP (CSUM_IP) /* XXX add ipv6 here too? */ 202 203 /* 204 * mbuf types. 205 */ 206 #define MT_NOTMBUF 0 /* USED INTERNALLY ONLY! Object is not mbuf */ 207 #define MT_DATA 1 /* dynamic (data) allocation */ 208 #define MT_HEADER 2 /* packet header */ 209 #if 0 210 #define MT_SOCKET 3 /* socket structure */ 211 #define MT_PCB 4 /* protocol control block */ 212 #define MT_RTABLE 5 /* routing tables */ 213 #define MT_HTABLE 6 /* IMP host tables */ 214 #define MT_ATABLE 7 /* address resolution tables */ 215 #endif 216 #define MT_SONAME 8 /* socket name */ 217 #if 0 218 #define MT_SOOPTS 10 /* socket options */ 219 #endif 220 #define MT_FTABLE 11 /* fragment reassembly header */ 221 #if 0 222 #define MT_RIGHTS 12 /* access rights */ 223 #define MT_IFADDR 13 /* interface address */ 224 #endif 225 #define MT_TAG 13 /* volatile metadata associated to pkts */ 226 #define MT_CONTROL 14 /* extra-data protocol message */ 227 #define MT_OOBDATA 15 /* expedited data */ 228 #define MT_NTYPES 16 /* number of mbuf types for mbtypes[] */ 229 230 /* 231 * Mbuf and cluster allocation statistics PCPU structure. 232 */ 233 struct mbpstat { 234 u_long mb_mbfree; 235 u_long mb_mbbucks; 236 u_long mb_clfree; 237 u_long mb_clbucks; 238 long mb_mbtypes[MT_NTYPES]; 239 short mb_active; 240 }; 241 242 /* 243 * General mbuf allocator statistics structure. 244 * XXX: Modifications of these are not protected by any mutex locks nor by 245 * any atomic() manipulations. As a result, we may occasionally lose 246 * a count or two. Luckily, not all of these fields are modified at all 247 * and remain static, and those that are manipulated are only manipulated 248 * in failure situations, which do not occur (hopefully) very often. 249 */ 250 struct mbstat { 251 u_long m_drops; /* times failed to allocate */ 252 u_long m_wait; /* times succesfully returned from wait */ 253 u_long m_drain; /* times drained protocols for space */ 254 u_long m_mcfail; /* XXX: times m_copym failed */ 255 u_long m_mpfail; /* XXX: times m_pullup failed */ 256 u_long m_msize; /* length of an mbuf */ 257 u_long m_mclbytes; /* length of an mbuf cluster */ 258 u_long m_minclsize; /* min length of data to allocate a cluster */ 259 u_long m_mlen; /* length of data in an mbuf */ 260 u_long m_mhlen; /* length of data in a header mbuf */ 261 u_int m_mbperbuck; /* number of mbufs per "bucket" */ 262 u_int m_clperbuck; /* number of clusters per "bucket" */ 263 /* Number of mbtypes (gives # elems in mbpstat's mb_mbtypes[] array: */ 264 short m_numtypes; 265 /* XXX: Sendfile stats should eventually move to their own struct */ 266 u_long sf_iocnt; /* times sendfile had to do disk I/O */ 267 u_long sf_allocfail; /* times sfbuf allocation failed */ 268 u_long sf_allocwait; /* times sfbuf allocation had to wait */ 269 }; 270 271 /* 272 * Flags specifying how an allocation should be made. 273 * M_DONTWAIT means "don't block if nothing is available" whereas 274 * M_TRYWAIT means "block for mbuf_wait ticks at most if nothing is 275 * available." 276 */ 277 #define M_DONTWAIT 0x4 /* don't conflict with M_NOWAIT */ 278 #define M_TRYWAIT 0x8 /* or M_WAITOK */ 279 #define M_WAIT M_TRYWAIT /* XXX: deprecated */ 280 #define MBTOM(how) ((how) & M_TRYWAIT ? M_WAITOK : M_NOWAIT) 281 282 #ifdef _KERNEL 283 /*- 284 * mbuf external reference count management macros. 285 * 286 * MEXT_IS_REF(m): true if (m) is not the only mbuf referencing 287 * the external buffer ext_buf. 288 * 289 * MEXT_REM_REF(m): remove reference to m_ext object. 290 * 291 * MEXT_ADD_REF(m): add reference to m_ext object already 292 * referred to by (m). 293 */ 294 #define MEXT_IS_REF(m) (*((m)->m_ext.ref_cnt) > 1) 295 296 #define MEXT_REM_REF(m) do { \ 297 KASSERT(*((m)->m_ext.ref_cnt) > 0, ("m_ext refcnt < 0")); \ 298 atomic_subtract_int((m)->m_ext.ref_cnt, 1); \ 299 } while(0) 300 301 #define MEXT_ADD_REF(m) atomic_add_int((m)->m_ext.ref_cnt, 1) 302 303 /* 304 * mbuf, cluster, and external object allocation macros 305 * (for compatibility purposes). 306 */ 307 /* NB: M_COPY_PKTHDR is deprecated. Use M_MOVE_PKTHDR or m_dup_pktdr. */ 308 #define M_MOVE_PKTHDR(to, from) m_move_pkthdr((to), (from)) 309 #define m_getclr(how, type) m_get_clrd((how), (type)) 310 #define MGET(m, how, type) ((m) = m_get((how), (type))) 311 #define MGETHDR(m, how, type) ((m) = m_gethdr((how), (type))) 312 #define MCLGET(m, how) m_clget((m), (how)) 313 #define MEXTADD(m, buf, size, free, args, flags, type) \ 314 m_extadd((m), (caddr_t)(buf), (size), (free), (args), (flags), (type)) 315 316 /* 317 * MEXTFREE(m): disassociate (and possibly free) an external object from (m). 318 * 319 * If the atomic_cmpset_int() returns 0, then we effectively do nothing 320 * in terms of "cleaning up" (freeing the ext buf and ref. counter) as 321 * this means that either there are still references, or another thread 322 * is taking care of the clean-up. 323 */ 324 #define MEXTFREE(m) do { \ 325 struct mbuf *_mb = (m); \ 326 \ 327 MEXT_REM_REF(_mb); \ 328 if (atomic_cmpset_int(_mb->m_ext.ref_cnt, 0, 1)) \ 329 _mext_free(_mb); \ 330 _mb->m_flags &= ~M_EXT; \ 331 } while (0) 332 333 /* 334 * Evaluate TRUE if it's safe to write to the mbuf m's data region (this 335 * can be both the local data payload, or an external buffer area, 336 * depending on whether M_EXT is set). 337 */ 338 #define M_WRITABLE(m) (!((m)->m_flags & M_RDONLY) && (!((m)->m_flags \ 339 & M_EXT) || !MEXT_IS_REF(m))) 340 341 /* 342 * Check if the supplied mbuf has a packet header, or else panic. 343 */ 344 #define M_ASSERTPKTHDR(m) \ 345 KASSERT(m != NULL && m->m_flags & M_PKTHDR, \ 346 ("%s: no mbuf packet header!", __func__)) 347 348 /* 349 * Ensure that the supplied mbuf is a valid, non-free mbuf. 350 */ 351 #define M_ASSERTVALID(m) \ 352 KASSERT((((struct mbuf *)m)->m_flags & M_FREELIST) == 0, \ 353 ("%s: attempted use of a free mbuf!", __func__)) 354 355 /* 356 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place 357 * an object of the specified size at the end of the mbuf, longword aligned. 358 */ 359 #define M_ALIGN(m, len) do { \ 360 (m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1); \ 361 } while (0) 362 363 /* 364 * As above, for mbufs allocated with m_gethdr/MGETHDR 365 * or initialized by M_COPY_PKTHDR. 366 */ 367 #define MH_ALIGN(m, len) do { \ 368 (m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1); \ 369 } while (0) 370 371 /* 372 * Compute the amount of space available 373 * before the current start of data in an mbuf. 374 * 375 * The M_WRITABLE() is a temporary, conservative safety measure: the burden 376 * of checking writability of the mbuf data area rests solely with the caller. 377 */ 378 #define M_LEADINGSPACE(m) \ 379 ((m)->m_flags & M_EXT ? \ 380 (M_WRITABLE(m) ? (m)->m_data - (m)->m_ext.ext_buf : 0): \ 381 (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \ 382 (m)->m_data - (m)->m_dat) 383 384 /* 385 * Compute the amount of space available 386 * after the end of data in an mbuf. 387 * 388 * The M_WRITABLE() is a temporary, conservative safety measure: the burden 389 * of checking writability of the mbuf data area rests solely with the caller. 390 */ 391 #define M_TRAILINGSPACE(m) \ 392 ((m)->m_flags & M_EXT ? \ 393 (M_WRITABLE(m) ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size \ 394 - ((m)->m_data + (m)->m_len) : 0) : \ 395 &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len)) 396 397 /* 398 * Arrange to prepend space of size plen to mbuf m. 399 * If a new mbuf must be allocated, how specifies whether to wait. 400 * If the allocation fails, the original mbuf chain is freed and m is 401 * set to NULL. 402 */ 403 #define M_PREPEND(m, plen, how) do { \ 404 struct mbuf **_mmp = &(m); \ 405 struct mbuf *_mm = *_mmp; \ 406 int _mplen = (plen); \ 407 int __mhow = (how); \ 408 \ 409 if (M_LEADINGSPACE(_mm) >= _mplen) { \ 410 _mm->m_data -= _mplen; \ 411 _mm->m_len += _mplen; \ 412 } else \ 413 _mm = m_prepend(_mm, _mplen, __mhow); \ 414 if (_mm != NULL && _mm->m_flags & M_PKTHDR) \ 415 _mm->m_pkthdr.len += _mplen; \ 416 *_mmp = _mm; \ 417 } while (0) 418 419 /* 420 * Change mbuf to new type. 421 * This is a relatively expensive operation and should be avoided. 422 */ 423 #define MCHTYPE(m, t) m_chtype((m), (t)) 424 425 /* Length to m_copy to copy all. */ 426 #define M_COPYALL 1000000000 427 428 /* Compatibility with 4.3. */ 429 #define m_copy(m, o, l) m_copym((m), (o), (l), M_DONTWAIT) 430 431 extern int max_datalen; /* MHLEN - max_hdr */ 432 extern int max_hdr; /* Largest link + protocol header */ 433 extern int max_linkhdr; /* Largest link-level header */ 434 extern int max_protohdr; /* Largest protocol header */ 435 extern struct mbstat mbstat; /* General mbuf stats/infos */ 436 extern int nmbclusters; /* Maximum number of clusters */ 437 extern int nmbcnt; /* Scale kmem_map for counter space */ 438 extern int nmbufs; /* Maximum number of mbufs */ 439 440 void _mext_free(struct mbuf *); 441 void m_adj(struct mbuf *, int); 442 int m_apply(struct mbuf *, int, int, 443 int (*)(void *, void *, unsigned int), void *); 444 void m_cat(struct mbuf *, struct mbuf *); 445 void m_chtype(struct mbuf *, short); 446 void m_clget(struct mbuf *, int); 447 void m_extadd(struct mbuf *, caddr_t, u_int, 448 void (*)(void *, void *), void *, int, int); 449 void m_copyback(struct mbuf *, int, int, caddr_t); 450 void m_copydata(const struct mbuf *, int, int, caddr_t); 451 struct mbuf *m_copym(struct mbuf *, int, int, int); 452 struct mbuf *m_copypacket(struct mbuf *, int); 453 void m_copy_pkthdr(struct mbuf *, struct mbuf *); 454 struct mbuf *m_defrag(struct mbuf *, int); 455 struct mbuf *m_devget(char *, int, int, struct ifnet *, 456 void (*)(char *, caddr_t, u_int)); 457 struct mbuf *m_dup(struct mbuf *, int); 458 int m_dup_pkthdr(struct mbuf *, struct mbuf *, int); 459 u_int m_fixhdr(struct mbuf *); 460 struct mbuf *m_fragment(struct mbuf *, int, int); 461 struct mbuf *m_free(struct mbuf *); 462 void m_freem(struct mbuf *); 463 struct mbuf *m_get(int, short); 464 struct mbuf *m_get_clrd(int, short); 465 struct mbuf *m_getcl(int, short, int); 466 struct mbuf *m_gethdr(int, short); 467 struct mbuf *m_gethdr_clrd(int, short); 468 struct mbuf *m_getm(struct mbuf *, int, int, short); 469 struct mbuf *m_getptr(struct mbuf *, int, int *); 470 u_int m_length(struct mbuf *, struct mbuf **); 471 void m_move_pkthdr(struct mbuf *, struct mbuf *); 472 struct mbuf *m_prepend(struct mbuf *, int, int); 473 void m_print(const struct mbuf *); 474 struct mbuf *m_pulldown(struct mbuf *, int, int, int *); 475 struct mbuf *m_pullup(struct mbuf *, int); 476 struct mbuf *m_split(struct mbuf *, int, int); 477 478 /* 479 * Packets may have annotations attached by affixing a list 480 * of "packet tags" to the pkthdr structure. Packet tags are 481 * dynamically allocated semi-opaque data structures that have 482 * a fixed header (struct m_tag) that specifies the size of the 483 * memory block and a <cookie,type> pair that identifies it. 484 * The cookie is a 32-bit unique unsigned value used to identify 485 * a module or ABI. By convention this value is chose as the 486 * date+time that the module is created, expressed as the number of 487 * seconds since the epoch (e.g., using date -u +'%s'). The type value 488 * is an ABI/module-specific value that identifies a particular annotation 489 * and is private to the module. For compatibility with systems 490 * like OpenBSD that define packet tags w/o an ABI/module cookie, 491 * the value PACKET_ABI_COMPAT is used to implement m_tag_get and 492 * m_tag_find compatibility shim functions and several tag types are 493 * defined below. Users that do not require compatibility should use 494 * a private cookie value so that packet tag-related definitions 495 * can be maintained privately. 496 * 497 * Note that the packet tag returned by m_tag_allocate has the default 498 * memory alignment implemented by malloc. To reference private data 499 * one can use a construct like: 500 * 501 * struct m_tag *mtag = m_tag_allocate(...); 502 * struct foo *p = (struct foo *)(mtag+1); 503 * 504 * if the alignment of struct m_tag is sufficient for referencing members 505 * of struct foo. Otherwise it is necessary to embed struct m_tag within 506 * the private data structure to insure proper alignment; e.g., 507 * 508 * struct foo { 509 * struct m_tag tag; 510 * ... 511 * }; 512 * struct foo *p = (struct foo *) m_tag_allocate(...); 513 * struct m_tag *mtag = &p->tag; 514 */ 515 516 /* 517 * Persistent tags stay with an mbuf until the mbuf is reclaimed. 518 * Otherwise tags are expected to ``vanish'' when they pass through 519 * a network interface. For most interfaces this happens normally 520 * as the tags are reclaimed when the mbuf is free'd. However in 521 * some special cases reclaiming must be done manually. An example 522 * is packets that pass through the loopback interface. Also, one 523 * must be careful to do this when ``turning around'' packets (e.g., 524 * icmp_reflect). 525 * 526 * To mark a tag persistent bit-or this flag in when defining the 527 * tag id. The tag will then be treated as described above. 528 */ 529 #define MTAG_PERSISTENT 0x800 530 531 #define PACKET_TAG_NONE 0 /* Nadda */ 532 533 /* Packet tag for use with PACKET_ABI_COMPAT. */ 534 #define PACKET_TAG_IPSEC_IN_DONE 1 /* IPsec applied, in */ 535 #define PACKET_TAG_IPSEC_OUT_DONE 2 /* IPsec applied, out */ 536 #define PACKET_TAG_IPSEC_IN_CRYPTO_DONE 3 /* NIC IPsec crypto done */ 537 #define PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED 4 /* NIC IPsec crypto req'ed */ 538 #define PACKET_TAG_IPSEC_IN_COULD_DO_CRYPTO 5 /* NIC notifies IPsec */ 539 #define PACKET_TAG_IPSEC_PENDING_TDB 6 /* Reminder to do IPsec */ 540 #define PACKET_TAG_BRIDGE 7 /* Bridge processing done */ 541 #define PACKET_TAG_GIF 8 /* GIF processing done */ 542 #define PACKET_TAG_GRE 9 /* GRE processing done */ 543 #define PACKET_TAG_IN_PACKET_CHECKSUM 10 /* NIC checksumming done */ 544 #define PACKET_TAG_ENCAP 11 /* Encap. processing */ 545 #define PACKET_TAG_IPSEC_SOCKET 12 /* IPSEC socket ref */ 546 #define PACKET_TAG_IPSEC_HISTORY 13 /* IPSEC history */ 547 #define PACKET_TAG_IPV6_INPUT 14 /* IPV6 input processing */ 548 549 /* 550 * As a temporary and low impact solution to replace the even uglier 551 * approach used so far in some parts of the network stack (which relies 552 * on global variables), packet tag-like annotations are stored in MT_TAG 553 * mbufs (or lookalikes) prepended to the actual mbuf chain. 554 * 555 * m_type = MT_TAG 556 * m_flags = m_tag_id 557 * m_next = next buffer in chain. 558 * 559 * BE VERY CAREFUL not to pass these blocks to the mbuf handling routines. 560 */ 561 #define _m_tag_id m_hdr.mh_flags 562 563 /* Packet tags used in the FreeBSD network stack. */ 564 #define PACKET_TAG_DUMMYNET 15 /* dummynet info */ 565 #define PACKET_TAG_IPFW 16 /* ipfw classification */ 566 #define PACKET_TAG_DIVERT 17 /* divert info */ 567 #define PACKET_TAG_IPFORWARD 18 /* ipforward info */ 568 #define PACKET_TAG_MACLABEL (19 | MTAG_PERSISTENT) /* MAC label */ 569 #define PACKET_TAG_IPFASTFWD_OURS 20 /* IP fastforward dropback */ 570 571 /* Packet tag routines. */ 572 struct m_tag *m_tag_alloc(u_int32_t, int, int, int); 573 void m_tag_delete(struct mbuf *, struct m_tag *); 574 void m_tag_delete_chain(struct mbuf *, struct m_tag *); 575 struct m_tag *m_tag_locate(struct mbuf *, u_int32_t, int, struct m_tag *); 576 struct m_tag *m_tag_copy(struct m_tag *, int); 577 int m_tag_copy_chain(struct mbuf *, struct mbuf *, int); 578 void m_tag_delete_nonpersistent(struct mbuf *); 579 580 /* 581 * Initialize the list of tags associated with an mbuf. 582 */ 583 static __inline void 584 m_tag_init(struct mbuf *m) 585 { 586 SLIST_INIT(&m->m_pkthdr.tags); 587 } 588 589 /* 590 * Setup the contents of a tag. Note that this does not 591 * fillin the free method; the caller is expected to do that. 592 * 593 * XXX probably should be called m_tag_init; but that was 594 * already taken. 595 */ 596 static __inline void 597 m_tag_setup(struct m_tag *t, u_int32_t cookie, int type, int len) 598 { 599 t->m_tag_id = type; 600 t->m_tag_len = len; 601 t->m_tag_cookie = cookie; 602 } 603 604 /* 605 * Reclaim resources associated with a tag. 606 */ 607 static __inline void 608 m_tag_free(struct m_tag *t) 609 { 610 (*t->m_tag_free)(t); 611 } 612 613 /* 614 * Return the first tag associated with an mbuf. 615 */ 616 static __inline struct m_tag * 617 m_tag_first(struct mbuf *m) 618 { 619 return SLIST_FIRST(&m->m_pkthdr.tags); 620 } 621 622 /* 623 * Return the next tag in the list of tags associated with an mbuf. 624 */ 625 static __inline struct m_tag * 626 m_tag_next(struct mbuf *m, struct m_tag *t) 627 { 628 return SLIST_NEXT(t, m_tag_link); 629 } 630 631 /* 632 * Prepend a tag to the list of tags associated with an mbuf. 633 */ 634 static __inline void 635 m_tag_prepend(struct mbuf *m, struct m_tag *t) 636 { 637 SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link); 638 } 639 640 /* 641 * Unlink a tag from the list of tags associated with an mbuf. 642 */ 643 static __inline void 644 m_tag_unlink(struct mbuf *m, struct m_tag *t) 645 { 646 SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link); 647 } 648 649 /* These are for OpenBSD compatibility. */ 650 #define MTAG_ABI_COMPAT 0 /* compatibility ABI */ 651 652 static __inline struct m_tag * 653 m_tag_get(int type, int length, int wait) 654 { 655 return m_tag_alloc(MTAG_ABI_COMPAT, type, length, wait); 656 } 657 658 static __inline struct m_tag * 659 m_tag_find(struct mbuf *m, int type, struct m_tag *start) 660 { 661 return SLIST_EMPTY(&m->m_pkthdr.tags) ? 662 NULL : m_tag_locate(m, MTAG_ABI_COMPAT, type, start); 663 } 664 #endif /* _KERNEL */ 665 666 #endif /* !_SYS_MBUF_H_ */ 667