O'Reilly logo

Linux Device Drivers, Second Edition by Alessandro Rubini, Jonathan Corbet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Quick Reference

This section provides a reference for the concepts introduced in this chapter. It also explains the role of each header file that a driver needs to include. The lists of fields in the net_device and sk_buff structures, however, are not repeated here.

#include <linux/netdevice.h>

This header hosts the definitions of struct net_device and struct net_device_stats, and includes a few other headers that are needed by network drivers.

int register_netdev(struct net_device *dev); , void unregister_netdev(struct net_device *dev);

Register and unregister a network device.

SET_MODULE_OWNER(struct net_device *dev);

This macro will store a pointer to the current module in the device structure (or in any structure with an owner field, actually); it is used to enable the networking subsystem to manage the module’s use count.

netif_start_queue(struct net_device *dev); , netif_stop_queue(struct net_device *dev); , netif_wake_queue(struct net_device *dev);

These functions control the passing of packets to the driver for transmission. No packets will be transmitted until netif_start_queue has been called. netif_stop_queue suspends transmission, and netif_wake_queue restarts the queue and pokes the network layer to restart transmitting packets.

void netif_rx(struct sk_buff *skb);

This function can be called (including at interrupt time) to notify the kernel that a packet has been received and encapsulated into a socket buffer.

#include <linux/if.h>

Included by netdevice.h, this file declares the interface flags (IFF_ macros) and struct ifmap, which has a major role in the ioctl implementation for network drivers.

void netif_carrier_off(struct net_device *dev); , void netif_carrier_on(struct net_device *dev); , int netif_carrier_ok(struct net_device *dev);

The first two functions may be used to tell the kernel whether a carrier signal is currently present on the given interface. netif_carrier_ok will test the carrier state as reflected in the device structure.

#include <linux/if_ether.h> , ETH_ALEN , ETH_P_IP , struct ethhdr;

Included by netdevice.h, if_ether.h defines all the ETH_ macros used to represent octet lengths (such as the address length) and network protocols (such as IP). It also defines the ethhdr structure.

#include <linux/skbuff.h>

The definition of struct sk_buff and related structures, as well as several inline functions to act on the buffers. This header is included by netdevice.h.

struct sk_buff *alloc_skb(unsigned int len, int priority); , struct sk_buff *dev_alloc_skb(unsigned int len); , void kfree_skb(struct sk_buff *skb); , void dev_kfree_skb(struct sk_buff *skb);

These functions handle the allocation and freeing of socket buffers. Drivers should normally use the dev_ variants, which are intended for that purpose.

unsigned char *skb_put(struct sk_buff *skb, int len); , unsigned char *__skb_put(struct sk_buff *skb, int len); , unsigned char *skb_push(struct sk_buff *skb, int len); , unsigned char *__skb_push(struct sk_buff *skb, int len);

These functions add data to an skb; skb_put puts the data at the end of the skb, while skb_push puts it at the beginning. The regular versions perform checking to ensure that adequate space is available; double-underscore versions leave those tests out.

int skb_headroom(struct sk_buff *skb); , int skb_tailroom(struct sk_buff *skb); , void skb_reserve(struct sk_buff *skb, int len);

These functions perform management of space within an skb. skb_headroom and skb_tailroom tell how much space is available at the beginning and end, respectively, of an skb. skb_reserve may be used to reserve space at the beginning of an skb, which must be empty.

unsigned char *skb_pull(struct sk_buff *skb, int len);

skb_pull will “remove” data from an skb by adjusting the internal pointers.

#include <linux/etherdevice.h> , void ether_setup(struct net_device *dev);

This function sets most device methods to the general-purpose implementation for Ethernet drivers. It also sets dev->flags and assigns the next available ethx name to dev->name if the first character in the name is a blank space or the null character.

unsigned short eth_type_trans(struct sk_buff *skb, struct net_device *dev);

When an Ethernet interface receives a packet, this function can be called to set skb->pkt_type. The return value is a protocol number that is usually stored in skb->protocol.

#include <linux/sockios.h> , SIOCDEVPRIVATE

This is the first of 16 ioctl commands that can be implemented by each driver for its own private use. All the network ioctl commands are defined in sockios.h.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required