2 * Copyright (c) 1995 Danny Gasparovski
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
9 * mbuf's in SLiRP are much simpler than the real mbufs in
10 * FreeBSD. They are fixed size, determined by the MTU,
11 * so that one whole packet can fit. Mbuf's cannot be
12 * chained together. If there's more data than the mbuf
13 * could hold, an external g_malloced buffer is pointed to
14 * by m_ext (and the data pointers) and M_EXT is set in
20 #define MBUF_THRESH 30
23 * Find a nice value for msize
26 (offsetof(struct mbuf, m_dat) + IF_MAXLINKHDR + TCPIPHDR_DELTA + IF_MTU)
31 slirp
->m_freelist
.qh_link
= slirp
->m_freelist
.qh_rlink
= &slirp
->m_freelist
;
32 slirp
->m_usedlist
.qh_link
= slirp
->m_usedlist
.qh_rlink
= &slirp
->m_usedlist
;
35 void m_cleanup(Slirp
*slirp
)
37 struct mbuf
*m
, *next
;
39 m
= (struct mbuf
*) slirp
->m_usedlist
.qh_link
;
40 while ((struct quehead
*) m
!= &slirp
->m_usedlist
) {
42 if (m
->m_flags
& M_EXT
) {
48 m
= (struct mbuf
*) slirp
->m_freelist
.qh_link
;
49 while ((struct quehead
*) m
!= &slirp
->m_freelist
) {
57 * Get an mbuf from the free list, if there are none
60 * Because fragmentation can occur if we alloc new mbufs and
61 * free old mbufs, we mark all mbufs above mbuf_thresh as M_DOFREE,
62 * which tells m_free to actually g_free() it
67 register struct mbuf
*m
;
72 if (slirp
->m_freelist
.qh_link
== &slirp
->m_freelist
) {
73 m
= g_malloc(SLIRP_MSIZE
);
74 slirp
->mbuf_alloced
++;
75 if (slirp
->mbuf_alloced
> MBUF_THRESH
)
79 m
= (struct mbuf
*) slirp
->m_freelist
.qh_link
;
83 /* Insert it in the used list */
84 insque(m
,&slirp
->m_usedlist
);
85 m
->m_flags
= (flags
| M_USEDLIST
);
88 m
->m_size
= SLIRP_MSIZE
- offsetof(struct mbuf
, m_dat
);
93 m
->resolution_requested
= false;
94 m
->expiration_date
= (uint64_t)-1;
95 DEBUG_ARG("m = %p", m
);
100 m_free(struct mbuf
*m
)
103 DEBUG_CALL("m_free");
104 DEBUG_ARG("m = %p", m
);
107 /* Remove from m_usedlist */
108 if (m
->m_flags
& M_USEDLIST
)
111 /* If it's M_EXT, free() it */
112 if (m
->m_flags
& M_EXT
) {
116 * Either free() it or put it on the free list
118 if (m
->m_flags
& M_DOFREE
) {
119 m
->slirp
->mbuf_alloced
--;
121 } else if ((m
->m_flags
& M_FREELIST
) == 0) {
122 insque(m
,&m
->slirp
->m_freelist
);
123 m
->m_flags
= M_FREELIST
; /* Clobber other flags */
129 * Copy data from one mbuf to the end of
130 * the other.. if result is too big for one mbuf, allocate
131 * an M_EXT data segment
134 m_cat(struct mbuf
*m
, struct mbuf
*n
)
137 * If there's no room, realloc
139 if (M_FREEROOM(m
) < n
->m_len
)
140 m_inc(m
, m
->m_len
+ n
->m_len
);
142 memcpy(m
->m_data
+m
->m_len
, n
->m_data
, n
->m_len
);
143 m
->m_len
+= n
->m_len
;
149 /* make m 'size' bytes large from m_data */
151 m_inc(struct mbuf
*m
, int size
)
155 /* some compilers throw up on gotos. This one we can fake. */
156 if (M_ROOM(m
) > size
) {
160 if (m
->m_flags
& M_EXT
) {
161 gapsize
= m
->m_data
- m
->m_ext
;
162 m
->m_ext
= g_realloc(m
->m_ext
, size
+ gapsize
);
164 gapsize
= m
->m_data
- m
->m_dat
;
165 m
->m_ext
= g_malloc(size
+ gapsize
);
166 memcpy(m
->m_ext
, m
->m_dat
, m
->m_size
);
170 m
->m_data
= m
->m_ext
+ gapsize
;
171 m
->m_size
= size
+ gapsize
;
177 m_adj(struct mbuf
*m
, int len
)
194 * Copy len bytes from m, starting off bytes into n
197 m_copy(struct mbuf
*n
, struct mbuf
*m
, int off
, int len
)
199 if (len
> M_FREEROOM(n
))
202 memcpy((n
->m_data
+ n
->m_len
), (m
->m_data
+ off
), len
);
209 * Given a pointer into an mbuf, return the mbuf
210 * XXX This is a kludge, I should eliminate the need for it
211 * Fortunately, it's not used often
214 dtom(Slirp
*slirp
, void *dat
)
219 DEBUG_ARG("dat = %p", dat
);
221 /* bug corrected for M_EXT buffers */
222 for (m
= (struct mbuf
*) slirp
->m_usedlist
.qh_link
;
223 (struct quehead
*) m
!= &slirp
->m_usedlist
;
225 if (m
->m_flags
& M_EXT
) {
226 if( (char *)dat
>=m
->m_ext
&& (char *)dat
<(m
->m_ext
+ m
->m_size
) )
229 if( (char *)dat
>= m
->m_dat
&& (char *)dat
<(m
->m_dat
+ m
->m_size
) )
234 DEBUG_ERROR("dtom failed");
236 return (struct mbuf
*)0;