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 static int mbuf_alloced = 0;
21 static MBufRec m_freelist, m_usedlist;
22 static int mbuf_thresh = 30;
23 static int mbuf_max = 0;
24 static int msize;
25
26 /*
27 * How much room is in the mbuf, from m_data to the end of the mbuf
28 */
29 #define M_ROOM(m) ((m->m_flags & M_EXT)? \
30 (((m)->m_ext + (m)->m_size) - (m)->m_data) \
31 : \
32 (((m)->m_dat + (m)->m_size) - (m)->m_data))
33
34 /*
35 * How much free room there is
36 */
37 #define M_FREEROOM(m) (M_ROOM(m) - (m)->m_len)
38
39
40 void
mbuf_init()41 mbuf_init()
42 {
43 m_freelist.m_next = m_freelist.m_prev = &m_freelist;
44 m_usedlist.m_next = m_usedlist.m_prev = &m_usedlist;
45 msize_init();
46 }
47
48 void
msize_init()49 msize_init()
50 {
51 /*
52 * Find a nice value for msize
53 * XXX if_maxlinkhdr already in mtu
54 */
55 msize = (if_mtu > if_mru ? if_mtu : if_mru) +
56 if_maxlinkhdr + sizeof(struct m_hdr ) + 6;
57 }
58
59 static void
mbuf_insque(MBuf m,MBuf head)60 mbuf_insque(MBuf m, MBuf head)
61 {
62 m->m_next = head->m_next;
63 m->m_prev = head;
64 head->m_next = m;
65 m->m_next->m_prev = m;
66 }
67
68 static void
mbuf_remque(MBuf m)69 mbuf_remque(MBuf m)
70 {
71 m->m_prev->m_next = m->m_next;
72 m->m_next->m_prev = m->m_prev;
73 m->m_next = m->m_prev = m;
74 }
75
76 /*
77 * Get an mbuf from the free list, if there are none
78 * malloc one
79 *
80 * Because fragmentation can occur if we alloc new mbufs and
81 * free old mbufs, we mark all mbufs above mbuf_thresh as M_DOFREE,
82 * which tells m_free to actually free() it
83 */
84 MBuf
mbuf_alloc(void)85 mbuf_alloc(void)
86 {
87 register MBuf m;
88 int flags = 0;
89
90 DEBUG_CALL("mbuf_alloc");
91
92 if (m_freelist.m_next == &m_freelist) {
93 m = (MBuf) malloc(msize);
94 if (m == NULL) goto end_error;
95 mbuf_alloced++;
96 if (mbuf_alloced > mbuf_thresh)
97 flags = M_DOFREE;
98 if (mbuf_alloced > mbuf_max)
99 mbuf_max = mbuf_alloced;
100 } else {
101 m = m_freelist.m_next;
102 mbuf_remque(m);
103 }
104
105 /* Insert it in the used list */
106 mbuf_insque(m,&m_usedlist);
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_next2 = NULL;
114 m->m_prev2 = NULL;
115 end_error:
116 DEBUG_ARG("m = %lx", (long )m);
117 return m;
118 }
119
120 void
mbuf_free(MBuf m)121 mbuf_free(MBuf m)
122 {
123
124 DEBUG_CALL("mbuf_free");
125 DEBUG_ARG("m = %lx", (long )m);
126
127 if(m) {
128 /* Remove from m_usedlist */
129 if (m->m_flags & M_USEDLIST)
130 mbuf_remque(m);
131
132 /* If it's M_EXT, free() it */
133 if (m->m_flags & M_EXT)
134 free(m->m_ext);
135
136 /*
137 * Either free() it or put it on the free list
138 */
139 if (m->m_flags & M_DOFREE) {
140 free(m);
141 mbuf_alloced--;
142 } else if ((m->m_flags & M_FREELIST) == 0) {
143 mbuf_insque(m,&m_freelist);
144 m->m_flags = M_FREELIST; /* Clobber other flags */
145 }
146 } /* if(m) */
147 }
148
149 /*
150 * Copy data from one mbuf to the end of
151 * the other.. if result is too big for one mbuf, malloc()
152 * an M_EXT data segment
153 */
154 void
mbuf_append(MBuf m,MBuf n)155 mbuf_append(MBuf m, MBuf n)
156 {
157 /*
158 * If there's no room, realloc
159 */
160 if (M_FREEROOM(m) < n->m_len)
161 mbuf_ensure(m, m->m_size+MINCSIZE);
162
163 memcpy(m->m_data+m->m_len, n->m_data, n->m_len);
164 m->m_len += n->m_len;
165
166 mbuf_free(n);
167 }
168
169
170 /* make m size bytes large */
171 void
mbuf_ensure(MBuf m,int size)172 mbuf_ensure(MBuf m, int size)
173 {
174 int datasize;
175
176 /* some compiles throw up on gotos. This one we can fake. */
177 if(m->m_size > size) return;
178
179 if (m->m_flags & M_EXT) {
180 datasize = m->m_data - m->m_ext;
181 m->m_ext = (char *)realloc(m->m_ext,size);
182 m->m_data = m->m_ext + datasize;
183 } else {
184 char *dat;
185 datasize = m->m_data - m->m_dat;
186 dat = (char *)malloc(size);
187 memcpy(dat, m->m_dat, m->m_size);
188
189 m->m_ext = dat;
190 m->m_data = m->m_ext + datasize;
191 m->m_flags |= M_EXT;
192 }
193
194 m->m_size = size;
195 }
196
197
198
199 void
mbuf_trim(MBuf m,int len)200 mbuf_trim(MBuf m, int len)
201 {
202 if (m == NULL)
203 return;
204 if (len >= 0) {
205 /* Trim from head */
206 m->m_data += len;
207 m->m_len -= len;
208 } else {
209 /* Trim from tail */
210 len = -len;
211 m->m_len -= len;
212 }
213 }
214
215
216 /*
217 * Copy len bytes from m, starting off bytes into n
218 */
219 int
mbuf_copy(MBuf n,MBuf m,int off,int len)220 mbuf_copy(MBuf n, MBuf m, int off, int len)
221 {
222 if (len > M_FREEROOM(n))
223 return -1;
224
225 memcpy((n->m_data + n->m_len), (m->m_data + off), len);
226 n->m_len += len;
227 return 0;
228 }
229
230 int
mbuf_freeroom(MBuf m)231 mbuf_freeroom( MBuf m )
232 {
233 return M_FREEROOM(m);
234 }
235
236 /*
237 * Given a pointer into an mbuf, return the mbuf
238 * XXX This is a kludge, I should eliminate the need for it
239 * Fortunately, it's not used often
240 */
241 MBuf
mbuf_from(void * dat)242 mbuf_from(void* dat)
243 {
244 MBuf m;
245
246 DEBUG_CALL("mbuf_from");
247 DEBUG_ARG("dat = %lx", (long )dat);
248
249 /* bug corrected for M_EXT buffers */
250 for (m = m_usedlist.m_next; m != &m_usedlist; m = m->m_next) {
251 if (m->m_flags & M_EXT) {
252 if( (unsigned)((char*)dat - m->m_ext) < (unsigned)m->m_size )
253 goto Exit;
254 } else {
255 if( (unsigned)((char *)dat - m->m_dat) < (unsigned)m->m_size )
256 goto Exit;
257 }
258 }
259 m = NULL;
260 DEBUG_ERROR((dfd, "mbuf_from failed"));
261 Exit:
262 return m;
263 }
264
265 void
mbufstats()266 mbufstats()
267 {
268 MBuf m;
269 int i;
270
271 lprint(" \r\n");
272
273 lprint("Mbuf stats:\r\n");
274
275 lprint(" %6d mbufs allocated (%d max)\r\n", mbuf_alloced, mbuf_max);
276
277 i = 0;
278 for (m = m_freelist.m_next; m != &m_freelist; m = m->m_next)
279 i++;
280 lprint(" %6d mbufs on free list\r\n", i);
281
282 i = 0;
283 for (m = m_usedlist.m_next; m != &m_usedlist; m = m->m_next)
284 i++;
285 lprint(" %6d mbufs on used list\r\n", i);
286 lprint(" %6d mbufs queued as packets\r\n\r\n", if_queued);
287 }
288