Deep understanding of Linux Network Technology Insider--Protocol processing function

Source: Internet
Author: User
Tags htons

When network frames enter the network layer, it is necessary to distinguish between different network protocols for processing, which involves protocol processing functions.

First we receive a data frame from the driver and analyze the data frame's bottom-up transmission flow in the protocol stack.

When a device driver receives a data frame, it saves it in a Sk_buff buffer data structure and initializes it.

struct Sk_buff {... __be16          protocol:16;  ......}
In this buffer structure, there is a protocol field that identifies the protocol for the network layer.
We know that after the network frame is processed in the device driver, the device driver calls NETIF_RECEIVE_SKB to transfer the data frame to the upper layer of the Protocol stack (the network level), NETIF_RECEIVE_SKB this will query the protocol in the Sk_buff structure, The network layer protocol that identifies the network frame and then determines the next processing function for the network frame. (such as protocol is ETH_P_IP, the IP_RCV function is called.) )

The protocol is defined in Include/linux/if_ether.h as follows:

#define Eth_p_802_3 0x0001      /* Dummy type for 802.3 frames  */#define ETH_P_AX25  0x0002 &nbs P    /* Dummy Protocol ID for ax.25  */#define Eth_p_all   0x0003      /* every packet (b E careful!!!) */                       /*eth_p_all not a real deal, but as a wildcard, primarily for sniff Sniffer, etc */                                &NB Sp     #define Eth_p_802_2 0x0004      /* 802.2 frames         */#define ETH_P_SN  AP  0x0005      /* Internal only        */#define ETH_P_DDCMP     0x0006          /* DEC ddcmp:internal only    /#define ETH_P_WAN_PPP   0x0007           /* Dummy type for WAN PPP frames*/#define ETH_P_PPP_MP    0x0008       &nBsp  /* Dummy type for PPP MP frames */...


Organization of protocol Handler functionsIn the kernel, each of these protocols has a struct Packet_tpye description:
struct Packet_type {    __be16  type;   /* This is really htons (Ether_type). *///Two Layer protocol type, ETH_P_IP, eth_p_arp, etc.    struct net_device   *dev;   /* NULL    is the wildcarded here *///hook function, such as IP_RCV (), ARP_RCV (), and so on    int         (*func) (struct Sk_buff *,                     struct Net_device *,                     struct packet_type *,                     struct net_device *);    struct Sk_buff      * (*gso_segment) (struct sk_buff *skb,                        int features);    int         (*gso_send_check) (struct sk_buff *skb);    struct Sk_buff      * * (*gro_receive) (struct sk_buff **head,                           struct sk_buff *skb);    int         (*gro_complete) (struct sk_buff *skb);    void            *af_packet_priv;    struct list_head    list;};
Where member Func is the hook function (protocol handler) for each protocol.To speed up convenience, most protocols are organized by a hash table, and 16 lists are organized into an array of ptype_base[16].The Eth_p_all class protocol is organized into global variable Ptype_all.
Registration of protocol processing functionsVarious protocols are registered through Dev_add_pack.

Registration agreement: The PACKET_TYPE structure is linked to the type corresponding to the list_head above                                                                               void Dev_add_pack (struct packet_type *pt) {    int hash;                     SPIN_LOCK_BH (&ptype_lock);    if (Pt->type = = htons (eth_p_all))  //type for Eth_p_all, hang on Ptype_all above List_add_rcu        (&pt->list, & Ptype_all);    else {  //Otherwise, hang on ptype_base[type&15] above        hash = Ntohs (pt->type) & Ptype_hash_mask;        List_add_rcu (&pt->list, &ptype_base[hash]);    }      SPIN_UNLOCK_BH (&ptype_lock);   } Export_symbol (Dev_add_pack);  

Next we take IPv4 as an example of how reliable and specific protocol stack functions are organized. (We need to enter the initialization analysis of the IPv4 module)

static struct Packet_type Ip_packet_type __read_mostly = {    . Type = Cpu_to_be16 (eth_p_ip),    . Func = IP_RCV,   // The IPv4 protocol handler function, which is used in NETIF_RECEIVE_SKB      . Gso_send_check = Inet_gso_send_check,    . gso_segment = Inet_gso_segment,    . gro_receive = inet_gro_receive,    . Gro_complete = Inet_gro_complete,};     


static int __init inet_init (void) {...    Dev_add_pack (&ip_packet_type);                                                                                                                        

Thus, the network frame, after the network driver processing, through the NETIF_RECEIVE_SKB, finally transferred to the specific protocol processing function to handle.







Deep understanding of Linux Network Technology Insider--Protocol processing function

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.