⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 packet_mmap.txt

📁 linux 内核源代码
💻 TXT
📖 第 1 页 / 共 2 页
字号:
--------------------------------------------------------------------------------+ ABSTRACT--------------------------------------------------------------------------------This file documents the CONFIG_PACKET_MMAP option available with the PACKETsocket interface on 2.4 and 2.6 kernels. This type of sockets is used for capture network traffic with utilities like tcpdump or any other that uses the libpcap library. You can find the latest version of this document at    http://pusa.uv.es/~ulisses/packet_mmap/Please send me your comments to    Ulisses Alonso Camaró <uaca@i.hate.spam.alumni.uv.es>-------------------------------------------------------------------------------+ Why use PACKET_MMAP--------------------------------------------------------------------------------In Linux 2.4/2.6 if PACKET_MMAP is not enabled, the capture process is veryinefficient. It uses very limited buffers and requires one system callto capture each packet, it requires two if you want to get packet's timestamp (like libpcap always does).In the other hand PACKET_MMAP is very efficient. PACKET_MMAP provides a size configurable circular buffer mapped in user space. This way reading packets just needs to wait for them, most of the time there is no need to issue a single system call. By using a shared buffer between the kernel and the user also has the benefit of minimizing packet copies.It's fine to use PACKET_MMAP to improve the performance of the capture process, but it isn't everything. At least, if you are capturing at high speeds (this is relative to the cpu speed), you should check if the device driver of your network interface card supports some sort of interrupt load mitigation or (even better) if it supports NAPI, also make sure it is enabled.--------------------------------------------------------------------------------+ How to use CONFIG_PACKET_MMAP--------------------------------------------------------------------------------From the user standpoint, you should use the higher level libpcap library, whichis a de facto standard, portable across nearly all operating systemsincluding Win32. Said that, at time of this writing, official libpcap 0.8.1 is out and doesn't includesupport for PACKET_MMAP, and also probably the libpcap included in your distribution. I'm aware of two implementations of PACKET_MMAP in libpcap:    http://pusa.uv.es/~ulisses/packet_mmap/  (by Simon Patarin, based on libpcap 0.6.2)    http://public.lanl.gov/cpw/              (by Phil Wood, based on lastest libpcap)The rest of this document is intended for people who want to understandthe low level details or want to improve libpcap by including PACKET_MMAPsupport.--------------------------------------------------------------------------------+ How to use CONFIG_PACKET_MMAP directly--------------------------------------------------------------------------------From the system calls stand point, the use of PACKET_MMAP involvesthe following process:[setup]     socket() -------> creation of the capture socket            setsockopt() ---> allocation of the circular buffer (ring)            mmap() ---------> mapping of the allocated buffer to the                              user process[capture]   poll() ---------> to wait for incoming packets[shutdown]  close() --------> destruction of the capture socket and                              deallocation of all associated                               resources.socket creation and destruction is straight forward, and is done the same way with or without PACKET_MMAP:int fd;fd= socket(PF_PACKET, mode, htons(ETH_P_ALL))where mode is SOCK_RAW for the raw interface were link levelinformation can be captured or SOCK_DGRAM for the cookedinterface where link level information capture is not supported and a link level pseudo-header is provided by the kernel.The destruction of the socket and all associated resourcesis done by a simple call to close(fd).Next I will describe PACKET_MMAP settings and it's constraints,also the mapping of the circular buffer in the user process and the use of this buffer.--------------------------------------------------------------------------------+ PACKET_MMAP settings--------------------------------------------------------------------------------To setup PACKET_MMAP from user level code is done with a call like     setsockopt(fd, SOL_PACKET, PACKET_RX_RING, (void *) &req, sizeof(req))The most significant argument in the previous call is the req parameter, this parameter must to have the following structure:    struct tpacket_req    {        unsigned int    tp_block_size;  /* Minimal size of contiguous block */        unsigned int    tp_block_nr;    /* Number of blocks */        unsigned int    tp_frame_size;  /* Size of frame */        unsigned int    tp_frame_nr;    /* Total number of frames */    };This structure is defined in /usr/include/linux/if_packet.h and establishes a circular buffer (ring) of unswappable memory mapped in the capture process. Being mapped in the capture process allows reading the captured frames and related meta-information like timestamps without requiring a system call.Captured frames are grouped in blocks. Each block is a physically contiguous region of memory and holds tp_block_size/tp_frame_size frames. The total number of blocks is tp_block_nr. Note that tp_frame_nr is a redundant parameter because    frames_per_block = tp_block_size/tp_frame_sizeindeed, packet_set_ring checks that the following condition is true    frames_per_block * tp_block_nr == tp_frame_nrLets see an example, with the following values:     tp_block_size= 4096     tp_frame_size= 2048     tp_block_nr  = 4     tp_frame_nr  = 8we will get the following buffer structure:        block #1                 block #2         +---------+---------+    +---------+---------+    | frame 1 | frame 2 |    | frame 3 | frame 4 |    +---------+---------+    +---------+---------+            block #3                 block #4+---------+---------+    +---------+---------+| frame 5 | frame 6 |    | frame 7 | frame 8 |+---------+---------+    +---------+---------+A frame can be of any size with the only condition it can fit in a block. A blockcan only hold an integer number of frames, or in other words, a frame cannot be spawned accross two blocks, so there are some details you have to take into account when choosing the frame_size. See "Mapping and use of the circular buffer (ring)".--------------------------------------------------------------------------------+ PACKET_MMAP setting constraints--------------------------------------------------------------------------------In kernel versions prior to 2.4.26 (for the 2.4 branch) and 2.6.5 (2.6 branch),the PACKET_MMAP buffer could hold only 32768 frames in a 32 bit architecture or16384 in a 64 bit architecture. For information on these kernel versionssee http://pusa.uv.es/~ulisses/packet_mmap/packet_mmap.pre-2.4.26_2.6.5.txt Block size limit------------------As stated earlier, each block is a contiguous physical region of memory. These memory regions are allocated with calls to the __get_free_pages() function. As the name indicates, this function allocates pages of memory, and the secondargument is "order" or a power of two number of pages, that is (for PAGE_SIZE == 4096) order=0 ==> 4096 bytes, order=1 ==> 8192 bytes, order=2 ==> 16384 bytes, etc. The maximum size of a region allocated by __get_free_pages is determined by the MAX_ORDER macro. More precisely the limit can be calculated as:   PAGE_SIZE << MAX_ORDER   In a i386 architecture PAGE_SIZE is 4096 bytes    In a 2.4/i386 kernel MAX_ORDER is 10   In a 2.6/i386 kernel MAX_ORDER is 11So get_free_pages can allocate as much as 4MB or 8MB in a 2.4/2.6 kernel respectively, with an i386 architecture.User space programs can include /usr/include/sys/user.h and /usr/include/linux/mmzone.h to get PAGE_SIZE MAX_ORDER declarations.The pagesize can also be determined dynamically with the getpagesize (2) system call.  Block number limit--------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -