1 /* 2 * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)mbuf.h 7.14 (Berkeley) 12/05/90 8 */ 9 10 #ifndef M_WAITOK 11 #include "malloc.h" 12 #endif 13 14 /* 15 * Mbufs are of a single size, MSIZE (machine/machparam.h), which 16 * includes overhead. An mbuf may add a single "mbuf cluster" of size 17 * MCLBYTES (also in machine/machparam.h), which has no additional overhead 18 * and is used instead of the internal data area; this is done when 19 * at least MINCLSIZE of data must be stored. 20 */ 21 22 #define MLEN (MSIZE - sizeof(struct m_hdr)) /* normal data len */ 23 #define MHLEN (MLEN - sizeof(struct pkthdr)) /* data len w/pkthdr */ 24 25 #define MINCLSIZE (MHLEN + MLEN) /* smallest amount to put in cluster */ 26 #define M_MAXCOMPRESS (MHLEN / 2) /* max amount to copy for compression */ 27 28 /* 29 * Macros for type conversion 30 * mtod(m,t) - convert mbuf pointer to data pointer of correct type 31 * dtom(x) - convert data pointer within mbuf to mbuf pointer (XXX) 32 * mtocl(x) - convert pointer within cluster to cluster index # 33 * cltom(x) - convert cluster # to ptr to beginning of cluster 34 */ 35 #define mtod(m,t) ((t)((m)->m_data)) 36 #define dtom(x) ((struct mbuf *)((int)(x) & ~(MSIZE-1))) 37 #define mtocl(x) (((u_int)(x) - (u_int)mbutl) >> MCLSHIFT) 38 #define cltom(x) ((caddr_t)((u_int)mbutl + ((u_int)(x) >> MCLSHIFT))) 39 40 /* header at beginning of each mbuf: */ 41 struct m_hdr { 42 struct mbuf *mh_next; /* next buffer in chain */ 43 struct mbuf *mh_nextpkt; /* next chain in queue/record */ 44 int mh_len; /* amount of data in this mbuf */ 45 caddr_t mh_data; /* location of data */ 46 short mh_type; /* type of data in this mbuf */ 47 short mh_flags; /* flags; see below */ 48 }; 49 50 /* record/packet header in first mbuf of chain; valid if M_PKTHDR set */ 51 struct pkthdr { 52 int len; /* total packet length */ 53 struct ifnet *rcvif; /* rcv interface */ 54 }; 55 56 /* description of external storage mapped into mbuf, valid if M_EXT set */ 57 struct m_ext { 58 caddr_t ext_buf; /* start of buffer */ 59 void (*ext_free)(); /* free routine if not the usual */ 60 u_int ext_size; /* size of buffer, for ext_free */ 61 }; 62 63 struct mbuf { 64 struct m_hdr m_hdr; 65 union { 66 struct { 67 struct pkthdr MH_pkthdr; /* M_PKTHDR set */ 68 union { 69 struct m_ext MH_ext; /* M_EXT set */ 70 char MH_databuf[MHLEN]; 71 } MH_dat; 72 } MH; 73 char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */ 74 } M_dat; 75 }; 76 #define m_next m_hdr.mh_next 77 #define m_len m_hdr.mh_len 78 #define m_data m_hdr.mh_data 79 #define m_type m_hdr.mh_type 80 #define m_flags m_hdr.mh_flags 81 #define m_nextpkt m_hdr.mh_nextpkt 82 #define m_act m_nextpkt 83 #define m_pkthdr M_dat.MH.MH_pkthdr 84 #define m_ext M_dat.MH.MH_dat.MH_ext 85 #define m_pktdat M_dat.MH.MH_dat.MH_databuf 86 #define m_dat M_dat.M_databuf 87 88 /* mbuf flags */ 89 #define M_EXT 0x0001 /* has associated external storage */ 90 #define M_PKTHDR 0x0002 /* start of record */ 91 #define M_EOR 0x0004 /* end of record */ 92 93 /* mbuf pkthdr flags, also in m_flags */ 94 #define M_BCAST 0x0100 /* send/received as link-level broadcast */ 95 #define M_MCAST 0x0200 /* send/received as link-level multicast */ 96 97 /* flags copied when copying m_pkthdr */ 98 #define M_COPYFLAGS (M_PKTHDR|M_EOR|M_BCAST|M_MCAST) 99 100 /* mbuf types */ 101 #define MT_FREE 0 /* should be on free list */ 102 #define MT_DATA 1 /* dynamic (data) allocation */ 103 #define MT_HEADER 2 /* packet header */ 104 #define MT_SOCKET 3 /* socket structure */ 105 #define MT_PCB 4 /* protocol control block */ 106 #define MT_RTABLE 5 /* routing tables */ 107 #define MT_HTABLE 6 /* IMP host tables */ 108 #define MT_ATABLE 7 /* address resolution tables */ 109 #define MT_SONAME 8 /* socket name */ 110 #define MT_SOOPTS 10 /* socket options */ 111 #define MT_FTABLE 11 /* fragment reassembly header */ 112 #define MT_RIGHTS 12 /* access rights */ 113 #define MT_IFADDR 13 /* interface address */ 114 #define MT_CONTROL 14 /* extra-data protocol message */ 115 #define MT_OOBDATA 15 /* expedited data */ 116 117 /* flags to m_get/MGET */ 118 #define M_DONTWAIT M_NOWAIT 119 #define M_WAIT M_WAITOK 120 121 /* 122 * mbuf allocation/deallocation macros: 123 * 124 * MGET(struct mbuf *m, int how, int type) 125 * allocates an mbuf and initializes it to contain internal data. 126 * 127 * MGETHDR(struct mbuf *m, int how, int type) 128 * allocates an mbuf and initializes it to contain a packet header 129 * and internal data. 130 */ 131 #define MGET(m, how, type) { \ 132 MALLOC((m), struct mbuf *, MSIZE, mbtypes[type], (how)); \ 133 if (m) { \ 134 (m)->m_type = (type); \ 135 mbstat.m_mtypes[type]++; \ 136 (m)->m_next = (struct mbuf *)NULL; \ 137 (m)->m_nextpkt = (struct mbuf *)NULL; \ 138 (m)->m_data = (m)->m_dat; \ 139 (m)->m_flags = 0; \ 140 } else \ 141 (m) = m_retry((how), (type)); \ 142 } 143 144 #define MGETHDR(m, how, type) { \ 145 MALLOC((m), struct mbuf *, MSIZE, mbtypes[type], (how)); \ 146 if (m) { \ 147 (m)->m_type = (type); \ 148 mbstat.m_mtypes[type]++; \ 149 (m)->m_next = (struct mbuf *)NULL; \ 150 (m)->m_nextpkt = (struct mbuf *)NULL; \ 151 (m)->m_data = (m)->m_pktdat; \ 152 (m)->m_flags = M_PKTHDR; \ 153 } else \ 154 (m) = m_retryhdr((how), (type)); \ 155 } 156 157 /* 158 * Mbuf cluster macros. 159 * MCLALLOC(caddr_t p, int how) allocates an mbuf cluster. 160 * MCLGET adds such clusters to a normal mbuf; 161 * the flag M_EXT is set upon success. 162 * MCLFREE releases a reference to a cluster allocated by MCLALLOC, 163 * freeing the cluster if the reference count has reached 0. 164 * 165 * Normal mbuf clusters are normally treated as character arrays 166 * after allocation, but use the first word of the buffer as a free list 167 * pointer while on the free list. 168 */ 169 union mcluster { 170 union mcluster *mcl_next; 171 char mcl_buf[MCLBYTES]; 172 }; 173 174 #define MCLALLOC(p, how) \ 175 { int ms = splimp(); \ 176 if (mclfree == 0) \ 177 (void)m_clalloc(1, (how)); \ 178 if ((p) = (caddr_t)mclfree) { \ 179 ++mclrefcnt[mtocl(p)]; \ 180 mbstat.m_clfree--; \ 181 mclfree = ((union mcluster *)(p))->mcl_next; \ 182 } \ 183 splx(ms); \ 184 } 185 186 #define MCLGET(m, how) \ 187 { MCLALLOC((m)->m_ext.ext_buf, (how)); \ 188 if ((m)->m_ext.ext_buf != NULL) { \ 189 (m)->m_data = (m)->m_ext.ext_buf; \ 190 (m)->m_flags |= M_EXT; \ 191 (m)->m_ext.ext_size = MCLBYTES; \ 192 } \ 193 } 194 195 #define MCLFREE(p) \ 196 { int ms = splimp(); \ 197 if (--mclrefcnt[mtocl(p)] == 0) { \ 198 ((union mcluster *)(p))->mcl_next = mclfree; \ 199 mclfree = (union mcluster *)(p); \ 200 mbstat.m_clfree++; \ 201 } \ 202 splx(ms); \ 203 } 204 205 /* 206 * MFREE(struct mbuf *m, struct mbuf *n) 207 * Free a single mbuf and associated external storage. 208 * Place the successor, if any, in n. 209 */ 210 #ifdef notyet 211 #define MFREE(m, n) \ 212 { mbstat.m_mtypes[(m)->m_type]--; \ 213 if ((m)->m_flags & M_EXT) { \ 214 if ((m)->m_ext.ext_free) \ 215 (*((m)->m_ext.ext_free))((m)->m_ext.ext_buf, \ 216 (m)->m_ext.ext_size); \ 217 else \ 218 MCLFREE((m)->m_ext.ext_buf); \ 219 } \ 220 (n) = (m)->m_next; \ 221 FREE((m), mbtypes[(m)->m_type]); \ 222 } 223 #else /* notyet */ 224 #define MFREE(m, nn) \ 225 { mbstat.m_mtypes[(m)->m_type]--; \ 226 if ((m)->m_flags & M_EXT) { \ 227 MCLFREE((m)->m_ext.ext_buf); \ 228 } \ 229 (nn) = (m)->m_next; \ 230 FREE((m), mbtypes[(m)->m_type]); \ 231 } 232 #endif 233 234 /* 235 * Copy mbuf pkthdr from from to to. 236 * from must have M_PKTHDR set, and to must be empty. 237 */ 238 #define M_COPY_PKTHDR(to, from) { \ 239 (to)->m_pkthdr = (from)->m_pkthdr; \ 240 (to)->m_flags = (from)->m_flags & M_COPYFLAGS; \ 241 (to)->m_data = (to)->m_pktdat; \ 242 } 243 244 /* 245 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place 246 * an object of the specified size at the end of the mbuf, longword aligned. 247 */ 248 #define M_ALIGN(m, len) \ 249 { (m)->m_data += (MLEN - (len)) &~ (sizeof(long) - 1); } 250 /* 251 * As above, for mbufs allocated with m_gethdr/MGETHDR 252 * or initialized by M_COPY_PKTHDR. 253 */ 254 #define MH_ALIGN(m, len) \ 255 { (m)->m_data += (MHLEN - (len)) &~ (sizeof(long) - 1); } 256 257 /* 258 * Compute the amount of space available 259 * before the current start of data in an mbuf. 260 */ 261 #define M_LEADINGSPACE(m) \ 262 ((m)->m_flags & M_EXT ? /* (m)->m_data - (m)->m_ext.ext_buf */ 0 : \ 263 (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \ 264 (m)->m_data - (m)->m_dat) 265 266 /* 267 * Compute the amount of space available 268 * after the end of data in an mbuf. 269 */ 270 #define M_TRAILINGSPACE(m) \ 271 ((m)->m_flags & M_EXT ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size - \ 272 ((m)->m_data + (m)->m_len) : \ 273 &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len)) 274 275 /* 276 * Arrange to prepend space of size plen to mbuf m. 277 * If a new mbuf must be allocated, how specifies whether to wait. 278 * If how is M_DONTWAIT and allocation fails, the original mbuf chain 279 * is freed and m is set to NULL. 280 */ 281 #define M_PREPEND(m, plen, how) { \ 282 if (M_LEADINGSPACE(m) >= (plen)) { \ 283 (m)->m_data -= (plen); \ 284 (m)->m_len += (plen); \ 285 } else \ 286 (m) = m_prepend((m), (plen), (how)); \ 287 if ((m) && (m)->m_flags & M_PKTHDR) \ 288 (m)->m_pkthdr.len += (plen); \ 289 } 290 291 /* change mbuf to new type */ 292 #define MCHTYPE(m, t) { \ 293 mbstat.m_mtypes[(m)->m_type]--; \ 294 mbstat.m_mtypes[t]++; \ 295 (m)->m_type = t;\ 296 } 297 298 /* length to m_copy to copy all */ 299 #define M_COPYALL 1000000000 300 301 /* compatiblity with 4.3 */ 302 #define m_copy(m, o, l) m_copym((m), (o), (l), M_DONTWAIT) 303 304 /* 305 * Mbuf statistics. 306 */ 307 struct mbstat { 308 u_long m_mbufs; /* mbufs obtained from page pool */ 309 u_long m_clusters; /* clusters obtained from page pool */ 310 u_long m_spare; /* spare field */ 311 u_long m_clfree; /* free clusters */ 312 u_long m_drops; /* times failed to find space */ 313 u_long m_wait; /* times waited for space */ 314 u_long m_drain; /* times drained protocols for space */ 315 u_short m_mtypes[256]; /* type specific mbuf allocations */ 316 }; 317 318 #ifdef KERNEL 319 extern struct mbuf *mbutl; /* virtual address of mclusters */ 320 extern char *mclrefcnt; /* cluster reference counts */ 321 struct mbstat mbstat; 322 int nmbclusters; 323 union mcluster *mclfree; 324 int max_linkhdr; /* largest link-level header */ 325 int max_protohdr; /* largest protocol header */ 326 int max_hdr; /* largest link+protocol header */ 327 int max_datalen; /* MHLEN - max_hdr */ 328 struct mbuf *m_get(), *m_gethdr(), *m_getclr(), *m_retry(), *m_retryhdr(); 329 struct mbuf *m_free(), *m_copym(), *m_pullup(), *m_prepend(); 330 int m_clalloc(); 331 extern int mbtypes[]; /* XXX */ 332 333 #ifdef MBTYPES 334 int mbtypes[] = { /* XXX */ 335 M_FREE, /* MT_FREE 0 /* should be on free list */ 336 M_MBUF, /* MT_DATA 1 /* dynamic (data) allocation */ 337 M_MBUF, /* MT_HEADER 2 /* packet header */ 338 M_SOCKET, /* MT_SOCKET 3 /* socket structure */ 339 M_PCB, /* MT_PCB 4 /* protocol control block */ 340 M_RTABLE, /* MT_RTABLE 5 /* routing tables */ 341 M_HTABLE, /* MT_HTABLE 6 /* IMP host tables */ 342 0, /* MT_ATABLE 7 /* address resolution tables */ 343 M_MBUF, /* MT_SONAME 8 /* socket name */ 344 0, /* 9 */ 345 M_SOOPTS, /* MT_SOOPTS 10 /* socket options */ 346 M_FTABLE, /* MT_FTABLE 11 /* fragment reassembly header */ 347 M_MBUF, /* MT_RIGHTS 12 /* access rights */ 348 M_IFADDR, /* MT_IFADDR 13 /* interface address */ 349 M_MBUF, /* MT_CONTROL 14 /* extra-data protocol message */ 350 M_MBUF, /* MT_OOBDATA 15 /* expedited data */ 351 #ifdef DATAKIT 352 25, 26, 27, 28, 29, 30, 31, 32 /* datakit ugliness */ 353 #endif 354 }; 355 #endif 356 #endif 357