4.linux Network device driver architecture
-------------------------------------- | Packet Sending | Packet Reception | -----> Network Protocol interface Layer | Dev_queue_xmit () | Netif_rx () | | -------------------------------------- | Structural Body Net_device | -----> Network Device interface layer -------------------------------------- | Packet Sending | Interrupt Handling | -----> Network-driven functional Layers | Hard_start_xmit () | Packet Reception | | -------------------------------------- | Network Device Media (physical layer) | -----> Network Equipment and media layer --------------------------------------
Hardware-related drivers (to provide hard_start_xmit, with data to be escalated with NETIF_RX)
5.sk_buff socket buffer for transferring data between layers in Linux. When sending a packet, the kernel must create a sk_buff that contains the transmitted data
Then the sk_buff to the lower level, each layer in the Sk_buff submitted to the next layer, each layer in the Sk_buff to add a different protocol Zhen head, until the network equipment sent. Receive the same principle.
struct Sk_buff {
/* These must be first. */
struct Sk_buff*next;
struct Sk_buff*prev;
/* net_devive structure in the network Device interface layer */
struct Net_device*dev;
....
/* control buffers, which can be used by each layer to hold private data */
CHARCB[48];
unsigned intlen,//data True length
data_len,//Data length
Mac_len; Length of the link layer frame header
/* Hook function garbage Collection */
void (*destructor) (struct sk_buff *skb);
Sk_buff_data_ttransport_header;
Sk_buff_data_tnetwork_header;
Sk_buff_data_tmac_header;
/* These elements must is at the end, see ALLOC_SKB () for details. */
Sk_buff_data_ttail;
Sk_buff_data_tend;
Unsigned char*head,
*data;
unsigned inttruesize;
Atomic_tusers;
};
5.1, Sk_buff structure:
--------------->*head
| head |
|---------------->*data
| data |
| cache |
|--------------->*tail
| tail |
--------------->*end
5.2. Distribution:
Sub-mating socket buffers: SKB allocations with gfp_atomic precedence because the function is often called in device drivers
Static inline struct Sk_buff *dev_alloc_skb (unsigned int length)
Allocates a socket buffer and a data buffer, the parameter len is the size of the data buffer, ARM is usually 32-bit aligned, and the parameter priority is the memory-allocation precedence.
Static inline struct Sk_buff *alloc_skb (unsigned int size,gfp_t priority)
5.3. Release:
Memory for freeing DEV_ALLOC_SKB for use in non-disruptive contexts
void Dev_kfree_skb (struct sk_buff *skb)
Used in the context of interrupts
static inline void Dev_kfree_skb_irq (struct sk_buff *skb)
Interrupts and non-interrupts can be used any, in fact, is to make a judgment
void Dev_kfree_skb_any (struct sk_buff *skb)
{
if (IN_IRQ () | | irqs_disabled ())
DEV_KFREE_SKB_IRQ (SKB);
Else
DEV_KFREE_SKB (SKB);
}
5.4. Change:
Buffer tail Add Data SKB
Static inline unsigned char *skb_put (struct sk_buff *skb, unsigned int len)
Skb->tail + = Len;
Skb->len + = Len;
Add data to the beginning of the buffer
Static inline unsigned char *skb_push (struct sk_buff *skb, unsigned int len)
Skb->data-= Len;
Skb->len + = Len;
Remove data from the beginning of the buffer
Static inline unsigned char *skb_pull (struct sk_buff *skb, unsigned int len)
Skb->len-= Len;
return skb->data + = Len;
Adjusting the buffer head
static inline void Skb_reserve (struct sk_buff *skb, int len)
Skb->data + = Len;
Skb->tail + = Len;
6.net_device Structural Body
struct Net_device
{
/* Network device Name */
Charname[ifnamsiz];
Unsigned longmem_end;/* shared memory End address */
Unsigned longmem_start;/* shared memory start address */
unsigned longbase_addr;/* I/O device base Address */
Unsigned intirq;/* device Interrupt number */
Unsigned charif_port;/* multi-port device which port to use */
Unsigned chardma;/* DMA Channel
/* Device initialization function, only called once */
Int (*init) (struct net_device *dev);
/* For obtaining status information for network devices */
struct net_device_stats* (*get_stats) (struct net_device *dev);
/* Store Detailed network device traffic statistics */
struct net_device_statsstats;
.......................
unsignedmtu;/* interface MTU (maximum transmission unit) value*/
Unsigned shorttype;/* hardware interface type */
Unsigned shorthard_header_len;/* length of hardware head */
/*mac Address */
unsigned chardev_addr[max_addr_len];
/* Private data for storing private data, netdev_priv () */
void *priv;
/* Start sending the packet */
Int (*hard_start_xmit) (struct sk_buff *skb,struct net_device *dev);
/* Timestamp format when starting to send data: jiffies */
unsigned long trans_start;
/* The length of the last packet received */
unsigned long last_rx;
/* Open for opening a network device, obtaining the desired IO address and interrupt number. Stop () to stop the network device */
Int (*open) (struct net_device *dev);
Int (*stop) (struct net_device *dev);
/* To set the MAC address of the device */
Int (*set_mac_address) (struct net_device *dev,void *addr);
/* For specific IO control */
Int (*do_ioctl) (struct Net_device *dev,
struct ifreq *ifr, int cmd);
/* For configuration interface, can be used to change the device's IO address and interrupt number */
Int (*set_config) (struct net_device *dev,struct ifmap *map);
/* Packet send timeout is called, can be used to restart the network card */
void (*tx_timeout) (struct net_device *dev);
/*LINUX4.0 has a structure, linux2.6 No, the collection of operating hardware */
struct Net_device_ops {
.....
};
};
Linux Network device drivers