1. 程式人生 > >[中英對照]User-Space Device Drivers in Linux: A First Look

[中英對照]User-Space Device Drivers in Linux: A First Look

rac down 一定的 restrict eal 並發 程序開發 boost opera

如對Linux用戶態驅動程序開發有興趣,請閱讀本文,否則請飄過。

User-Space Device Drivers in Linux: A First Look | 初識Linux用戶態設備驅動程序

User-Space Device Drivers in Linux: A First Look
Mats Liljegren
Senior Software Architect

Device drivers in Linux are traditionally run in kernel space, but can
also be run in user space. This paper will take a look at running
drivers in user space, trying to answer the questions in what degree
the driver can run in user space and what can be gained from this?

Linux設備驅動通常運行在內核空間,但是也可以運行在用戶空間。本文將介紹運行在用戶空間中的設備驅動程序,試圖回答以下兩個問題:驅動程序在用戶空間中運行的程度,以及從中獲得的好處。

In the ‘90s, user-space drivers in Linux were much about how
to make graphics run faster[1] by avoiding calling the kernel.
These drivers where commonly used by the X-windows server.

User-space driver has become ever more important, as a blog
post by Tedd Hoff[2] illustrates. In his case the kernel is seen as
the problem when trying to achieve high server connection capacity.

Network interface hardware companies like Intel, Texas Instruments
and Freescale have picked up on this and are now providing
software solutions for user-space drivers supporting their hardware.

在上世紀90年代,在Linux中的用戶空間驅動程序集中於如何使圖形運行得更快,通過避免內核調用。這些驅動程序通常在X-windows服務器上使用。用戶空間驅動程序變得越來越重要, 在Tedd Hoff發表的博客中有所論述。在他論述的例子中,內核被認為是問題之所在,當試圖提供高並發連接服務器能力(註:c10k問題)的時候。諸如英特爾、德州儀器公司和飛思卡爾這樣的網絡接口硬件公司已經開始研究這一問題,現在他們正在為支持他們的硬件的用戶空間驅動程序提供軟件解決方案。

1. Problems with kernel-space drivers 內核空間驅動程序存在的問題

Device drivers normally run in kernel space, since handling
interrupts and mapping hardware resources require privileges
that only the kernel space is allowed to have. However, it is not
without drawbacks.

設備驅動程序通常在內核空間中運行,因為中斷處理和硬件資源映射需要特權,對應的特權只有內核空間才允許擁有。然而,它也並非沒有缺點。

1.1 System call overhead 系統調用的開銷

Each call to the kernel must perform a switch from user mode
to supervisor mode, and then back again. This takes time, which can
become a performance bottleneck if the calls are frequent.
Furthermore, the overhead is very much non-predictable, which
has a negative performance impact on real-time applications.

對內核的每一個調用必須從用戶模式切換到超級管理(內核)模式,然後再返回。這顯然需要時間,如果調用頻繁的話,就會成為性能瓶頸。此外,開銷很大程度上是不可預測的,這對實時應用程序將產生負面的性能影響。

1.2 Steep learning curve 學習曲線陡峭

The kernel-space API is different. For example, malloc() needs
to be replaced by one of the several types of memory allocations
that the kernel can offer, such as kmalloc(), vmalloc(), alloc_pages()
or get_zeroed_page(). There is a lot to learn before becoming
productive.

跟用戶空間API相比,內核空間API有所不同。例如,如取代malloc()的話,內核就提供了幾種不同類型的內存分配API,比如kmalloc(), vmalloc(), alloc_pages()或get_zeroed_page()。想在內核編程方面卓有成效,需要學習的東西很多。

1.3 Interface stability 接口穩定性

The kernel-space API is less stable than user-space APIs, making
maintenance a challenge.

與用戶空間的API比較而言, 內核空間API更不穩定,這無疑給代碼維護帶來了很大的挑戰。

1.4 Harder to debug 調試更困難

Debugging is very different in kernel space. Some tools often
used by user-space applications can be used for the kernel.
However, they represent exceptions rather than rule, where
LTTNG[3] is an example of the exception. To compensate for
this, the kernel has a lot of debug, tracing and profiling code
that can be enabled at compile time.

在內核空間中,調試有所不同,而且非常不同於用戶空間調試。在用戶空間應用程序調試中經常使用的一些工具可以用於內核調試。然而,他們代表著異常而非常態, 例如LTTNG[3]就是一個例外。為了彌補這一點,內核存在著許多調試、跟蹤和分析代碼,這些代碼可以在編譯的時候被啟用。

1.5 Bugs more fatal 錯誤更加致命

A crashing or misbehaving kernel tends to have a more severe
impact on the system than a crashing or misbehaving application,
which can affect robustness as well as how easy it is to debug.

內核崩潰或行為不正確對系統的影響比應用程序崩潰或不正確對系統的影響更大,這影響到系統的健壯性以及調試的容易程度。

1.6 Restrictive language choice 編程語言選擇嚴格受限

The kernel space is a very different programming environment
than user space. It is more restricted, for example only C language
is supported. This rules out any script based prototyping.

內核空間與用戶空間的編程環境非常不一樣。它受到的限制更多,例如只支持C語言。這就將任何基於腳本的原型設計排除在外了。

2. User-space drivers 用戶空間驅動

If there are so many problems with having device drivers in
kernel space, is it time to have all drivers in user space instead?
As always, everything has its drawbacks, user-space drivers are
no exception.

Most of the issues with kernel-space drivers are solved by having
the driver in user space, but the issue with interface stability is
only true for very simple user-space drivers.

For more advanced user-space drivers, many of the interfaces
available for kernel-space drivers need to be re-implemented
for user-space drivers. This means that interface stability will still
be an issue.

既然在內核空間中的設備驅動程序存在著很多問題,那麽是否應該將所有驅動程序都放在用戶空間中呢?一如既往地,任何解決方案都有缺點,用戶空間驅動程序也不例外。內核空間驅動程序存在的大部分問題都可以通過用戶空間驅動程序給解決掉,但接口穩定性的問題是只適用於那些很簡單的用戶空間驅動程序。對於更高級的用戶空間設備驅動,許多在內核空間才可用的接口需要為用戶空間驅動重新實現一下,這意味著接口的穩定性仍然是一個問題。

3. Challenges with user-space drivers 用戶空間設備驅動面臨的挑戰

There is a fear in the Linux kernel community that user-space
drivers are used as a tool to avoid the kernel‘s GPLv2 license.
This would undermine the idea with free open source software
ideas that GPLv2 has. However, this is outside the scope of
this paper.

在Linux內核社區,有這樣一個恐懼,那就是用戶空間驅動程序被當做一個工具來避免了內核GPLv2許可。這無疑將破壞GPLv2一貫主張的開源軟件理念。然而,這一點超出了本文討論的範圍。

Apart from this there are technical challenges for user-space
drivers.

除此之外,用戶空間設備驅動還存在技術上的諸多挑戰。

3.1 Interrupt handling 中斷處理

Without question, interrupt handling is the biggest challenge
for a user-space driver. The function handling an interrupt is
called in privileged execution mode, often called supervisor
mode. User-space drivers have no permission to execute in
privileged execution mode, making it impossible for user-space
drivers to implement an interrupt handler.

毫無疑問,中斷處理是用戶空間設備驅動面臨的最大的挑戰。中斷處理函數在特權執行模式(又叫做超級管理模式)下才能被調用。用戶空間驅動程序不允許在特權模式下執行,這使得在用戶空間驅動裏實現一個中斷處理程序是不可能的。

There are two ways to deal with this problem: Either you do not
use interrupts, which means that you have to poll instead.
Or have a small kernel-space driver handling only the interrupt.
In the latter case you can inform the user-space driver of an
interrupt either by a blocking call, which unblocks when
an interrupt occurs, or using POSIX signal to preempt the
user-space driver.

解決這個問題有兩種辦法:要麽不使用中斷,要麽有一個內核空間的驅動來專門處理中斷。在前一種辦法中,不使用中斷意味著必須使用輪詢。在後一種辦法中,你可以通過阻塞調用來通知用戶空間驅動程序,在中斷發生時打開阻塞調用,或者使用POSIX信號來搶占用戶空間驅動。

Polling is beneficial if interrupts are frequent, since there is
considerable overhead associated with each interrupt, due to
the switch from user mode to supervisor mode and back that
it causes. Each poll attempt on the other hand is usually only
a check for a value on a specific memory address.

如果中斷頻繁發生的話,那麽輪詢就是有益的,因為每次中斷都有相當大的開銷,這些開銷來源於從用戶模式切換到內核模式,然後再從內核模式返回到用戶模式。另一方面,每一次輪詢通常只是對位於特定內存地址的值進行檢查而已(,所以輪詢有好處,能減少系統開銷)。

