LWIP_NetIf

网络接口管理

概述

网络接口管理属于 链路层

网络接口类型: 以太网接口 串行链路接口 回环接口

结构体: nerif

作用: 对网络硬件做统一封装,为协议栈上层提供统一的接口服务

本质: IP层与 物理媒介的接口,协议栈将所有的接口使用netif_list 链接起来

​ 数据发送: IP层根据IP地址,在netif_list中选择合适的netif 并调用 发送接口,将数据发送

​ 数据接收: netif 的数据接收函数自动被调用,完成数据包递交给IP层的任务

结构体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

struct netif {
/** pointer to next in linked list */
struct netif *next;

/** IP address configuration in network byte order */
struct ip_addr ip_addr;
struct ip_addr netmask;
struct ip_addr gw;

/** This function is called by the network device driver
* to pass a packet up the TCP/IP stack. */
err_t (* input)(struct pbuf *p, struct netif *inp);
/** This function is called by the IP module when it wants
* to send a packet on the interface. This function typically
* first resolves the hardware address, then sends the packet. */
err_t (* output)(struct netif *netif, struct pbuf *p,
struct ip_addr *ipaddr);
/** This function is called by the ARP module when it wants
* to send a packet on the interface. This function outputs
* the pbuf as-is on the link medium. */
err_t (* linkoutput)(struct netif *netif, struct pbuf *p);
/** This field can be set by the device driver and could point
* to state information for the device. */
void *state;
/** maximum transfer unit (in bytes) */
u16_t mtu;
/** number of bytes used in hwaddr */
u8_t hwaddr_len;
/** link level hardware address of this interface */
u8_t hwaddr[NETIF_MAX_HWADDR_LEN];
/** flags (see NETIF_FLAG_ above) */
u8_t flags;
/** descriptive abbreviation */
char name[2];
/** number of this interface */
u8_t num;
};

mtu

网络接口可以传送的最大数据包长度,以太网一般为1500。

IP层 分片的依据,分片必须由IP层完成,因为IP层可以完成差错检验和恢复功能

函数

netif_add

向系统注册一个网络接口设备

关键: 输入参数是上层决定的如下:

1
2
3
4
5
6
7
8

struct netif *netif
struct ip_addr *ipaddr
struct ip_addr *netmask,
struct ip_addr *gw,
void *state,
err_t (* init)(struct netif *netif), :接口的初始化函数
err_t (* input)(struct pbuf *p, struct netif *netif)); : 向IP层提交数据包的函数

回环接口 8

软件对网络硬件的模拟: IP地址被设置为 127.0.0.1

通信过程 : 见图 8-1 ,

​ 发送函数 : 将pbuf 链接到 loop_first 上 netfi_loop_output,并向操作系统注册一个定时事件,自动调用 netif_poll

​ 接收函数: netif_poll 将 loop_first 中的数据传递给IP层;

undefined