Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 12: Networks


12.7.1 Representation of Network Devices


In the kernel, each network device is represented by an instance of thenet_devicestructure.
Once a structure instance has been allocated and filled, it must be registered with the kernel using
register_netdevfromnet/core/dev.c. This function performs some initialization tasks and
registers the device within the generic device mechanism. This creates a sysfs entry (see Chapter 10.3)
/sys/class/net/<device>, which links to the device’s directory. A system with one PCI network card
and the loopback device has two entries in/sys/class/net:

root@meitner #ls -l /sys/class/net
total 0
lrwxrwxrwx 1 root root 0 2008-03-09 09:43 eth0 -> ../../devices/pci0000:00/0000:00:1c.5/
0000:02:00.0/net/eth0
lrwxrwxrwx 1 root root 0 2008-03-09 09:42 lo -> ../../devices/virtual/net/lo


Data Structure


Before discussing the contents ofstruct net_devicein detail, let us address the question of how the ker-
nel keeps track of the available network devices, and how a particular network device can be found. As
usual, the devices are not arranged globally, but on a per-namespace basis. Recall that three mechanisms
are available for each namespacenet:

❑ All network devices are stored in a singly linked list with the list headdev_base.
❑ Hashing by device name. The auxiliary functiondev_get_by_name(struct net *net, const
char *name)finds a network device on this hash.
❑ Hashing by interface index. The auxiliary functiondev_get_by_index(struct net *net, int
ifindex)finds thenet_deviceinstance given the interface index.

Thenet_devicestructure holds all conceivable information on the device. It spans more than 200 lines
and is one of the most voluminous structures in the kernel. As the structure is overburdened with details,
a much simplified — but still quite long — version is reproduced below.^10 Here’s the code:


struct net_device
{
char name[IFNAMSIZ];
/* device name hash chain */
struct hlist_node name_hlist;
/* I/O specific fields */
unsigned long mem_end; /* shared mem end */
unsigned long mem_start; /* shared mem start */
unsigned long base_addr; /* device I/O address */
unsigned int irq; /* device IRQ number */

unsigned long state;
struct list_head dev_list;
int (*init)(struct net_device *dev);

/* Interface index. Unique device identifier */

(^10) The kernel developers are not quite satisfied with the current state of the structure either. The source code states that ‘‘Actually,
this whole structure is a big mistake’’.

Free download pdf