When interrupts become scarcer, polling will instead do a lot
of work just to determine that there was no work to do. This is
bad for power saving.

當中斷變得稀少時,輪詢將會做大量的工作來確定沒有什麽工作可以做,這不利於節省能源消耗。

To get power saving when using user-space drivers with polling,
you can change the CPU clock frequency, or the number of
CPUs used, depending on work load. Both alternatives will
introduce ramp-up latency when there is a work load spike.

在用戶空間驅動程序使用輪詢的時候,如果要省電的話,可以根據工作負載來修改CPU的時鐘頻率,或者更改在用的CPU的個數。當遇到工作負載峰值的時候,這兩種方法都將引入急劇的延遲。

3.2 DMA 直接內存訪問

Many drivers use hardware dedicated to copying memory
areas managed by the CPU to or from memory areas managed
by hardware devices. Such dedicated hardware is called direct
memory access, or DMA. DMA relieves the CPU of such
memory copying.

許多驅動程序使用專門的硬件來做內存拷貝,從CPU管理的內存區域到硬件管理的內存區域,或相反。這種專門的硬件叫做DMA(直接內存訪問)。有了DMA,CPU得以從繁重的內存拷貝工作中解放出來。

There are some restrictions on the memory area used for
DMA. These restrictions are unique for each DMA device.
Common restrictions are that only a certain physical memory
range can be used, and that the physical memory range must be
consecutive.

給DMA使用的內存區域存在著一些限制。這些限制對於每一個DMA設備來說都是獨一無二的。通常的限制是只能使用一定的物理內存範圍,而且物理內存範圍必須是連續的。

Allocating memory that can be used for DMA transfers is
non-trivial for user-space drivers. However, since DMA
memory can be reused, you only need to allocate a pool of
memory to be used for DMA transfers at start-up. This means
that the kernel could help with providing such memory when
the user-space driver starts, but after that no further kernel
interactions would be needed.

分配可用於DMA傳輸的內存,對於用戶空間驅動程序來說是十分重要的。然而,由於用於DMA傳輸的內存是可以重用的,所以只需要分配一個內存池,以便在DMA傳輸啟動時被使用。這就意味著,當用戶空間驅動程序啟動時,內核空間可以提供這樣一段內存,但是在那之後,不再需要進一步的內核交互。

3.3 Device interdependencies

Devices are often structured in a hierarchy. For example the
clock might be propagated in a tree-like fashion using different
dividers for different devices and offer the possibility to power
off the clock signal to save power.

XXX

There can be devices acting as a bridge, for example a PCI host
bridge. In this case you need to setup the bridge in order to
have access to any device connected on the other side of
the bridge.

XXX

In kernel space there are frameworks helping a device driver
programmer to solve these problems, but those frameworks
are not available in user space.

XXX

Since it is usually only the startup and shutdown phases that
affect other devices, the device interdependencies can be
solved by a kernel-space driver, while the user-space driver
can handle the actual operation of the device.

XXX

3.4 Kernel services

Network device drivers normally interfaces the kernel network
stack, just like block device drivers normally interfaces the kernel
file system framework.

XXX

User-space drivers have no direct access to such kernel services,
and must re-implement them.

XXX

3.5 Client interface

The kernel has mechanisms for handling multiple clients
accessing the same resource, and for blocking threads waiting
for events or data from the device. These mechanisms are
available using standard interfaces like file descriptors, sockets,
or pipes.

XXX

To avoid using the kernel, the user-space driver needs to invent
its own interface.

XXX

4. Implementing user-space drivers | 用戶態設備驅動實現

技術分享

The picture above shows how a user-space driver might be 
designed. The application interfaces the user-space part of the 
driver. The user-space part handles the hardware, but uses its 
kernel-space part for startup, shutdown, and receiving interrupts.

XXX

There are several frameworks and software solutions available 
to help designing a user-space driver.

XXX

4.1 UIO

There is a framework in the kernel called UIO [5][4] which facilitate 
writing a kernel-space part of the user-space driver. UIO has 
mechanisms for providing memory mapped I/O accessible for 
the user-space part of the driver.

XXX

The allocated memory regions are presented using a device 
file, typically called /dev/uioX, where X is a sequence number 
for the device. The user-space part will then open the file and 
perform mmap() on it. After that, the user-space part has direct 
access to its device.

XXX

By reading from the same file being opened for mmap(), the 
user-space part will block until an interrupt occurs. The content 
read will be the number of interrupts that has occurred. You can 
use select() on the opened file to wait for other events as well.

