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
18 #include "qemu/osdep.h"
21 #define MBUF_THRESH 30
24 * Find a nice value for msize
27 (offsetof(struct mbuf, m_dat) + IF_MAXLINKHDR + TCPIPHDR_DELTA + IF_MTU)
32 slirp->m_freelist.qh_link = slirp->m_freelist.qh_rlink = &slirp->m_freelist;
33 slirp->m_usedlist.qh_link = slirp->m_usedlist.qh_rlink = &slirp->m_usedlist;
36 void m_cleanup(Slirp *slirp)
38 struct mbuf *m, *next;
40 m = (struct mbuf *) slirp->m_usedlist.qh_link;
41 while ((struct quehead *) m != &slirp->m_usedlist) {
43 if (m->m_flags & M_EXT) {
49 m = (struct mbuf *) slirp->m_freelist.qh_link;
50 while ((struct quehead *) m != &slirp->m_freelist) {
58 * Get an mbuf from the free list, if there are none
61 * Because fragmentation can occur if we alloc new mbufs and
62 * free old mbufs, we mark all mbufs above mbuf_thresh as M_DOFREE,
63 * which tells m_free to actually g_free() it
68 register struct mbuf *m;
73 if (slirp->m_freelist.qh_link == &slirp->m_freelist) {
74 m = g_malloc(SLIRP_MSIZE);
75 slirp->mbuf_alloced++;
76 if (slirp->mbuf_alloced > MBUF_THRESH)
80 m = (struct mbuf *) slirp->m_freelist.qh_link;
84 /* Insert it in the used list */
85 insque(m,&slirp->m_usedlist);
86 m->m_flags = (flags | M_USEDLIST);
89 m->m_size = SLIRP_MSIZE - offsetof(struct mbuf, m_dat);
94 m->resolution_requested = false;
95 m->expiration_date = (uint64_t)-1;
96 DEBUG_ARG("m = %p", m);
101 m_free(struct mbuf *m)
104 DEBUG_CALL("m_free");
105 DEBUG_ARG("m = %p", m);
108 /* Remove from m_usedlist */
109 if (m->m_flags & M_USEDLIST)
112 /* If it's M_EXT, free() it */
113 if (m->m_flags & M_EXT) {
117 * Either free() it or put it on the free list
119 if (m->m_flags & M_DOFREE) {
120 m->slirp->mbuf_alloced--;
122 } else if ((m->m_flags & M_FREELIST) == 0) {
123 insque(m,&m->slirp->m_freelist);
124 m->m_flags = M_FREELIST; /* Clobber other flags */
130 * Copy data from one mbuf to the end of
131 * the other.. if result is too big for one mbuf, allocate
132 * an M_EXT data segment
135 m_cat(struct mbuf *m, struct mbuf *n)
138 * If there's no room, realloc
140 if (M_FREEROOM(m) < n->m_len)
141 m_inc(m, m->m_len + n->m_len);
143 memcpy(m->m_data+m->m_len, n->m_data, n->m_len);
144 m->m_len += n->m_len;
150 /* make m 'size' bytes large from m_data */
152 m_inc(struct mbuf *m, int size)
156 /* some compilers throw up on gotos. This one we can fake. */
157 if (M_ROOM(m) > size) {
161 if (m->m_flags & M_EXT) {
162 gapsize = m->m_data - m->m_ext;
163 m->m_ext = g_realloc(m->m_ext, size + gapsize);
165 gapsize = m->m_data - m->m_dat;
166 m->m_ext = g_malloc(size + gapsize);
167 memcpy(m->m_ext, m->m_dat, m->m_size);
171 m->m_data = m->m_ext + gapsize;
172 m->m_size = size + gapsize;
178 m_adj(struct mbuf *m, int len)
195 * Copy len bytes from m, starting off bytes into n
198 m_copy(struct mbuf *n, struct mbuf *m, int off, int len)
200 if (len > M_FREEROOM(n))
203 memcpy((n->m_data + n->m_len), (m->m_data + off), len);
210 * Given a pointer into an mbuf, return the mbuf
211 * XXX This is a kludge, I should eliminate the need for it
212 * Fortunately, it's not used often
215 dtom(Slirp *slirp, void *dat)
220 DEBUG_ARG("dat = %p", dat);
222 /* bug corrected for M_EXT buffers */
223 for (m = (struct mbuf *) slirp->m_usedlist.qh_link;
224 (struct quehead *) m != &slirp->m_usedlist;
226 if (m->m_flags & M_EXT) {
227 if( (char *)dat>=m->m_ext && (char *)dat<(m->m_ext + m->m_size) )
230 if( (char *)dat >= m->m_dat && (char *)dat<(m->m_dat + m->m_size) )
235 DEBUG_ERROR((dfd, "dtom failed"));
237 return (struct mbuf *)0;