Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 10 Jul 2003 14:10:15 -0400
From:      kw3wong@engmail.uwaterloo.ca
To:        Luigi Rizzo <rizzo@icir.org>
Cc:        dsze@engmail.uwaterloo.ca
Subject:   Re: Question about bridging code
Message-ID:  <1057860615.3f0dac07e1418@www.nexusmail.uwaterloo.ca>
In-Reply-To: <20030709195455.A24039@xorpc.icir.org>
References:  <1057778632.3f0c6bc8af474@www.nexusmail.uwaterloo.ca> <20030709195455.A24039@xorpc.icir.org>

next in thread | previous in thread | raw e-mail | index | archive | help
Hi Luigi,

Thanks for response, the vmnet/tap stuff sounds like neat stuff. After reading 
the description of tap (from the vtun site), the system seems to make a lot of 
sense. However, I'm not sure how vmnet comes into play here - what purpose does 
it serve, shouldn't I just be able to read from the /dev/tap0 and bridge 
between tap0 and fxp0?

I tried writing a simple program to use tap. Basically, I have the bridge setup 
as such

    net.link.ether.bridge_cfg: fxp0:0,tap0:0 tap1:1 bge0:1

And I ran this quick test program that I wrote:

#include <sys/types.h>          // ssize_t
#include <sys/socket.h>         // ::socket
#include <netinet/in_systm.h>   // n_long
#include <netinet/in.h>         // IPPROTO_DIVERT, struct sockaddr_in
#include <netinet/ip.h>         // IP_MAXPACKET
#include <netinet/tcp.h>        // struct tcphdr
#include <fcntl.h>              // ::fcntl
#include <err.h>                // ::err
#include <errno.h>              // errno
#include <stdio.h>              // ::printf
#include <string.h>             // ::bcopy
#include <unistd.h>             // ::close

#define MAX(a, b)  (((a) > (b)) ? (a) : (b))

/** main
*/
int main()
{
    int                     nFDRight;
    int                     nFDLeft;
    unsigned char           kpucInPacket[IP_MAXPACKET];

    nFDRight = open("/dev/tap0", O_RDWR);
    if (nFDRight < 0)
        ::err(errno, "open");

    nFDLeft  = open("/dev/tap1", O_RDWR);
    if (nFDLeft < 0)
        ::err(errno, "open");

    fd_set masterReadSocks;
    fd_set currentReadSocks;

    FD_ZERO(&masterReadSocks);
    FD_SET(nFDRight,&masterReadSocks);
    FD_SET(nFDLeft, &masterReadSocks);

    int nMaxFD = MAX(nFDRight, nFDLeft);

    while (true)
    {
        ::bcopy(&masterReadSocks, &currentReadSocks, sizeof(fd_set));

        int nSelectValue =
            select(nMaxFD + 1,
                &currentReadSocks,
                NULL,
                NULL,
                NULL);

        printf("Unblocked on select\n");

        if (nSelectValue == -1)
        {
            /*  Signal interrupted, just continue
            */
            if (errno == EINTR)
                continue;

            ::err(errno, "select");
        }

        if (FD_ISSET(nFDRight, &currentReadSocks))
        {
            int nReadSize = read(nFDRight, kpucInPacket, sizeof(kpucInPacket));
            if (nReadSize < 0)
                err(errno, "read");

            if (write(nFDLeft, kpucInPacket, nReadSize) < 0)
                err(errno, "write");
        }

        if (FD_ISSET(nFDLeft, &currentReadSocks))
        {
            int nReadSize = read(nFDLeft, kpucInPacket, sizeof(kpucInPacket));
            if (nReadSize < 0)
                err(errno, "read");

            if (write(nFDRight, kpucInPacket, nReadSize) < 0)
                err(errno, "write");
        }
    }

    close(nFDLeft);
    close(nFDRight);
}


Unfortunately, it doesn't work, it only gets a read event when I make changes 
to the tap interface via ifconfig. I guess I don't fully understand how 
the /dev/tapN interface works, can you (or anybody who also know) point out 
what I'm doing wrong? Thanks again!

Bernie

----------------------------------------
This mail sent through www.mywaterloo.ca



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1057860615.3f0dac07e1418>