Pergunta

Linux has a feature to allow efficient capture of network packets by mmapping a shared buffer between the kernel and user. I'm trying to use this interface in a way that does not require root access (as I don't have it).

Often packet_mmap is used to look directly at all of the packets on the network, which would require root access. My application only requires the use of the standard linux UDP socket interface. I wish to use packet_mmap purely for efficiency - right now syscalls are eating over 50% of my CPU cycles.

Is there a way to configure packet_mmap such that it can be used from userspace?

Foi útil?

Solução 2

Looking at the Linux kernel Git repository, it appears that neither PF_INET sockets nor PF_INET6 sockets support memory-mapped access, so if by

My application only requires the use of the standard linux UDP socket interface. I wish to use packet_mmap purely for efficiency - right now syscalls are eating over 50% of my CPU cycles.

you mean you want to use memory-mapped access for a normal UDP or TCP socket, unfortunately, you can't. The same applies to raw IP sockets.

PF_PACKET sockets do support memory-mapped access, but they require elevated privileges, regardless of whether you're using memory-mapped access or not. They are not a replacement for PF_INET or PF_INET6 sockets; they are a mechanism for reading and writing link-layer packets, so if you want to run normal Internet applications atop them, good luck:

  1. you'll have to reimplement IP and whatever transport protocol you're using (UDP, TCP, etc.) yourself;
  2. you'll somehow have to keep the kernel's IP and transport protocol stack from processing those packets;

and you really don't want to try doing that.

(Note that by "elevated privileges" I don't necessarily mean "root privileges"; CAP_NET_RAW privileges should suffice. However, as I note, if you're trying to replace regular socket access, you don't want to use PF_PACKET sockets.)

Outras dicas

While this doesn't truly answer the question (since it's specifically about packet_mmap), given your parameters:

  1. Receiving UDP packets
  2. Want to reduce syscalls, nothing else.
  3. Willing to use Linux-specific features, but no root user
  4. Features of packet_mmap not really needed or desired.

I would recommend you forget completely about packet_mmap and instead have a look at recvmmsg (note the spelling, not a typo).

recvmmsg does not require special privilegues, it is very intuitive (no obscure stuff, it works just like readv), and it lets you receive many packets in one call, greatly reducing syscall overhead.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top