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 /* 41 * XXXMAC: Possibly this recursive include is a bad idea, but a lot 42 * of code exists that assumes it is sufficient to include just mbuf.h 43 */ 44 #include <sys/mac.h> 45 46 /* 47 * Mbufs are of a single size, MSIZE (machine/param.h), which 48 * includes overhead. An mbuf may add a single "mbuf cluster" of size 49 * MCLBYTES (also in machine/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 #endif /* _KERNEL */ 69 70 /* 71 * Header present at the beginning of every mbuf. 72 */ 73 struct m_hdr { 74 struct mbuf *mh_next; /* next buffer in chain */ 75 struct mbuf *mh_nextpkt; /* next chain in queue/record */ 76 caddr_t mh_data; /* location of data */ 77 int mh_len; /* amount of data in this mbuf */ 78 short mh_type; /* type of data in this mbuf */ 79 short mh_flags; /* flags; see below */ 80 }; 81 82 /* 83 * Record/packet header in first mbuf of chain; valid only if M_PKTHDR is set. 84 */ 85 struct pkthdr { 86 struct ifnet *rcvif; /* rcv interface */ 87 int len; /* total packet length */ 88 /* variables for ip and tcp reassembly */ 89 void *header; /* pointer to packet header */ 90 /* variables for hardware checksum */ 91 int csum_flags; /* flags regarding checksum */ 92 int csum_data; /* data field used by csum routines */ 93 struct mbuf *aux; /* extra data buffer; ipsec/others */ 94 struct label label; /* MAC label of data in packet */ 95 }; 96 97 /* 98 * Description of external storage mapped into mbuf; valid only if M_EXT is set. 99 */ 100 struct m_ext { 101 caddr_t ext_buf; /* start of buffer */ 102 void (*ext_free) /* free routine if not the usual */ 103 (void *, void *); 104 void *ext_args; /* optional argument pointer */ 105 u_int ext_size; /* size of buffer, for ext_free */ 106 u_int *ref_cnt; /* pointer to ref count info */ 107 int ext_type; /* type of external storage */ 108 }; 109 110 /* 111 * The core of the mbuf object along with some shortcut defines for 112 * practical purposes. 113 */ 114 struct mbuf { 115 struct m_hdr m_hdr; 116 union { 117 struct { 118 struct pkthdr MH_pkthdr; /* M_PKTHDR set */ 119 union { 120 struct m_ext MH_ext; /* M_EXT set */ 121 char MH_databuf[MHLEN]; 122 } MH_dat; 123 } MH; 124 char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */ 125 } M_dat; 126 }; 127 #define m_next m_hdr.mh_next 128 #define m_len m_hdr.mh_len 129 #define m_data m_hdr.mh_data 130 #define m_type m_hdr.mh_type 131 #define m_flags m_hdr.mh_flags 132 #define m_nextpkt m_hdr.mh_nextpkt 133 #define m_act m_nextpkt 134 #define m_pkthdr M_dat.MH.MH_pkthdr 135 #define m_ext M_dat.MH.MH_dat.MH_ext 136 #define m_pktdat M_dat.MH.MH_dat.MH_databuf 137 #define m_dat M_dat.M_databuf 138 139 /* 140 * mbuf flags. 141 */ 142 #define M_EXT 0x0001 /* has associated external storage */ 143 #define M_PKTHDR 0x0002 /* start of record */ 144 #define M_EOR 0x0004 /* end of record */ 145 #define M_RDONLY 0x0008 /* associated data is marked read-only */ 146 #define M_PROTO1 0x0010 /* protocol-specific */ 147 #define M_PROTO2 0x0020 /* protocol-specific */ 148 #define M_PROTO3 0x0040 /* protocol-specific */ 149 #define M_PROTO4 0x0080 /* protocol-specific */ 150 #define M_PROTO5 0x0100 /* protocol-specific */ 151 152 /* 153 * mbuf pkthdr flags (also stored in m_flags). 154 */ 155 #define M_BCAST 0x0200 /* send/received as link-level broadcast */ 156 #define M_MCAST 0x0400 /* send/received as link-level multicast */ 157 #define M_FRAG 0x0800 /* packet is a fragment of a larger packet */ 158 #define M_FIRSTFRAG 0x1000 /* packet is first fragment */ 159 #define M_LASTFRAG 0x2000 /* packet is last fragment */ 160 161 /* 162 * External buffer types: identify ext_buf type. 163 */ 164 #define EXT_CLUSTER 1 /* mbuf cluster */ 165 #define EXT_SFBUF 2 /* sendfile(2)'s sf_bufs */ 166 #define EXT_NET_DRV 100 /* custom ext_buf provided by net driver(s) */ 167 #define EXT_MOD_TYPE 200 /* custom module's ext_buf type */ 168 #define EXT_DISPOSABLE 300 /* can throw this buffer away w/page flipping */ 169 170 /* 171 * Flags copied when copying m_pkthdr. 172 */ 173 #define M_COPYFLAGS (M_PKTHDR|M_EOR|M_PROTO1|M_PROTO1|M_PROTO2|M_PROTO3 | \ 174 M_PROTO4|M_PROTO5|M_BCAST|M_MCAST|M_FRAG|M_RDONLY) 175 176 /* 177 * Flags indicating hw checksum support and sw checksum requirements. 178 */ 179 #define CSUM_IP 0x0001 /* will csum IP */ 180 #define CSUM_TCP 0x0002 /* will csum TCP */ 181 #define CSUM_UDP 0x0004 /* will csum UDP */ 182 #define CSUM_IP_FRAGS 0x0008 /* will csum IP fragments */ 183 #define CSUM_FRAGMENT 0x0010 /* will do IP fragmentation */ 184 185 #define CSUM_IP_CHECKED 0x0100 /* did csum IP */ 186 #define CSUM_IP_VALID 0x0200 /* ... the csum is valid */ 187 #define CSUM_DATA_VALID 0x0400 /* csum_data field is valid */ 188 #define CSUM_PSEUDO_HDR 0x0800 /* csum_data has pseudo hdr */ 189 190 #define CSUM_DELAY_DATA (CSUM_TCP | CSUM_UDP) 191 #define CSUM_DELAY_IP (CSUM_IP) /* XXX add ipv6 here too? */ 192 193 /* 194 * mbuf types. 195 */ 196 #define MT_NOTMBUF 0 /* USED INTERNALLY ONLY! Object is not mbuf */ 197 #define MT_DATA 1 /* dynamic (data) allocation */ 198 #define MT_HEADER 2 /* packet header */ 199 #if 0 200 #define MT_SOCKET 3 /* socket structure */ 201 #define MT_PCB 4 /* protocol control block */ 202 #define MT_RTABLE 5 /* routing tables */ 203 #define MT_HTABLE 6 /* IMP host tables */ 204 #define MT_ATABLE 7 /* address resolution tables */ 205 #endif 206 #define MT_SONAME 8 /* socket name */ 207 #if 0 208 #define MT_SOOPTS 10 /* socket options */ 209 #endif 210 #define MT_FTABLE 11 /* fragment reassembly header */ 211 #if 0 212 #define MT_RIGHTS 12 /* access rights */ 213 #define MT_IFADDR 13 /* interface address */ 214 #endif 215 #define MT_TAG 13 /* volatile metadata associated to pkts */ 216 #define MT_CONTROL 14 /* extra-data protocol message */ 217 #define MT_OOBDATA 15 /* expedited data */ 218 #define MT_NTYPES 16 /* number of mbuf types for mbtypes[] */ 219 220 /* 221 * Mbuf and cluster allocation statistics PCPU structure. 222 */ 223 struct mbpstat { 224 u_long mb_mbfree; 225 u_long mb_mbpgs; 226 u_long mb_clfree; 227 u_long mb_clpgs; 228 long mb_mbtypes[MT_NTYPES]; 229 short mb_active; 230 }; 231 232 /* 233 * General mbuf allocator statistics structure. 234 * XXX: Modifications of these are not protected by any mutex locks nor by 235 * any atomic() manipulations. As a result, we may occasionally lose 236 * a count or two. Luckily, not all of these fields are modified at all 237 * and remain static, and those that are manipulated are only manipulated 238 * in failure situations, which do not occur (hopefully) very often. 239 */ 240 struct mbstat { 241 u_long m_drops; /* times failed to allocate */ 242 u_long m_wait; /* times succesfully returned from wait */ 243 u_long m_drain; /* times drained protocols for space */ 244 u_long m_mcfail; /* XXX: times m_copym failed */ 245 u_long m_mpfail; /* XXX: times m_pullup failed */ 246 u_long m_msize; /* length of an mbuf */ 247 u_long m_mclbytes; /* length of an mbuf cluster */ 248 u_long m_minclsize; /* min length of data to allocate a cluster */ 249 u_long m_mlen; /* length of data in an mbuf */ 250 u_long m_mhlen; /* length of data in a header mbuf */ 251 /* Number of mbtypes (gives # elems in mbpstat's mb_mbtypes[] array: */ 252 short m_numtypes; 253 }; 254 255 /* 256 * Flags specifying how an allocation should be made. 257 * M_DONTWAIT means "don't block if nothing is available" whereas 258 * M_TRYWAIT means "block for mbuf_wait ticks at most if nothing is 259 * available." 260 */ 261 #define M_DONTWAIT 1 262 #define M_TRYWAIT 0 263 #define M_WAIT M_TRYWAIT /* XXX: Deprecated. */ 264 265 #ifdef _KERNEL 266 /*- 267 * mbuf external reference count management macros. 268 * 269 * MEXT_IS_REF(m): true if (m) is not the only mbuf referencing 270 * the external buffer ext_buf. 271 * 272 * MEXT_REM_REF(m): remove reference to m_ext object. 273 * 274 * MEXT_ADD_REF(m): add reference to m_ext object already 275 * referred to by (m). 276 */ 277 #define MEXT_IS_REF(m) (*((m)->m_ext.ref_cnt) > 1) 278 279 #define MEXT_REM_REF(m) do { \ 280 KASSERT(*((m)->m_ext.ref_cnt) > 0, ("m_ext refcnt < 0")); \ 281 atomic_subtract_int((m)->m_ext.ref_cnt, 1); \ 282 } while(0) 283 284 #define MEXT_ADD_REF(m) atomic_add_int((m)->m_ext.ref_cnt, 1) 285 286 /* 287 * mbuf, cluster, and external object allocation macros 288 * (for compatibility purposes). 289 */ 290 #define M_COPY_PKTHDR(to, from) m_copy_pkthdr(to, from) 291 #define m_getclr m_get_clrd 292 #define MGET(m, how, type) (m) = m_get((how), (type)) 293 #define MGETHDR(m, how, type) (m) = m_gethdr((how), (type)) 294 #define MCLGET(m, how) m_clget((m), (how)) 295 #define MEXTADD(m, buf, size, free, args, flags, type) \ 296 m_extadd((m), (caddr_t)(buf), (size), (free), (args), (flags), (type)) 297 298 /* 299 * MEXTFREE(m): disassociate (and possibly free) an external object from (m). 300 * 301 * If the atomic_cmpset_int() returns 0, then we effectively do nothing 302 * in terms of "cleaning up" (freeing the ext buf and ref. counter) as 303 * this means that either there are still references, or another thread 304 * is taking care of the clean-up. 305 */ 306 #define MEXTFREE(m) do { \ 307 struct mbuf *_mb = (m); \ 308 \ 309 MEXT_REM_REF(_mb); \ 310 if (atomic_cmpset_int(_mb->m_ext.ref_cnt, 0, 1)) \ 311 _mext_free(_mb); \ 312 _mb->m_flags &= ~M_EXT; \ 313 } while (0) 314 315 /* 316 * Evaluate TRUE if it's safe to write to the mbuf m's data region (this 317 * can be both the local data payload, or an external buffer area, 318 * depending on whether M_EXT is set). 319 */ 320 #define M_WRITABLE(m) (!((m)->m_flags & M_RDONLY) && (!((m)->m_flags \ 321 & M_EXT) || !MEXT_IS_REF(m))) 322 323 /* 324 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place 325 * an object of the specified size at the end of the mbuf, longword aligned. 326 */ 327 #define M_ALIGN(m, len) do { \ 328 (m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1); \ 329 } while (0) 330 331 /* 332 * As above, for mbufs allocated with m_gethdr/MGETHDR 333 * or initialized by M_COPY_PKTHDR. 334 */ 335 #define MH_ALIGN(m, len) do { \ 336 (m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1); \ 337 } while (0) 338 339 /* 340 * Compute the amount of space available 341 * before the current start of data in an mbuf. 342 * 343 * The M_WRITABLE() is a temporary, conservative safety measure: the burden 344 * of checking writability of the mbuf data area rests solely with the caller. 345 */ 346 #define M_LEADINGSPACE(m) \ 347 ((m)->m_flags & M_EXT ? \ 348 (M_WRITABLE(m) ? (m)->m_data - (m)->m_ext.ext_buf : 0): \ 349 (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \ 350 (m)->m_data - (m)->m_dat) 351 352 /* 353 * Compute the amount of space available 354 * after the end of data in an mbuf. 355 * 356 * The M_WRITABLE() is a temporary, conservative safety measure: the burden 357 * of checking writability of the mbuf data area rests solely with the caller. 358 */ 359 #define M_TRAILINGSPACE(m) \ 360 ((m)->m_flags & M_EXT ? \ 361 (M_WRITABLE(m) ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size \ 362 - ((m)->m_data + (m)->m_len) : 0) : \ 363 &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len)) 364 365 /* 366 * Arrange to prepend space of size plen to mbuf m. 367 * If a new mbuf must be allocated, how specifies whether to wait. 368 * If the allocation fails, the original mbuf chain is freed and m is 369 * set to NULL. 370 */ 371 #define M_PREPEND(m, plen, how) do { \ 372 struct mbuf **_mmp = &(m); \ 373 struct mbuf *_mm = *_mmp; \ 374 int _mplen = (plen); \ 375 int __mhow = (how); \ 376 \ 377 if (M_LEADINGSPACE(_mm) >= _mplen) { \ 378 _mm->m_data -= _mplen; \ 379 _mm->m_len += _mplen; \ 380 } else \ 381 _mm = m_prepend(_mm, _mplen, __mhow); \ 382 if (_mm != NULL && _mm->m_flags & M_PKTHDR) \ 383 _mm->m_pkthdr.len += _mplen; \ 384 *_mmp = _mm; \ 385 } while (0) 386 387 /* 388 * Change mbuf to new type. 389 * This is a relatively expensive operation and should be avoided. 390 */ 391 #define MCHTYPE(m, t) m_chtype((m), (t)) 392 393 /* Length to m_copy to copy all. */ 394 #define M_COPYALL 1000000000 395 396 /* Compatibility with 4.3 */ 397 #define m_copy(m, o, l) m_copym((m), (o), (l), M_DONTWAIT) 398 399 /* 400 * pkthdr.aux type tags. 401 */ 402 struct mauxtag { 403 int af; 404 int type; 405 void *p; 406 }; 407 408 /* 409 * Some packet tags to identify different mbuf annotations. 410 * 411 * Eventually, these annotations will end up in an appropriate chain 412 * (struct m_tag or similar, e.g. as in NetBSD) properly managed by 413 * the mbuf handling routines. 414 * 415 * As a temporary and low impact solution to replace the even uglier 416 * approach used so far in some parts of the network stack (which relies 417 * on global variables), these annotations are stored in MT_TAG 418 * mbufs (or lookalikes) prepended to the actual mbuf chain. 419 * 420 * m_type = MT_TAG 421 * m_flags = m_tag_id 422 * m_next = next buffer in chain. 423 * 424 * BE VERY CAREFUL not to pass these blocks to the mbuf handling routines. 425 * 426 */ 427 428 #define m_tag_id m_hdr.mh_flags 429 430 /* Packet tag types -- first ones are from NetBSD */ 431 432 #define PACKET_TAG_NONE 0 /* Nadda */ 433 #define PACKET_TAG_IPSEC_IN_DONE 1 /* IPsec applied, in */ 434 #define PACKET_TAG_IPSEC_OUT_DONE 2 /* IPsec applied, out */ 435 #define PACKET_TAG_IPSEC_IN_CRYPTO_DONE 3 /* NIC IPsec crypto done */ 436 #define PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED 4 /* NIC IPsec crypto req'ed */ 437 #define PACKET_TAG_IPSEC_IN_COULD_DO_CRYPTO 5 /* NIC notifies IPsec */ 438 #define PACKET_TAG_IPSEC_PENDING_TDB 6 /* Reminder to do IPsec */ 439 #define PACKET_TAG_BRIDGE 7 /* Bridge processing done */ 440 #define PACKET_TAG_GIF 8 /* GIF processing done */ 441 #define PACKET_TAG_GRE 9 /* GRE processing done */ 442 #define PACKET_TAG_IN_PACKET_CHECKSUM 10 /* NIC checksumming done */ 443 #define PACKET_TAG_ENCAP 11 /* Encap. processing */ 444 #define PACKET_TAG_IPSEC_SOCKET 12 /* IPSEC socket ref */ 445 #define PACKET_TAG_IPSEC_HISTORY 13 /* IPSEC history */ 446 #define PACKET_TAG_IPV6_INPUT 14 /* IPV6 input processing */ 447 448 /* Packet tags used in the FreeBSD network stack */ 449 #define PACKET_TAG_DUMMYNET 15 /* dummynet info */ 450 #define PACKET_TAG_IPFW 16 /* ipfw classification */ 451 #define PACKET_TAG_DIVERT 17 /* divert info */ 452 #define PACKET_TAG_IPFORWARD 18 /* ipforward info */ 453 454 #define PACKET_TAG_MAX 19 455 456 extern int max_datalen; /* MHLEN - max_hdr */ 457 extern int max_hdr; /* largest link + protocol header */ 458 extern int max_linkhdr; /* largest link-level header */ 459 extern int max_protohdr; /* largest protocol header */ 460 extern struct mbpstat mb_statpcpu[]; /* Per-CPU allocation stats. */ 461 extern struct mbstat mbstat; /* General mbuf stats/infos. */ 462 extern int nmbclusters; /* Maximum number of clusters */ 463 extern int nmbcnt; /* Scale kmem_map for counter space */ 464 extern int nmbufs; /* Maximum number of mbufs */ 465 extern int nsfbufs; /* Number of sendfile(2) bufs */ 466 467 void _mext_free(struct mbuf *); 468 void m_adj(struct mbuf *, int); 469 struct mbuf *m_aux_add(struct mbuf *, int, int); 470 struct mbuf *m_aux_add2(struct mbuf *, int, int, void *); 471 void m_aux_delete(struct mbuf *, struct mbuf *); 472 struct mbuf *m_aux_find(struct mbuf *, int, int); 473 struct mbuf *m_aux_find2(struct mbuf *, int, int, void *); 474 void m_cat(struct mbuf *, struct mbuf *); 475 void m_chtype(struct mbuf *, short); 476 void m_clget(struct mbuf *, int); 477 void m_extadd(struct mbuf *, caddr_t, u_int, 478 void (*free)(void *, void *), void *, short, int); 479 void m_copyback(struct mbuf *, int, int, caddr_t); 480 void m_copydata(const struct mbuf *, int, int, caddr_t); 481 struct mbuf *m_copym(struct mbuf *, int, int, int); 482 struct mbuf *m_copypacket(struct mbuf *, int); 483 void m_copy_pkthdr(struct mbuf *to, struct mbuf *from); 484 struct mbuf *m_devget(char *, int, int, struct ifnet *, 485 void (*copy)(char *, caddr_t, u_int)); 486 struct mbuf *m_dup(struct mbuf *, int); 487 struct mbuf *m_free(struct mbuf *); 488 void m_freem(struct mbuf *); 489 struct mbuf *m_get(int, short); 490 struct mbuf *m_get_clrd(int, short); 491 struct mbuf *m_getcl(int, short, int); 492 struct mbuf *m_gethdr(int, short); 493 struct mbuf *m_gethdr_clrd(int, short); 494 struct mbuf *m_getm(struct mbuf *, int, int, short); 495 struct mbuf *m_prepend(struct mbuf *, int, int); 496 void m_print(const struct mbuf *m); 497 struct mbuf *m_pulldown(struct mbuf *, int, int, int *); 498 struct mbuf *m_pullup(struct mbuf *, int); 499 struct mbuf *m_split(struct mbuf *, int, int); 500 #endif /* _KERNEL */ 501 502 #endif /* !_SYS_MBUF_H_ */ 503