PACKET socket通常用来发送和接收链路层的包。如果没有开启PACKET_MMAP,收发包的处理是非常低效的,一次系统调用只能取得一个包,如果想要知道包的时间戳,需要两次系统调用。
相反,PACKET_MMAP非常高效,它提供了一个大小可配置的环形缓存,该缓存映射到用户态,用来发送和接收数据包。这种方式下,读取数据包只需要等待它们。发送数据,则一次系统调用可以发送多个包。
接收方式:
socket() creation of the capture socket
setsockopt() allocation of the circular buffer (ring) option: PACKET_RX_RING
mmap() mapping of the allocated buffer to the user process
poll() to wait for incoming packets
close() destruction of the socket and deallocation of all associated resources
发送方式:
socket()
setsockopt() option: PACKET_TX_RING
bind()
mmap()
poll()
send()
close()
具体的使用时在了解。
参考
https://www.kernel.org/doc/Documentation/networking/packet_mmap.txt