Registering Network Devices

Each network device is registered in a two-step process:

1. alloc_netdev allocates a new instance of struct net_device, and a protocol-specific function fills the structure with typical values. For Ethernet devices, this function is ether_setup. Other protocols (not considerd in detail) use XXX_setup, where possible values for XXX include fddi (fiber distributed data), tr (token ring), ltalk (localtalk), hippi (high-performance parallel interface), or fc (fiber channel).

Some in-kernel pseudo-devices implementing specific ''interfaces'' without being bound to particular hardware also use the net_device framework. ppp_setup initializes devices for the PPP protocol, for example. Several more XXX_setup functions can be found across the kernel sources.

2. Once struct net_device is completely filled in, it needs to be registered with register_netdev or register_netdevice. The difference between both functions is that register_netdev allows for working with (limited) format strings for interface names. The name given in net_device->dev can contain the format specifier %d. When the device is registered, the kernel selects a unique number that is substituted for %d. Ethernet devices specify eth%d, for instance, and the kernel subsequently creates the devices eth0, eth1...

The convenience function alloc_etherdev(sizeof_priv) allocates an instance of struct net_device together with sizeof_priv bytes for private use — recall that net_device->priv is a pointer to driver-specific data associated with the device. Additionally, ether_setup mentioned above is called to set Ethernet-specific standard values.

The steps taken by register_netdevice are summarized in the code flow diagram in Figure 12-9.

Figure 12-9: Code flow diagram for register_netdevice.

Should a device-specific initialization function be provided by net_device->init, the kernel calls it before proceeding any further. A unique interface index that identifies the device unambiguously within its namespace is generated by dev_new_index. The index is stored in net_device->ifindex. After ensuring that the chosen name is not already in use and no device features (see netif_f_* in <netdevice.h> for a list of supported features) that would contradict themselves have been specified, the new device is added to the generic kernel object model with netdev_register_kobject. This also creates the sysfs entries mentioned above. Finally, the device is integrated into the namespace-specific list and the device name and interface index hash tables.

Continue reading here: Receiving Packets

Was this article helpful?

+5 -1