linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* mmap() returns with -EINVAL
@ 2009-07-30 13:19 leo mueller
  2009-08-16 14:11 ` Reto Glauser
  2009-08-16 14:47 ` LDB
  0 siblings, 2 replies; 3+ messages in thread
From: leo mueller @ 2009-07-30 13:19 UTC (permalink / raw)
  To: linux-c-programming

[-- Attachment #1: Type: text/plain, Size: 375 bytes --]

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

[-- Attachment #2: err.txt --]
[-- Type: text/plain, Size: 1497 bytes --]

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");
}



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-08-16 14:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-07-30 13:19 mmap() returns with -EINVAL leo mueller
2009-08-16 14:11 ` Reto Glauser
2009-08-16 14:47 ` LDB

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).