2004-04-22 02:10:48 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1995 Danny Gasparovski
|
|
|
|
*
|
|
|
|
* Please read the file COPYRIGHT for the
|
|
|
|
* terms and conditions of the copyright.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mbuf's in SLiRP are much simpler than the real mbufs in
|
|
|
|
* FreeBSD. They are fixed size, determined by the MTU,
|
|
|
|
* so that one whole packet can fit. Mbuf's cannot be
|
|
|
|
* chained together. If there's more data than the mbuf
|
|
|
|
* could hold, an external malloced buffer is pointed to
|
|
|
|
* by m_ext (and the data pointers) and M_EXT is set in
|
|
|
|
* the flags
|
|
|
|
*/
|
|
|
|
|
2016-01-29 18:49:59 +01:00
|
|
|
#include "qemu/osdep.h"
|
2004-04-22 02:10:48 +02:00
|
|
|
#include <slirp.h>
|
|
|
|
|
2007-10-26 21:01:16 +02:00
|
|
|
#define MBUF_THRESH 30
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find a nice value for msize
|
|
|
|
* XXX if_maxlinkhdr already in mtu
|
|
|
|
*/
|
2011-02-05 22:47:56 +01:00
|
|
|
#define SLIRP_MSIZE (IF_MTU + IF_MAXLINKHDR + offsetof(struct mbuf, m_dat) + 6)
|
2004-04-22 02:10:48 +02:00
|
|
|
|
|
|
|
void
|
2009-06-24 14:42:31 +02:00
|
|
|
m_init(Slirp *slirp)
|
2004-04-22 02:10:48 +02:00
|
|
|
{
|
2009-06-24 14:42:31 +02:00
|
|
|
slirp->m_freelist.m_next = slirp->m_freelist.m_prev = &slirp->m_freelist;
|
|
|
|
slirp->m_usedlist.m_next = slirp->m_usedlist.m_prev = &slirp->m_usedlist;
|
2004-04-22 02:10:48 +02:00
|
|
|
}
|
|
|
|
|
2012-02-29 19:14:23 +01:00
|
|
|
void m_cleanup(Slirp *slirp)
|
|
|
|
{
|
|
|
|
struct mbuf *m, *next;
|
|
|
|
|
|
|
|
m = slirp->m_usedlist.m_next;
|
|
|
|
while (m != &slirp->m_usedlist) {
|
|
|
|
next = m->m_next;
|
|
|
|
if (m->m_flags & M_EXT) {
|
|
|
|
free(m->m_ext);
|
|
|
|
}
|
|
|
|
free(m);
|
|
|
|
m = next;
|
|
|
|
}
|
|
|
|
m = slirp->m_freelist.m_next;
|
|
|
|
while (m != &slirp->m_freelist) {
|
|
|
|
next = m->m_next;
|
|
|
|
free(m);
|
|
|
|
m = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
/*
|
|
|
|
* Get an mbuf from the free list, if there are none
|
|
|
|
* malloc one
|
2007-09-16 23:08:06 +02:00
|
|
|
*
|
2004-04-22 02:10:48 +02:00
|
|
|
* Because fragmentation can occur if we alloc new mbufs and
|
|
|
|
* free old mbufs, we mark all mbufs above mbuf_thresh as M_DOFREE,
|
|
|
|
* which tells m_free to actually free() it
|
|
|
|
*/
|
|
|
|
struct mbuf *
|
2009-06-24 14:42:31 +02:00
|
|
|
m_get(Slirp *slirp)
|
2004-04-22 02:10:48 +02:00
|
|
|
{
|
|
|
|
register struct mbuf *m;
|
|
|
|
int flags = 0;
|
2007-09-16 23:08:06 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
DEBUG_CALL("m_get");
|
2007-09-16 23:08:06 +02:00
|
|
|
|
2009-06-24 14:42:31 +02:00
|
|
|
if (slirp->m_freelist.m_next == &slirp->m_freelist) {
|
2008-08-30 11:51:20 +02:00
|
|
|
m = (struct mbuf *)malloc(SLIRP_MSIZE);
|
2004-04-22 02:10:48 +02:00
|
|
|
if (m == NULL) goto end_error;
|
2009-06-24 14:42:31 +02:00
|
|
|
slirp->mbuf_alloced++;
|
|
|
|
if (slirp->mbuf_alloced > MBUF_THRESH)
|
2004-04-22 02:10:48 +02:00
|
|
|
flags = M_DOFREE;
|
2009-06-24 14:42:31 +02:00
|
|
|
m->slirp = slirp;
|
2004-04-22 02:10:48 +02:00
|
|
|
} else {
|
2009-06-24 14:42:31 +02:00
|
|
|
m = slirp->m_freelist.m_next;
|
2004-04-22 02:10:48 +02:00
|
|
|
remque(m);
|
|
|
|
}
|
2007-09-16 23:08:06 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
/* Insert it in the used list */
|
2009-06-24 14:42:31 +02:00
|
|
|
insque(m,&slirp->m_usedlist);
|
2004-04-22 02:10:48 +02:00
|
|
|
m->m_flags = (flags | M_USEDLIST);
|
2007-09-16 23:08:06 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
/* Initialise it */
|
2011-02-14 21:24:24 +01:00
|
|
|
m->m_size = SLIRP_MSIZE - offsetof(struct mbuf, m_dat);
|
2004-04-22 02:10:48 +02:00
|
|
|
m->m_data = m->m_dat;
|
|
|
|
m->m_len = 0;
|
2009-03-07 16:32:56 +01:00
|
|
|
m->m_nextpkt = NULL;
|
|
|
|
m->m_prevpkt = NULL;
|
2015-12-19 22:24:56 +01:00
|
|
|
m->resolution_requested = false;
|
2011-08-01 18:18:37 +02:00
|
|
|
m->expiration_date = (uint64_t)-1;
|
2004-04-22 02:10:48 +02:00
|
|
|
end_error:
|
2015-08-29 09:12:35 +02:00
|
|
|
DEBUG_ARG("m = %p", m);
|
2004-04-22 02:10:48 +02:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-03-07 16:32:56 +01:00
|
|
|
m_free(struct mbuf *m)
|
2004-04-22 02:10:48 +02:00
|
|
|
{
|
2007-09-16 23:08:06 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
DEBUG_CALL("m_free");
|
2015-08-29 09:12:35 +02:00
|
|
|
DEBUG_ARG("m = %p", m);
|
2007-09-16 23:08:06 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
if(m) {
|
|
|
|
/* Remove from m_usedlist */
|
|
|
|
if (m->m_flags & M_USEDLIST)
|
|
|
|
remque(m);
|
2007-09-16 23:08:06 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
/* If it's M_EXT, free() it */
|
|
|
|
if (m->m_flags & M_EXT)
|
|
|
|
free(m->m_ext);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Either free() it or put it on the free list
|
|
|
|
*/
|
|
|
|
if (m->m_flags & M_DOFREE) {
|
2009-06-24 14:42:31 +02:00
|
|
|
m->slirp->mbuf_alloced--;
|
2009-11-20 19:13:10 +01:00
|
|
|
free(m);
|
2004-04-22 02:10:48 +02:00
|
|
|
} else if ((m->m_flags & M_FREELIST) == 0) {
|
2009-06-24 14:42:31 +02:00
|
|
|
insque(m,&m->slirp->m_freelist);
|
2004-04-22 02:10:48 +02:00
|
|
|
m->m_flags = M_FREELIST; /* Clobber other flags */
|
|
|
|
}
|
|
|
|
} /* if(m) */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy data from one mbuf to the end of
|
|
|
|
* the other.. if result is too big for one mbuf, malloc()
|
|
|
|
* an M_EXT data segment
|
|
|
|
*/
|
|
|
|
void
|
2009-03-07 16:32:56 +01:00
|
|
|
m_cat(struct mbuf *m, struct mbuf *n)
|
2004-04-22 02:10:48 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If there's no room, realloc
|
|
|
|
*/
|
|
|
|
if (M_FREEROOM(m) < n->m_len)
|
|
|
|
m_inc(m,m->m_size+MINCSIZE);
|
2007-09-16 23:08:06 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
memcpy(m->m_data+m->m_len, n->m_data, n->m_len);
|
|
|
|
m->m_len += n->m_len;
|
|
|
|
|
|
|
|
m_free(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* make m size bytes large */
|
|
|
|
void
|
2009-03-07 16:32:56 +01:00
|
|
|
m_inc(struct mbuf *m, int size)
|
2004-04-22 02:10:48 +02:00
|
|
|
{
|
2006-05-01 12:59:02 +02:00
|
|
|
int datasize;
|
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
/* some compiles throw up on gotos. This one we can fake. */
|
|
|
|
if(m->m_size>size) return;
|
|
|
|
|
|
|
|
if (m->m_flags & M_EXT) {
|
2006-05-01 12:59:02 +02:00
|
|
|
datasize = m->m_data - m->m_ext;
|
2004-04-22 02:10:48 +02:00
|
|
|
m->m_ext = (char *)realloc(m->m_ext,size);
|
2006-05-01 12:59:02 +02:00
|
|
|
m->m_data = m->m_ext + datasize;
|
2004-04-22 02:10:48 +02:00
|
|
|
} else {
|
|
|
|
char *dat;
|
|
|
|
datasize = m->m_data - m->m_dat;
|
|
|
|
dat = (char *)malloc(size);
|
|
|
|
memcpy(dat, m->m_dat, m->m_size);
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
m->m_ext = dat;
|
|
|
|
m->m_data = m->m_ext + datasize;
|
|
|
|
m->m_flags |= M_EXT;
|
|
|
|
}
|
2007-09-16 23:08:06 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
m->m_size = size;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2009-03-07 16:32:56 +01:00
|
|
|
m_adj(struct mbuf *m, int len)
|
2004-04-22 02:10:48 +02:00
|
|
|
{
|
|
|
|
if (m == NULL)
|
|
|
|
return;
|
|
|
|
if (len >= 0) {
|
|
|
|
/* Trim from head */
|
|
|
|
m->m_data += len;
|
|
|
|
m->m_len -= len;
|
|
|
|
} else {
|
|
|
|
/* Trim from tail */
|
|
|
|
len = -len;
|
|
|
|
m->m_len -= len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy len bytes from m, starting off bytes into n
|
|
|
|
*/
|
|
|
|
int
|
2009-03-07 16:32:56 +01:00
|
|
|
m_copy(struct mbuf *n, struct mbuf *m, int off, int len)
|
2004-04-22 02:10:48 +02:00
|
|
|
{
|
|
|
|
if (len > M_FREEROOM(n))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memcpy((n->m_data + n->m_len), (m->m_data + off), len);
|
|
|
|
n->m_len += len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a pointer into an mbuf, return the mbuf
|
|
|
|
* XXX This is a kludge, I should eliminate the need for it
|
|
|
|
* Fortunately, it's not used often
|
|
|
|
*/
|
|
|
|
struct mbuf *
|
2009-06-24 14:42:31 +02:00
|
|
|
dtom(Slirp *slirp, void *dat)
|
2004-04-22 02:10:48 +02:00
|
|
|
{
|
|
|
|
struct mbuf *m;
|
2007-09-16 23:08:06 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
DEBUG_CALL("dtom");
|
2015-08-29 09:12:35 +02:00
|
|
|
DEBUG_ARG("dat = %p", dat);
|
2004-04-22 02:10:48 +02:00
|
|
|
|
|
|
|
/* bug corrected for M_EXT buffers */
|
2009-06-24 14:42:31 +02:00
|
|
|
for (m = slirp->m_usedlist.m_next; m != &slirp->m_usedlist;
|
|
|
|
m = m->m_next) {
|
2004-04-22 02:10:48 +02:00
|
|
|
if (m->m_flags & M_EXT) {
|
|
|
|
if( (char *)dat>=m->m_ext && (char *)dat<(m->m_ext + m->m_size) )
|
|
|
|
return m;
|
|
|
|
} else {
|
|
|
|
if( (char *)dat >= m->m_dat && (char *)dat<(m->m_dat + m->m_size) )
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 23:08:06 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
DEBUG_ERROR((dfd, "dtom failed"));
|
2007-09-16 23:08:06 +02:00
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
return (struct mbuf *)0;
|
|
|
|
}
|