blob: d13698839764b992822b5e6bc84fa30c6490e8d1 [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
Peter Maydell7df74822016-01-29 17:49:59 +000018#include "qemu/osdep.h"
bellardf0cbd3e2004-04-22 00:10:48 +000019#include <slirp.h>
20
blueswir19634d902007-10-26 19:01:16 +000021#define MBUF_THRESH 30
blueswir19634d902007-10-26 19:01:16 +000022
23/*
24 * Find a nice value for msize
blueswir19634d902007-10-26 19:01:16 +000025 */
Guillaume Subiron98c63052016-03-15 10:31:20 +010026#define SLIRP_MSIZE\
27 (offsetof(struct mbuf, m_dat) + IF_MAXLINKHDR + TCPIPHDR_DELTA + IF_MTU)
bellardf0cbd3e2004-04-22 00:10:48 +000028
29void
Jan Kiszka460fec62009-06-24 14:42:31 +020030m_init(Slirp *slirp)
bellardf0cbd3e2004-04-22 00:10:48 +000031{
Samuel Thibault67e3eee2016-02-22 22:29:21 +010032 slirp->m_freelist.qh_link = slirp->m_freelist.qh_rlink = &slirp->m_freelist;
33 slirp->m_usedlist.qh_link = slirp->m_usedlist.qh_rlink = &slirp->m_usedlist;
bellardf0cbd3e2004-04-22 00:10:48 +000034}
35
Jan Kiszkaa68adc22012-02-29 19:14:23 +010036void m_cleanup(Slirp *slirp)
37{
38 struct mbuf *m, *next;
39
Samuel Thibault67e3eee2016-02-22 22:29:21 +010040 m = (struct mbuf *) slirp->m_usedlist.qh_link;
41 while ((struct quehead *) m != &slirp->m_usedlist) {
Jan Kiszkaa68adc22012-02-29 19:14:23 +010042 next = m->m_next;
43 if (m->m_flags & M_EXT) {
44 free(m->m_ext);
45 }
46 free(m);
47 m = next;
48 }
Samuel Thibault67e3eee2016-02-22 22:29:21 +010049 m = (struct mbuf *) slirp->m_freelist.qh_link;
50 while ((struct quehead *) m != &slirp->m_freelist) {
Jan Kiszkaa68adc22012-02-29 19:14:23 +010051 next = m->m_next;
52 free(m);
53 m = next;
54 }
55}
56
bellardf0cbd3e2004-04-22 00:10:48 +000057/*
58 * Get an mbuf from the free list, if there are none
59 * malloc one
ths5fafdf22007-09-16 21:08:06 +000060 *
bellardf0cbd3e2004-04-22 00:10:48 +000061 * Because fragmentation can occur if we alloc new mbufs and
62 * free old mbufs, we mark all mbufs above mbuf_thresh as M_DOFREE,
63 * which tells m_free to actually free() it
64 */
65struct mbuf *
Jan Kiszka460fec62009-06-24 14:42:31 +020066m_get(Slirp *slirp)
bellardf0cbd3e2004-04-22 00:10:48 +000067{
68 register struct mbuf *m;
69 int flags = 0;
ths5fafdf22007-09-16 21:08:06 +000070
bellardf0cbd3e2004-04-22 00:10:48 +000071 DEBUG_CALL("m_get");
ths5fafdf22007-09-16 21:08:06 +000072
Samuel Thibault67e3eee2016-02-22 22:29:21 +010073 if (slirp->m_freelist.qh_link == &slirp->m_freelist) {
blueswir179383c92008-08-30 09:51:20 +000074 m = (struct mbuf *)malloc(SLIRP_MSIZE);
bellardf0cbd3e2004-04-22 00:10:48 +000075 if (m == NULL) goto end_error;
Jan Kiszka460fec62009-06-24 14:42:31 +020076 slirp->mbuf_alloced++;
77 if (slirp->mbuf_alloced > MBUF_THRESH)
bellardf0cbd3e2004-04-22 00:10:48 +000078 flags = M_DOFREE;
Jan Kiszka460fec62009-06-24 14:42:31 +020079 m->slirp = slirp;
bellardf0cbd3e2004-04-22 00:10:48 +000080 } else {
Samuel Thibault67e3eee2016-02-22 22:29:21 +010081 m = (struct mbuf *) slirp->m_freelist.qh_link;
bellardf0cbd3e2004-04-22 00:10:48 +000082 remque(m);
83 }
ths5fafdf22007-09-16 21:08:06 +000084
bellardf0cbd3e2004-04-22 00:10:48 +000085 /* Insert it in the used list */
Jan Kiszka460fec62009-06-24 14:42:31 +020086 insque(m,&slirp->m_usedlist);
bellardf0cbd3e2004-04-22 00:10:48 +000087 m->m_flags = (flags | M_USEDLIST);
ths5fafdf22007-09-16 21:08:06 +000088
bellardf0cbd3e2004-04-22 00:10:48 +000089 /* Initialise it */
Anthony Liguori8fe30462011-02-14 14:24:24 -060090 m->m_size = SLIRP_MSIZE - offsetof(struct mbuf, m_dat);
bellardf0cbd3e2004-04-22 00:10:48 +000091 m->m_data = m->m_dat;
92 m->m_len = 0;
blueswir1511d2b12009-03-07 15:32:56 +000093 m->m_nextpkt = NULL;
94 m->m_prevpkt = NULL;
Guillaume Subironfc3779a2015-12-19 22:24:56 +010095 m->resolution_requested = false;
Fabien Chouteau1ab74ce2011-08-01 18:18:37 +020096 m->expiration_date = (uint64_t)-1;
bellardf0cbd3e2004-04-22 00:10:48 +000097end_error:
Stefan Weilecc804c2015-08-29 09:12:35 +020098 DEBUG_ARG("m = %p", m);
bellardf0cbd3e2004-04-22 00:10:48 +000099 return m;
100}
101
102void
blueswir1511d2b12009-03-07 15:32:56 +0000103m_free(struct mbuf *m)
bellardf0cbd3e2004-04-22 00:10:48 +0000104{
ths5fafdf22007-09-16 21:08:06 +0000105
bellardf0cbd3e2004-04-22 00:10:48 +0000106 DEBUG_CALL("m_free");
Stefan Weilecc804c2015-08-29 09:12:35 +0200107 DEBUG_ARG("m = %p", m);
ths5fafdf22007-09-16 21:08:06 +0000108
bellardf0cbd3e2004-04-22 00:10:48 +0000109 if(m) {
110 /* Remove from m_usedlist */
111 if (m->m_flags & M_USEDLIST)
112 remque(m);
ths5fafdf22007-09-16 21:08:06 +0000113
bellardf0cbd3e2004-04-22 00:10:48 +0000114 /* If it's M_EXT, free() it */
115 if (m->m_flags & M_EXT)
116 free(m->m_ext);
117
118 /*
119 * Either free() it or put it on the free list
120 */
121 if (m->m_flags & M_DOFREE) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200122 m->slirp->mbuf_alloced--;
Mark McLoughline0cf6d12009-11-20 18:13:10 +0000123 free(m);
bellardf0cbd3e2004-04-22 00:10:48 +0000124 } else if ((m->m_flags & M_FREELIST) == 0) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200125 insque(m,&m->slirp->m_freelist);
bellardf0cbd3e2004-04-22 00:10:48 +0000126 m->m_flags = M_FREELIST; /* Clobber other flags */
127 }
128 } /* if(m) */
129}
130
131/*
132 * Copy data from one mbuf to the end of
133 * the other.. if result is too big for one mbuf, malloc()
134 * an M_EXT data segment
135 */
136void
blueswir1511d2b12009-03-07 15:32:56 +0000137m_cat(struct mbuf *m, struct mbuf *n)
bellardf0cbd3e2004-04-22 00:10:48 +0000138{
139 /*
140 * If there's no room, realloc
141 */
142 if (M_FREEROOM(m) < n->m_len)
143 m_inc(m,m->m_size+MINCSIZE);
ths5fafdf22007-09-16 21:08:06 +0000144
bellardf0cbd3e2004-04-22 00:10:48 +0000145 memcpy(m->m_data+m->m_len, n->m_data, n->m_len);
146 m->m_len += n->m_len;
147
148 m_free(n);
149}
150
151
152/* make m size bytes large */
153void
blueswir1511d2b12009-03-07 15:32:56 +0000154m_inc(struct mbuf *m, int size)
bellardf0cbd3e2004-04-22 00:10:48 +0000155{
bellard4b6ccfd2006-05-01 10:59:02 +0000156 int datasize;
157
bellardf0cbd3e2004-04-22 00:10:48 +0000158 /* some compiles throw up on gotos. This one we can fake. */
159 if(m->m_size>size) return;
160
161 if (m->m_flags & M_EXT) {
bellard4b6ccfd2006-05-01 10:59:02 +0000162 datasize = m->m_data - m->m_ext;
bellardf0cbd3e2004-04-22 00:10:48 +0000163 m->m_ext = (char *)realloc(m->m_ext,size);
bellard4b6ccfd2006-05-01 10:59:02 +0000164 m->m_data = m->m_ext + datasize;
bellardf0cbd3e2004-04-22 00:10:48 +0000165 } else {
bellardf0cbd3e2004-04-22 00:10:48 +0000166 char *dat;
167 datasize = m->m_data - m->m_dat;
168 dat = (char *)malloc(size);
bellardf0cbd3e2004-04-22 00:10:48 +0000169 memcpy(dat, m->m_dat, m->m_size);
ths3b46e622007-09-17 08:09:54 +0000170
bellardf0cbd3e2004-04-22 00:10:48 +0000171 m->m_ext = dat;
172 m->m_data = m->m_ext + datasize;
173 m->m_flags |= M_EXT;
174 }
ths5fafdf22007-09-16 21:08:06 +0000175
bellardf0cbd3e2004-04-22 00:10:48 +0000176 m->m_size = size;
177
178}
179
180
181
182void
blueswir1511d2b12009-03-07 15:32:56 +0000183m_adj(struct mbuf *m, int len)
bellardf0cbd3e2004-04-22 00:10:48 +0000184{
185 if (m == NULL)
186 return;
187 if (len >= 0) {
188 /* Trim from head */
189 m->m_data += len;
190 m->m_len -= len;
191 } else {
192 /* Trim from tail */
193 len = -len;
194 m->m_len -= len;
195 }
196}
197
198
199/*
200 * Copy len bytes from m, starting off bytes into n
201 */
202int
blueswir1511d2b12009-03-07 15:32:56 +0000203m_copy(struct mbuf *n, struct mbuf *m, int off, int len)
bellardf0cbd3e2004-04-22 00:10:48 +0000204{
205 if (len > M_FREEROOM(n))
206 return -1;
207
208 memcpy((n->m_data + n->m_len), (m->m_data + off), len);
209 n->m_len += len;
210 return 0;
211}
212
213
214/*
215 * Given a pointer into an mbuf, return the mbuf
216 * XXX This is a kludge, I should eliminate the need for it
217 * Fortunately, it's not used often
218 */
219struct mbuf *
Jan Kiszka460fec62009-06-24 14:42:31 +0200220dtom(Slirp *slirp, void *dat)
bellardf0cbd3e2004-04-22 00:10:48 +0000221{
222 struct mbuf *m;
ths5fafdf22007-09-16 21:08:06 +0000223
bellardf0cbd3e2004-04-22 00:10:48 +0000224 DEBUG_CALL("dtom");
Stefan Weilecc804c2015-08-29 09:12:35 +0200225 DEBUG_ARG("dat = %p", dat);
bellardf0cbd3e2004-04-22 00:10:48 +0000226
227 /* bug corrected for M_EXT buffers */
Samuel Thibault67e3eee2016-02-22 22:29:21 +0100228 for (m = (struct mbuf *) slirp->m_usedlist.qh_link;
229 (struct quehead *) m != &slirp->m_usedlist;
Jan Kiszka460fec62009-06-24 14:42:31 +0200230 m = m->m_next) {
bellardf0cbd3e2004-04-22 00:10:48 +0000231 if (m->m_flags & M_EXT) {
232 if( (char *)dat>=m->m_ext && (char *)dat<(m->m_ext + m->m_size) )
233 return m;
234 } else {
235 if( (char *)dat >= m->m_dat && (char *)dat<(m->m_dat + m->m_size) )
236 return m;
237 }
238 }
ths5fafdf22007-09-16 21:08:06 +0000239
bellardf0cbd3e2004-04-22 00:10:48 +0000240 DEBUG_ERROR((dfd, "dtom failed"));
ths5fafdf22007-09-16 21:08:06 +0000241
bellardf0cbd3e2004-04-22 00:10:48 +0000242 return (struct mbuf *)0;
243}