XXX

For user-space network drivers there are specialized solutions 
specific for certain hardware.

4.2 DPDK

Data Plane Development Kit, DPDK[6], is a solution from Intel 
for user-space network drivers using Intel (x86) hardware. DPDK 
defines an execution environment which contains user-space 
network drivers. This execution environment defines a thread 
for each CPU, called lcore in DPDK. For maximum throughput 
you should not have any other thread running on that CPU.

XXX

While this package of libraries focuses on forwarding applications, 
you can implement server applications as well. For server DPDK 
applications you need to implement your own network stack 
and accept a DPDK specific interface for accessing the network.

XXX

Much effort has been put in memory handling, since this is often 
critical for reaching the best possible performance. There are 
special allocation and deallocation functions that try to minimize 
TLB[10] misses, use the most local memory for NUMA[11] 
systems and ensure even spread on multi-channel memory 
architectures [12]. 

XXX

4.3 USDPAA

User-space Data Plane Acceleration Architecture, USDPAA[7] , is 
a solution from Freescale for the same use case as DPDK but 
designed for their QorIQ architecture (PowerPC and ARM. 
The big difference is that QorIQ uses hardware for allocating, 
de-allocating and queuing network packet buffers. This makes 
memory management easier for the application.

XXX

4.4 TransportNetLib

TransportNetLib[8] is a solution from Texas Instruments. It 
is similar to USDPAA but for the Keystone architecture (ARM).

XXX

4.5 Open DataPlane

Open DataPlane, ODP[9], is a solution initiated by Linaro to do 
the same as DPDK, USDPAA and TransportNetLib, but with 
vendor generic interfaces.

4.6 Trying out DPDK

To get the feeling for the potential performance gain from having 
a user mode network device driver, a DPDK benchmark application 
was designed and executed.

技術分享

XXX

The design of the application can be seen in the picture above. 
It executes as four instances each running on its own CPU, or 
lcore, as DPDK calls them.

XXX

Each instance is dedicated to its own Ethernet device sending 
and receiving network packets. The packets sent has a magic 
word used for validating the packets and a timestamp used for 
measuring transport latency.

XXX

The instances are then paired using loopback cables. To be able 
to compare user-space driver with kernel-space driver, one 
pair accesses the hardware directly using the driver available in 
DPDK, and the other pair uses the pcap[13] interface. All four 
Ethernet devices are on the same PCI network card.

XXXX

There is a fifth lcore (not shown in the picture above) which 
periodically collects statistics and displays it to the screen.

XXX

The hardware used was as follows: 
  o Supermicro A1SAi-2750F mother board using Intel Atom 
    C2750 CPU.  This CPU has 8 cores with no hyperthreading. 
  o 16GB of memory. 
  o Intel Ethernet server adapter i350-T4, 1000 Mbps.

XXX

The table below shows the throughput and latency for user-space 
driver compared to kernel-space driver.

技術分享

XXX

A graph showing the throughput:

技術分享

XXX

A graph showing the latency:

技術分享

XXX

The theoretical throughput maximum is the sum of the send 
and receives speed for the network interface. In this case this 
is 1000 Mbps in each direction, giving a theoretical maximum 
of 2000 Mbps. The throughput includes packet headers and 
padding.

XXX

User-space driver achieved a throughput boost of about four 
times over kernel-space driver.

XXX

Latency was calculated by comparing the timestamp value found 
in the network packet with the current clock when packet was 
received. The latency for user-space driver was slightly less than 
for kernel-space driver.

XXX

Four threads, each continuously running netperf TCP streaming 
test against loop-back interface, were used as a stress while 
running the DPDK benchmark application. This had no noticeable 
impact on the measurements.

XXX

5. Conclusion | 總結陳詞

Implementing a user-space driver requires some work 
and knowledge. The major challenges are interrupts versus 
polling, power management and designing interface towards 
driver clients.

XXX

Support for user-space network drivers is a lot more developed 
than for other kinds of user-space drivers, especially for doing 
data plane forwarding type of applications.

XXXX

A user-space driver can do everything a kernel-space driver 
can, except for implementing an interrupt handler.

XXX

Comparing a user-space network driver with a kernel-space 
network driver showed about four times better throughput 
for the user space driver. Latency did not show a significant 
difference.

XXX

The real-time characteristics should be good for user-space 
drivers since they do not invoke the kernel. This was not verified 
in this paper, though.

XXX

[中英對照]User-Space Device Drivers in Linux: A First Look