深入理解Linux網路技術內幕——協議處理函數

來源:互聯網
上載者:User

標籤:linux   中斷   核心   協議棧   

網路幀在進入網路層時,需要區分不同的網路通訊協定進行處理,這就需要涉及協議處理函數。

首先我們從驅動接收到一個資料幀,分析資料幀在協議棧中自下而上的傳輸串流程。

裝置驅動程式在接收到一個資料幀時,會將其儲存在一個sk_buff緩衝區資料結構,並對其進行初始化。

struct sk_buff {......__be16          protocol:16;  ......}
在這個緩衝區結構體中,有一個protocol欄位,用於標識網路層的協議。
我們知道網路幀在裝置驅動程式中處理後,裝置驅動程式會調用netif_receive_skb用以將資料幀向協議棧上層傳輸(網路層),netif_receive_skb這是就會查詢sk_buff結構體中的protocol,識別網路幀的網路層協議,然後決定網路幀接下來的處理函數。(如protocol為ETH_P_IP,則調用ip_rcv函數。)

protocol的定義在include/linux/if_ether.h中,如下所示:

#define ETH_P_802_3 0x0001      /* Dummy type for 802.3 frames  */#define ETH_P_AX25  0x0002      /* Dummy protocol id for AX.25  */#define ETH_P_ALL   0x0003      /* Every packet (be careful!!!) */                        /*ETH_P_ALL不是真正的協議,而是作為萬用字元,主要用於sniff嗅探器等*/                                      #define ETH_P_802_2 0x0004      /* 802.2 frames         */#define ETH_P_SNAP  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          /* Dummy type for PPP MP frames */......


協議處理函數的組織在核心中,每種協議都有結構體packet_tpye描述:
struct packet_type {    __be16  type;   /* This is really htons(ether_type). */ //二層協議類型,ETH_P_IP、ETH_P_ARP等等    struct net_device   *dev;   /* NULL is wildcarded here       */    //鉤子函數了,如 ip_rcv()、arp_rcv()等等    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;};
其中成員func即為各個協議的鉤子函數(協議處理函數).為了加快方便速度,大部分協議通過雜湊表來組織,十六個列表組織成一個數組ptype_base[16]. 而ETH_P_ALL類的協議則被組織到全域變數ptype_all中.
協議處理函數的註冊各種協議通過dev_add_pack註冊.

//註冊協議:把packet_type結構掛在與type對應的list_head上面                                                                               void dev_add_pack(struct packet_type *pt){    int hash;                     spin_lock_bh(&ptype_lock);    if (pt->type == htons(ETH_P_ALL))  //type為ETH_P_ALL時,掛在ptype_all上面        list_add_rcu(&pt->list, &ptype_all);    else {  //否則,掛在ptype_base[type&15]上面        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);  

接下來我們以ipv4為例,可靠具體的協議棧函數是如何組織的.(我們需要進入ipv4模組的初始化分析)

static struct packet_type ip_packet_type __read_mostly = {    .type = cpu_to_be16(ETH_P_IP),    .func = ip_rcv,   //ipv4 的協議處理函數 ,在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);......                                                                                                                        }

由此,網路幀,經過網路驅動程式處理,通過netif_receive_skb,最終轉入具體的協議處理函數進行處理.







深入理解Linux網路技術內幕——協議處理函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.