blob: 4fefb043bf882979ff2396e5bc1b4f5bbeb20aa4 [file] [log] [blame]
bellardf0cbd3e2004-04-22 00:10:48 +00001/*
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
blueswir19634d902007-10-26 19:01:16 +000020#define MBUF_THRESH 30
blueswir19634d902007-10-26 19:01:16 +000021
22/*
23 * Find a nice value for msize
24 * XXX if_maxlinkhdr already in mtu
25 */
Bruce Rogers53fae6d2011-02-05 14:47:56 -070026#define SLIRP_MSIZE (IF_MTU + IF_MAXLINKHDR + offsetof(struct mbuf, m_dat) + 6)
bellardf0cbd3e2004-04-22 00:10:48 +000027
28void
Jan Kiszka460fec62009-06-24 14:42:31 +020029m_init(Slirp *slirp)
bellardf0cbd3e2004-04-22 00:10:48 +000030{
Jan Kiszka460fec62009-06-24 14:42:31 +020031 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;
bellardf0cbd3e2004-04-22 00:10:48 +000033}
34
Jan Kiszkaa68adc22012-02-29 19:14:23 +010035void m_cleanup(Slirp *slirp)
36{
37 struct mbuf *m, *next;
38
39 m = slirp->m_usedlist.m_next;
40 while (m != &slirp->m_usedlist) {
41 next = m->m_next;
42 if (m->m_flags & M_EXT) {
43 free(m->m_ext);
44 }
45 free(m);
46 m = next;
47 }
48 m = slirp->m_freelist.m_next;
49 while (m != &slirp->m_freelist) {
50 next = m->m_next;
51 free(m);
52 m = next;
53 }
54}
55
bellardf0cbd3e2004-04-22 00:10:48 +000056/*
57 * Get an mbuf from the free list, if there are none
58 * malloc one
ths5fafdf22007-09-16 21:08:06 +000059 *
bellardf0cbd3e2004-04-22 00:10:48 +000060 * Because fragmentation can occur if we alloc new mbufs and
61 * free old mbufs, we mark all mbufs above mbuf_thresh as M_DOFREE,
62 * which tells m_free to actually free() it
63 */
64struct mbuf *
Jan Kiszka460fec62009-06-24 14:42:31 +020065m_get(Slirp *slirp)
bellardf0cbd3e2004-04-22 00:10:48 +000066{
67 register struct mbuf *m;
68 int flags = 0;
ths5fafdf22007-09-16 21:08:06 +000069
bellardf0cbd3e2004-04-22 00:10:48 +000070 DEBUG_CALL("m_get");
ths5fafdf22007-09-16 21:08:06 +000071
Jan Kiszka460fec62009-06-24 14:42:31 +020072 if (slirp->m_freelist.m_next == &slirp->m_freelist) {
blueswir179383c92008-08-30 09:51:20 +000073 m = (struct mbuf *)malloc(SLIRP_MSIZE);
bellardf0cbd3e2004-04-22 00:10:48 +000074 if (m == NULL) goto end_error;
Jan Kiszka460fec62009-06-24 14:42:31 +020075 slirp->mbuf_alloced++;
76 if (slirp->mbuf_alloced > MBUF_THRESH)
bellardf0cbd3e2004-04-22 00:10:48 +000077 flags = M_DOFREE;
Jan Kiszka460fec62009-06-24 14:42:31 +020078 m->slirp = slirp;
bellardf0cbd3e2004-04-22 00:10:48 +000079 } else {
Jan Kiszka460fec62009-06-24 14:42:31 +020080 m = slirp->m_freelist.m_next;
bellardf0cbd3e2004-04-22 00:10:48 +000081 remque(m);
82 }
ths5fafdf22007-09-16 21:08:06 +000083
bellardf0cbd3e2004-04-22 00:10:48 +000084 /* Insert it in the used list */
Jan Kiszka460fec62009-06-24 14:42:31 +020085 insque(m,&slirp->m_usedlist);
bellardf0cbd3e2004-04-22 00:10:48 +000086 m->m_flags = (flags | M_USEDLIST);
ths5fafdf22007-09-16 21:08:06 +000087
bellardf0cbd3e2004-04-22 00:10:48 +000088 /* Initialise it */
Anthony Liguori8fe30462011-02-14 14:24:24 -060089 m->m_size = SLIRP_MSIZE - offsetof(struct mbuf, m_dat);
bellardf0cbd3e2004-04-22 00:10:48 +000090 m->m_data = m->m_dat;
91 m->m_len = 0;
blueswir1511d2b12009-03-07 15:32:56 +000092 m->m_nextpkt = NULL;
93 m->m_prevpkt = NULL;
Fabien Chouteau1ab74ce2011-08-01 18:18:37 +020094 m->arp_requested = false;
95 m->expiration_date = (uint64_t)-1;
bellardf0cbd3e2004-04-22 00:10:48 +000096end_error:
97 DEBUG_ARG("m = %lx", (long )m);
98 return m;
99}
100
101void
blueswir1511d2b12009-03-07 15:32:56 +0000102m_free(struct mbuf *m)
bellardf0cbd3e2004-04-22 00:10:48 +0000103{
ths5fafdf22007-09-16 21:08:06 +0000104
bellardf0cbd3e2004-04-22 00:10:48 +0000105 DEBUG_CALL("m_free");
106 DEBUG_ARG("m = %lx", (long )m);
ths5fafdf22007-09-16 21:08:06 +0000107
bellardf0cbd3e2004-04-22 00:10:48 +0000108 if(m) {
109 /* Remove from m_usedlist */
110 if (m->m_flags & M_USEDLIST)
111 remque(m);
ths5fafdf22007-09-16 21:08:06 +0000112
bellardf0cbd3e2004-04-22 00:10:48 +0000113 /* If it's M_EXT, free() it */
114 if (m->m_flags & M_EXT)
115 free(m->m_ext);
116
117 /*
118 * Either free() it or put it on the free list
119 */
120 if (m->m_flags & M_DOFREE) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200121 m->slirp->mbuf_alloced--;
Mark McLoughline0cf6d12009-11-20 18:13:10 +0000122 free(m);
bellardf0cbd3e2004-04-22 00:10:48 +0000123 } else if ((m->m_flags & M_FREELIST) == 0) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200124 insque(m,&m->slirp->m_freelist);
bellardf0cbd3e2004-04-22 00:10:48 +0000125 m->m_flags = M_FREELIST; /* Clobber other flags */
126 }
127 } /* if(m) */
128}
129
130/*
131 * Copy data from one mbuf to the end of
132 * the other.. if result is too big for one mbuf, malloc()
133 * an M_EXT data segment
134 */
135void
blueswir1511d2b12009-03-07 15:32:56 +0000136m_cat(struct mbuf *m, struct mbuf *n)
bellardf0cbd3e2004-04-22 00:10:48 +0000137{
138 /*
139 * If there's no room, realloc
140 */
141 if (M_FREEROOM(m) < n->m_len)
142 m_inc(m,m->m_size+MINCSIZE);
ths5fafdf22007-09-16 21:08:06 +0000143
bellardf0cbd3e2004-04-22 00:10:48 +0000144 memcpy(m->m_data+m->m_len, n->m_data, n->m_len);
145 m->m_len += n->m_len;
146
147 m_free(n);
148}
149
150
151/* make m size bytes large */
152void
blueswir1511d2b12009-03-07 15:32:56 +0000153m_inc(struct mbuf *m, int size)
bellardf0cbd3e2004-04-22 00:10:48 +0000154{
bellard4b6ccfd2006-05-01 10:59:02 +0000155 int datasize;
156
bellardf0cbd3e2004-04-22 00:10:48 +0000157 /* some compiles throw up on gotos. This one we can fake. */
158 if(m->m_size>size) return;
159
160 if (m->m_flags & M_EXT) {
bellard4b6ccfd2006-05-01 10:59:02 +0000161 datasize = m->m_data - m->m_ext;
bellardf0cbd3e2004-04-22 00:10:48 +0000162 m->m_ext = (char *)realloc(m->m_ext,size);
bellard4b6ccfd2006-05-01 10:59:02 +0000163 m->m_data = m->m_ext + datasize;
bellardf0cbd3e2004-04-22 00:10:48 +0000164 } else {
bellardf0cbd3e2004-04-22 00:10:48 +0000165 char *dat;
166 datasize = m->m_data - m->m_dat;
167 dat = (char *)malloc(size);
bellardf0cbd3e2004-04-22 00:10:48 +0000168 memcpy(dat, m->m_dat, m->m_size);
ths3b46e622007-09-17 08:09:54 +0000169
bellardf0cbd3e2004-04-22 00:10:48 +0000170 m->m_ext = dat;
171 m->m_data = m->m_ext + datasize;
172 m->m_flags |= M_EXT;
173 }
ths5fafdf22007-09-16 21:08:06 +0000174
bellardf0cbd3e2004-04-22 00:10:48 +0000175 m->m_size = size;
176
177}
178
179
180
181void
blueswir1511d2b12009-03-07 15:32:56 +0000182m_adj(struct mbuf *m, int len)
bellardf0cbd3e2004-04-22 00:10:48 +0000183{
184 if (m == NULL)
185 return;
186 if (len >= 0) {
187 /* Trim from head */
188 m->m_data += len;
189 m->m_len -= len;
190 } else {
191 /* Trim from tail */
192 len = -len;
193 m->m_len -= len;
194 }
195}
196
197
198/*
199 * Copy len bytes from m, starting off bytes into n
200 */
201int
blueswir1511d2b12009-03-07 15:32:56 +0000202m_copy(struct mbuf *n, struct mbuf *m, int off, int len)
bellardf0cbd3e2004-04-22 00:10:48 +0000203{
204 if (len > M_FREEROOM(n))
205 return -1;
206
207 memcpy((n->m_data + n->m_len), (m->m_data + off), len);
208 n->m_len += len;
209 return 0;
210}
211
212
213/*
214 * Given a pointer into an mbuf, return the mbuf
215 * XXX This is a kludge, I should eliminate the need for it
216 * Fortunately, it's not used often
217 */
218struct mbuf *
Jan Kiszka460fec62009-06-24 14:42:31 +0200219dtom(Slirp *slirp, void *dat)
bellardf0cbd3e2004-04-22 00:10:48 +0000220{
221 struct mbuf *m;
ths5fafdf22007-09-16 21:08:06 +0000222
bellardf0cbd3e2004-04-22 00:10:48 +0000223 DEBUG_CALL("dtom");
224 DEBUG_ARG("dat = %lx", (long )dat);
225
226 /* bug corrected for M_EXT buffers */
Jan Kiszka460fec62009-06-24 14:42:31 +0200227 for (m = slirp->m_usedlist.m_next; m != &slirp->m_usedlist;
228 m = m->m_next) {
bellardf0cbd3e2004-04-22 00:10:48 +0000229 if (m->m_flags & M_EXT) {
230 if( (char *)dat>=m->m_ext && (char *)dat<(m->m_ext + m->m_size) )
231 return m;
232 } else {
233 if( (char *)dat >= m->m_dat && (char *)dat<(m->m_dat + m->m_size) )
234 return m;
235 }
236 }
ths5fafdf22007-09-16 21:08:06 +0000237
bellardf0cbd3e2004-04-22 00:10:48 +0000238 DEBUG_ERROR((dfd, "dtom failed"));
ths5fafdf22007-09-16 21:08:06 +0000239
bellardf0cbd3e2004-04-22 00:10:48 +0000240 return (struct mbuf *)0;
241}