2 * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
3 * based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
4 * Internet Initiative Japan, Inc (IIJ)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/usr.sbin/ppp/mbuf.c,v 1.36.2.5 2002/09/01 02:12:28 brian Exp $
29 * $DragonFly: src/usr.sbin/ppp/mbuf.c,v 1.3 2008/05/19 10:19:49 corecode Exp $
32 #include <sys/types.h>
33 #include <sys/select.h>
46 #include "descriptor.h"
50 #define BUCKET_CHUNK 20
51 #define BUCKET_HASH 256
60 static struct mbucket
{
65 } *bucket
[(M_MAXLEN
+ sizeof(struct mbuf
)) / BUCKET_HASH
];
67 #define M_BINDEX(sz) (((sz) + sizeof(struct mbuf) - 1) / BUCKET_HASH)
68 #define M_BUCKET(sz) (bucket + M_BINDEX(sz))
69 #define M_ROUNDUP(sz) ((M_BINDEX(sz) + 1) * BUCKET_HASH)
71 static struct memmap
{
77 static unsigned long long mbuf_Mallocs
, mbuf_Frees
;
80 m_length(struct mbuf
*bp
)
84 for (len
= 0; bp
; bp
= bp
->m_next
)
92 static const char * const mbufdesc
[MB_MAX
] = {
93 "ip in", "ip out", "ipv6 in", "ipv6 out", "nat in", "nat out",
94 "mp in", "mp out", "vj in", "vj out", "icompd in", "icompd out",
95 "compd in", "compd out", "lqr in", "lqr out", "echo in", "echo out",
96 "proto in", "proto out", "acf in", "acf out", "sync in", "sync out",
97 "hdlc in", "hdlc out", "async in", "async out", "cbcp in", "cbcp out",
98 "chap in", "chap out", "pap in", "pap out", "ccp in", "ccp out",
99 "ipcp in", "ipcp out", "ipv6cp in", "ipv6cp out", "lcp in", "lcp out"
102 return type
< 0 || type
>= MB_MAX
? "unknown" : mbufdesc
[type
];
106 m_get(size_t m_len
, int type
)
113 log_Printf(LogERROR
, "Bad mbuf type %d\n", type
);
117 if (m_len
> M_MAXLEN
|| m_len
== 0) {
118 log_Printf(LogERROR
, "Request for mbuf size %lu (\"%s\") denied !\n",
119 (u_long
)m_len
, mbuftype(type
));
120 AbortProgram(EX_OSERR
);
123 mb
= M_BUCKET(m_len
);
124 size
= M_ROUNDUP(m_len
);
127 /* We've got some free blocks of the right size */
129 if (--(*mb
)->u
.f
.count
== 0)
130 *mb
= (*mb
)->u
.f
.next
;
132 ((struct mbucket
*)((char *)*mb
+ size
))->u
.f
.count
= (*mb
)->u
.f
.count
;
133 *mb
= (struct mbucket
*)((char *)*mb
+ size
);
134 (*mb
)->u
.f
.next
= NULL
;
138 * Allocate another chunk of mbufs, use the first and put the rest on
141 *mb
= (struct mbucket
*)malloc(BUCKET_CHUNK
* size
);
143 log_Printf(LogALERT
, "Failed to allocate memory (%lu)\n",
144 (unsigned long)BUCKET_CHUNK
* size
);
145 AbortProgram(EX_OSERR
);
148 *mb
= (struct mbucket
*)((char *)*mb
+ size
);
149 (*mb
)->u
.f
.count
= BUCKET_CHUNK
- 1;
150 (*mb
)->u
.f
.next
= NULL
;
155 memset(bp
, '\0', sizeof(struct mbuf
));
156 bp
->m_size
= size
- sizeof *bp
;
160 MemMap
[type
].fragments
++;
161 MemMap
[type
].octets
+= bp
->m_size
;
167 m_free(struct mbuf
*bp
)
169 struct mbucket
**mb
, *f
;
172 if ((f
= (struct mbucket
*)bp
) != NULL
) {
173 MemMap
[bp
->m_type
].fragments
--;
174 MemMap
[bp
->m_type
].octets
-= bp
->m_size
;
177 mb
= M_BUCKET(bp
->m_size
);
190 m_freem(struct mbuf
*bp
)
197 mbuf_Read(struct mbuf
*bp
, void *v
, size_t len
)
202 while (bp
&& len
> 0) {
208 memcpy(ptr
, MBUF_CTOP(bp
), nb
);
218 while (bp
&& bp
->m_len
== 0)
225 mbuf_View(struct mbuf
*bp
, void *v
, size_t len
)
230 while (bp
&& l
> 0) {
235 memcpy(ptr
, MBUF_CTOP(bp
), nb
);
245 m_prepend(struct mbuf
*bp
, const void *ptr
, size_t len
, size_t extra
)
249 if (bp
&& bp
->m_offset
) {
250 if (bp
->m_offset
>= len
) {
253 memcpy(MBUF_CTOP(bp
), ptr
, len
);
257 memcpy(bp
+ 1, (const char *)ptr
+ len
, bp
->m_offset
);
258 bp
->m_len
+= bp
->m_offset
;
262 head
= m_get(len
+ extra
, bp
? bp
->m_type
: MB_UNKNOWN
);
263 head
->m_offset
= extra
;
264 head
->m_len
-= extra
;
266 memcpy(MBUF_CTOP(head
), ptr
, len
);
273 m_adj(struct mbuf
*bp
, ssize_t n
)
286 if ((n
= m_length(bp
) + n
) <= 0) {
290 for (; bp
; bp
= bp
->m_next
, n
-= bp
->m_len
)
303 mbuf_Write(struct mbuf
*bp
, const void *ptr
, size_t m_len
)
313 nb
= (m_len
< bp
->m_len
) ? m_len
: bp
->m_len
;
314 memcpy(MBUF_CTOP(bp
), ptr
, nb
);
321 mbuf_Show(struct cmdargs
const *arg
)
325 prompt_Printf(arg
->prompt
, "Fragments (octets) in use:\n");
326 for (i
= 0; i
< MB_MAX
; i
+= 2)
327 prompt_Printf(arg
->prompt
, "%10.10s: %04lu (%06lu)\t"
328 "%10.10s: %04lu (%06lu)\n",
329 mbuftype(i
), (u_long
)MemMap
[i
].fragments
,
330 (u_long
)MemMap
[i
].octets
, mbuftype(i
+1),
331 (u_long
)MemMap
[i
+1].fragments
, (u_long
)MemMap
[i
+1].octets
);
334 prompt_Printf(arg
->prompt
, "%10.10s: %04lu (%06lu)\n",
335 mbuftype(i
), (u_long
)MemMap
[i
].fragments
,
336 (u_long
)MemMap
[i
].octets
);
338 prompt_Printf(arg
->prompt
, "Mallocs: %llu, Frees: %llu\n",
339 mbuf_Mallocs
, mbuf_Frees
);
345 m_dequeue(struct mqueue
*q
)
349 log_Printf(LogDEBUG
, "m_dequeue: queue len = %lu\n", (u_long
)q
->len
);
352 q
->top
= q
->top
->m_nextpkt
;
354 if (q
->top
== NULL
) {
357 log_Printf(LogERROR
, "m_dequeue: Not zero (%lu)!!!\n",
360 bp
->m_nextpkt
= NULL
;
367 m_enqueue(struct mqueue
*queue
, struct mbuf
*bp
)
371 queue
->last
->m_nextpkt
= bp
;
374 queue
->last
= queue
->top
= bp
;
376 log_Printf(LogDEBUG
, "m_enqueue: len = %lu\n", (unsigned long)queue
->len
);
381 m_pullup(struct mbuf
*bp
)
383 /* Put it all in one contigous (aligned) mbuf */
386 if (bp
->m_next
!= NULL
) {
390 nbp
= m_get(m_length(bp
), bp
->m_type
);
392 for (cp
= MBUF_CTOP(nbp
); bp
; bp
= m_free(bp
)) {
393 memcpy(cp
, MBUF_CTOP(bp
), bp
->m_len
);
398 #ifndef __i386__ /* Do any other archs not care about alignment ? */
399 else if ((bp
->m_offset
& (sizeof(long) - 1)) != 0) {
400 bcopy(MBUF_CTOP(bp
), bp
+ 1, bp
->m_len
);
410 m_settype(struct mbuf
*bp
, int type
)
412 for (; bp
; bp
= bp
->m_next
)
413 if (type
!= bp
->m_type
) {
414 MemMap
[bp
->m_type
].fragments
--;
415 MemMap
[bp
->m_type
].octets
-= bp
->m_size
;
417 MemMap
[type
].fragments
++;
418 MemMap
[type
].octets
+= bp
->m_size
;
423 m_append(struct mbuf
*bp
, const void *v
, size_t sz
)
430 if (m
->m_size
- m
->m_len
> sz
)
433 m
->m_next
= m_prepend(NULL
, v
, sz
, 0);
435 bp
= m_prepend(NULL
, v
, sz
, 0);