misc.c revision 20bc99aa3ad84a338b7a9c1edc14682bce25d761
0N/A/* $Id$ */
0N/A/** @file
0N/A * NAT - helpers.
0N/A */
0N/A
0N/A/*
0N/A * Copyright (C) 2006-2010 Sun Microsystems, Inc.
0N/A *
4764N/A * This file is part of VirtualBox Open Source Edition (OSE), as
0N/A * available from http://www.virtualbox.org. This file is free software;
0N/A * you can redistribute it and/or modify it under the terms of the GNU
0N/A * General Public License (GPL) as published by the Free Software
0N/A * Foundation, in version 2 as it comes in the "COPYING" file of the
0N/A * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
0N/A * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
0N/A * Clara, CA 95054 USA or visit http://www.sun.com if you need
0N/A * additional information or have any questions.
0N/A */
0N/A
0N/A/*
0N/A * This code is based on:
0N/A *
0N/A * Copyright (c) 1995 Danny Gasparovski.
0N/A *
0N/A * Please read the file COPYRIGHT for the
0N/A * terms and conditions of the copyright.
0N/A */
0N/A
0N/A#define WANT_SYS_IOCTL_H
0N/A#include <slirp.h>
0N/A
0N/A#ifndef HAVE_INET_ATON
0N/Aint
0N/Ainet_aton(const char *cp, struct in_addr *ia)
0N/A{
0N/A u_int32_t addr = inet_addr(cp);
0N/A if (addr == 0xffffffff)
0N/A return 0;
0N/A ia->s_addr = addr;
0N/A return 1;
2490N/A}
0N/A#endif
2490N/A
2490N/A/*
2490N/A * Get our IP address and put it in our_addr
2490N/A */
2490N/Avoid
2490N/Agetouraddr(PNATState pData)
2490N/A{
2490N/A our_addr.s_addr = loopback_addr.s_addr;
2490N/A}
2490N/A
2490N/Astruct quehead
0N/A{
0N/A struct quehead *qh_link;
0N/A struct quehead *qh_rlink;
0N/A};
0N/A
0N/Avoid
0N/Ainsque(PNATState pData, void *a, void *b)
0N/A{
0N/A register struct quehead *element = (struct quehead *) a;
0N/A register struct quehead *head = (struct quehead *) b;
0N/A element->qh_link = head->qh_link;
0N/A head->qh_link = (struct quehead *)element;
0N/A element->qh_rlink = (struct quehead *)head;
0N/A ((struct quehead *)(element->qh_link))->qh_rlink = (struct quehead *)element;
0N/A}
2490N/A
2490N/Avoid
0N/Aremque(PNATState pData, void *a)
0N/A{
2490N/A register struct quehead *element = (struct quehead *) a;
2490N/A ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
0N/A ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
0N/A element->qh_rlink = NULL;
0N/A /* element->qh_link = NULL; TCP FIN1 crashes if you do this. Why ? */
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Set fd blocking and non-blocking
0N/A */
2490N/Avoid
0N/Afd_nonblock(int fd)
0N/A{
0N/A#ifdef FIONBIO
0N/A int opt = 1;
0N/A
0N/A ioctlsocket(fd, FIONBIO, &opt);
0N/A#else
0N/A int opt;
0N/A
0N/A opt = fcntl(fd, F_GETFL, 0);
0N/A opt |= O_NONBLOCK;
2490N/A fcntl(fd, F_SETFL, opt);
2490N/A#endif
0N/A}
2490N/A
2490N/Avoid
2490N/Afd_block(int fd)
2490N/A{
2490N/A#ifdef FIONBIO
2490N/A int opt = 0;
0N/A
0N/A ioctlsocket(fd, FIONBIO, &opt);
0N/A#else
0N/A int opt;
0N/A
0N/A opt = fcntl(fd, F_GETFL, 0);
2490N/A opt &= ~O_NONBLOCK;
2490N/A fcntl(fd, F_SETFL, opt);
0N/A#endif
0N/A}
0N/A
0N/A#ifdef VBOX_WITH_SLIRP_BSD_MBUF
0N/A#define ITEM_MAGIC 0xdead0001
0N/Astruct item
0N/A{
0N/A uint32_t magic;
0N/A uma_zone_t zone;
0N/A uint32_t ref_count;
0N/A LIST_ENTRY(item) list;
0N/A};
0N/A
0N/A#define ZONE_MAGIC 0xdead0002
0N/Astruct uma_zone
0N/A{
0N/A uint32_t magic;
0N/A PNATState pData; /* to minimize changes in the rest of UMA emulation code */
0N/A RTCRITSECT csZone;
0N/A const char *name;
0N/A size_t size; /* item size */
0N/A ctor_t pfCtor;
0N/A dtor_t pfDtor;
0N/A zinit_t pfInit;
0N/A zfini_t pfFini;
0N/A uma_alloc_t pfAlloc;
0N/A uma_free_t pfFree;
0N/A int max_items;
2490N/A int cur_items;
2490N/A LIST_HEAD(RT_NOTHING, item) used_items;
0N/A LIST_HEAD(RT_NOTHING, item) free_items;
0N/A uma_zone_t master_zone;
0N/A void *area;
0N/A};
0N/A
0N/A
0N/Astatic void *slirp_uma_alloc(uma_zone_t zone,
0N/A int size, uint8_t *pflags, int fWait)
0N/A{
0N/A struct item *it;
0N/A uint8_t *sub_area;
0N/A void *ret = NULL;
0N/A
0N/A RTCritSectEnter(&zone->csZone);
0N/A for (;;)
0N/A {
0N/A if (!LIST_EMPTY(&zone->free_items))
0N/A {
0N/A zone->cur_items++;
0N/A it = LIST_FIRST(&zone->free_items);
0N/A LIST_REMOVE(it, list);
LIST_INSERT_HEAD(&zone->used_items, it, list);
if (zone->pfInit)
zone->pfInit(zone->pData, (void *)&it[1], zone->size, M_DONTWAIT);
ret = (void *)&it[1];
break;
}
if (!zone->master_zone)
{
/* We're on master zone and we cant allocate more */
Log2(("NAT: no room on %s zone\n", zone->name));
break;
}
/* we're on sub-zone we need get chunk of master zone and split
* it for sub-zone conforming chunks.
*/
sub_area = slirp_uma_alloc(zone->master_zone, zone->master_zone->size, NULL, 0);
if (!sub_area)
{
/* No room on master */
Log2(("NAT: no room on %s zone for %s zone\n", zone->master_zone->name, zone->name));
break;
}
zone->max_items++;
it = &((struct item *)sub_area)[-1];
/* it's chunk descriptor of master zone we should remove it
* from the master list first
*/
Assert((it->zone && it->zone->magic == ZONE_MAGIC));
RTCritSectEnter(&it->zone->csZone);
/* @todo should we alter count of master counters? */
LIST_REMOVE(it, list);
RTCritSectLeave(&it->zone->csZone);
/* @todo '+ zone->size' should be depend on flag */
memset(it, 0, sizeof(struct item));
it->zone = zone;
it->magic = ITEM_MAGIC;
LIST_INSERT_HEAD(&zone->free_items, it, list);
if (zone->cur_items >= zone->max_items)
LogRel(("NAT: zone(%s) has reached it maximum\n", zone->name));
}
RTCritSectLeave(&zone->csZone);
return ret;
}
static void slirp_uma_free(void *item, int size, uint8_t flags)
{
struct item *it;
uma_zone_t zone;
Assert(item);
it = &((struct item *)item)[-1];
Assert(it->magic == ITEM_MAGIC);
zone = it->zone;
/* check bourder magic */
Assert((*(uint32_t *)(((uint8_t *)&it[1]) + zone->size) == 0xabadbabe));
RTCritSectEnter(&zone->csZone);
Assert(zone->magic == ZONE_MAGIC);
LIST_REMOVE(it, list);
LIST_INSERT_HEAD(&zone->free_items, it, list);
zone->cur_items--;
RTCritSectLeave(&zone->csZone);
}
uma_zone_t uma_zcreate(PNATState pData, char *name, size_t size,
ctor_t ctor, dtor_t dtor, zinit_t init, zfini_t fini, int flags1, int flags2)
{
uma_zone_t zone = RTMemAllocZ(sizeof(struct uma_zone));
Assert((pData));
zone->magic = ZONE_MAGIC;
zone->pData = pData;
zone->name = name;
zone->size = size;
zone->pfCtor = ctor;
zone->pfDtor = dtor;
zone->pfInit = init;
zone->pfFini = fini;
zone->pfAlloc = slirp_uma_alloc;
zone->pfFree = slirp_uma_free;
RTCritSectInit(&zone->csZone);
return zone;
}
uma_zone_t uma_zsecond_create(char *name, ctor_t ctor,
dtor_t dtor, zinit_t init, zfini_t fini, uma_zone_t master)
{
uma_zone_t zone;
#if 0
if (master->pfAlloc != NULL)
zone = (uma_zone_t)master->pfAlloc(master, sizeof(struct uma_zone), NULL, 0);
#endif
Assert(master);
zone = RTMemAllocZ(sizeof(struct uma_zone));
if (zone == NULL)
return NULL;
Assert((master && master->pData));
zone->magic = ZONE_MAGIC;
zone->pData = master->pData;
zone->name = name;
zone->pfCtor = ctor;
zone->pfDtor = dtor;
zone->pfInit = init;
zone->pfFini = fini;
zone->pfAlloc = slirp_uma_alloc;
zone->pfFree = slirp_uma_free;
zone->size = master->size;
zone->master_zone = master;
RTCritSectInit(&zone->csZone);
return zone;
}
void uma_zone_set_max(uma_zone_t zone, int max)
{
int i = 0;
struct item *it;
zone->max_items = max;
zone->area = RTMemAllocZ(max * (sizeof(struct item) + zone->size + sizeof(uint32_t)));
for (; i < max; ++i)
{
it = (struct item *)(((uint8_t *)zone->area) + i*(sizeof(struct item) + zone->size + sizeof(uint32_t)));
it->magic = ITEM_MAGIC;
it->zone = zone;
*(uint32_t *)(((uint8_t *)&it[1]) + zone->size) = 0xabadbabe;
LIST_INSERT_HEAD(&zone->free_items, it, list);
}
}
void uma_zone_set_allocf(uma_zone_t zone, uma_alloc_t pfAlloc)
{
zone->pfAlloc = pfAlloc;
}
void uma_zone_set_freef(uma_zone_t zone, uma_free_t pfFree)
{
zone->pfFree = pfFree;
}
uint32_t *uma_find_refcnt(uma_zone_t zone, void *mem)
{
/*@todo (vvl) this function supposed to work with special zone storing
reference counters */
struct item *it = (struct item *)mem; /* 1st element */
Assert(mem != NULL);
Assert(zone->magic == ZONE_MAGIC);
/* for returning pointer to counter we need get 0 elemnt */
Assert(it[-1].magic == ITEM_MAGIC);
return &it[-1].ref_count;
}
void *uma_zalloc_arg(uma_zone_t zone, void *args, int how)
{
void *mem;
Assert(zone->magic == ZONE_MAGIC);
if (zone->pfAlloc == NULL)
return NULL;
RTCritSectEnter(&zone->csZone);
mem = zone->pfAlloc(zone, zone->size, NULL, 0);
if (zone->pfCtor)
zone->pfCtor(zone->pData, mem, zone->size, args, M_DONTWAIT);
RTCritSectLeave(&zone->csZone);
return mem;
}
void uma_zfree(uma_zone_t zone, void *item)
{
uma_zfree_arg(zone, item, NULL);
}
void uma_zfree_arg(uma_zone_t zone, void *mem, void *flags)
{
struct item *it;
Assert(zone->magic == ZONE_MAGIC);
if (zone->pfFree == NULL)
return;
Assert((mem));
RTCritSectEnter(&zone->csZone);
it = &((struct item *)mem)[-1];
if (it->magic != ITEM_MAGIC)
{
Log(("NAT:UMA: %p seems to be allocated on heap ... freeing\n", mem));
RTMemFree(mem);
RTCritSectLeave(&zone->csZone);
return;
}
Assert((zone->magic == ZONE_MAGIC && zone == it->zone));
if (zone->pfDtor)
zone->pfDtor(zone->pData, mem, zone->size, flags);
zone->pfFree(mem, 0, 0);
RTCritSectLeave(&zone->csZone);
}
int uma_zone_exhausted_nolock(uma_zone_t zone)
{
return 0;
}
void zone_drain(uma_zone_t zone)
{
}
void slirp_null_arg_free(void *mem, void *arg)
{
/*@todo (r=vvl) make it wiser*/
Assert(mem);
RTMemFree(mem);
}
void *uma_zalloc(uma_zone_t zone, int len)
{
return NULL;
}
struct mbuf *slirp_ext_m_get(PNATState pData, size_t cbMin, void **ppvBuf, size_t *pcbBuf)
{
struct mbuf *m;
size_t size = MCLBYTES;
if (cbMin < MSIZE)
size = MCLBYTES;
else if (cbMin < MCLBYTES)
size = MCLBYTES;
else if (cbMin < MJUM9BYTES)
size = MJUM9BYTES;
else if (cbMin < MJUM16BYTES)
size = MJUM16BYTES;
else
AssertMsgFailed(("Unsupported size"));
m = m_getjcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR, size);
m->m_len = size;
*ppvBuf = mtod(m, void *);
*pcbBuf = size;
return m;
}
void slirp_ext_m_free(PNATState pData, struct mbuf *m)
{
m_free(pData, m);
}
static void zone_destroy(uma_zone_t zone)
{
RTCritSectEnter(&zone->csZone);
LogRel(("NAT: zone(nm:%s, used:%d)\n", zone->name, zone->cur_items));
if (zone->master_zone)
RTMemFree(zone->area);
RTCritSectLeave(&zone->csZone);
RTCritSectDelete(&zone->csZone);
RTMemFree(zone);
}
void m_fini(PNATState pData)
{
zone_destroy(pData->zone_mbuf);
zone_destroy(pData->zone_clust);
zone_destroy(pData->zone_pack);
zone_destroy(pData->zone_jumbop);
zone_destroy(pData->zone_jumbo9);
zone_destroy(pData->zone_jumbo16);
/*@todo do finalize here.*/
}
#endif /* VBOX_WITH_SLIRP_BSD_MBUF */