1 | /*
|
---|
2 | * Copyright (c) 1995 Danny Gasparovski
|
---|
3 | *
|
---|
4 | * Please read the file COPYRIGHT for the
|
---|
5 | * terms and conditions of the copyright.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
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
|
---|
15 | * the flags
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include <slirp.h>
|
---|
19 |
|
---|
20 |
|
---|
21 | void
|
---|
22 | m_init(PNATState pData)
|
---|
23 | {
|
---|
24 | m_freelist.m_next = m_freelist.m_prev = &m_freelist;
|
---|
25 | m_usedlist.m_next = m_usedlist.m_prev = &m_usedlist;
|
---|
26 | mbuf_alloced = 0;
|
---|
27 |
|
---|
28 | VBOX_SLIRP_LOCK_CREATE(&pData->m_usedlist_mutex);
|
---|
29 | VBOX_SLIRP_LOCK_CREATE(&pData->m_freelist_mutex);
|
---|
30 | VBOX_SLIRP_LOCK_CREATE(&pData->mbuf_alloced_mutex);
|
---|
31 |
|
---|
32 | msize_init(pData);
|
---|
33 | }
|
---|
34 |
|
---|
35 | void
|
---|
36 | msize_init(PNATState pData)
|
---|
37 | {
|
---|
38 | /*
|
---|
39 | * Find a nice value for msize
|
---|
40 | * XXX if_maxlinkhdr already in mtu
|
---|
41 | */
|
---|
42 | msize = (if_mtu>if_mru?if_mtu:if_mru) +
|
---|
43 | if_maxlinkhdr + sizeof(struct m_hdr ) + 6;
|
---|
44 | }
|
---|
45 |
|
---|
46 | /*
|
---|
47 | * Get an mbuf from the free list, if there are none
|
---|
48 | * malloc one
|
---|
49 | *
|
---|
50 | * Because fragmentation can occur if we alloc new mbufs and
|
---|
51 | * free old mbufs, we mark all mbufs above mbuf_thresh as M_DOFREE,
|
---|
52 | * which tells m_free to actually free() it
|
---|
53 | */
|
---|
54 | struct mbuf *
|
---|
55 | m_get(PNATState pData)
|
---|
56 | {
|
---|
57 | register struct mbuf *m;
|
---|
58 | int flags = 0;
|
---|
59 | #ifdef VBOX_WITH_SYNC_SLIRP
|
---|
60 | int on_free_list = 0;
|
---|
61 | #endif
|
---|
62 |
|
---|
63 | DEBUG_CALL("m_get");
|
---|
64 |
|
---|
65 | VBOX_SLIRP_LOCK(pData->m_freelist_mutex);
|
---|
66 |
|
---|
67 | if (m_freelist.m_next == &m_freelist) {
|
---|
68 | m = (struct mbuf *)malloc(msize);
|
---|
69 | if (m == NULL) {
|
---|
70 | VBOX_SLIRP_UNLOCK(pData->m_freelist_mutex);
|
---|
71 | goto end_error;
|
---|
72 | }
|
---|
73 |
|
---|
74 | VBOX_SLIRP_LOCK(pData->mbuf_alloced_mutex);
|
---|
75 |
|
---|
76 | mbuf_alloced++;
|
---|
77 | if (mbuf_alloced > mbuf_thresh)
|
---|
78 | flags = M_DOFREE;
|
---|
79 | if (mbuf_alloced > mbuf_max)
|
---|
80 | mbuf_max = mbuf_alloced;
|
---|
81 | VBOX_SLIRP_UNLOCK(pData->mbuf_alloced_mutex);
|
---|
82 | VBOX_SLIRP_LOCK_CREATE(&m->m_mutex);
|
---|
83 | VBOX_SLIRP_LOCK(m->m_mutex);
|
---|
84 | } else {
|
---|
85 | m = m_freelist.m_next;
|
---|
86 | VBOX_SLIRP_LOCK(m->m_mutex);
|
---|
87 | struct mbuf *n = m->m_next;
|
---|
88 | if (n != NULL)
|
---|
89 | VBOX_SLIRP_LOCK(n->m_mutex);
|
---|
90 | remque(pData, m);
|
---|
91 | if (m->m_next != NULL)
|
---|
92 | VBOX_SLIRP_UNLOCK(n->m_mutex);
|
---|
93 | }
|
---|
94 |
|
---|
95 | VBOX_SLIRP_UNLOCK(pData->m_freelist_mutex);
|
---|
96 |
|
---|
97 | VBOX_SLIRP_LOCK(pData->m_usedlist_mutex);
|
---|
98 | /* Insert it in the used list */
|
---|
99 | struct mbuf *n = m_usedlist.m_next;
|
---|
100 | if (n != &m_usedlist)
|
---|
101 | VBOX_SLIRP_LOCK(n->m_mutex);
|
---|
102 | insque(pData, m,&m_usedlist);
|
---|
103 | if (n != &m_usedlist)
|
---|
104 | VBOX_SLIRP_LOCK(n->m_mutex);
|
---|
105 | VBOX_SLIRP_UNLOCK(m->m_mutex);
|
---|
106 |
|
---|
107 | m->m_flags = (flags | M_USEDLIST);
|
---|
108 |
|
---|
109 | /* Initialise it */
|
---|
110 | m->m_size = msize - sizeof(struct m_hdr);
|
---|
111 | m->m_data = m->m_dat;
|
---|
112 | m->m_len = 0;
|
---|
113 | m->m_nextpkt = 0;
|
---|
114 | m->m_prevpkt = 0;
|
---|
115 |
|
---|
116 | end_error:
|
---|
117 | DEBUG_ARG("m = %lx", (long )m);
|
---|
118 | return m;
|
---|
119 | }
|
---|
120 |
|
---|
121 | void
|
---|
122 | m_free(PNATState pData, struct mbuf *m)
|
---|
123 | {
|
---|
124 |
|
---|
125 | DEBUG_CALL("m_free");
|
---|
126 | DEBUG_ARG("m = %lx", (long )m);
|
---|
127 |
|
---|
128 | if(m) {
|
---|
129 | /* Remove from m_usedlist */
|
---|
130 | if (m->m_flags & M_USEDLIST) {
|
---|
131 | VBOX_SLIRP_LOCK(pData->m_usedlist_mutex);
|
---|
132 | struct mbuf *p, *n;
|
---|
133 | p = (m->m_prev);
|
---|
134 | n = (m->m_next);
|
---|
135 | VBOX_SLIRP_LOCK(m->m_next);
|
---|
136 | if (n != NULL)
|
---|
137 | VBOX_SLIRP_LOCK(n->m_next);
|
---|
138 | if (p != NULL)
|
---|
139 | VBOX_SLIRP_UNLOCK(p->m_next);
|
---|
140 | remque(pData, m);
|
---|
141 | if (n != NULL)
|
---|
142 | VBOX_SLIRP_UNLOCK(n->m_next);
|
---|
143 | if (p != NULL)
|
---|
144 | VBOX_SLIRP_LOCK(p->m_next);
|
---|
145 | VBOX_SLIRP_UNLOCK(pData->m_usedlist_mutex);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /* If it's M_EXT, free() it */
|
---|
149 | if (m->m_flags & M_EXT)
|
---|
150 | free(m->m_ext);
|
---|
151 |
|
---|
152 | /*
|
---|
153 | * Either free() it or put it on the free list
|
---|
154 | */
|
---|
155 | if (m->m_flags & M_DOFREE) {
|
---|
156 | u32ptr_done(pData, ptr_to_u32(pData, m), m);
|
---|
157 | VBOX_SLIRP_UNLOCK(m->m_mutex);
|
---|
158 | VBOX_SLIRP_LOCK_DESTROY(m->m_mutex);
|
---|
159 | free(m);
|
---|
160 | #ifdef VBOX_WITH_SYNC_SLIRP
|
---|
161 | m = NULL;
|
---|
162 | #endif
|
---|
163 | VBOX_SLIRP_LOCK(pData->mbuf_alloced_mutex);
|
---|
164 | mbuf_alloced--;
|
---|
165 | VBOX_SLIRP_UNLOCK(pData->mbuf_alloced_mutex);
|
---|
166 | } else if ((m->m_flags & M_FREELIST) == 0) {
|
---|
167 | VBOX_SLIRP_LOCK(pData->m_freelist_mutex);
|
---|
168 | insque(pData, m,&m_freelist);
|
---|
169 | m->m_flags = M_FREELIST; /* Clobber other flags */
|
---|
170 | VBOX_SLIRP_UNLOCK(pData->m_freelist_mutex);
|
---|
171 | }
|
---|
172 | } /* if(m) */
|
---|
173 | }
|
---|
174 |
|
---|
175 | /*
|
---|
176 | * Copy data from one mbuf to the end of
|
---|
177 | * the other.. if result is too big for one mbuf, malloc()
|
---|
178 | * an M_EXT data segment
|
---|
179 | */
|
---|
180 | void
|
---|
181 | m_cat(PNATState pData, register struct mbuf *m, register struct mbuf *n)
|
---|
182 | {
|
---|
183 | /*
|
---|
184 | * If there's no room, realloc
|
---|
185 | */
|
---|
186 |
|
---|
187 | if (M_FREEROOM(m) < n->m_len)
|
---|
188 | m_inc(m,m->m_size+MINCSIZE);
|
---|
189 |
|
---|
190 | memcpy(m->m_data+m->m_len, n->m_data, n->m_len);
|
---|
191 | m->m_len += n->m_len;
|
---|
192 |
|
---|
193 | m_free(pData, n);
|
---|
194 |
|
---|
195 | }
|
---|
196 |
|
---|
197 |
|
---|
198 | /* make m size bytes large */
|
---|
199 | void
|
---|
200 | m_inc(m, size)
|
---|
201 | struct mbuf *m;
|
---|
202 | int size;
|
---|
203 | {
|
---|
204 | int datasize;
|
---|
205 |
|
---|
206 | /* some compiles throw up on gotos. This one we can fake. */
|
---|
207 | if(m->m_size>size) {
|
---|
208 | return;
|
---|
209 | }
|
---|
210 |
|
---|
211 | if (m->m_flags & M_EXT) {
|
---|
212 | datasize = m->m_data - m->m_ext;
|
---|
213 | m->m_ext = (char *)realloc(m->m_ext,size);
|
---|
214 | /* if (m->m_ext == NULL)
|
---|
215 | * return (struct mbuf *)NULL;
|
---|
216 | */
|
---|
217 | m->m_data = m->m_ext + datasize;
|
---|
218 | } else {
|
---|
219 | char *dat;
|
---|
220 | datasize = m->m_data - m->m_dat;
|
---|
221 | dat = (char *)malloc(size);
|
---|
222 | /* if (dat == NULL)
|
---|
223 | * return (struct mbuf *)NULL;
|
---|
224 | */
|
---|
225 | memcpy(dat, m->m_dat, m->m_size);
|
---|
226 |
|
---|
227 | m->m_ext = dat;
|
---|
228 | m->m_data = m->m_ext + datasize;
|
---|
229 | m->m_flags |= M_EXT;
|
---|
230 | }
|
---|
231 |
|
---|
232 | m->m_size = size;
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 |
|
---|
237 | void
|
---|
238 | m_adj(m, len)
|
---|
239 | struct mbuf *m;
|
---|
240 | int len;
|
---|
241 | {
|
---|
242 | if (m == NULL)
|
---|
243 | return;
|
---|
244 | if (len >= 0) {
|
---|
245 | /* Trim from head */
|
---|
246 | m->m_data += len;
|
---|
247 | m->m_len -= len;
|
---|
248 | } else {
|
---|
249 | /* Trim from tail */
|
---|
250 | len = -len;
|
---|
251 | m->m_len -= len;
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 |
|
---|
256 | /*
|
---|
257 | * Copy len bytes from m, starting off bytes into n
|
---|
258 | */
|
---|
259 | int
|
---|
260 | m_copy(n, m, off, len)
|
---|
261 | struct mbuf *n, *m;
|
---|
262 | int off, len;
|
---|
263 | {
|
---|
264 | if (len > M_FREEROOM(n)) {
|
---|
265 | return -1;
|
---|
266 | }
|
---|
267 |
|
---|
268 | memcpy((n->m_data + n->m_len), (m->m_data + off), len);
|
---|
269 | n->m_len += len;
|
---|
270 | return 0;
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 | /*
|
---|
275 | * Given a pointer into an mbuf, return the mbuf
|
---|
276 | * XXX This is a kludge, I should eliminate the need for it
|
---|
277 | * Fortunately, it's not used often
|
---|
278 | */
|
---|
279 | struct mbuf *
|
---|
280 | dtom(PNATState pData, void *dat)
|
---|
281 | {
|
---|
282 | struct mbuf *m;
|
---|
283 |
|
---|
284 | DEBUG_CALL("dtom");
|
---|
285 | DEBUG_ARG("dat = %lx", (long )dat);
|
---|
286 |
|
---|
287 | /* bug corrected for M_EXT buffers */
|
---|
288 | #ifndef VBOX_WITH_SYNC_SLIRP
|
---|
289 | for (m = m_usedlist.m_next; m != &m_usedlist; m = m->m_next) {
|
---|
290 | #else
|
---|
291 | struct mbuf *mnext;
|
---|
292 | VBOX_SLIRP_LOCK(pData->m_usedlist_mutex);
|
---|
293 | m = m_usedlist.m_next;
|
---|
294 | while(1) {
|
---|
295 | mnext = m->m_next;
|
---|
296 | #endif
|
---|
297 | if (m->m_flags & M_EXT) {
|
---|
298 | if( (char *)dat>=m->m_ext && (char *)dat<(m->m_ext + m->m_size) ) {
|
---|
299 | return m;
|
---|
300 | }
|
---|
301 | } else {
|
---|
302 | if( (char *)dat >= m->m_dat && (char *)dat<(m->m_dat + m->m_size) ) {
|
---|
303 | return m;
|
---|
304 | }
|
---|
305 | }
|
---|
306 | #ifdef VBOX_WITH_SYNC_SLIRP
|
---|
307 | m = mnext;
|
---|
308 | #endif
|
---|
309 | }
|
---|
310 |
|
---|
311 | DEBUG_ERROR((dfd, "dtom failed"));
|
---|
312 |
|
---|
313 | return (struct mbuf *)0;
|
---|
314 | }
|
---|
315 |
|
---|