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