VirtualBox

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

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

NAT: don't hide bugs with extra sizes.

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