inet_init()初始化分析
阿新 • • 發佈:2018-12-31
本文參看程式碼版本為2.6.35.8
inet_init 分析
1,proto_register():分別註冊變數tcp_prot,udp_prot,raw_prot。不負責的聽說這裡是註冊到了私有連結串列,僅用於/proc。2,sock_register(inet_family_ops),在全域性陣列net_family裡面註冊inet_family_ops。
static const struct net_proto_family inet_family_ops = {
.family = PF_INET,
.create = inet_create, //建立socket時需要呼叫的函式
.owner = THIS_MODULE,
};
3,inet_add_protocol():分別註冊變數icmp_protocol udp_protocol tcp_protocol,在全域性陣列inet_protos裡面,填充IPPROTO_ICMP IPPROTO_TCP IPPROTO_UDP對應區域。猜測該陣列用於ip層向上送資料時,被查詢以確定正確的四層接收函式。tcp_protocol定義如下:
static const struct net_protocol tcp_protocol = {
.handler = tcp_v4_rcv, //最為重要的傳輸層接收函式
.err_handler = tcp_v4_err,
.gso_send_check = tcp_v4_gso_send_check,
.gso_segment = tcp_tso_segment,
.gro_receive = tcp4_gro_receive,
.gro_complete = tcp4_gro_complete,
.no_policy = 1,
.netns_ok = 1,
};
4,全域性陣列inetsw表示支援socket的四層處理協議表(The inetsw table contains everything that inet_create needs to build a new socket.)呼叫inet_register_protosw()分別處理陣列inetsw_array的資料項。將proto和proto_ops聯絡起來了。猜測proto_ops提供了socket的統一處理介面(也就是就是可以採用socket->ops->sendmsg()),proto結構體則提供了在四層統一的介面(sock->sk_prot->accept),對於不同的協議初始化為不用的處理函式,相見inet_create。
static struct inet_protosw inetsw_array[] =
{
{
.type = SOCK_STREAM,
.protocol = IPPROTO_TCP,
.prot = &tcp_prot,
.ops = &inet_stream_ops,
.no_check = 0,
.flags = INET_PROTOSW_PERMANENT |
INET_PROTOSW_ICSK,
},
{
.type = SOCK_DGRAM,
.protocol = IPPROTO_UDP,
.prot = &udp_prot,.ops = &inet_dgram_ops,
.no_check = UDP_CSUM_DEFAULT,
.flags = INET_PROTOSW_PERMANENT,
},{
.type = SOCK_RAW,
.protocol = IPPROTO_IP, /* wild card */
.prot = &raw_prot,
.ops = &inet_sockraw_ops,
.no_check = UDP_CSUM_DEFAULT,
.flags = INET_PROTOSW_REUSE,
}
};
6,dev_add_pack(&ip_packet_type),註冊三層資料包處理函式,ip_rcv,對於包頭為ETH_P_IP的二層資料,傳遞給ip_rcv。
static struct packet_type ip_packet_type __read_mostly = {
.type = cpu_to_be16(ETH_P_IP),
.func = ip_rcv,
.gso_send_check = inet_gso_send_check,
.gso_segment = inet_gso_segment,
.gro_receive = inet_gro_receive,
.gro_complete = inet_gro_complete,
};
fs_initcall(inet_init); //在系統初始化時被呼叫