All of lore.kernel.org
 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

* Re: mmap() returns with -EINVAL
  2009-07-30 13:19 mmap() returns with -EINVAL leo mueller
@ 2009-08-16 14:11 ` Reto Glauser
  2009-08-16 14:47 ` LDB
  1 sibling, 0 replies; 3+ messages in thread
From: Reto Glauser @ 2009-08-16 14:11 UTC (permalink / raw)
  To: leo mueller; +Cc: linux-c-programming

On 07/30/2009 03:19 PM,  leo mueller wrote:
>
> 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?
>

maybe somethings wrong with your socket. mmap-ping a fd for example only works
if you open the fd O_RDWR, e.g.:

   /** Open a file for writing.
    *  - Creating the file if it doesn't exist.
    *  - Truncating it to 0 size if it already exists. (not really needed)
    *
    * Note: "O_WRONLY" mode is not sufficient when mmap-ing.
   **/
   fd = open( FILEPATH, O_RDWR | O_CREAT | O_TRUNC, ( mode_t )0600 );
   if ( fd == -1 )
   {
      perror( "Error opening file for writing" );
      exit( EXIT_FAILURE );
   }

   /**
    * Now the file is ready to be mmap-ped.
   **/
   map = mmap( 0, FILESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
   if ( map == MAP_FAILED )
   {
      close( fd );
      perror( "Error mmapping the file" );
      exit( EXIT_FAILURE );
   }



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

* Re: mmap() returns with -EINVAL
  2009-07-30 13:19 mmap() returns with -EINVAL leo mueller
  2009-08-16 14:11 ` Reto Glauser
@ 2009-08-16 14:47 ` LDB
  1 sibling, 0 replies; 3+ messages in thread
From: LDB @ 2009-08-16 14:47 UTC (permalink / raw)
  To: leo mueller; +Cc: linux-c-programming

leo mueller wrote:
> 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
> 

It has to be one of the reason below:


   EINVAL We don't like addr, length, or offset (e.g., they are too large, or
not aligned on a page boundary).

   EINVAL (since Linux 2.6.12) length was 0.

   EINVAL flags contained neither MAP_PRIVATE or MAP_SHARED, or contained both
of these values.



Also, according to the man pages under the BUGS section:


   SUSv3 specifies that mmap() should fail if length is 0.
   However, in kernels  before 2.6.12, mmap() succeeded in this case: no
   mapping was created and the call returned addr.  Since kernel 2.6.12,
   mmap() fails with the error EINVAL for this case.

Lastly, since mmap() has been superseded by mmap2(), you might want to
try the latter.




^ 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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.