1 /* SPDX-License-Identifier: BSD-3-Clause */
3 * Copyright (c) 1995 Danny Gasparovski
7 * mbuf's in SLiRP are much simpler than the real mbufs in
8 * FreeBSD. They are fixed size, determined by the MTU,
9 * so that one whole packet can fit. Mbuf's cannot be
10 * chained together. If there's more data than the mbuf
11 * could hold, an external g_malloced buffer is pointed to
12 * by m_ext (and the data pointers) and M_EXT is set in
18 #define MBUF_THRESH 30
21 * Find a nice value for msize
24 (offsetof(struct mbuf, m_dat) + IF_MAXLINKHDR + TCPIPHDR_DELTA + IF_MTU)
29 slirp
->m_freelist
.qh_link
= slirp
->m_freelist
.qh_rlink
= &slirp
->m_freelist
;
30 slirp
->m_usedlist
.qh_link
= slirp
->m_usedlist
.qh_rlink
= &slirp
->m_usedlist
;
33 void m_cleanup(Slirp
*slirp
)
35 struct mbuf
*m
, *next
;
37 m
= (struct mbuf
*) slirp
->m_usedlist
.qh_link
;
38 while ((struct quehead
*) m
!= &slirp
->m_usedlist
) {
40 if (m
->m_flags
& M_EXT
) {
46 m
= (struct mbuf
*) slirp
->m_freelist
.qh_link
;
47 while ((struct quehead
*) m
!= &slirp
->m_freelist
) {
55 * Get an mbuf from the free list, if there are none
58 * Because fragmentation can occur if we alloc new mbufs and
59 * free old mbufs, we mark all mbufs above mbuf_thresh as M_DOFREE,
60 * which tells m_free to actually g_free() it
65 register struct mbuf
*m
;
70 if (slirp
->m_freelist
.qh_link
== &slirp
->m_freelist
) {
71 m
= g_malloc(SLIRP_MSIZE
);
72 slirp
->mbuf_alloced
++;
73 if (slirp
->mbuf_alloced
> MBUF_THRESH
)
77 m
= (struct mbuf
*) slirp
->m_freelist
.qh_link
;
81 /* Insert it in the used list */
82 insque(m
,&slirp
->m_usedlist
);
83 m
->m_flags
= (flags
| M_USEDLIST
);
86 m
->m_size
= SLIRP_MSIZE
- offsetof(struct mbuf
, m_dat
);
91 m
->resolution_requested
= false;
92 m
->expiration_date
= (uint64_t)-1;
93 DEBUG_ARG("m = %p", m
);
98 m_free(struct mbuf
*m
)
101 DEBUG_CALL("m_free");
102 DEBUG_ARG("m = %p", m
);
105 /* Remove from m_usedlist */
106 if (m
->m_flags
& M_USEDLIST
)
109 /* If it's M_EXT, free() it */
110 if (m
->m_flags
& M_EXT
) {
114 * Either free() it or put it on the free list
116 if (m
->m_flags
& M_DOFREE
) {
117 m
->slirp
->mbuf_alloced
--;
119 } else if ((m
->m_flags
& M_FREELIST
) == 0) {
120 insque(m
,&m
->slirp
->m_freelist
);
121 m
->m_flags
= M_FREELIST
; /* Clobber other flags */
127 * Copy data from one mbuf to the end of
128 * the other.. if result is too big for one mbuf, allocate
129 * an M_EXT data segment
132 m_cat(struct mbuf
*m
, struct mbuf
*n
)
135 * If there's no room, realloc
137 if (M_FREEROOM(m
) < n
->m_len
)
138 m_inc(m
, m
->m_len
+ n
->m_len
);
140 memcpy(m
->m_data
+m
->m_len
, n
->m_data
, n
->m_len
);
141 m
->m_len
+= n
->m_len
;
147 /* make m 'size' bytes large from m_data */
149 m_inc(struct mbuf
*m
, int size
)
153 /* some compilers throw up on gotos. This one we can fake. */
154 if (M_ROOM(m
) > size
) {
158 if (m
->m_flags
& M_EXT
) {
159 gapsize
= m
->m_data
- m
->m_ext
;
160 m
->m_ext
= g_realloc(m
->m_ext
, size
+ gapsize
);
162 gapsize
= m
->m_data
- m
->m_dat
;
163 m
->m_ext
= g_malloc(size
+ gapsize
);
164 memcpy(m
->m_ext
, m
->m_dat
, m
->m_size
);
168 m
->m_data
= m
->m_ext
+ gapsize
;
169 m
->m_size
= size
+ gapsize
;
175 m_adj(struct mbuf
*m
, int len
)
192 * Copy len bytes from m, starting off bytes into n
195 m_copy(struct mbuf
*n
, struct mbuf
*m
, int off
, int len
)
197 if (len
> M_FREEROOM(n
))
200 memcpy((n
->m_data
+ n
->m_len
), (m
->m_data
+ off
), len
);
207 * Given a pointer into an mbuf, return the mbuf
208 * XXX This is a kludge, I should eliminate the need for it
209 * Fortunately, it's not used often
212 dtom(Slirp
*slirp
, void *dat
)
217 DEBUG_ARG("dat = %p", dat
);
219 /* bug corrected for M_EXT buffers */
220 for (m
= (struct mbuf
*) slirp
->m_usedlist
.qh_link
;
221 (struct quehead
*) m
!= &slirp
->m_usedlist
;
223 if (m
->m_flags
& M_EXT
) {
224 if( (char *)dat
>=m
->m_ext
&& (char *)dat
<(m
->m_ext
+ m
->m_size
) )
227 if( (char *)dat
>= m
->m_dat
&& (char *)dat
<(m
->m_dat
+ m
->m_size
) )
232 DEBUG_ERROR("dtom failed");
234 return (struct mbuf
*)0;