Added -lgcc_pic back again.
[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
bbc3f849 34 * $Id: uipc_mbuf.c,v 1.3 1993/10/16 15:25:07 rgrimes 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
62mbinit()
63{
64 int s;
65
66#if CLBYTES < 4096
67#define NCL_INIT (4096/CLBYTES)
68#else
69#define NCL_INIT 1
70#endif
71 s = splimp();
72 if (m_clalloc(NCL_INIT, M_DONTWAIT) == 0)
73 goto bad;
74 splx(s);
75 return;
76bad:
77 panic("mbinit");
78}
79
80/*
81 * Allocate some number of mbuf clusters
82 * and place on cluster free list.
83 * Must be called at splimp.
84 */
85/* ARGSUSED */
86m_clalloc(ncl, how) /* 31 Aug 92*/
87 register int ncl;
88{
89 int npg, mbx;
90 register caddr_t p;
91 register int i;
92 static int logged;
93
94 npg = ncl * CLSIZE;
95 /* 31 Aug 92*/
96 p = (caddr_t)kmem_malloc(mb_map, ctob(npg), !(how&M_DONTWAIT));
97 if (p == NULL) {
98 if (logged == 0) {
99 logged++;
100 log(LOG_ERR, "mb_map full\n");
101 }
102 return (0);
103 }
104 ncl = ncl * CLBYTES / MCLBYTES;
105 for (i = 0; i < ncl; i++) {
106 ((union mcluster *)p)->mcl_next = mclfree;
107 mclfree = (union mcluster *)p;
108 p += MCLBYTES;
109 mbstat.m_clfree++;
110 }
111 mbstat.m_clusters += ncl;
112 return (1);
113}
114
115/*
116 * When MGET failes, ask protocols to free space when short of memory,
117 * then re-attempt to allocate an mbuf.
118 */
119struct mbuf *
120m_retry(i, t)
121 int i, t;
122{
123 register struct mbuf *m;
124
125 m_reclaim();
126#define m_retry(i, t) (struct mbuf *)0
127 MGET(m, i, t);
128#undef m_retry
129 return (m);
130}
131
132/*
133 * As above; retry an MGETHDR.
134 */
135struct mbuf *
136m_retryhdr(i, t)
137 int i, t;
138{
139 register struct mbuf *m;
140
141 m_reclaim();
142#define m_retryhdr(i, t) (struct mbuf *)0
143 MGETHDR(m, i, t);
144#undef m_retryhdr
145 return (m);
146}
147
148m_reclaim()
149{
150 register struct domain *dp;
151 register struct protosw *pr;
152 int s = splimp();
153
154 for (dp = domains; dp; dp = dp->dom_next)
155 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
156 if (pr->pr_drain)
157 (*pr->pr_drain)();
158 splx(s);
159 mbstat.m_drain++;
160}
161
162/*
163 * Space allocation routines.
164 * These are also available as macros
165 * for critical paths.
166 */
167struct mbuf *
168m_get(how, type) /* 31 Aug 92*/
169 int how, type;
170{
171 register struct mbuf *m;
172
173 MGET(m, how, type);
174 return (m);
175}
176
177struct mbuf *
178m_gethdr(how, type) /* 31 Aug 92*/
179 int how, type;
180{
181 register struct mbuf *m;
182
183 MGETHDR(m, how, type);
184 return (m);
185}
186
187struct mbuf *
188m_getclr(how, type) /* 31 Aug 92*/
189 int how, type;
190{
191 register struct mbuf *m;
192
193 MGET(m, how, type);
194 if (m == 0)
195 return (0);
196 bzero(mtod(m, caddr_t), MLEN);
197 return (m);
198}
199
200struct mbuf *
201m_free(m)
202 struct mbuf *m;
203{
204 register struct mbuf *n;
205
206 MFREE(m, n);
207 return (n);
208}
209
210m_freem(m)
211 register struct mbuf *m;
212{
213 register struct mbuf *n;
214
215 if (m == NULL)
216 return;
217 do {
218 MFREE(m, n);
219 } while (m = n);
220}
221
222/*
223 * Mbuffer utility routines.
224 */
225
226/*
227 * Lesser-used path for M_PREPEND:
228 * allocate new mbuf to prepend to chain,
229 * copy junk along.
230 */
231struct mbuf *
232m_prepend(m, len, how)
233 register struct mbuf *m;
234 int len, how;
235{
236 struct mbuf *mn;
237
238 MGET(mn, how, m->m_type);
239 if (mn == (struct mbuf *)NULL) {
240 m_freem(m);
241 return ((struct mbuf *)NULL);
242 }
243 if (m->m_flags & M_PKTHDR) {
244 M_COPY_PKTHDR(mn, m);
245 m->m_flags &= ~M_PKTHDR;
246 }
247 mn->m_next = m;
248 m = mn;
249 if (len < MHLEN)
250 MH_ALIGN(m, len);
251 m->m_len = len;
252 return (m);
253}
254
255/*
256 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
257 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
258 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
259 */
260int MCFail;
261
262struct mbuf *
263m_copym(m, off0, len, wait)
264 register struct mbuf *m;
265 int off0, wait;
266 register int len;
267{
268 register struct mbuf *n, **np;
269 register int off = off0;
270 struct mbuf *top;
271 int copyhdr = 0;
272
273 if (off < 0 || len < 0)
274 panic("m_copym");
275 if (off == 0 && m->m_flags & M_PKTHDR)
276 copyhdr = 1;
277 while (off > 0) {
278 if (m == 0)
279 panic("m_copym");
280 if (off < m->m_len)
281 break;
282 off -= m->m_len;
283 m = m->m_next;
284 }
285 np = &top;
286 top = 0;
287 while (len > 0) {
288 if (m == 0) {
289 if (len != M_COPYALL)
290 panic("m_copym");
291 break;
292 }
293 MGET(n, wait, m->m_type);
294 *np = n;
295 if (n == 0)
296 goto nospace;
297 if (copyhdr) {
298 M_COPY_PKTHDR(n, m);
299 if (len == M_COPYALL)
300 n->m_pkthdr.len -= off0;
301 else
302 n->m_pkthdr.len = len;
303 copyhdr = 0;
304 }
305 n->m_len = MIN(len, m->m_len - off);
306 if (m->m_flags & M_EXT) {
307 n->m_data = m->m_data + off;
308 mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
309 n->m_ext = m->m_ext;
310 n->m_flags |= M_EXT;
311 } else
312 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
313 (unsigned)n->m_len);
314 if (len != M_COPYALL)
315 len -= n->m_len;
316 off = 0;
317 m = m->m_next;
318 np = &n->m_next;
319 }
320 if (top == 0)
321 MCFail++;
322 return (top);
323nospace:
324 m_freem(top);
325 MCFail++;
326 return (0);
327}
328
329/*
330 * Copy data from an mbuf chain starting "off" bytes from the beginning,
331 * continuing for "len" bytes, into the indicated buffer.
332 */
333m_copydata(m, off, len, cp)
334 register struct mbuf *m;
335 register int off;
336 register int len;
337 caddr_t cp;
338{
339 register unsigned count;
340
341 if (off < 0 || len < 0)
342 panic("m_copydata");
343 while (off > 0) {
344 if (m == 0)
345 panic("m_copydata");
346 if (off < m->m_len)
347 break;
348 off -= m->m_len;
349 m = m->m_next;
350 }
351 while (len > 0) {
352 if (m == 0)
353 panic("m_copydata");
354 count = MIN(m->m_len - off, len);
355 bcopy(mtod(m, caddr_t) + off, cp, count);
356 len -= count;
357 cp += count;
358 off = 0;
359 m = m->m_next;
360 }
361}
362
363/*
364 * Concatenate mbuf chain n to m.
365 * Both chains must be of the same type (e.g. MT_DATA).
366 * Any m_pkthdr is not updated.
367 */
368m_cat(m, n)
369 register struct mbuf *m, *n;
370{
371 while (m->m_next)
372 m = m->m_next;
373 while (n) {
374 if (m->m_flags & M_EXT ||
375 m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
376 /* just join the two chains */
377 m->m_next = n;
378 return;
379 }
380 /* splat the data from one into the other */
381 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
382 (u_int)n->m_len);
383 m->m_len += n->m_len;
384 n = m_free(n);
385 }
386}
387
388m_adj(mp, req_len)
389 struct mbuf *mp;
390{
391 register int len = req_len;
392 register struct mbuf *m;
393 register count;
394
395 if ((m = mp) == NULL)
396 return;
397 if (len >= 0) {
398 /*
399 * Trim from head.
400 */
401 while (m != NULL && len > 0) {
402 if (m->m_len <= len) {
403 len -= m->m_len;
404 m->m_len = 0;
405 m = m->m_next;
406 } else {
407 m->m_len -= len;
408 m->m_data += len;
409 len = 0;
410 }
411 }
412 m = mp;
413 if (mp->m_flags & M_PKTHDR)
414 m->m_pkthdr.len -= (req_len - len);
415 } else {
416 /*
417 * Trim from tail. Scan the mbuf chain,
418 * calculating its length and finding the last mbuf.
419 * If the adjustment only affects this mbuf, then just
420 * adjust and return. Otherwise, rescan and truncate
421 * after the remaining size.
422 */
423 len = -len;
424 count = 0;
425 for (;;) {
426 count += m->m_len;
427 if (m->m_next == (struct mbuf *)0)
428 break;
429 m = m->m_next;
430 }
431 if (m->m_len >= len) {
432 m->m_len -= len;
433 if ((mp = m)->m_flags & M_PKTHDR)
434 m->m_pkthdr.len -= len;
435 return;
436 }
437 count -= len;
438 if (count < 0)
439 count = 0;
440 /*
441 * Correct length for chain is "count".
442 * Find the mbuf with last data, adjust its length,
443 * and toss data from remaining mbufs on chain.
444 */
445 m = mp;
446 if (m->m_flags & M_PKTHDR)
447 m->m_pkthdr.len = count;
448 for (; m; m = m->m_next) {
449 if (m->m_len >= count) {
450 m->m_len = count;
451 break;
452 }
453 count -= m->m_len;
454 }
455 while (m = m->m_next)
456 m->m_len = 0;
457 }
458}
459
460/*
461 * Rearange an mbuf chain so that len bytes are contiguous
462 * and in the data area of an mbuf (so that mtod and dtom
463 * will work for a structure of size len). Returns the resulting
464 * mbuf chain on success, frees it and returns null on failure.
465 * If there is room, it will add up to max_protohdr-len extra bytes to the
466 * contiguous region in an attempt to avoid being called next time.
467 */
468int MPFail;
469
470struct mbuf *
471m_pullup(n, len)
472 register struct mbuf *n;
473 int len;
474{
475 register struct mbuf *m;
476 register int count;
477 int space;
478
479 /*
480 * If first mbuf has no cluster, and has room for len bytes
481 * without shifting current data, pullup into it,
482 * otherwise allocate a new mbuf to prepend to the chain.
483 */
484 if ((n->m_flags & M_EXT) == 0 &&
485 n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
486 if (n->m_len >= len)
487 return (n);
488 m = n;
489 n = n->m_next;
490 len -= m->m_len;
491 } else {
492 if (len > MHLEN)
493 goto bad;
494 MGET(m, M_DONTWAIT, n->m_type);
495 if (m == 0)
496 goto bad;
497 m->m_len = 0;
498 if (n->m_flags & M_PKTHDR) {
499 M_COPY_PKTHDR(m, n);
500 n->m_flags &= ~M_PKTHDR;
501 }
502 }
503 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
504 do {
505 count = min(min(max(len, max_protohdr), space), n->m_len);
506 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
507 (unsigned)count);
508 len -= count;
509 m->m_len += count;
510 n->m_len -= count;
511 space -= count;
512 if (n->m_len)
513 n->m_data += count;
514 else
515 n = m_free(n);
516 } while (len > 0 && n);
517 if (len > 0) {
518 (void) m_free(m);
519 goto bad;
520 }
521 m->m_next = n;
522 return (m);
523bad:
524 m_freem(n);
525 MPFail++;
526 return (0);
527}