Data Structure

Before discussing the contents of struct net_device in detail, let us address the question of how the kernel 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 namespace net:

□ All network devices are stored in a singly linked list with the list head dev_base.

□ Hashing by device name. The auxiliary function dev_get_by_name(struct net *net, const char *name) finds a network device on this hash.

□ Hashing by interface index. The auxiliary function dev_get_by_index(struct net *net, int ifindex) finds the net_device instance given the interface index.

The net_device structure 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;

unsigned long mem_end;

unsigned long mem_start;

unsigned long base_addr;

unsigned int irq;

unsigned long state;

struct list_head dev_list;

int (*init)(struct net_device *dev);

/* Interface index. Unique device identifier */

10The 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''.

int ifindex;

struct net_device_stats* (*get_stats)(struct net_device *dev);

/* Hardware header description */ const struct header_ops *header_ops;

unsigned short unsigned unsigned short unsigned short flags; /* interface flags (a la BSD) */ mtu; /* interface MTU value */

type; /* interface hardware type */

hard_header_len; /* hardware hdr length

unsigned char perm_addr[MAX_ADDR_LEN]; /* permanent hw address */

unsigned char int addr_len; promiscuity;

/* hardware address length

/* Protocol specific pointers */ void *atalk_ptr;

void *ip_ptr;

void *dn_ptr;

void *ip6_ptr;

void *ec_ptr;

/* IPv4 specific data */ /* DECnet specific data */ /* IPv6 specific data */ /* Econet specific data */

unsigned long unsigned long last_rx; trans_start;

/* Interface address info used in eth_type_trans() */

unsigned char dev_addr[MAX_ADDR_LEN]; /* hw address, (before bcast because most packets are unicast) */

unsigned char int (*hard_start_xmit)


(struct sk_buff *skb, struct net_device *dev);

/* Called after device is detached from network. */

void (*uninit)(struct net_device *dev);

/* Called after last user reference disappears. */

void (*destructor)(struct net_device *dev);

/* Pointers to interface service routines.

int int

(*open)(struct net_device *dev); (*stop)(struct net_device *dev);

void int int int

(*set_multicast_list)(struct net_device *dev); (*set_mac_address)(struct net_device *dev, void *addr); (*do_ioctl)(struct net_device *dev, struct ifreq *ifr, int cmd); (*set_config)(struct net_device *dev, struct ifmap *map); (*change_mtu)(struct net_device *dev, int new_mtu)

void (*tx_timeout) (struct net_device *dev);

int (*neigh_setup)(struct net_device *dev, struct neigh_parms *);

/* Network namespace this network device is inside */ struct net *nd_net;

/* class/net/name entry */ struct device dev;

The abbreviations Rx and Tx that appear in the structure are often also used in function names, variable names, and comments. They stand for Receive and Transmit, respectively, and crop up a few times in the following sections.

The name of the network device is stored in name. It consists of a string followed by a number to differentiate between multiple adapters of the same type (if, e.g., the system has two Ethernet cards). Table 12-2 lists the most common device classes.

Table 12-2: Designations for Network Devices


Device class


Ethernet adapter, regardless of cable type and transmission speed


PPP connection via modem


ISDN cards


Asynchronous transfer mode, interface to high-speed network cards


Loopback device for communication with the local computer

Symbolic names for network cards are used, for example, when parameters are set using the ifconfig tool.

In the kernel, network cards have a unique index number that is assigned dynamically when they are registered and is held in the ifindex element. Recall that the kernel provides the dev_get_by_name and dev_get_by_index functions to find the net_device instance of a network card by reference to its name or index number.

Some structure elements define device properties that are relevant for the network layer and the network access layer:

□ mtu (maximum transfer unit) specifies the maximum length of a transfer frame. Protocols of the network layer must observe this value and may need to split packets into smaller units.

□ type holds the hardware type of the device and uses constants from <if_arp.h>. For example, arphrd_ether and arphdr_ieee802 stand for 10 Mbit and 802.2 Ethernet, arphrd_appletlk for AppleTalk, and arphrd_loopback for the loopback device.

□ dev_addr stores the hardware address of the device (e.g., the MAC address for Ethernet cards), and addr_len specifies the address length. broadcast is the broadcast address used to send messages to attached stations.

□ ip_ptr, ip6_ptr, atalk_ptr, and so on are pointers to protocol-specific data not manipulated by the generic code.

Continue reading here: Several of these pointers may have a nonnull value because a network device can be used with several network protocols at the same time

Was this article helpful?

0 -1