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
|
2017-02-05 00:08:34 +01:00
|
|
|
* could hold, an external g_malloced buffer is pointed to
|
2004-04-22 02:10:48 +02:00
|
|
|
* 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"
|
2016-06-22 19:11:19 +02:00
|
|
|
#include "slirp.h"
|
2004-04-22 02:10:48 +02:00
|
|
|
|
2007-10-26 21:01:16 +02:00
|
|
|
#define MBUF_THRESH 30
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find a nice value for msize
|
|
|
|
*/
|
2016-03-15 10:31:20 +01:00
|
|
|
#define SLIRP_MSIZE\
|
|
|
|
(offsetof(struct mbuf, m_dat) + IF_MAXLINKHDR + TCPIPHDR_DELTA + IF_MTU)
|
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
|
|
|
{
|
2016-02-22 22:29:21 +01:00
|
|
|
slirp->m_freelist.qh_link = slirp->m_freelist.qh_rlink = &slirp->m_freelist;
|
|
|
|
slirp->m_usedlist.qh_link = slirp->m_usedlist.qh_rlink = &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;
|
|
|
|
|
2016-02-22 22:29:21 +01:00
|
|
|
m = (struct mbuf *) slirp->m_usedlist.qh_link;
|
|
|
|
while ((struct quehead *) m != &slirp->m_usedlist) {
|
2012-02-29 19:14:23 +01:00
|
|
|
next = m->m_next;
|
|
|
|
if (m->m_flags & M_EXT) {
|
2017-02-05 00:08:34 +01:00
|
|
|
g_free(m->m_ext);
|
2012-02-29 19:14:23 +01:00
|
|
|
}
|
2017-02-05 00:08:34 +01:00
|
|
|
g_free(m);
|
2012-02-29 19:14:23 +01:00
|
|
|
m = next;
|
|
|
|
}
|
2016-02-22 22:29:21 +01:00
|
|
|
m = (struct mbuf *) slirp->m_freelist.qh_link;
|
|
|
|
while ((struct quehead *) m != &slirp->m_freelist) {
|
2012-02-29 19:14:23 +01:00
|
|
|
next = m->m_next;
|
2017-02-05 00:08:34 +01:00
|
|
|
g_free(m);
|
2012-02-29 19:14:23 +01:00
|
|
|
m = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-22 02:10:48 +02:00
|
|
|
/*
|
|
|
|
* Get an mbuf from the free list, if there are none
|
2017-02-05 00:08:34 +01:00
|
|
|
* allocate 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,
|
2017-02-05 00:08:34 +01:00
|
|
|
* which tells m_free to actually g_free() it
|
2004-04-22 02:10:48 +02:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
2016-02-22 22:29:21 +01:00
|
|
|
if (slirp->m_freelist.qh_link == &slirp->m_freelist) {
|
2017-02-05 00:08:34 +01:00
|
|
|
m = g_malloc(SLIRP_MSIZE);
|
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 {
|
2016-02-22 22:29:21 +01:00
|
|
|
m = (struct mbuf *) slirp->m_freelist.qh_link;
|
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;
|
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 */
|
2017-02-05 00:08:34 +01:00
|
|
|
if (m->m_flags & M_EXT) {
|
|
|
|
g_free(m->m_ext);
|
|
|
|
}
|
2004-04-22 02:10:48 +02:00
|
|
|
/*
|
|
|
|
* 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--;
|
2017-02-05 00:08:34 +01:00
|
|
|
g_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
|
2017-02-05 00:08:34 +01:00
|
|
|
* the other.. if result is too big for one mbuf, allocate
|
2004-04-22 02:10:48 +02:00
|
|
|
* 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)
|
2018-06-05 20:08:35 +02:00
|
|
|
m_inc(m, m->m_len + n->m_len);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-05 20:08:35 +02:00
|
|
|
/* make m 'size' bytes large from m_data */
|
2004-04-22 02:10:48 +02:00
|
|
|
void
|
2009-03-07 16:32:56 +01:00
|
|
|
m_inc(struct mbuf *m, int size)
|
2004-04-22 02:10:48 +02:00
|
|
|
{
|
2018-06-05 20:08:35 +02:00
|
|
|
int datasize;
|
2006-05-01 12:59:02 +02:00
|
|
|
|
2018-06-05 20:08:35 +02:00
|
|
|
/* some compilers throw up on gotos. This one we can fake. */
|
|
|
|
if (m->m_size > size) {
|
|
|
|
return;
|
|
|
|
}
|
2004-04-22 02:10:48 +02:00
|
|
|
|
2018-06-05 20:08:35 +02:00
|
|
|
if (m->m_flags & M_EXT) {
|
|
|
|
datasize = m->m_data - m->m_ext;
|
|
|
|
m->m_ext = g_realloc(m->m_ext, size + datasize);
|
|
|
|
} else {
|
|
|
|
datasize = m->m_data - m->m_dat;
|
|
|
|
m->m_ext = g_malloc(size + datasize);
|
|
|
|
memcpy(m->m_ext, m->m_dat, m->m_size);
|
|
|
|
m->m_flags |= M_EXT;
|
|
|
|
}
|
2007-09-16 23:08:06 +02:00
|
|
|
|
2018-06-05 20:08:35 +02:00
|
|
|
m->m_data = m->m_ext + datasize;
|
|
|
|
m->m_size = size + datasize;
|
2004-04-22 02:10:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 */
|
2016-02-22 22:29:21 +01:00
|
|
|
for (m = (struct mbuf *) slirp->m_usedlist.qh_link;
|
|
|
|
(struct quehead *) m != &slirp->m_usedlist;
|
2009-06-24 14:42:31 +02:00
|
|
|
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;
|
|
|
|
}
|