Process Analysis of transmitting Ethernet frames received at the Linux data link layer to the network layer

Source: Internet
Author: User

This article is a stage record. If there is an error, please point it out to Tom...

 

(14 bytes) Ethernet header = (6 bytes) Destination MAC address + (6 bytes) source MAC address + (two bytes) Frame Type value,

Frame Type value: for example, IPv4 is 0x0800 and ARP is 0x0806.

 

After the link layer (NIC-driven macb_rx_frame () receives an Ethernet frame, the interface function netif_receive_skb () (netif_rx actually calls netif_receive_skb) to the network layer, this interface function differentiates the frame types and submits them to different protocol handlers.

 

Data structure:

 

Each protocol must define a packet_type structure, which leads to relevant protocol data processing functions. All nodes form a linked list (hash linked list ).

/* Include/Linux/netdevice. H */

Struct packet_type {

_ Be16 type;/* this is really htons (ether_type ).*/

Struct net_device * dev;/* null is wildcarded here */

INT (* func) (struct sk_buff *,

Struct net_device *,

Struct packet_type *,

Struct net_device *);

Void * af_packet_priv;

Struct list_head list;

};

 

Parameter description:
Type: Ethernet frame type, 16 bits.
Dev: the device of the attached Nic. If it is null, all NICs are matched.
FUNC: The Protocol entry receives the processing function.
Af_packet_priv: Protocol private data.
List: chain table buckle.

 

Generally, the packet_type structure of each protocol is static. Only the type and func parameters are provided during initialization. Each protocol must add this structure to the system type linked list during initialization.

/* Net/CORE/dev. C */

1. Add a node: void dev_add_pack (struct packet_type * PT );

2. delete a node: void _ dev_remove_pack (struct packet_type * PT );

For example:/* Net/IPv4/af_inet.c */

/*
* IP protocol layer initialiser
*/

Static struct packet_type ip_packet_type = {
. Type = _ constant_htons (eth_p_ip ),
. Func = ip_rcv,
. Gso_send_check = inet_gso_send_check,
. Gso_segment = inet_gso_segment,
};

 

Static int _ init inet_init (void)
{

Struct sk_buff * dummy_skb;
Struct inet_protosw * q;
Struct list_head * R;
Int rc =-einval;

...

Dev_add_pack (& ip_packet_type );

...

}

Because the IP Protocol cannot be used as the kernel module, the function is not uninstalled and it is not necessary to call the dev_remove_pack () function.

 

 

Receiving process:

Through linked list mounting, the Linux kernel can easily add receiving and processing functions of various protocols.

 

 

/* Net/CORE/dev. C */

Int netif_receive_skb (struct sk_buff * SKB)
{

Struct packet_type * ptype, * pt_prev;

......

 

// Query and process all nodes in the ethereum linked list first

List_for_each_entry_rcu (ptype, & ptype_all, list)

{

If (! Ptype-> Dev | ptype-> Dev = SKB-> Dev)

{

If (pt_prev)

Ret = deliver_skb (SKB, pt_prev, orig_dev );

Pt_prev = ptype;

}

}

......

 

Type = SKB-> protocol;

// Query the hash linked list of the specified protocol.

List_for_each_entry_rcu (ptype, & ptype_base [ntohs (type) & 15], list)

{

If (ptype-> type = type &&(! Ptype-> Dev | ptype-> Dev = SKB-> Dev ))

{

If (pt_prev)

Ret = deliver_skb (SKB, pt_prev, orig_dev );

Pt_prev = ptype;

}

}

......

}

NIC Driver ---> netif_rx () ---> netif_receive_skb ()-> deliver_skb ()-> packet_type.func ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.