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 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
24 * XXX if_maxlinkhdr already in mtu
26 #define SLIRP_MSIZE (IF_MTU + IF_MAXLINKHDR + sizeof(struct m_hdr ) + 6)
31 slirp
->m_freelist
.m_next
= slirp
->m_freelist
.m_prev
= &slirp
->m_freelist
;
32 slirp
->m_usedlist
.m_next
= slirp
->m_usedlist
.m_prev
= &slirp
->m_usedlist
;
36 * Get an mbuf from the free list, if there are none
39 * Because fragmentation can occur if we alloc new mbufs and
40 * free old mbufs, we mark all mbufs above mbuf_thresh as M_DOFREE,
41 * which tells m_free to actually free() it
46 register struct mbuf
*m
;
51 if (slirp
->m_freelist
.m_next
== &slirp
->m_freelist
) {
52 m
= (struct mbuf
*)malloc(SLIRP_MSIZE
);
53 if (m
== NULL
) goto end_error
;
54 slirp
->mbuf_alloced
++;
55 if (slirp
->mbuf_alloced
> MBUF_THRESH
)
59 m
= slirp
->m_freelist
.m_next
;
63 /* Insert it in the used list */
64 insque(m
,&slirp
->m_usedlist
);
65 m
->m_flags
= (flags
| M_USEDLIST
);
68 m
->m_size
= SLIRP_MSIZE
- sizeof(struct m_hdr
);
74 DEBUG_ARG("m = %lx", (long )m
);
79 m_free(struct mbuf
*m
)
83 DEBUG_ARG("m = %lx", (long )m
);
86 /* Remove from m_usedlist */
87 if (m
->m_flags
& M_USEDLIST
)
90 /* If it's M_EXT, free() it */
91 if (m
->m_flags
& M_EXT
)
95 * Either free() it or put it on the free list
97 if (m
->m_flags
& M_DOFREE
) {
99 m
->slirp
->mbuf_alloced
--;
100 } else if ((m
->m_flags
& M_FREELIST
) == 0) {
101 insque(m
,&m
->slirp
->m_freelist
);
102 m
->m_flags
= M_FREELIST
; /* Clobber other flags */
108 * Copy data from one mbuf to the end of
109 * the other.. if result is too big for one mbuf, malloc()
110 * an M_EXT data segment
113 m_cat(struct mbuf
*m
, struct mbuf
*n
)
116 * If there's no room, realloc
118 if (M_FREEROOM(m
) < n
->m_len
)
119 m_inc(m
,m
->m_size
+MINCSIZE
);
121 memcpy(m
->m_data
+m
->m_len
, n
->m_data
, n
->m_len
);
122 m
->m_len
+= n
->m_len
;
128 /* make m size bytes large */
130 m_inc(struct mbuf
*m
, int size
)
134 /* some compiles throw up on gotos. This one we can fake. */
135 if(m
->m_size
>size
) return;
137 if (m
->m_flags
& M_EXT
) {
138 datasize
= m
->m_data
- m
->m_ext
;
139 m
->m_ext
= (char *)realloc(m
->m_ext
,size
);
140 m
->m_data
= m
->m_ext
+ datasize
;
143 datasize
= m
->m_data
- m
->m_dat
;
144 dat
= (char *)malloc(size
);
145 memcpy(dat
, m
->m_dat
, m
->m_size
);
148 m
->m_data
= m
->m_ext
+ datasize
;
159 m_adj(struct mbuf
*m
, int len
)
176 * Copy len bytes from m, starting off bytes into n
179 m_copy(struct mbuf
*n
, struct mbuf
*m
, int off
, int len
)
181 if (len
> M_FREEROOM(n
))
184 memcpy((n
->m_data
+ n
->m_len
), (m
->m_data
+ off
), len
);
191 * Given a pointer into an mbuf, return the mbuf
192 * XXX This is a kludge, I should eliminate the need for it
193 * Fortunately, it's not used often
196 dtom(Slirp
*slirp
, void *dat
)
201 DEBUG_ARG("dat = %lx", (long )dat
);
203 /* bug corrected for M_EXT buffers */
204 for (m
= slirp
->m_usedlist
.m_next
; m
!= &slirp
->m_usedlist
;
206 if (m
->m_flags
& M_EXT
) {
207 if( (char *)dat
>=m
->m_ext
&& (char *)dat
<(m
->m_ext
+ m
->m_size
) )
210 if( (char *)dat
>= m
->m_dat
&& (char *)dat
<(m
->m_dat
+ m
->m_size
) )
215 DEBUG_ERROR((dfd
, "dtom failed"));
217 return (struct mbuf
*)0;