mmap() returns with -EINVAL

leo mueller <[email protected]>
Newsgroups org.kernel.vger.linux-c-programming
Message-ID <[email protected]>
hi all,

in my attached code snippet i try to mmap the incoming socket data
accoring to the kernel documentation
which can also be found online:
http://lxr.linux.no/linux+v2.6.30/Documentation/networking/packet_mmap.txt

by doing a mmap() my program exits with -EINVAL and up to now i have
no clue why... parametes should be right.

do you have any idea?

big thanks,
daniel
err.txt (text/plain, 1.5 KB)
typedef unsigned char *ring_buff_t;

[...]

ring_buff_t create_virt_ring(int sock)
{
    int ret;
    ring_buff_t rb;
    struct tpacket_req req;

    memset(&req, 0, sizeof(req));

    dbg("pagesize: %d\n", getpagesize());

    req.tp_block_size = getpagesize();
    req.tp_frame_size = TP_RX_FRAME_SIZ;
    req.tp_block_nr = TP_RX_BLOCKS;
    req.tp_frame_nr = req.tp_block_size / req.tp_frame_size * req.tp_block_nr;

    ret = setsockopt(sock, SOL_SOCKET, PACKET_RX_RING, (void *) &req, sizeof(req));
    if(ret < 0){
        err("setsockopt: creation of rx ring failed: %d - ", errno);
        perror("");
        goto _out_err;
    }

    rb = mmap(0, (size_t) req.tp_block_nr * req.tp_block_size, PROT_READ | PROT_WRITE, MAP_SHARED, sock, 0);
    if(rb == MAP_FAILED){
        err("mmap: cannot mmap the rx ring: %d - ", errno);
        perror("");
        goto _out_mmap;
    }

    dbg("kernel ringbuff allocated\n");

    return rb;

_out_mmap:
    destroy_virt_ring(sock, rb);
_out_err:
    close(sock);
    exit(1);
}

[...]

void destroy_virt_ring(int sock, ring_buff_t rb)
{
    int ret;
    struct tpacket_req req;

    memset(&req, 0, sizeof(req));
    ret = setsockopt(sock, SOL_PACKET, PACKET_RX_RING, (void *) &req, sizeof(req));
    if(ret < 0){
        err("setsockopt: destruction of rx ring failed: %d - ", errno);
        perror("");
    }

	if(rb){
        munmap(rb, TP_RX_BLOCKS * getpagesize());
        rb = 0;
    }
    
    dbg("kernel ringbuff deallocated\n");
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.