/*-
* Copyright (c) 2017-2018 Internet Initiative Japan Inc.
* Copyright (c) 2010 Antti Kantee.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* The BUFSIZE come from shmif_dumpbus.c because there're not any experiences to
* make a decision about best size.
*/
#define BUFSIZE 64*1024
/*
* dowakeup, shmif_lockbus and shmif_unlockbus copy from
* sys/rump/net/lib/libshimif/if_shmem.c. So if you have to understood locking
* mechanism, you also see that.
*/
static int
dowakeup(int memfd)
{
struct iovec iov;
uint32_t ver = SHMIF_VERSION;
/*
* This locking needs work and will misbehave severely if:
* 1) the backing memory has to be paged in
* 2) some lockholder exits while holding the lock
*/
static void
shmif_lockbus(struct shmif_mem *busmem)
{
int i = 0;
while (__predict_false(atomic_cas_32(&busmem->shm_lock,
LOCK_UNLOCKED, LOCK_LOCKED) == LOCK_LOCKED)) {
if (__predict_false(++i > LOCK_COOLDOWN)) {
usleep(1000);
i = 0;
}
continue;
}
membar_enter();
}
static void
shmif_unlockbus(struct shmif_mem *busmem)
{
unsigned int old __diagused;
membar_exit();
old = atomic_swap_32(&busmem->shm_lock, LOCK_UNLOCKED);
assert(old == LOCK_LOCKED);
}
int
main(int argc, char *argv[])
{
struct stat sb;
void *busmem;
struct shmif_mem *bmem;
int fd;
const u_char *pkt;
char *buf;
char pcap_errbuf[PCAP_ERRBUF_SIZE];
pcap_t *pcap;
struct pcap_pkthdr pcaphdr;
argc -= optind;
argv += optind;
if (argc != 2)
usage();
buf = malloc(BUFSIZE);
if (buf == NULL)
err(EXIT_FAILURE, "malloc");
if (bmem->shm_magic != SHMIF_MAGIC)
errx(EXIT_FAILURE, "%s not a shmif bus", argv[1]);
/*
* if bmem->shm_version is 0, it indicates to not write any packets
* by anyone.
*/
if (bmem->shm_version != SHMIF_VERSION && bmem->shm_version != 0)
errx(EXIT_FAILURE, "bus version %d, program %d",
bmem->shm_version, SHMIF_VERSION);