Processing Program for receiving Ethernet frames in Linux Kernel

Source: Internet
Author: User
Article Title: The processing program for receiving Ethernet frames in the Linux kernel. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

1. Preface

In addition to the 6-byte destination MAC address and 6-byte source MAC address, the Ethernet frame type value of the two bytes also exists in the Ethernet header, for example, IPv4 is 0x0800, ARP is 0x0806, etc, after the NIC driver receives the Ethernet frame, it uses the interface function netif_receive_skb () (netif_rx actually calls netif_receive_skb) to deliver it to the upper layer, and this interface function completes the distinction between the Ethernet frame types, deliver to different protocol handlers. If you want to write a processing program for an Ethernet frame, you need to add the corresponding code. The following is the Linux kernel 2.6 code.

2. 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.

3. processing functions

3.1 Add a node

       
        
/* Net/core/dev. c * // *** dev_add_pack-add packet handler * @ pt: packet type declaration ** Add a protocol handler to the networking stack. the passed & packet_type * is linked into kernel lists and may not be freed until it has been * removed from the kernel lists. ** This call does not sleep therefore it can not * guarantee all CPU's that are in middle of processing ing packets * will see the new packet type (until the next received packet ). */void dev_add_pack (struct packet_type * pt) {int hash; spin_lock_bh (& ptype_lock); // if the type is all Ethernet, the node is linked to the ptype_all chain if (pt-> type = htons (ETH_P_ALL) {netdev_nit ++; list_add_rcu (& pt-> list, & ptype_all );} else {// obtain a HASH based on the protocol type. A total of 15 HASH linked list hash = ntohs (pt-> type) & 15; // link the node to the HASH linked list, list_add_rcu is a list_add Linked list Operation protected by smp_wmb (). list_add_rcu (& pt-> list, & ptype_base [hash]);} spin_unlock_bh (& ptype_lock );}
       

 

[1] [2] [3] Next page

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.