Commit | Line | Data |
---|---|---|
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 |
2fc65aea | 34 | * $Id: uipc_mbuf.c,v 1.8 1994/05/05 23:48:00 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 */ |
50 | struct mbstat mbstat; | |
51 | int nmbclusters; | |
52 | union mcluster *mclfree; | |
53 | int max_linkhdr; | |
54 | int max_protohdr; | |
55 | int max_hdr; | |
56 | int max_datalen; | |
57 | ||
15637ed4 RG |
58 | extern vm_map_t mb_map; |
59 | struct mbuf *mbutl; | |
60 | char *mclrefcnt; | |
61 | ||
4c45483e GW |
62 | static void m_reclaim(void); |
63 | ||
64 | void | |
15637ed4 RG |
65 | mbinit() |
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; | |
79 | bad: | |
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 | 89 | int |
15637ed4 RG |
90 | m_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 | */ | |
124 | struct mbuf * | |
125 | m_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 | */ | |
140 | struct mbuf * | |
141 | m_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 | 153 | static void |
15637ed4 RG |
154 | m_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 | */ | |
173 | struct mbuf * | |
174 | m_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 | ||
183 | struct mbuf * | |
184 | m_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 | ||
193 | struct mbuf * | |
194 | m_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 | ||
206 | struct mbuf * | |
207 | m_free(m) | |
208 | struct mbuf *m; | |
209 | { | |
210 | register struct mbuf *n; | |
211 | ||
212 | MFREE(m, n); | |
213 | return (n); | |
214 | } | |
215 | ||
4c45483e | 216 | void |
15637ed4 RG |
217 | m_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 | */ | |
238 | struct mbuf * | |
239 | m_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 | */ | |
267 | int MCFail; | |
268 | ||
269 | struct mbuf * | |
270 | m_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 = ⊤ | |
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; | |
4014f930 DG |
315 | if (m->m_ext.ext_free == (void (*)())0) |
316 | mclrefcnt[mtocl(m->m_ext.ext_buf)]++; | |
15637ed4 RG |
317 | n->m_ext = m->m_ext; |
318 | n->m_flags |= M_EXT; | |
319 | } else | |
320 | bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), | |
321 | (unsigned)n->m_len); | |
322 | if (len != M_COPYALL) | |
323 | len -= n->m_len; | |
324 | off = 0; | |
325 | m = m->m_next; | |
326 | np = &n->m_next; | |
327 | } | |
328 | if (top == 0) | |
329 | MCFail++; | |
330 | return (top); | |
331 | nospace: | |
332 | m_freem(top); | |
333 | MCFail++; | |
334 | return (0); | |
335 | } | |
336 | ||
337 | /* | |
338 | * Copy data from an mbuf chain starting "off" bytes from the beginning, | |
339 | * continuing for "len" bytes, into the indicated buffer. | |
340 | */ | |
4c45483e | 341 | void |
15637ed4 RG |
342 | m_copydata(m, off, len, cp) |
343 | register struct mbuf *m; | |
344 | register int off; | |
345 | register int len; | |
346 | caddr_t cp; | |
347 | { | |
348 | register unsigned count; | |
349 | ||
350 | if (off < 0 || len < 0) | |
351 | panic("m_copydata"); | |
352 | while (off > 0) { | |
353 | if (m == 0) | |
354 | panic("m_copydata"); | |
355 | if (off < m->m_len) | |
356 | break; | |
357 | off -= m->m_len; | |
358 | m = m->m_next; | |
359 | } | |
360 | while (len > 0) { | |
361 | if (m == 0) | |
362 | panic("m_copydata"); | |
363 | count = MIN(m->m_len - off, len); | |
364 | bcopy(mtod(m, caddr_t) + off, cp, count); | |
365 | len -= count; | |
366 | cp += count; | |
367 | off = 0; | |
368 | m = m->m_next; | |
369 | } | |
370 | } | |
371 | ||
372 | /* | |
373 | * Concatenate mbuf chain n to m. | |
374 | * Both chains must be of the same type (e.g. MT_DATA). | |
375 | * Any m_pkthdr is not updated. | |
376 | */ | |
4c45483e | 377 | void |
15637ed4 RG |
378 | m_cat(m, n) |
379 | register struct mbuf *m, *n; | |
380 | { | |
381 | while (m->m_next) | |
382 | m = m->m_next; | |
383 | while (n) { | |
384 | if (m->m_flags & M_EXT || | |
385 | m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) { | |
386 | /* just join the two chains */ | |
387 | m->m_next = n; | |
388 | return; | |
389 | } | |
390 | /* splat the data from one into the other */ | |
391 | bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, | |
392 | (u_int)n->m_len); | |
393 | m->m_len += n->m_len; | |
394 | n = m_free(n); | |
395 | } | |
396 | } | |
397 | ||
4c45483e | 398 | void |
15637ed4 RG |
399 | m_adj(mp, req_len) |
400 | struct mbuf *mp; | |
4c45483e | 401 | int req_len; |
15637ed4 RG |
402 | { |
403 | register int len = req_len; | |
404 | register struct mbuf *m; | |
405 | register count; | |
406 | ||
407 | if ((m = mp) == NULL) | |
408 | return; | |
409 | if (len >= 0) { | |
410 | /* | |
411 | * Trim from head. | |
412 | */ | |
413 | while (m != NULL && len > 0) { | |
414 | if (m->m_len <= len) { | |
415 | len -= m->m_len; | |
416 | m->m_len = 0; | |
417 | m = m->m_next; | |
418 | } else { | |
419 | m->m_len -= len; | |
420 | m->m_data += len; | |
421 | len = 0; | |
422 | } | |
423 | } | |
424 | m = mp; | |
425 | if (mp->m_flags & M_PKTHDR) | |
426 | m->m_pkthdr.len -= (req_len - len); | |
427 | } else { | |
428 | /* | |
429 | * Trim from tail. Scan the mbuf chain, | |
430 | * calculating its length and finding the last mbuf. | |
431 | * If the adjustment only affects this mbuf, then just | |
432 | * adjust and return. Otherwise, rescan and truncate | |
433 | * after the remaining size. | |
434 | */ | |
435 | len = -len; | |
436 | count = 0; | |
437 | for (;;) { | |
438 | count += m->m_len; | |
439 | if (m->m_next == (struct mbuf *)0) | |
440 | break; | |
441 | m = m->m_next; | |
442 | } | |
443 | if (m->m_len >= len) { | |
444 | m->m_len -= len; | |
d250cecd GW |
445 | if (mp->m_flags & M_PKTHDR) |
446 | mp->m_pkthdr.len -= len; | |
15637ed4 RG |
447 | return; |
448 | } | |
449 | count -= len; | |
450 | if (count < 0) | |
451 | count = 0; | |
452 | /* | |
453 | * Correct length for chain is "count". | |
454 | * Find the mbuf with last data, adjust its length, | |
455 | * and toss data from remaining mbufs on chain. | |
456 | */ | |
457 | m = mp; | |
458 | if (m->m_flags & M_PKTHDR) | |
459 | m->m_pkthdr.len = count; | |
460 | for (; m; m = m->m_next) { | |
461 | if (m->m_len >= count) { | |
462 | m->m_len = count; | |
463 | break; | |
464 | } | |
465 | count -= m->m_len; | |
466 | } | |
467 | while (m = m->m_next) | |
468 | m->m_len = 0; | |
469 | } | |
470 | } | |
471 | ||
472 | /* | |
473 | * Rearange an mbuf chain so that len bytes are contiguous | |
474 | * and in the data area of an mbuf (so that mtod and dtom | |
475 | * will work for a structure of size len). Returns the resulting | |
476 | * mbuf chain on success, frees it and returns null on failure. | |
477 | * If there is room, it will add up to max_protohdr-len extra bytes to the | |
478 | * contiguous region in an attempt to avoid being called next time. | |
479 | */ | |
480 | int MPFail; | |
481 | ||
482 | struct mbuf * | |
483 | m_pullup(n, len) | |
484 | register struct mbuf *n; | |
485 | int len; | |
486 | { | |
487 | register struct mbuf *m; | |
488 | register int count; | |
489 | int space; | |
490 | ||
491 | /* | |
492 | * If first mbuf has no cluster, and has room for len bytes | |
493 | * without shifting current data, pullup into it, | |
494 | * otherwise allocate a new mbuf to prepend to the chain. | |
495 | */ | |
496 | if ((n->m_flags & M_EXT) == 0 && | |
497 | n->m_data + len < &n->m_dat[MLEN] && n->m_next) { | |
498 | if (n->m_len >= len) | |
499 | return (n); | |
500 | m = n; | |
501 | n = n->m_next; | |
502 | len -= m->m_len; | |
503 | } else { | |
504 | if (len > MHLEN) | |
505 | goto bad; | |
506 | MGET(m, M_DONTWAIT, n->m_type); | |
507 | if (m == 0) | |
508 | goto bad; | |
509 | m->m_len = 0; | |
510 | if (n->m_flags & M_PKTHDR) { | |
511 | M_COPY_PKTHDR(m, n); | |
512 | n->m_flags &= ~M_PKTHDR; | |
513 | } | |
514 | } | |
515 | space = &m->m_dat[MLEN] - (m->m_data + m->m_len); | |
516 | do { | |
517 | count = min(min(max(len, max_protohdr), space), n->m_len); | |
518 | bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, | |
519 | (unsigned)count); | |
520 | len -= count; | |
521 | m->m_len += count; | |
522 | n->m_len -= count; | |
523 | space -= count; | |
524 | if (n->m_len) | |
525 | n->m_data += count; | |
526 | else | |
527 | n = m_free(n); | |
528 | } while (len > 0 && n); | |
529 | if (len > 0) { | |
530 | (void) m_free(m); | |
531 | goto bad; | |
532 | } | |
533 | m->m_next = n; | |
534 | return (m); | |
535 | bad: | |
536 | m_freem(n); | |
537 | MPFail++; | |
538 | return (0); | |
539 | } | |
4c45483e GW |
540 | |
541 | /* | |
542 | * Copy data from a buffer back into the indicated mbuf chain, | |
543 | * starting "off" bytes from the beginning, extending the mbuf | |
544 | * chain if necessary. | |
545 | */ | |
546 | void | |
547 | m_copyback(m0, off, len, cp) | |
548 | struct mbuf *m0; | |
549 | register int off; | |
550 | register int len; | |
551 | caddr_t cp; | |
552 | ||
553 | { | |
554 | register int mlen; | |
555 | register struct mbuf *m = m0, *n; | |
556 | int totlen = 0; | |
557 | ||
558 | if (m0 == 0) | |
559 | return; | |
560 | while (off > (mlen = m->m_len)) { | |
561 | off -= mlen; | |
562 | totlen += mlen; | |
563 | if (m->m_next == 0) { | |
564 | n = m_getclr(M_DONTWAIT, m->m_type); | |
565 | if (n == 0) | |
566 | goto out; | |
567 | n->m_len = min(MLEN, len + off); | |
568 | m->m_next = n; | |
569 | } | |
570 | m = m->m_next; | |
571 | } | |
572 | while (len > 0) { | |
573 | mlen = min (m->m_len - off, len); | |
574 | bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen); | |
575 | cp += mlen; | |
576 | len -= mlen; | |
577 | mlen += off; | |
578 | off = 0; | |
579 | totlen += mlen; | |
580 | if (len == 0) | |
581 | break; | |
582 | if (m->m_next == 0) { | |
583 | n = m_get(M_DONTWAIT, m->m_type); | |
584 | if (n == 0) | |
585 | break; | |
586 | n->m_len = min(MLEN, len); | |
587 | m->m_next = n; | |
588 | } | |
589 | m = m->m_next; | |
590 | } | |
591 | out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) | |
592 | m->m_pkthdr.len = totlen; | |
593 | } | |
594 | ||
fde1aeb2 GW |
595 | struct mbuf * |
596 | m_split (m0, len0, wait) | |
597 | register struct mbuf *m0; | |
598 | int len0; | |
599 | int wait; | |
600 | { | |
601 | register struct mbuf *m, *n; | |
602 | unsigned len = len0, remain; | |
603 | ||
604 | for (m = m0; m && len > m -> m_len; m = m -> m_next) | |
605 | len -= m -> m_len; | |
606 | if (m == 0) | |
607 | return (0); | |
608 | remain = m -> m_len - len; | |
609 | if (m0 -> m_flags & M_PKTHDR) { | |
610 | MGETHDR(n, wait, m0 -> m_type); | |
611 | if (n == 0) | |
612 | return (0); | |
613 | n -> m_pkthdr.rcvif = m0 -> m_pkthdr.rcvif; | |
614 | n -> m_pkthdr.len = m0 -> m_pkthdr.len - len0; | |
615 | m0 -> m_pkthdr.len = len0; | |
616 | if (m -> m_flags & M_EXT) | |
617 | goto extpacket; | |
618 | if (remain > MHLEN) { | |
619 | /* m can't be the lead packet */ | |
620 | MH_ALIGN(n, 0); | |
621 | n -> m_next = m_split (m, len, wait); | |
622 | if (n -> m_next == 0) { | |
623 | (void) m_free (n); | |
624 | return (0); | |
625 | } else | |
626 | return (n); | |
627 | } else | |
628 | MH_ALIGN(n, remain); | |
629 | } else if (remain == 0) { | |
630 | n = m -> m_next; | |
631 | m -> m_next = 0; | |
632 | return (n); | |
633 | } else { | |
634 | MGET(n, wait, m -> m_type); | |
635 | if (n == 0) | |
636 | return (0); | |
637 | M_ALIGN(n, remain); | |
638 | } | |
639 | extpacket: | |
640 | if (m -> m_flags & M_EXT) { | |
641 | n -> m_flags |= M_EXT; | |
642 | n -> m_ext = m -> m_ext; | |
4014f930 DG |
643 | if (m->m_ext.ext_free == (void (*)())0) |
644 | mclrefcnt[mtocl (m -> m_ext.ext_buf)]++; | |
fde1aeb2 GW |
645 | n -> m_data = m -> m_data + len; |
646 | } else { | |
647 | bcopy (mtod (m, caddr_t) + len, mtod (n, caddr_t), remain); | |
648 | } | |
649 | n -> m_len = remain; | |
650 | m -> m_len = len; | |
651 | n -> m_next = m -> m_next; | |
652 | m -> m_next = 0; | |
653 | return (n); | |
654 | } | |
655 | ||
656 | /* The following taken from netiso/iso_chksum.c */ | |
657 | struct mbuf * | |
658 | m_append(head, m) /* XXX */ | |
659 | struct mbuf *head, *m; | |
660 | { | |
661 | register struct mbuf *n; | |
662 | ||
663 | if (m == 0) | |
664 | return head; | |
665 | if (head == 0) | |
666 | return m; | |
667 | n = head; | |
668 | while (n->m_next) | |
669 | n = n->m_next; | |
670 | n->m_next = m; | |
671 | return head; | |
672 | } | |
673 | ||
674 | /* | |
675 | * FUNCTION: m_datalen | |
676 | * | |
677 | * PURPOSE: returns length of the mbuf chain. | |
678 | * used all over the iso code. | |
679 | * | |
680 | * RETURNS: integer | |
681 | * | |
682 | * SIDE EFFECTS: none | |
683 | * | |
684 | * NOTES: | |
685 | */ | |
686 | int | |
687 | m_datalen (morig) /* XXX */ | |
688 | struct mbuf *morig; | |
689 | { | |
690 | int s = splimp(); | |
691 | register struct mbuf *n=morig; | |
692 | register int datalen = 0; | |
693 | ||
694 | if( morig == (struct mbuf *)0) | |
695 | return 0; | |
696 | for(;;) { | |
697 | datalen += n->m_len; | |
698 | if (n->m_next == (struct mbuf *)0 ) { | |
699 | break; | |
700 | } | |
701 | n = n->m_next; | |
702 | } | |
703 | splx(s); | |
704 | return datalen; | |
705 | } | |
706 | ||
707 | int | |
708 | m_compress(in, out) | |
709 | register struct mbuf *in, **out; | |
710 | { | |
711 | register int datalen = 0; | |
712 | int s = splimp(); | |
713 | ||
714 | if(!in->m_next) { | |
715 | *out = in; | |
716 | splx(s); | |
717 | return in->m_len; | |
718 | } | |
719 | MGET((*out), M_DONTWAIT, MT_DATA); | |
720 | if(! (*out)) { | |
721 | *out = in; | |
722 | splx(s); | |
723 | return -1; | |
724 | } | |
725 | (*out)->m_len = 0; | |
726 | (*out)->m_act = 0; | |
727 | ||
728 | while (in) { | |
fde1aeb2 GW |
729 | if ( in->m_len == 0) { |
730 | in = in->m_next; | |
731 | continue; | |
732 | } | |
733 | if (((*out)->m_flags & M_EXT) == 0) { | |
734 | int len; | |
735 | ||
736 | len = M_TRAILINGSPACE(*out); | |
737 | len = MIN(len, in->m_len); | |
738 | datalen += len; | |
739 | ||
740 | bcopy(mtod(in, caddr_t), mtod((*out), caddr_t) + (*out)->m_len, | |
741 | (unsigned)len); | |
742 | ||
743 | (*out)->m_len += len; | |
744 | in->m_len -= len; | |
745 | continue; | |
746 | } else { | |
747 | /* (*out) is full */ | |
748 | if( !((*out)->m_next = m_get(M_DONTWAIT, MT_DATA))) { | |
749 | m_freem(*out); | |
750 | *out = in; | |
751 | splx(s); | |
752 | return -1; | |
753 | } | |
754 | (*out)->m_len = 0; | |
755 | (*out)->m_act = 0; | |
756 | *out = (*out)->m_next; | |
757 | } | |
758 | } | |
759 | m_freem(in); | |
760 | splx(s); | |
761 | return datalen; | |
762 | } |