Driver Architecture in A Linux World
Driver Architecture in A Linux World
Abstract
Vendors of modern networking hardware realize that the devices they provide require a great deal of
software in order to function effectively. As a result, many hardware vendors provide large amounts of
software for use with their devices. However, the prevalent use of the VxWorks 1 operating system has
allowed hardware vendors to become careless with the architecture of the driver software they provide.
Often the driver software provided by hardware vendors is difficult to use in a protected environment
like Linux .2 This paper begins by discussing strategies for making use of such drivers in a Linux
environment, and continues by discussing how hardware vendors might architect Linux -friendly driver
software. Finally, the paper discusses how vendors can provide first-class Linux support for their
hardware and why it is in their best interest to do so. This paper is a must read both for system
integrators new to Linux and for driver software architects unfamiliar with the Linux environment.
complicated hardware, networking hardware vendors VxWorks . More recently, Linux has become a
provide driver software for use with their products. strong player in the world of embedded systems and
This software is generally not a formal device driver particularly in the embedded networking arena. At
as defined by an operating system, but rather an first glance, VxWorks and Linux may seem to
informal collection of software designed to ease the have much in common with one another. For ex-
use of the driven hardware. Nevertheless, this soft- ample, both support one or more of the industry
ware is often incorporated directly into commercial standard Posix APIs. However, these two operat-
end products. This situation has provided hardware ing systems are distinctly different in many ways.
vendors with the opportunity to differentiate their Supporting one rarely implies support of the other.
1 VxWorks is a registered trademark of Wind River Systems, Inc.
2 Linux is a registered trademark of Linus Torvalds.
This situation is aggravated by the fact that many erally insufficient for control of these devices. Other
hardware vendors take advantage of the less restric-
forms of hardware access generally require execution
tive “features” of the VxWorks environment when in kernel context, and vendor-provided conglomerate
implementing their informal driver software. The
drivers tend to be poorly segregated between hard-
Linux kernel enforces a strict driver model for ware access and higher-level functionality. The solu-
hardware access and it segregates different software tion seems obvious: package the conglomerate driver
components into separate address spaces. In con-
in a way that allows it to execute within the context
trast, VxWorks does not enforce any strict driver of the kernel.
model or memory access protection.3 The software A vendor-provided driver tends to have a top-level
provided by many hardware vendors makes no dis- API that is intended for use by developers in cre-
tinctions between hardware access routines, applica- ating a working system. Developers will expect to
tion support libraries, or system service tasks. The use this API when developing their networking ap-
lack of such distinctions is incompatible with the pro-
plications. However, in this model the actual con-
tected environment provided by Linux . As Fig- glomerate driver will reside in a separate address
ure 1 illustrates, the result is a software conglomer- space from the rest of the application. This makes
ate that can be very difficult to integrate effectively
traditional linking impossible. One solution is to
into a Linux system. provide a library for the application to link against
that mimics the published API of the conglomerate
driver. Underneath, this doppleganger 4 library will
communicate to the kernel-resident code by estab-
lished means, such as an ioctl() call to an actual
presumable that every thread will be scheduled even- As discussed earlier, the Linux kernel restricts
tually, this is a potential problem with any thread ex- many kinds of hardware accesses to only code run-
ecuting within the kernel’s context. There is a well- ning in the kernel’s context. Complex networking
known kernel patch maintained by Robert Love that hardware will generally require more than simple
will tend to solve this particular problem.[1] How- memory-mapped register accesses in order to func-
ever, not everyone will be using this patch at the tion properly. If the body of an existing conglomer-
present time or even in the near future.5 ate driver is to reside in a user’s context, some means
A less tangible problem with this approach relates must be devised to account for these non-memory-
to the amount of code running in the kernel’s con- mapped hardware accesses. The general approach
text. Vendor-provided conglomerate drivers tend to to providing this type of access is to implement a
be rather large. These conglomerate drivers tend small formal device driver that executes in the ker-
to provide a lot of functionality and are often tar- nel’s context. This small driver provides the required
geted at multiple devices within a family of hard- functionality (e.g. DMA transfer setup) through a
ware. It is generally accepted that the number of small suite of custom ioctl() calls. As depicted in
bugs in a piece of software is proportional to the size Figure 3, generally only a few small changes to the
of that software. So, the potential for bugs in driver conglomerate driver are required to make this strat-
code is fairly large. By placing code in the kernel’s egy work.
context, one makes it more difficult to debug that
code and makes the consequences of bugs in that
code greater than if the same code were running in
a user’s context. By itself, this should be enough
itself running in the kernel’s context and the con- the Linux kernel restricts most hardware accesses
glomerate driver running in a user’s context; or, b) to code running in the kernel’s context; and, con-
the net driver needs to be able to exchange incoming glomerate drivers do not adequately separate hard-
and outgoing frames with the conglomerate driver ware access code from other conglomerate compo-
running in a user’s context. Either of these possibil- nents. The deceptively simple answer is that one
ities is expensive with regard to context switches. In must remove this conflict between the Linux ar-
particular, option b) requires as many as three sepa- chitecture and the architecture of the conglomerate
rate context switches for any given frame received at driver. This means dismantling the conglomerate
the CPU. Obviously, this is more context switching driver and reconstructing it in a form compatible
to run within the kernel’s context. However, the Linux -specific code to the “other” library or by cre-
other components almost always need to run in a ating a doppleganger library that provides the same
user’s context. The task at hand is to separate symbol definitions as the hardware access library but
the two application-oriented components from the uses Linux -specific code to drive the hardware ac-
system-oriented hardware access component. The cess code running in the kernel’s context. The end
result should look something like the depiction in result is a functional solution in nearly every case.
Figure 4 below. In most cases, however, this solution is not for the
faint hearted. Once a conglomerate driver has ex-
isted for some time, the lack of architectural disci-
pline amongst conglomerate driver developers tends
to lead to a spaghetti-like mess of hardware ac-
cesses within service tasks, application callbacks
from within interrupt handlers, and other question-
able practices that can be difficult to translate to
remove unnecessary hardware accesses or to formal- and the hardware it drives, but also enough Linux
ize those accesses in a way that is supportable in knowledge and experience to accomplish the task.
the Linux environment. The end result should be Covering both of those competencies can be diffi-
two distinct object libraries. The hardware access cult, and the investment of time and resources can
library should have no dependencies on the “other” be expensive. However, the payoff is in better per-
library, while the “other” library should at most re- formance, easier debugging, and greater stability. In
quire access to the functions defined in the hardware the long run this is the cheapest of the alternatives
access library. In particular, there should be abso- discussed so far.
lutely no use of shared global variables between the
two libraries and no calling of functions defined in the
“other” library from functions defined in the hard- 5 Driver Architecture 101
ware access library.
With the former conglomerate driver broken into So far, the discussion has been about how to make
these two distinct libraries, one has the basic ingre- use of an existing conglomerate driver in a Linux
dients necessary for implementing this solution. The environment. This discussion has been pertinent be-
against applications requiring access to the hardware Linux if they were developed properly in the first
controlled by the former conglomerate driver. The place! A set of driver libraries that are architected
interface between the hardware access library and in a Linux -friendly way should work equally well
the “other” library is implemented either by adding with VxWorks or any other operating system.
8 Hopefully this applies to everyone. . .
How does one structure a driver library so that it
vendor may want to make its hardware as attrac-
tecture should provide for a hardware access layer. the vendor’s driver architecture is Linux -friendly.
This layer should encapsulate all of the atomic oper- A vendor might do this either by purifying what ex-
ations one would want to perform on the hardware. ists now or by making sure that the driver is archi-
Once defined, this layer should not be violated for tected correctly in the first place. As well, the vendor
any reason whatsoever. Also, any dependencies on should conduct the exercise of running the driver un-
the hardware access must be “one-way”—calls from der Linux in order to ensure that the driver actu-
ically not allowed. In Linux , the hardware access cises will serve to guarantee that the Linux market
layer will be run within the kernel’s context. In an is available to the vendor’s hardware.
unprotected operating system, this layer will be just Michael Tiemann,9 CTO of Red Hat, Inc., has sug-
another part of the driver library. gested that Linux should be viewed as a platform
A driver architected in this way should work reason- rather than simply as an operating system.[3] What
ably well either in a protected operating system like
does this mean for a hardware vendor? For one thing,
Linux or in an unprotected operating system like
this means that the vendor should implement stan-
VxWorks . A driver library architected in this fash- dard Linux device drivers as appropriate for the
ion from the beginning should introduce neither ex- capabilities of the vendor’s devices. Moreover, a wise
tra runtime overhead nor extra complexity in either vendor will determine how to take advantage of ex-
a protected or unprotected environment. Further- isting Linux functionality10 as well as how to ex-
more, this basic design framework is more in line with tend it in ways that are appropriate for the vendor’s
modern software design practices and should lead to hardware. In this way, the vendor’s hardware be-
fundamentally better software with fewer bugs and comes accessible to any software already available for
better stability. There really is no excuse for not fol- Linux that is related to the functions provided by
lowing this basic design framework when architecting the vendor’s hardware. It should be clear that this
a new hardware driver. would benefit both the hardware vendor and the sys-
Linux users are more likely to use hardware with
driver library architected in a Linux -friendly way
hardware that is well supported by Linux is un-
ment like Linux or an unprotected environment
likely to require much custom software to make use
like VxWorks . And, due to the better design prin-
7 Conclusion
6 Embracing Linux
For some time, VxWorks has dominated the mar-
Not that long ago, Linux was an operating system ket for embedded networking operating systems.
only for desktop and server computers. However,
This market dominance has allowed networking
in the past few years Linux has become quite a hardware vendors to concentrate only on supporting
significant player in the world of embedded systems VxWorks with the software they provide to drive
and particularly in the embedded networking arena.
their hardware. Unfortunately, the unprotected na-
Some have suggested that Linux will become the ture of VxWorks has fostered the development of
dominant operating system in the embedded systems
driver software that is difficult to use with a pro-
market.[2] What is certain is that Linux will con- tected operating system like Linux . There are ways
tinue to be a significant player in the embedded net- to use a more-or-less intact conglomerate driver with