现在的位置: 首页 > 综合 > 正文

《Understanding Linux networking 》1.21Memory caches

2017年08月16日 ⁄ 综合 ⁄ 共 4219字 ⁄ 字号 评论关闭

 1.2. Common Coding Patterns

Each networking feature, like any other kernel feature, is just one of the citizens inside the kernel. As such, it must make proper and fair use of memory, CPU, and all other shared resources. Most features are not written as standalone pieces of kernel code, but interact with other kernel components more or less heavily depending on the feature. They therefore try, as much as possible, to follow similar mechanisms to implement similar functionalities (there is no need to reinvent the wheel every time).
每个网络功能模块,就像其他内核功能一样,是内核的做成部分。所以他也要公平合理的应用内存,CPU 等所有的共享资源。大部分功能并没有写成独立的内核代码,不同的网络功能模块跟其他的内核模块都有或多或少的联系。所以他们尽量根据相同的原理来实现相似的功能(没必要每次都重新发明车轮)

Some requirements are common to several kernel components, such as the need to allocate several instances of the same data structure type, the need to keep track of references to an instance of a data structure to avoid unsafe memory deallocations, etc. In the following subsections, we will view common ways in Linux to handle such requirements. I will also talk about common coding tricks that you may come across while browsing the kernel's code.
很多需求是是多个内核模块共通的,像分配相同数据类型实例的需求,跟踪摸个数据类型的实例避免不安全的内存重新分配的需求等。下面的子章节里,我们会了解到在Linux系统中处理此类需求的常用方法,也会讨论那些你在浏览核心代码的时候碰到到的编码技巧

This book uses subsystem as a loose term to describe a collection of files that implement a major set of features such as IP or routingand that tend to be maintained by the same people and to change in lockstep. In the rest of the chapter, I'll also use the term kernel component to refer to these subsystems, because the conventions discussed here apply to most parts of the kernel, not just those involved in networking.
书中subsystem 作为一个不精确术语,他用来描述某些文件的集合。这些文件实现 象IP或路由等功能的集合,它们都是拥有相同的适用对象必须同步更改。在剩下的章节里,我依然会用这个术语 来描述内核组成部分。因为这里讨论的规则适用于大部分内核组成部分而不紧紧是网络功能 .

1.2.1. Memory Caches
The kernel uses the kmalloc and kfree functions to allocate and free a memory block, respectively. The syntax of those two functions is similar to that of the two sister calls, malloc and free, from the libc user-space library. For more details on kmalloc and kfree, please refer to Linux Device Drivers (O'Reilly).
内核用kmalloc 和kfree函数 分配和释放内存块。这两个函数的语法同 c语言库函数 malloc 和free 相似。从《Linux Device Drivers》
可以了解到更详细的描述。
It is common for a kernel component to allocate several instances of the same data structure type. When allocation and deallocation are expected to happen often, the associated kernel component initialization routine (for example, fib_hash_init for the routing table) usually allocates a special memory cache that will be used for the allocations. When a memory block is freed, it is actually returned to the same cache from which it was allocated.
一个内核组件通常需要分配一个数据结构的多个实例。当分配和回收频繁发生时,相关联的内核组件初始化程序经常会分配一块特殊的内存块,用来加速内存分配。当一个内存块被释放,他会返回他被分配到的内存。

Some examples of network data structures for which the kernel maintains dedicated memory caches include:
内核专用的网络数据结构内存模块包括:

Socket buffer descriptors

This cache, allocated by skb_init in net/core/sk_buff.c, is used for the allocation of sk_buff buffer descriptors. The sk_buff structure is probably the one that registers the highest number of allocations and deallocations in the networking subsystem.
这个缓存由net/core/sk_buff.c 里的 skb_init函数分配。sk_buff结构是网路子系统中最分配和释放最频繁的数据结构置一

Neighboring protocol mappings
Each neighboring protocol uses a memory cache to allocate the data structures that store L3-to-L2 address mappings. See Chapter 27.
邻居协议使用内存缓存来分配neighbour结构,这个结构保存L3到L2的地址映射关系。
Routing tables
The routing code uses two memory caches for two of the data structures that define routes. See Chapter 32.
Here are the key kernel functions used to deal with memory caches:
路由代码使用两个内存缓存来分配两种数据结构,这两种数据结构定义了路由表。以下是使用内存缓存时会用到的一些函数:
kmem_cache_create

kmem_cache_destroy

Create and destroy a cache.

kmem_cache_alloc

kmem_cache_free

Allocate and return a buffer to the cache. They are usually called via wrappers, which manage the requests for allocation and deallocation at a higher level. For example, the request to free an instance of an sk_buff buffer with kfree_skb ends up calling kmem_cache_free only when all the references to the buffer have been released and all the necessary cleanup has been done by the interested subsystems (for instance, the firewall).
从内存缓存中分配或释放一个对象。他们通常会在一个包装函数中被调用,这个包装函数在更高的层次上处理分配和释放的请求。比如:kfree_skb函数处理释放sk_buff的请求,但是只有在任何对此结构的引用释放之后并且相关的子系统(比如,防火墙)已执行了清除操作之后,才调用kmem_cache_free释放这个sk_buff。
The limit on the number of instances that can be allocated from a given cache (when present) is usually enforced by the wrappers around kmem_cache_alloc, and are sometimes configurable with a parameter in /proc.
从一个给定的内存缓存中能够分配多少个实例的数量限制通常在kmem_cache_alloc的包装函数中指定,但是有时也能够通过/proc文档系统中的参数来调整。
For more details on how memory caches are implemented and how they interface to the slab allocator, please refer to Understanding the Linux Kernel (O'Reilly).

抱歉!评论已关闭.