摘 要:为避免创建缓冲区过程中必须指定大小和多次释放而导致可能的内存泄露和代码崩溃的弊端,提出一种自适应的嵌入式协议栈的缓冲区管理机制AutoBuf。它是基于抽象缓冲区接口而设计的,具有自适应性,支持动态内存的自动分配与回收,同时实现了嵌入式TCP/IP协议栈各层之间的零拷贝通信。在基于研究平台S3C44B0X的Web server网络数据监控系统上的测试结果表明,该缓冲区的设计满足嵌入式系统网络通信的应用需求,是一种高效、可靠的缓冲区管理机制。
关键词:嵌入式协议栈; 抽象缓冲区; 零拷贝; 内存分配
中图分类号:TP316文献标志码:A
文章编号:1001-3695(2009)06-2254-03
doi:10.3969/j.issn.1001-3695.2009.06.077
Design and implementation of adaptive buffer for embedded protocol stack
WANG Pei-dong, WU Xian-wei
(College of Computer Science & Technology, Harbin University of Science & Technology, Harbin 150080, China)
Abstract:
To avoid traditional method of creating buffer, which must have the size of buffer and free memory for many times, which will result in memory leaks and codes crash. This paper proposed a flexible buffer management mechanism AutoBuf for embedded network protocol stack. It was adaptive and scalable and based on an abstract buffer interface, supported dynamic me-mory allocation and backup. By using the AutoBuf buffer management mechanism with data zero copy technology, it implemented to transfer data through the embedded network protocol stack. The management mechanism had been applied to the Web server system base on S3C44b0X platform successfully. The results in real network condition show that the system provides a good performance and meets the necessary of embedded network system.
Key words:embedded stack; abstract buffer; zero-copy; memory allocation
随着网络技术的快速发展,主机间的通信速率已经提高到了千兆数量级,同时多媒体应用还要求网络协议支持实时业务。嵌入式设备网络化已经深入到日常生活中,而将嵌入式设备接入到互联网需要网络协议栈的支持。通过分析Linux系统中TCP/IP协议栈的实现过程,可以看出在协议栈中要有大量数据不断输入输出,而管理这些即时数据的关键是协议栈中的缓冲区管理机制,因此对嵌入式协议栈的缓冲区管理将直接影响到数据的传输速率和安全。通用以太网的缓冲区管理机制,例如4.4BSD mbuf[1]和现行Linux系统中的sk_buf[2]多是在大内存、高处理速率的基础上设计的,非常庞大复杂。由于嵌入式设备的硬件资源有限,特别是可用物理内存的限制,通用的协议栈必然不适用于嵌入式设备,在应用时要对标准的TCP/IP协议进行裁剪[3]和重新设计缓冲区管理机制。
1 缓冲区管理机制的性能需求分析
缓冲区管理[4]是对内存提供一种统一的管理手段,通过该手段能够对可用内存提供分配、回收、数据操作等行为。内存的分配操作是根据一定的内存分配策略从缓冲区中获得相应大小的内存空间;缓冲区的数据操作主要是向缓冲区写数据,从缓冲区读数据,在缓冲区中删除数据,对空闲的内存块进行合并等行为;内存的回收就是将已空闲的内存重新变为可用内存,以供存储其他新的数据。












