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 * Mbufs are of a single size, MSIZE (machine/machparam.h), which 42 * includes overhead. An mbuf may add a single "mbuf cluster" of size 43 * MCLBYTES (also in machine/machparam.h), which has no additional overhead 44 * and is used instead of the internal data area; this is done when 45 * at least MINCLSIZE of data must be stored. 46 */ 47 48 #define MLEN (MSIZE - sizeof(struct m_hdr)) /* normal data len */ 49 #define MHLEN (MLEN - sizeof(struct pkthdr)) /* data len w/pkthdr */ 50 51 #define MINCLSIZE (MHLEN + 1) /* smallest amount to put in cluster */ 52 #define M_MAXCOMPRESS (MHLEN / 2) /* max amount to copy for compression */ 53 54 /* 55 * Macros for type conversion 56 * mtod(m,t) - convert mbuf pointer to data pointer of correct type 57 * dtom(x) - convert data pointer within mbuf to mbuf pointer (XXX) 58 * mtocl(x) - convert pointer within cluster to cluster index # 59 * cltom(x) - convert cluster # to ptr to beginning of cluster 60 */ 61 #define mtod(m,t) ((t)((m)->m_data)) 62 #define dtom(x) ((struct mbuf *)((intptr_t)(x) & ~(MSIZE-1))) 63 #define mtocl(x) (((uintptr_t)(x) - (uintptr_t)mbutl) >> MCLSHIFT) 64 #define cltom(x) ((caddr_t)((uintptr_t)mbutl + \ 65 ((uintptr_t)(x) << MCLSHIFT))) 66 67 /* header at beginning of each mbuf: */ 68 struct m_hdr { 69 struct mbuf *mh_next; /* next buffer in chain */ 70 struct mbuf *mh_nextpkt; /* next chain in queue/record */ 71 caddr_t mh_data; /* location of data */ 72 int mh_len; /* amount of data in this mbuf */ 73 short mh_type; /* type of data in this mbuf */ 74 short mh_flags; /* flags; see below */ 75 }; 76 77 /* record/packet header in first mbuf of chain; valid if M_PKTHDR set */ 78 struct pkthdr { 79 struct ifnet *rcvif; /* rcv interface */ 80 int len; /* total packet length */ 81 82 /* variables for ip and tcp reassembly */ 83 void *header; /* pointer to packet header */ 84 }; 85 86 /* description of external storage mapped into mbuf, valid if M_EXT set */ 87 struct m_ext { 88 caddr_t ext_buf; /* start of buffer */ 89 void (*ext_free) /* free routine if not the usual */ 90 __P((caddr_t, u_int)); 91 u_int ext_size; /* size of buffer, for ext_free */ 92 void (*ext_ref) /* add a reference to the ext object */ 93 __P((caddr_t, u_int)); 94 }; 95 96 struct mbuf { 97 struct m_hdr m_hdr; 98 union { 99 struct { 100 struct pkthdr MH_pkthdr; /* M_PKTHDR set */ 101 union { 102 struct m_ext MH_ext; /* M_EXT set */ 103 char MH_databuf[MHLEN]; 104 } MH_dat; 105 } MH; 106 char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */ 107 } M_dat; 108 }; 109 #define m_next m_hdr.mh_next 110 #define m_len m_hdr.mh_len 111 #define m_data m_hdr.mh_data 112 #define m_type m_hdr.mh_type 113 #define m_flags m_hdr.mh_flags 114 #define m_nextpkt m_hdr.mh_nextpkt 115 #define m_act m_nextpkt 116 #define m_pkthdr M_dat.MH.MH_pkthdr 117 #define m_ext M_dat.MH.MH_dat.MH_ext 118 #define m_pktdat M_dat.MH.MH_dat.MH_databuf 119 #define m_dat M_dat.M_databuf 120 121 /* mbuf flags */ 122 #define M_EXT 0x0001 /* has associated external storage */ 123 #define M_PKTHDR 0x0002 /* start of record */ 124 #define M_EOR 0x0004 /* end of record */ 125 #define M_PROTO1 0x0008 /* protocol-specific */ 126 #define M_PROTO2 0x0010 /* protocol-specific */ 127 #define M_PROTO3 0x0020 /* protocol-specific */ 128 #define M_PROTO4 0x0040 /* protocol-specific */ 129 #define M_PROTO5 0x0080 /* protocol-specific */ 130 131 /* mbuf pkthdr flags, also in m_flags */ 132 #define M_BCAST 0x0100 /* send/received as link-level broadcast */ 133 #define M_MCAST 0x0200 /* send/received as link-level multicast */ 134 #define M_FRAG 0x0400 /* packet is a fragment of a larger packet */ 135 136 /* flags copied when copying m_pkthdr */ 137 #define M_COPYFLAGS (M_PKTHDR|M_EOR|M_PROTO1|M_PROTO1|M_PROTO2|M_PROTO3|M_PROTO4|M_PROTO5|M_BCAST|M_MCAST|M_FRAG) 138 139 /* mbuf types */ 140 #define MT_FREE 0 /* should be on free list */ 141 #define MT_DATA 1 /* dynamic (data) allocation */ 142 #define MT_HEADER 2 /* packet header */ 143 /*efine MT_SOCKET 3*/ /* socket structure */ 144 /*efine MT_PCB 4*/ /* protocol control block */ 145 /*efine MT_RTABLE 5*/ /* routing tables */ 146 /*efine MT_HTABLE 6*/ /* IMP host tables */ 147 /*efine MT_ATABLE 7*/ /* address resolution tables */ 148 #define MT_SONAME 8 /* socket name */ 149 /*efine MT_SOOPTS 10*/ /* socket options */ 150 #define MT_FTABLE 11 /* fragment reassembly header */ 151 /*efine MT_RIGHTS 12*/ /* access rights */ 152 /*efine MT_IFADDR 13*/ /* interface address */ 153 #define MT_CONTROL 14 /* extra-data protocol message */ 154 #define MT_OOBDATA 15 /* expedited data */ 155 156 /* flags to m_get/MGET */ 157 #define M_DONTWAIT 1 158 #define M_WAIT 0 159 160 /* Freelists: 161 * 162 * Normal mbuf clusters are normally treated as character arrays 163 * after allocation, but use the first word of the buffer as a free list 164 * pointer while on the free list. 165 */ 166 union mcluster { 167 union mcluster *mcl_next; 168 char mcl_buf[MCLBYTES]; 169 }; 170 171 /* 172 * mbuf utility macros: 173 * 174 * MBUFLOCK(code) 175 * prevents a section of code from from being interrupted by network 176 * drivers. 177 */ 178 #define MBUFLOCK(code) \ 179 do { int ms = splimp(); \ 180 { code } \ 181 splx(ms); \ 182 } while(0) 183 184 /* 185 * mbuf allocation/deallocation macros: 186 * 187 * MGET(struct mbuf *m, int how, int type) 188 * allocates an mbuf and initializes it to contain internal data. 189 * 190 * MGETHDR(struct mbuf *m, int how, int type) 191 * allocates an mbuf and initializes it to contain a packet header 192 * and internal data. 193 */ 194 #define MGET(m, how, type) { \ 195 int _ms = splimp(); \ 196 if (mmbfree == 0) \ 197 (void)m_mballoc(1, (how)); \ 198 if (((m) = mmbfree) != 0) { \ 199 mmbfree = (m)->m_next; \ 200 mbstat.m_mtypes[MT_FREE]--; \ 201 (m)->m_type = (type); \ 202 mbstat.m_mtypes[type]++; \ 203 (m)->m_next = (struct mbuf *)NULL; \ 204 (m)->m_nextpkt = (struct mbuf *)NULL; \ 205 (m)->m_data = (m)->m_dat; \ 206 (m)->m_flags = 0; \ 207 splx(_ms); \ 208 } else { \ 209 splx(_ms); \ 210 (m) = m_retry((how), (type)); \ 211 } \ 212 } 213 214 #define MGETHDR(m, how, type) { \ 215 int _ms = splimp(); \ 216 if (mmbfree == 0) \ 217 (void)m_mballoc(1, (how)); \ 218 if (((m) = mmbfree) != 0) { \ 219 mmbfree = (m)->m_next; \ 220 mbstat.m_mtypes[MT_FREE]--; \ 221 (m)->m_type = (type); \ 222 mbstat.m_mtypes[type]++; \ 223 (m)->m_next = (struct mbuf *)NULL; \ 224 (m)->m_nextpkt = (struct mbuf *)NULL; \ 225 (m)->m_data = (m)->m_pktdat; \ 226 (m)->m_flags = M_PKTHDR; \ 227 splx(_ms); \ 228 } else { \ 229 splx(_ms); \ 230 (m) = m_retryhdr((how), (type)); \ 231 } \ 232 } 233 234 /* 235 * Mbuf cluster macros. 236 * MCLALLOC(caddr_t p, int how) allocates an mbuf cluster. 237 * MCLGET adds such clusters to a normal mbuf; 238 * the flag M_EXT is set upon success. 239 * MCLFREE releases a reference to a cluster allocated by MCLALLOC, 240 * freeing the cluster if the reference count has reached 0. 241 */ 242 #define MCLALLOC(p, how) \ 243 MBUFLOCK( \ 244 if (mclfree == 0) \ 245 (void)m_clalloc(1, (how)); \ 246 if (((p) = (caddr_t)mclfree) != 0) { \ 247 ++mclrefcnt[mtocl(p)]; \ 248 mbstat.m_clfree--; \ 249 mclfree = ((union mcluster *)(p))->mcl_next; \ 250 } \ 251 ) 252 253 #define MCLGET(m, how) \ 254 { MCLALLOC((m)->m_ext.ext_buf, (how)); \ 255 if ((m)->m_ext.ext_buf != NULL) { \ 256 (m)->m_data = (m)->m_ext.ext_buf; \ 257 (m)->m_flags |= M_EXT; \ 258 (m)->m_ext.ext_free = NULL; \ 259 (m)->m_ext.ext_ref = NULL; \ 260 (m)->m_ext.ext_size = MCLBYTES; \ 261 } \ 262 } 263 264 #define MCLFREE1(p) \ 265 do { \ 266 if (--mclrefcnt[mtocl(p)] == 0) { \ 267 ((union mcluster *)(p))->mcl_next = mclfree; \ 268 mclfree = (union mcluster *)(p); \ 269 mbstat.m_clfree++; \ 270 } \ 271 } while (0) 272 273 #define MCLFREE(p) \ 274 MBUFLOCK( \ 275 MCLFREE1(p); \ 276 ) 277 278 #define MEXTFREE1(m) \ 279 do { \ 280 if ((m)->m_ext.ext_free) \ 281 (*((m)->m_ext.ext_free))((m)->m_ext.ext_buf, \ 282 (m)->m_ext.ext_size); \ 283 else { \ 284 char *p = (m)->m_ext.ext_buf; \ 285 MCLFREE1(p); \ 286 } \ 287 } while (0) 288 289 #define MEXTFREE(m) \ 290 MBUFLOCK( \ 291 MCLEXTFREE1(m); \ 292 ) 293 294 /* 295 * MFREE(struct mbuf *m, struct mbuf *n) 296 * Free a single mbuf and associated external storage. 297 * Place the successor, if any, in n. 298 */ 299 #define MFREE(m, n) \ 300 MBUFLOCK( \ 301 mbstat.m_mtypes[(m)->m_type]--; \ 302 if ((m)->m_flags & M_EXT) { \ 303 MEXTFREE1(m); \ 304 } \ 305 (n) = (m)->m_next; \ 306 (m)->m_type = MT_FREE; \ 307 mbstat.m_mtypes[MT_FREE]++; \ 308 (m)->m_next = mmbfree; \ 309 mmbfree = (m); \ 310 ) 311 312 /* 313 * Copy mbuf pkthdr from from to to. 314 * from must have M_PKTHDR set, and to must be empty. 315 */ 316 #define M_COPY_PKTHDR(to, from) { \ 317 (to)->m_pkthdr = (from)->m_pkthdr; \ 318 (to)->m_flags = (from)->m_flags & M_COPYFLAGS; \ 319 (to)->m_data = (to)->m_pktdat; \ 320 } 321 322 /* 323 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place 324 * an object of the specified size at the end of the mbuf, longword aligned. 325 */ 326 #define M_ALIGN(m, len) \ 327 { (m)->m_data += (MLEN - (len)) &~ (sizeof(long) - 1); } 328 /* 329 * As above, for mbufs allocated with m_gethdr/MGETHDR 330 * or initialized by M_COPY_PKTHDR. 331 */ 332 #define MH_ALIGN(m, len) \ 333 { (m)->m_data += (MHLEN - (len)) &~ (sizeof(long) - 1); } 334 335 /* 336 * Compute the amount of space available 337 * before the current start of data in an mbuf. 338 */ 339 #define M_LEADINGSPACE(m) \ 340 ((m)->m_flags & M_EXT ? /* (m)->m_data - (m)->m_ext.ext_buf */ 0 : \ 341 (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \ 342 (m)->m_data - (m)->m_dat) 343 344 /* 345 * Compute the amount of space available 346 * after the end of data in an mbuf. 347 */ 348 #define M_TRAILINGSPACE(m) \ 349 ((m)->m_flags & M_EXT ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size - \ 350 ((m)->m_data + (m)->m_len) : \ 351 &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len)) 352 353 /* 354 * Arrange to prepend space of size plen to mbuf m. 355 * If a new mbuf must be allocated, how specifies whether to wait. 356 * If how is M_DONTWAIT and allocation fails, the original mbuf chain 357 * is freed and m is set to NULL. 358 */ 359 #define M_PREPEND(m, plen, how) { \ 360 if (M_LEADINGSPACE(m) >= (plen)) { \ 361 (m)->m_data -= (plen); \ 362 (m)->m_len += (plen); \ 363 } else \ 364 (m) = m_prepend((m), (plen), (how)); \ 365 if ((m) && (m)->m_flags & M_PKTHDR) \ 366 (m)->m_pkthdr.len += (plen); \ 367 } 368 369 /* change mbuf to new type */ 370 #define MCHTYPE(m, t) do { \ 371 MBUFLOCK(mbstat.m_mtypes[(m)->m_type]--; mbstat.m_mtypes[t]++;); \ 372 (m)->m_type = t;\ 373 } while(0) 374 375 /* length to m_copy to copy all */ 376 #define M_COPYALL 1000000000 377 378 /* compatibility with 4.3 */ 379 #define m_copy(m, o, l) m_copym((m), (o), (l), M_DONTWAIT) 380 381 /* 382 * Mbuf statistics. 383 */ 384 struct mbstat { 385 u_long m_mbufs; /* mbufs obtained from page pool */ 386 u_long m_clusters; /* clusters obtained from page pool */ 387 u_long m_spare; /* spare field */ 388 u_long m_clfree; /* free clusters */ 389 u_long m_drops; /* times failed to find space */ 390 u_long m_wait; /* times waited for space */ 391 u_long m_drain; /* times drained protocols for space */ 392 u_short m_mtypes[256]; /* type specific mbuf allocations */ 393 u_long m_mcfail; /* times m_copym failed */ 394 u_long m_mpfail; /* times m_pullup failed */ 395 u_long m_msize; /* length of an mbuf */ 396 u_long m_mclbytes; /* length of an mbuf cluster */ 397 u_long m_minclsize; /* min length of data to allocate a cluster */ 398 u_long m_mlen; /* length of data in an mbuf */ 399 u_long m_mhlen; /* length of data in a header mbuf */ 400 }; 401 402 #ifdef KERNEL 403 extern struct mbuf *mbutl; /* virtual address of mclusters */ 404 extern char *mclrefcnt; /* cluster reference counts */ 405 extern struct mbstat mbstat; 406 extern int nmbclusters; 407 extern int nmbufs; 408 extern int nsfbufs; 409 extern struct mbuf *mmbfree; 410 extern union mcluster *mclfree; 411 extern int max_linkhdr; /* largest link-level header */ 412 extern int max_protohdr; /* largest protocol header */ 413 extern int max_hdr; /* largest link+protocol header */ 414 extern int max_datalen; /* MHLEN - max_hdr */ 415 416 struct mbuf *m_copym __P((struct mbuf *, int, int, int)); 417 struct mbuf *m_copypacket __P((struct mbuf *, int)); 418 struct mbuf *m_devget __P((char *, int, int, struct ifnet *, 419 void (*copy)(char *, caddr_t, u_int))); 420 struct mbuf *m_free __P((struct mbuf *)); 421 struct mbuf *m_get __P((int, int)); 422 struct mbuf *m_getclr __P((int, int)); 423 struct mbuf *m_gethdr __P((int, int)); 424 struct mbuf *m_prepend __P((struct mbuf *,int,int)); 425 void m_print __P((const struct mbuf *m)); 426 struct mbuf *m_pullup __P((struct mbuf *, int)); 427 struct mbuf *m_retry __P((int, int)); 428 struct mbuf *m_retryhdr __P((int, int)); 429 struct mbuf *m_split __P((struct mbuf *,int,int)); 430 void m_adj __P((struct mbuf *, int)); 431 void m_cat __P((struct mbuf *,struct mbuf *)); 432 int m_mballoc __P((int, int)); 433 int m_clalloc __P((int, int)); 434 void m_copyback __P((struct mbuf *, int, int, caddr_t)); 435 void m_copydata __P((struct mbuf *,int,int,caddr_t)); 436 void m_freem __P((struct mbuf *)); 437 #endif /* KERNEL */ 438 439 #endif /* !_SYS_MBUF_H_ */ 440