Rename aout_imgact.c and shell_imgact.c to imgact_* for consistency.
[unix-history] / sys / kern / uipc_mbuf.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1982, 1986, 1988, 1991 Regents of the University of California.
3 * 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 *
600f7f07 33 * from: @(#)uipc_mbuf.c 7.19 (Berkeley) 4/20/91
fde1aeb2 34 * $Id: uipc_mbuf.c,v 1.5 1993/11/25 01:33:32 wollman Exp $
15637ed4
RG
35 */
36
37#include "param.h"
dd18dc33 38#include "systm.h"
15637ed4
RG
39#include "proc.h"
40#include "malloc.h"
41#define MBTYPES
42#include "mbuf.h"
43#include "kernel.h"
44#include "syslog.h"
45#include "domain.h"
46#include "protosw.h"
47#include "vm/vm.h"
48
bbc3f849
GW
49/* From sys/mbuf.h */
50struct mbstat mbstat;
51int nmbclusters;
52union mcluster *mclfree;
53int max_linkhdr;
54int max_protohdr;
55int max_hdr;
56int max_datalen;
57
15637ed4
RG
58extern vm_map_t mb_map;
59struct mbuf *mbutl;
60char *mclrefcnt;
61
4c45483e
GW
62static void m_reclaim(void);
63
64void
15637ed4
RG
65mbinit()
66{
67 int s;
68
69#if CLBYTES < 4096
70#define NCL_INIT (4096/CLBYTES)
71#else
72#define NCL_INIT 1
73#endif
74 s = splimp();
75 if (m_clalloc(NCL_INIT, M_DONTWAIT) == 0)
76 goto bad;
77 splx(s);
78 return;
79bad:
80 panic("mbinit");
81}
82
83/*
84 * Allocate some number of mbuf clusters
85 * and place on cluster free list.
86 * Must be called at splimp.
87 */
88/* ARGSUSED */
4c45483e 89int
15637ed4
RG
90m_clalloc(ncl, how) /* 31 Aug 92*/
91 register int ncl;
4c45483e 92 int how;
15637ed4
RG
93{
94 int npg, mbx;
95 register caddr_t p;
96 register int i;
97 static int logged;
98
99 npg = ncl * CLSIZE;
100 /* 31 Aug 92*/
101 p = (caddr_t)kmem_malloc(mb_map, ctob(npg), !(how&M_DONTWAIT));
102 if (p == NULL) {
103 if (logged == 0) {
104 logged++;
105 log(LOG_ERR, "mb_map full\n");
106 }
107 return (0);
108 }
109 ncl = ncl * CLBYTES / MCLBYTES;
110 for (i = 0; i < ncl; i++) {
111 ((union mcluster *)p)->mcl_next = mclfree;
112 mclfree = (union mcluster *)p;
113 p += MCLBYTES;
114 mbstat.m_clfree++;
115 }
116 mbstat.m_clusters += ncl;
117 return (1);
118}
119
120/*
121 * When MGET failes, ask protocols to free space when short of memory,
122 * then re-attempt to allocate an mbuf.
123 */
124struct mbuf *
125m_retry(i, t)
126 int i, t;
127{
128 register struct mbuf *m;
129
130 m_reclaim();
131#define m_retry(i, t) (struct mbuf *)0
132 MGET(m, i, t);
133#undef m_retry
134 return (m);
135}
136
137/*
138 * As above; retry an MGETHDR.
139 */
140struct mbuf *
141m_retryhdr(i, t)
142 int i, t;
143{
144 register struct mbuf *m;
145
146 m_reclaim();
147#define m_retryhdr(i, t) (struct mbuf *)0
148 MGETHDR(m, i, t);
149#undef m_retryhdr
150 return (m);
151}
152
4c45483e 153static void
15637ed4
RG
154m_reclaim()
155{
156 register struct domain *dp;
157 register struct protosw *pr;
158 int s = splimp();
159
160 for (dp = domains; dp; dp = dp->dom_next)
161 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
162 if (pr->pr_drain)
163 (*pr->pr_drain)();
164 splx(s);
165 mbstat.m_drain++;
166}
167
168/*
169 * Space allocation routines.
170 * These are also available as macros
171 * for critical paths.
172 */
173struct mbuf *
174m_get(how, type) /* 31 Aug 92*/
175 int how, type;
176{
177 register struct mbuf *m;
178
179 MGET(m, how, type);
180 return (m);
181}
182
183struct mbuf *
184m_gethdr(how, type) /* 31 Aug 92*/
185 int how, type;
186{
187 register struct mbuf *m;
188
189 MGETHDR(m, how, type);
190 return (m);
191}
192
193struct mbuf *
194m_getclr(how, type) /* 31 Aug 92*/
195 int how, type;
196{
197 register struct mbuf *m;
198
199 MGET(m, how, type);
200 if (m == 0)
201 return (0);
202 bzero(mtod(m, caddr_t), MLEN);
203 return (m);
204}
205
206struct mbuf *
207m_free(m)
208 struct mbuf *m;
209{
210 register struct mbuf *n;
211
212 MFREE(m, n);
213 return (n);
214}
215
4c45483e 216void
15637ed4
RG
217m_freem(m)
218 register struct mbuf *m;
219{
220 register struct mbuf *n;
221
222 if (m == NULL)
223 return;
224 do {
225 MFREE(m, n);
226 } while (m = n);
227}
228
229/*
230 * Mbuffer utility routines.
231 */
232
233/*
234 * Lesser-used path for M_PREPEND:
235 * allocate new mbuf to prepend to chain,
236 * copy junk along.
237 */
238struct mbuf *
239m_prepend(m, len, how)
240 register struct mbuf *m;
241 int len, how;
242{
243 struct mbuf *mn;
244
245 MGET(mn, how, m->m_type);
246 if (mn == (struct mbuf *)NULL) {
247 m_freem(m);
248 return ((struct mbuf *)NULL);
249 }
250 if (m->m_flags & M_PKTHDR) {
251 M_COPY_PKTHDR(mn, m);
252 m->m_flags &= ~M_PKTHDR;
253 }
254 mn->m_next = m;
255 m = mn;
256 if (len < MHLEN)
257 MH_ALIGN(m, len);
258 m->m_len = len;
259 return (m);
260}
261
262/*
263 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
264 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
265 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
266 */
267int MCFail;
268
269struct mbuf *
270m_copym(m, off0, len, wait)
271 register struct mbuf *m;
272 int off0, wait;
273 register int len;
274{
275 register struct mbuf *n, **np;
276 register int off = off0;
277 struct mbuf *top;
278 int copyhdr = 0;
279
280 if (off < 0 || len < 0)
281 panic("m_copym");
282 if (off == 0 && m->m_flags & M_PKTHDR)
283 copyhdr = 1;
284 while (off > 0) {
285 if (m == 0)
286 panic("m_copym");
287 if (off < m->m_len)
288 break;
289 off -= m->m_len;
290 m = m->m_next;
291 }
292 np = &top;
293 top = 0;
294 while (len > 0) {
295 if (m == 0) {
296 if (len != M_COPYALL)
297 panic("m_copym");
298 break;
299 }
300 MGET(n, wait, m->m_type);
301 *np = n;
302 if (n == 0)
303 goto nospace;
304 if (copyhdr) {
305 M_COPY_PKTHDR(n, m);
306 if (len == M_COPYALL)
307 n->m_pkthdr.len -= off0;
308 else
309 n->m_pkthdr.len = len;
310 copyhdr = 0;
311 }
312 n->m_len = MIN(len, m->m_len - off);
313 if (m->m_flags & M_EXT) {
314 n->m_data = m->m_data + off;
315 mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
316 n->m_ext = m->m_ext;
317 n->m_flags |= M_EXT;
318 } else
319 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
320 (unsigned)n->m_len);
321 if (len != M_COPYALL)
322 len -= n->m_len;
323 off = 0;
324 m = m->m_next;
325 np = &n->m_next;
326 }
327 if (top == 0)
328 MCFail++;
329 return (top);
330nospace:
331 m_freem(top);
332 MCFail++;
333 return (0);
334}
335
336/*
337 * Copy data from an mbuf chain starting "off" bytes from the beginning,
338 * continuing for "len" bytes, into the indicated buffer.
339 */
4c45483e 340void
15637ed4
RG
341m_copydata(m, off, len, cp)
342 register struct mbuf *m;
343 register int off;
344 register int len;
345 caddr_t cp;
346{
347 register unsigned count;
348
349 if (off < 0 || len < 0)
350 panic("m_copydata");
351 while (off > 0) {
352 if (m == 0)
353 panic("m_copydata");
354 if (off < m->m_len)
355 break;
356 off -= m->m_len;
357 m = m->m_next;
358 }
359 while (len > 0) {
360 if (m == 0)
361 panic("m_copydata");
362 count = MIN(m->m_len - off, len);
363 bcopy(mtod(m, caddr_t) + off, cp, count);
364 len -= count;
365 cp += count;
366 off = 0;
367 m = m->m_next;
368 }
369}
370
371/*
372 * Concatenate mbuf chain n to m.
373 * Both chains must be of the same type (e.g. MT_DATA).
374 * Any m_pkthdr is not updated.
375 */
4c45483e 376void
15637ed4
RG
377m_cat(m, n)
378 register struct mbuf *m, *n;
379{
380 while (m->m_next)
381 m = m->m_next;
382 while (n) {
383 if (m->m_flags & M_EXT ||
384 m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
385 /* just join the two chains */
386 m->m_next = n;
387 return;
388 }
389 /* splat the data from one into the other */
390 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
391 (u_int)n->m_len);
392 m->m_len += n->m_len;
393 n = m_free(n);
394 }
395}
396
4c45483e 397void
15637ed4
RG
398m_adj(mp, req_len)
399 struct mbuf *mp;
4c45483e 400 int req_len;
15637ed4
RG
401{
402 register int len = req_len;
403 register struct mbuf *m;
404 register count;
405
406 if ((m = mp) == NULL)
407 return;
408 if (len >= 0) {
409 /*
410 * Trim from head.
411 */
412 while (m != NULL && len > 0) {
413 if (m->m_len <= len) {
414 len -= m->m_len;
415 m->m_len = 0;
416 m = m->m_next;
417 } else {
418 m->m_len -= len;
419 m->m_data += len;
420 len = 0;
421 }
422 }
423 m = mp;
424 if (mp->m_flags & M_PKTHDR)
425 m->m_pkthdr.len -= (req_len - len);
426 } else {
427 /*
428 * Trim from tail. Scan the mbuf chain,
429 * calculating its length and finding the last mbuf.
430 * If the adjustment only affects this mbuf, then just
431 * adjust and return. Otherwise, rescan and truncate
432 * after the remaining size.
433 */
434 len = -len;
435 count = 0;
436 for (;;) {
437 count += m->m_len;
438 if (m->m_next == (struct mbuf *)0)
439 break;
440 m = m->m_next;
441 }
442 if (m->m_len >= len) {
443 m->m_len -= len;
444 if ((mp = m)->m_flags & M_PKTHDR)
445 m->m_pkthdr.len -= len;
446 return;
447 }
448 count -= len;
449 if (count < 0)
450 count = 0;
451 /*
452 * Correct length for chain is "count".
453 * Find the mbuf with last data, adjust its length,
454 * and toss data from remaining mbufs on chain.
455 */
456 m = mp;
457 if (m->m_flags & M_PKTHDR)
458 m->m_pkthdr.len = count;
459 for (; m; m = m->m_next) {
460 if (m->m_len >= count) {
461 m->m_len = count;
462 break;
463 }
464 count -= m->m_len;
465 }
466 while (m = m->m_next)
467 m->m_len = 0;
468 }
469}
470
471/*
472 * Rearange an mbuf chain so that len bytes are contiguous
473 * and in the data area of an mbuf (so that mtod and dtom
474 * will work for a structure of size len). Returns the resulting
475 * mbuf chain on success, frees it and returns null on failure.
476 * If there is room, it will add up to max_protohdr-len extra bytes to the
477 * contiguous region in an attempt to avoid being called next time.
478 */
479int MPFail;
480
481struct mbuf *
482m_pullup(n, len)
483 register struct mbuf *n;
484 int len;
485{
486 register struct mbuf *m;
487 register int count;
488 int space;
489
490 /*
491 * If first mbuf has no cluster, and has room for len bytes
492 * without shifting current data, pullup into it,
493 * otherwise allocate a new mbuf to prepend to the chain.
494 */
495 if ((n->m_flags & M_EXT) == 0 &&
496 n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
497 if (n->m_len >= len)
498 return (n);
499 m = n;
500 n = n->m_next;
501 len -= m->m_len;
502 } else {
503 if (len > MHLEN)
504 goto bad;
505 MGET(m, M_DONTWAIT, n->m_type);
506 if (m == 0)
507 goto bad;
508 m->m_len = 0;
509 if (n->m_flags & M_PKTHDR) {
510 M_COPY_PKTHDR(m, n);
511 n->m_flags &= ~M_PKTHDR;
512 }
513 }
514 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
515 do {
516 count = min(min(max(len, max_protohdr), space), n->m_len);
517 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
518 (unsigned)count);
519 len -= count;
520 m->m_len += count;
521 n->m_len -= count;
522 space -= count;
523 if (n->m_len)
524 n->m_data += count;
525 else
526 n = m_free(n);
527 } while (len > 0 && n);
528 if (len > 0) {
529 (void) m_free(m);
530 goto bad;
531 }
532 m->m_next = n;
533 return (m);
534bad:
535 m_freem(n);
536 MPFail++;
537 return (0);
538}
4c45483e
GW
539
540/*
541 * Copy data from a buffer back into the indicated mbuf chain,
542 * starting "off" bytes from the beginning, extending the mbuf
543 * chain if necessary.
544 */
545void
546m_copyback(m0, off, len, cp)
547 struct mbuf *m0;
548 register int off;
549 register int len;
550 caddr_t cp;
551
552{
553 register int mlen;
554 register struct mbuf *m = m0, *n;
555 int totlen = 0;
556
557 if (m0 == 0)
558 return;
559 while (off > (mlen = m->m_len)) {
560 off -= mlen;
561 totlen += mlen;
562 if (m->m_next == 0) {
563 n = m_getclr(M_DONTWAIT, m->m_type);
564 if (n == 0)
565 goto out;
566 n->m_len = min(MLEN, len + off);
567 m->m_next = n;
568 }
569 m = m->m_next;
570 }
571 while (len > 0) {
572 mlen = min (m->m_len - off, len);
573 bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
574 cp += mlen;
575 len -= mlen;
576 mlen += off;
577 off = 0;
578 totlen += mlen;
579 if (len == 0)
580 break;
581 if (m->m_next == 0) {
582 n = m_get(M_DONTWAIT, m->m_type);
583 if (n == 0)
584 break;
585 n->m_len = min(MLEN, len);
586 m->m_next = n;
587 }
588 m = m->m_next;
589 }
590out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
591 m->m_pkthdr.len = totlen;
592}
593
fde1aeb2
GW
594struct mbuf *
595m_split (m0, len0, wait)
596 register struct mbuf *m0;
597 int len0;
598 int wait;
599{
600 register struct mbuf *m, *n;
601 unsigned len = len0, remain;
602
603 for (m = m0; m && len > m -> m_len; m = m -> m_next)
604 len -= m -> m_len;
605 if (m == 0)
606 return (0);
607 remain = m -> m_len - len;
608 if (m0 -> m_flags & M_PKTHDR) {
609 MGETHDR(n, wait, m0 -> m_type);
610 if (n == 0)
611 return (0);
612 n -> m_pkthdr.rcvif = m0 -> m_pkthdr.rcvif;
613 n -> m_pkthdr.len = m0 -> m_pkthdr.len - len0;
614 m0 -> m_pkthdr.len = len0;
615 if (m -> m_flags & M_EXT)
616 goto extpacket;
617 if (remain > MHLEN) {
618 /* m can't be the lead packet */
619 MH_ALIGN(n, 0);
620 n -> m_next = m_split (m, len, wait);
621 if (n -> m_next == 0) {
622 (void) m_free (n);
623 return (0);
624 } else
625 return (n);
626 } else
627 MH_ALIGN(n, remain);
628 } else if (remain == 0) {
629 n = m -> m_next;
630 m -> m_next = 0;
631 return (n);
632 } else {
633 MGET(n, wait, m -> m_type);
634 if (n == 0)
635 return (0);
636 M_ALIGN(n, remain);
637 }
638extpacket:
639 if (m -> m_flags & M_EXT) {
640 n -> m_flags |= M_EXT;
641 n -> m_ext = m -> m_ext;
642 mclrefcnt[mtocl (m -> m_ext.ext_buf)]++;
643 n -> m_data = m -> m_data + len;
644 } else {
645 bcopy (mtod (m, caddr_t) + len, mtod (n, caddr_t), remain);
646 }
647 n -> m_len = remain;
648 m -> m_len = len;
649 n -> m_next = m -> m_next;
650 m -> m_next = 0;
651 return (n);
652}
653
654/* The following taken from netiso/iso_chksum.c */
655struct mbuf *
656m_append(head, m) /* XXX */
657 struct mbuf *head, *m;
658{
659 register struct mbuf *n;
660
661 if (m == 0)
662 return head;
663 if (head == 0)
664 return m;
665 n = head;
666 while (n->m_next)
667 n = n->m_next;
668 n->m_next = m;
669 return head;
670}
671
672/*
673 * FUNCTION: m_datalen
674 *
675 * PURPOSE: returns length of the mbuf chain.
676 * used all over the iso code.
677 *
678 * RETURNS: integer
679 *
680 * SIDE EFFECTS: none
681 *
682 * NOTES:
683 */
684int
685m_datalen (morig) /* XXX */
686 struct mbuf *morig;
687{
688 int s = splimp();
689 register struct mbuf *n=morig;
690 register int datalen = 0;
691
692 if( morig == (struct mbuf *)0)
693 return 0;
694 for(;;) {
695 datalen += n->m_len;
696 if (n->m_next == (struct mbuf *)0 ) {
697 break;
698 }
699 n = n->m_next;
700 }
701 splx(s);
702 return datalen;
703}
704
705int
706m_compress(in, out)
707 register struct mbuf *in, **out;
708{
709 register int datalen = 0;
710 int s = splimp();
711
712 if(!in->m_next) {
713 *out = in;
714 splx(s);
715 return in->m_len;
716 }
717 MGET((*out), M_DONTWAIT, MT_DATA);
718 if(! (*out)) {
719 *out = in;
720 splx(s);
721 return -1;
722 }
723 (*out)->m_len = 0;
724 (*out)->m_act = 0;
725
726 while (in) {
727 if (in->m_flags & M_EXT) {
728#ifdef DEBUG
729 ASSERT(in->m_len == 0);
730#endif
731 }
732 if ( in->m_len == 0) {
733 in = in->m_next;
734 continue;
735 }
736 if (((*out)->m_flags & M_EXT) == 0) {
737 int len;
738
739 len = M_TRAILINGSPACE(*out);
740 len = MIN(len, in->m_len);
741 datalen += len;
742
743 bcopy(mtod(in, caddr_t), mtod((*out), caddr_t) + (*out)->m_len,
744 (unsigned)len);
745
746 (*out)->m_len += len;
747 in->m_len -= len;
748 continue;
749 } else {
750 /* (*out) is full */
751 if( !((*out)->m_next = m_get(M_DONTWAIT, MT_DATA))) {
752 m_freem(*out);
753 *out = in;
754 splx(s);
755 return -1;
756 }
757 (*out)->m_len = 0;
758 (*out)->m_act = 0;
759 *out = (*out)->m_next;
760 }
761 }
762 m_freem(in);
763 splx(s);
764 return datalen;
765}