why is it not possible to use mmap with socket fd as an argument?
i know it's not possibl开发者_开发知识库e, i'm trying to understand the true reason behind it OS wise
Because the concept of a socket simply doesn't map to the concept of a random-access in-memory array, which is the abstraction which mmap
gives you. A file on a block-device (disk) usually allows random read/write access. This maps nicely to an in-memory contiguous array, which also gives you random read/write access.
A socket, however, is usually stream (or packet/datagram) oriented. Meaning, a stream of data gets sent over the socket, and a stream of data is received from the socket. But you can't, for example, write/read to the Nth byte of an open socket stream - that simply doesn't make any sense conceptually.
In fact it is possible with some protocol families in linux, namely:
- PF_NETLINK
- PF_PACKET
For the rest of protocols mmapping is not implemented/possible. For example PF_INET
The mmap system call for socket gets dispatched here
See also:
- Documentation/networking/packet_mmap.txt
There is whole thing called packet mmap. Search on google. There is program example in elixir maintained and doc page with example. Just search
Program link in kernel source maintained to view
https://elixir.bootlin.com/linux/v3.14.32/source/tools/testing/selftests/net
Tutorial https://www.kernel.org/doc/html/latest/networking/packet_mmap.html
What is it. Use socket fd with mmap. So mapping socket to rx and tx descriptor in device driver.
To understand more about device descriptors u can look at this code https://github.com/torvalds/linux/blob/master/drivers/net/ethernet/realtek/r8169_main.c in case anyone is are curious and stumble upon this page and linux device drivers pdf
精彩评论