一篇帶給你Lwip網(wǎng)口接口netif
01網(wǎng)絡(luò)接口簡介
TCP/IP參考模型中網(wǎng)絡(luò)分為4層:應(yīng)用層、傳輸層、網(wǎng)絡(luò)層和網(wǎng)絡(luò)接口層。網(wǎng)絡(luò)接口層就對應(yīng)著我們實際的網(wǎng)卡,LWIP支持多網(wǎng)口設(shè)計,LWIP中使用netif來描述每種網(wǎng)絡(luò)接口的特性,如接口的IP地址、接口狀態(tài)等等。那么如果有多個網(wǎng)卡的話LWIP是如何來組織這些網(wǎng)卡呢?在LWIP中用鏈表netif_list來管理眾多的網(wǎng)卡,當(dāng)上層有數(shù)據(jù)要發(fā)送的時候LWIP會從netif_list鏈表中選擇一個合適的網(wǎng)卡來將數(shù)據(jù)發(fā)送出去。
02netif結(jié)構(gòu)
netif結(jié)構(gòu)體在文件netif.h中有定義,netif結(jié)構(gòu)體中每個成員變量的含義如下:
next:該字段指向下一個neitif類型的結(jié)構(gòu)體,因為LWIP可以支持多個網(wǎng)絡(luò)接口,當(dāng)設(shè)備有多個網(wǎng)絡(luò)接口的話LWIP就會把所有的netif結(jié)構(gòu)體組成鏈表來管理這些網(wǎng)絡(luò)接口。
ipaddr,netmask和gw:分別為網(wǎng)絡(luò)接口的IP地址、子網(wǎng)掩碼和默認(rèn)網(wǎng)關(guān)。
input:此字段為一個函數(shù),這個函數(shù)將網(wǎng)卡接收到的數(shù)據(jù)交給IP層。
output:此字段為一個函數(shù),當(dāng)IP層向接口發(fā)送一個數(shù)據(jù)包時調(diào)用此函數(shù)。這個函數(shù)通常首先解析硬件地址,然后發(fā)送數(shù)據(jù)包。此字段我們一般使用etharp.c中的etharp_output()函數(shù)。
linkoutput:此字段為一個函數(shù),該函數(shù)被ARP模塊調(diào)用,完成網(wǎng)絡(luò)數(shù)據(jù)的發(fā)送。上面說的etharp_output函數(shù)將IP數(shù)據(jù)包封裝成以太網(wǎng)數(shù)據(jù)幀以后就會調(diào)用linkoutput函數(shù)將數(shù)據(jù)發(fā)送出去。
state:用來定義一些關(guān)于接口的信息,用戶可以自行設(shè)置。
mtu:網(wǎng)絡(luò)接口所能傳輸?shù)淖畲髷?shù)據(jù)長度,一般設(shè)置為1500。
hwaddr_len:網(wǎng)卡MAC地址長度,6個字節(jié)。
hwaddr:MAC地址。
flags:網(wǎng)絡(luò)的接口狀態(tài),屬性信息字段。
name:網(wǎng)卡的名字。
num:此字段為協(xié)議棧為每個網(wǎng)絡(luò)接口設(shè)置的一個編號,編號從0開始。
flags的取值:
其中
在網(wǎng)絡(luò)初始化中,在ethernetif.c中
他們對應(yīng)的都是一個函數(shù)
etharp_output:ip層發(fā)送數(shù)據(jù)包函數(shù)
low_level_output :ARP模塊發(fā)送函數(shù),更底層的函數(shù),其實etharp_output還是調(diào)用此函數(shù)
03相關(guān)函數(shù)
3.1、netif_add
- netif_add(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t
- *gw, void *state, netif_init_fn init, netif_input_fn input)
就是把netif網(wǎng)絡(luò)接口添加到網(wǎng)絡(luò)list隊列中,向網(wǎng)卡列表中添加一個網(wǎng)卡
3.2、netif_set_default
- netif_set_default(struct netif *netif)
將這個網(wǎng)卡設(shè)置為默認(rèn)網(wǎng)卡
3.3、netif_set_up
- netif_set_up(struct netif *netif)
打開網(wǎng)卡
































