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 this notice is preserved and that due credit is given 7 * to the University of California at Berkeley. The name of the University 8 * may not be used to endorse or promote products derived from this 9 * software without specific prior written permission. This software 10 * is provided ``as is'' without express or implied warranty. 11 * 12 * @(#)uipc_mbuf.c 7.7 (Berkeley) 02/27/88 13 */ 14 15 #include "../machine/pte.h" 16 17 #include "param.h" 18 #include "dir.h" 19 #include "user.h" 20 #include "proc.h" 21 #include "cmap.h" 22 #include "map.h" 23 #include "mbuf.h" 24 #include "vm.h" 25 #include "kernel.h" 26 #include "syslog.h" 27 #include "domain.h" 28 #include "protosw.h" 29 30 mbinit() 31 { 32 int s; 33 34 #if CLBYTES < 4096 35 #define NCL_INIT (4096/CLBYTES) 36 #else 37 #define NCL_INIT 1 38 #endif 39 s = splimp(); 40 if (m_clalloc(NCL_INIT, MPG_MBUFS, M_DONTWAIT) == 0) 41 goto bad; 42 if (m_clalloc(NCL_INIT, MPG_CLUSTERS, M_DONTWAIT) == 0) 43 goto bad; 44 splx(s); 45 return; 46 bad: 47 panic("mbinit"); 48 } 49 50 /* 51 * Must be called at splimp. 52 */ 53 /* ARGSUSED */ 54 caddr_t 55 m_clalloc(ncl, how, canwait) 56 register int ncl; 57 int how; 58 { 59 int npg, mbx; 60 register struct mbuf *m; 61 register int i; 62 static int logged; 63 64 npg = ncl * CLSIZE; 65 mbx = rmalloc(mbmap, (long)npg); 66 if (mbx == 0) { 67 if (logged == 0) { 68 logged++; 69 log(LOG_ERR, "mbuf map full\n"); 70 } 71 return (0); 72 } 73 m = cltom(mbx * NBPG / MCLBYTES); 74 if (memall(&Mbmap[mbx], npg, proc, CSYS) == 0) { 75 rmfree(mbmap, (long)npg, (long)mbx); 76 return (0); 77 } 78 vmaccess(&Mbmap[mbx], (caddr_t)m, npg); 79 switch (how) { 80 81 case MPG_CLUSTERS: 82 ncl = ncl * CLBYTES / MCLBYTES; 83 for (i = 0; i < ncl; i++) { 84 m->m_off = 0; 85 m->m_next = mclfree; 86 mclfree = m; 87 m += MCLBYTES / sizeof (*m); 88 mbstat.m_clfree++; 89 } 90 mbstat.m_clusters += ncl; 91 break; 92 93 case MPG_MBUFS: 94 for (i = ncl * CLBYTES / sizeof (*m); i > 0; i--) { 95 m->m_off = 0; 96 m->m_type = MT_DATA; 97 mbstat.m_mtypes[MT_DATA]++; 98 mbstat.m_mbufs++; 99 (void) m_free(m); 100 m++; 101 } 102 break; 103 } 104 return ((caddr_t)m); 105 } 106 107 m_pgfree(addr, n) 108 caddr_t addr; 109 int n; 110 { 111 112 #ifdef lint 113 addr = addr; n = n; 114 #endif 115 } 116 117 /* 118 * Must be called at splimp. 119 */ 120 m_expand(canwait) 121 int canwait; 122 { 123 register struct domain *dp; 124 register struct protosw *pr; 125 int tries; 126 127 for (tries = 0;; ) { 128 if (m_clalloc(1, MPG_MBUFS, canwait)) 129 return (1); 130 if (canwait == 0 || tries++) 131 return (0); 132 133 /* ask protocols to free space */ 134 for (dp = domains; dp; dp = dp->dom_next) 135 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; 136 pr++) 137 if (pr->pr_drain) 138 (*pr->pr_drain)(); 139 mbstat.m_drain++; 140 } 141 } 142 143 /* NEED SOME WAY TO RELEASE SPACE */ 144 145 /* 146 * Space allocation routines. 147 * These are also available as macros 148 * for critical paths. 149 */ 150 struct mbuf * 151 m_get(canwait, type) 152 int canwait, type; 153 { 154 register struct mbuf *m; 155 156 MGET(m, canwait, type); 157 return (m); 158 } 159 160 struct mbuf * 161 m_getclr(canwait, type) 162 int canwait, type; 163 { 164 register struct mbuf *m; 165 166 MGET(m, canwait, type); 167 if (m == 0) 168 return (0); 169 bzero(mtod(m, caddr_t), MLEN); 170 return (m); 171 } 172 173 struct mbuf * 174 m_free(m) 175 struct mbuf *m; 176 { 177 register struct mbuf *n; 178 179 MFREE(m, n); 180 return (n); 181 } 182 183 /* 184 * Get more mbufs; called from MGET macro if mfree list is empty. 185 * Must be called at splimp. 186 */ 187 /*ARGSUSED*/ 188 struct mbuf * 189 m_more(canwait, type) 190 int canwait, type; 191 { 192 register struct mbuf *m; 193 194 while (m_expand(canwait) == 0) { 195 if (canwait == M_WAIT) { 196 mbstat.m_wait++; 197 m_want++; 198 sleep((caddr_t)&mfree, PZERO - 1); 199 } else { 200 mbstat.m_drops++; 201 return (NULL); 202 } 203 } 204 #define m_more(x,y) (panic("m_more"), (struct mbuf *)0) 205 MGET(m, canwait, type); 206 #undef m_more 207 return (m); 208 } 209 210 m_freem(m) 211 register struct mbuf *m; 212 { 213 register struct mbuf *n; 214 register int s; 215 216 if (m == NULL) 217 return; 218 s = splimp(); 219 do { 220 MFREE(m, n); 221 } while (m = n); 222 splx(s); 223 } 224 225 /* 226 * Mbuffer utility routines. 227 */ 228 229 /* 230 /* 231 * Make a copy of an mbuf chain starting "off" bytes from the beginning, 232 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf. 233 * Should get M_WAIT/M_DONTWAIT from caller. 234 */ 235 struct mbuf * 236 m_copy(m, off, len) 237 register struct mbuf *m; 238 int off; 239 register int len; 240 { 241 register struct mbuf *n, **np; 242 struct mbuf *top, *p; 243 244 if (len == 0) 245 return (0); 246 if (off < 0 || len < 0) 247 panic("m_copy"); 248 while (off > 0) { 249 if (m == 0) 250 panic("m_copy"); 251 if (off < m->m_len) 252 break; 253 off -= m->m_len; 254 m = m->m_next; 255 } 256 np = ⊤ 257 top = 0; 258 while (len > 0) { 259 if (m == 0) { 260 if (len != M_COPYALL) 261 panic("m_copy"); 262 break; 263 } 264 MGET(n, M_DONTWAIT, m->m_type); 265 *np = n; 266 if (n == 0) 267 goto nospace; 268 n->m_len = MIN(len, m->m_len - off); 269 if (m->m_off > MMAXOFF) { 270 p = mtod(m, struct mbuf *); 271 n->m_off = ((int)p - (int)n) + off; 272 mclrefcnt[mtocl(p)]++; 273 } else 274 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 275 (unsigned)n->m_len); 276 if (len != M_COPYALL) 277 len -= n->m_len; 278 off = 0; 279 m = m->m_next; 280 np = &n->m_next; 281 } 282 return (top); 283 nospace: 284 m_freem(top); 285 return (0); 286 } 287 288 /* 289 * Copy data from an mbuf chain starting "off" bytes from the beginning, 290 * continuing for "len" bytes, into the indicated buffer. 291 */ 292 struct mbuf * 293 m_copydata(m, off, len, cp) 294 register struct mbuf *m; 295 int off; 296 register int len; 297 caddr_t *cp; 298 { 299 register unsigned count; 300 301 if (off < 0 || len < 0) 302 panic("m_copydata"); 303 while (off > 0) { 304 if (m == 0) 305 panic("m_copydata"); 306 if (off < m->m_len) 307 break; 308 off -= m->m_len; 309 m = m->m_next; 310 } 311 while (len > 0) { 312 if (m == 0) 313 panic("m_copydata"); 314 count = MIN(m->m_len, len); 315 bcopy(mtod(m, caddr_t) + off, cp, count); 316 len -= count; 317 off = 0; 318 m = m->m_next; 319 } 320 } 321 322 m_cat(m, n) 323 register struct mbuf *m, *n; 324 { 325 while (m->m_next) 326 m = m->m_next; 327 while (n) { 328 if (m->m_off >= MMAXOFF || 329 m->m_off + m->m_len + n->m_len > MMAXOFF) { 330 /* just join the two chains */ 331 m->m_next = n; 332 return; 333 } 334 /* splat the data from one into the other */ 335 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 336 (u_int)n->m_len); 337 m->m_len += n->m_len; 338 n = m_free(n); 339 } 340 } 341 342 m_adj(mp, len) 343 struct mbuf *mp; 344 register int len; 345 { 346 register struct mbuf *m; 347 register count; 348 349 if ((m = mp) == NULL) 350 return; 351 if (len >= 0) { 352 while (m != NULL && len > 0) { 353 if (m->m_len <= len) { 354 len -= m->m_len; 355 m->m_len = 0; 356 m = m->m_next; 357 } else { 358 m->m_len -= len; 359 m->m_off += len; 360 break; 361 } 362 } 363 } else { 364 /* 365 * Trim from tail. Scan the mbuf chain, 366 * calculating its length and finding the last mbuf. 367 * If the adjustment only affects this mbuf, then just 368 * adjust and return. Otherwise, rescan and truncate 369 * after the remaining size. 370 */ 371 len = -len; 372 count = 0; 373 for (;;) { 374 count += m->m_len; 375 if (m->m_next == (struct mbuf *)0) 376 break; 377 m = m->m_next; 378 } 379 if (m->m_len >= len) { 380 m->m_len -= len; 381 return; 382 } 383 count -= len; 384 /* 385 * Correct length for chain is "count". 386 * Find the mbuf with last data, adjust its length, 387 * and toss data from remaining mbufs on chain. 388 */ 389 for (m = mp; m; m = m->m_next) { 390 if (m->m_len >= count) { 391 m->m_len = count; 392 break; 393 } 394 count -= m->m_len; 395 } 396 while (m = m->m_next) 397 m->m_len = 0; 398 } 399 } 400 401 /* 402 * Rearange an mbuf chain so that len bytes are contiguous 403 * and in the data area of an mbuf (so that mtod and dtom 404 * will work for a structure of size len). Returns the resulting 405 * mbuf chain on success, frees it and returns null on failure. 406 * If there is room, it will add up to MPULL_EXTRA bytes to the 407 * contiguous region in an attempt to avoid being called next time. 408 */ 409 struct mbuf * 410 m_pullup(n, len) 411 register struct mbuf *n; 412 int len; 413 { 414 register struct mbuf *m; 415 register int count; 416 int space; 417 418 if (n->m_off + len <= MMAXOFF && n->m_next) { 419 m = n; 420 n = n->m_next; 421 len -= m->m_len; 422 } else { 423 if (len > MLEN) 424 goto bad; 425 MGET(m, M_DONTWAIT, n->m_type); 426 if (m == 0) 427 goto bad; 428 m->m_len = 0; 429 } 430 space = MMAXOFF - m->m_off; 431 do { 432 count = MIN(MIN(space - m->m_len, len + MPULL_EXTRA), n->m_len); 433 bcopy(mtod(n, caddr_t), mtod(m, caddr_t)+m->m_len, 434 (unsigned)count); 435 len -= count; 436 m->m_len += count; 437 n->m_len -= count; 438 if (n->m_len) 439 n->m_off += count; 440 else 441 n = m_free(n); 442 } while (len > 0 && n); 443 if (len > 0) { 444 (void) m_free(m); 445 goto bad; 446 } 447 m->m_next = n; 448 return (m); 449 bad: 450 m_freem(n); 451 return (0); 452 } 453