VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/mbuf.c@ 21415

Last change on this file since 21415 was 21415, checked in by vboxsync, 15 years ago

NAT: correct assertion expression

  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
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
21void
22m_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 msize_init(pData);
28}
29
30void
31msize_init(PNATState pData)
32{
33 /*
34 * Find a nice value for msize
35 * XXX if_maxlinkhdr already in mtu
36 */
37 msize = (if_mtu>if_mru ? if_mtu : if_mru)
38 + if_maxlinkhdr + sizeof(struct m_hdr ) + 6;
39}
40
41/*
42 * Get an mbuf from the free list, if there are none
43 * malloc one
44 *
45 * Because fragmentation can occur if we alloc new mbufs and
46 * free old mbufs, we mark all mbufs above mbuf_thresh as M_DOFREE,
47 * which tells m_free to actually free() it
48 */
49struct mbuf *
50m_get(PNATState pData)
51{
52 register struct mbuf *m;
53 int flags = 0;
54
55 DEBUG_CALL("m_get");
56
57 if (m_freelist.m_next == &m_freelist)
58 {
59 m = (struct mbuf *)RTMemAlloc(msize);
60 if (m == NULL)
61 goto end_error;
62 mbuf_alloced++;
63 if (mbuf_alloced > mbuf_thresh)
64 flags = M_DOFREE;
65 if (mbuf_alloced > mbuf_max)
66 mbuf_max = mbuf_alloced;
67 }
68 else
69 {
70 m = m_freelist.m_next;
71 remque(pData, m);
72 }
73
74 /* Insert it in the used list */
75 insque(pData, m, &m_usedlist);
76 m->m_flags = (flags | M_USEDLIST);
77
78 /* Initialise it */
79 m->m_size = msize - sizeof(struct m_hdr);
80 m->m_data = m->m_dat;
81 m->m_len = 0;
82 m->m_nextpkt = 0;
83 m->m_prevpkt = 0;
84#ifdef VBOX_WITH_SLIRP_ALIAS
85 m->m_la = NULL;
86#endif
87
88end_error:
89 DEBUG_ARG("m = %lx", (long )m);
90 return m;
91}
92
93void
94m_free(PNATState pData, struct mbuf *m)
95{
96
97 DEBUG_CALL("m_free");
98 DEBUG_ARG("m = %lx", (long )m);
99
100 if(m)
101 {
102 /* Remove from m_usedlist */
103 if (m->m_flags & M_USEDLIST)
104 remque(pData, m);
105
106 /* If it's M_EXT, free() it */
107 if (m->m_flags & M_EXT)
108 RTMemFree(m->m_ext);
109
110 /*
111 * Either free() it or put it on the free list
112 */
113 if (m->m_flags & M_DOFREE)
114 {
115 RTMemFree(m);
116 mbuf_alloced--;
117 }
118 else if ((m->m_flags & M_FREELIST) == 0)
119 {
120 insque(pData, m,&m_freelist);
121 m->m_flags = M_FREELIST; /* Clobber other flags */
122 }
123 } /* if(m) */
124}
125
126/*
127 * Copy data from one mbuf to the end of
128 * the other.. if result is too big for one mbuf, malloc()
129 * an M_EXT data segment
130 */
131void
132m_cat(PNATState pData, register struct mbuf *m, register struct mbuf *n)
133{
134 /*
135 * If there's no room, realloc
136 */
137 if (M_FREEROOM(m) < n->m_len)
138 m_inc(m,m->m_size+MINCSIZE);
139
140 memcpy(m->m_data+m->m_len, n->m_data, n->m_len);
141 m->m_len += n->m_len;
142
143 m_free(pData, n);
144}
145
146
147/* make m size bytes large */
148void
149m_inc(struct mbuf *m, int size)
150{
151 int datasize;
152
153 /* some compiles throw up on gotos. This one we can fake. */
154 if (m->m_size > size)
155 return;
156
157 if (m->m_flags & M_EXT)
158 {
159 datasize = m->m_data - m->m_ext;
160 m->m_ext = (char *)RTMemRealloc(m->m_ext, size);
161#if 0
162 if (m->m_ext == NULL)
163 return (struct mbuf *)NULL;
164#endif
165 m->m_data = m->m_ext + datasize;
166 }
167 else
168 {
169 char *dat;
170 datasize = m->m_data - m->m_dat;
171 dat = (char *)RTMemAlloc(size);
172#if 0
173 if (dat == NULL)
174 return (struct mbuf *)NULL;
175#endif
176 memcpy(dat, m->m_dat, m->m_size);
177
178 m->m_ext = dat;
179 m->m_data = m->m_ext + datasize;
180 m->m_flags |= M_EXT;
181 }
182
183 m->m_size = size;
184}
185
186
187void
188m_adj(struct mbuf *m, int len)
189{
190 if (m == NULL)
191 return;
192 if (len >= 0)
193 {
194 /* Trim from head */
195 m->m_data += len;
196 m->m_len -= len;
197 }
198 else
199 {
200 /* Trim from tail */
201 len = -len;
202 m->m_len -= len;
203 }
204 Assert(m->m_len >= 0);
205}
206
207
208/*
209 * Copy len bytes from m, starting off bytes into n
210 */
211int
212m_copy(struct mbuf *n, struct mbuf *m, int off, int len)
213{
214 if (len > M_FREEROOM(n))
215 return -1;
216
217 memcpy((n->m_data + n->m_len), (m->m_data + off), len);
218 n->m_len += len;
219 return 0;
220}
221
222
223/*
224 * Given a pointer into an mbuf, return the mbuf
225 * XXX This is a kludge, I should eliminate the need for it
226 * Fortunately, it's not used often
227 */
228struct mbuf *
229dtom(PNATState pData, void *dat)
230{
231 struct mbuf *m;
232
233 DEBUG_CALL("dtom");
234 DEBUG_ARG("dat = %lx", (long )dat);
235
236 /* bug corrected for M_EXT buffers */
237 for (m = m_usedlist.m_next; m != &m_usedlist; m = m->m_next)
238 {
239 if (m->m_flags & M_EXT)
240 {
241 if ( (char *)dat >= m->m_ext
242 && (char *)dat < (m->m_ext + m->m_size))
243 return m;
244 }
245 else
246 {
247 if ( (char *)dat >= m->m_dat
248 && (char *)dat < (m->m_dat + m->m_size))
249 return m;
250 }
251 }
252
253 DEBUG_ERROR((dfd, "dtom failed"));
254
255 return (struct mbuf *)0;
256}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette