您的位置:首页 > 理论基础 > 计算机网络

LwIP网络接口结构体---netif

2012-02-23 17:06 274 查看
 在lwIP中,是通过结构体netif来描述一个硬件网络接口的,在单网卡中,这个结构体只有一个,多网卡中可有何网卡数目相同的netif结构体,它们构成一个数据链。下面的代码选自netif.h,是netif结构体的代码描述:

[cpp]
view plaincopyprint?

/** Generic data structure used for all lwIP network interfaces. 
 *  The following fields should be filled in by the initialization 
 *  function for the device driver: hwaddr_len, hwaddr[], mtu, flags  
 *  用于所有lwIP网络接口的通用数据结构体 
 *  设备驱动函数在初始化时应该填充以下数据域:hwaddr_len,hwaddr[],mtu,flags.*/  
  
struct netif {  
  /** pointer to next in linked list */  
  struct netif *next;  
  
  /** IP address configuration in network byte order */  
  struct ip_addr ip_addr;       //IP地址
  
  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.  
      这个函数由网络设备驱动所调用,从网卡中接收数据包并传递给TCP/IP协议栈*/  
  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.  
       当IP模块向接口发送一个数据包时调用此函数。这个函数通常首先解析 
       硬件地址,然后发送数据包*/  
  err_t (* output)(struct netif *netif, struct pbuf *p,    //IP层调用此函数向网卡发送一包数据
  
       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.  
      当ARP模块向接口发送一个数据包时调用此函数。这个函数向链路输出一个pbuf。*/  
  err_t (* linkoutput)(struct netif *netif, struct pbuf *p);     //ARP模块调用这个函数向网卡发送一包数据
  
  
#if LWIP_NETIF_STATUS_CALLBACK    //netif状态被改变时该函数被调用,未使用
  
  /** This function is called when the netif state is set to up or down 
   */  
  void (* status_callback)(struct netif *netif);  
#endif /* LWIP_NETIF_STATUS_CALLBACK */
  
  
#if LWIP_NETIF_LINK_CALLBACK        //未使用
  
  /** This function is called when the netif link is set to up or down 
      netif连接改变时调用 
   */  
  void (* link_callback)(struct netif *netif);  
#endif /* LWIP_NETIF_LINK_CALLBACK */
  
  
  /** This field can be set by the device driver and could point 
   *  to state information for the device.  
   *  这个域能够通过设备驱动设置并且能够指向设备的信息状态,供用户自行使用*/  
  void *state;  
  
#if LWIP_DHCP     //使能DHCP
  
  /** the DHCP client state information for this netif */  
  struct dhcp *dhcp;  
#endif /* LWIP_DHCP */   
  
#if LWIP_AUTOIP   
  /** the AutoIP client state information for this netif */  
  struct autoip *autoip;  
#endif   
  
#if LWIP_NETIF_HOSTNAME
  
  /* the hostname for this netif, NULL is a valid value NULL也是一个有效值*/  
  char*  hostname;  
#endif /* LWIP_NETIF_HOSTNAME */
  
  
  /** maximum transfer unit (in bytes) 最大发送单元(单位:字节)以太网为1500*/  
  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];       //=6
  
  /** flags (see NETIF_FLAG_ above) 标志位*/  
  u8_t flags;  
  /** descriptive abbreviation 描述缩写*/  
  char name[2];  
  /** number of this interface 接口号,如果两个网络接口具有相同的描述缩写(即上面的name字段),就用num字段来区分相同类型的不同网络接口*/  
  u8_t num;  
  
#if LWIP_SNMP   
  /** link type (from "snmp_ifType" enum from snmp.h) 连接类型*/  
  u8_t link_type;  
  /** (estimate) link speed (预计)连接速度*/  
  u32_t link_speed;  
  /** timestamp at last change made (up/down) 最后up/down变化时间戳*/  
  u32_t ts;  
  /** counters 计数器*/  
  u32_t ifinoctets;  
  u32_t ifinucastpkts;  
  u32_t ifinnucastpkts;  
  u32_t ifindiscards;  
  u32_t ifoutoctets;  
  u32_t ifoutucastpkts;  
  u32_t ifoutnucastpkts;  
  u32_t ifoutdiscards;  
#endif /* LWIP_SNMP */
  
  
#if LWIP_IGMP   
  /* This function could be called to add or delete a entry in the multicast filter table of the ethernet MAC.*/  
  err_t (*igmp_mac_filter)( struct netif *netif, struct ip_addr *group, u8_t action);  
#endif /* LWIP_IGMP */   
#if LWIP_NETIF_HWADDRHINT
  
  u8_t *addr_hint;  
#endif /* LWIP_NETIF_HWADDRHINT */
  
#if ENABLE_LOOPBACK   
  /* List of packets to be queued for ourselves. */  
  struct pbuf *loop_first;  
  struct pbuf *loop_last;  
#if LWIP_LOOPBACK_MAX_PBUFS   
  u16_t loop_cnt_current;  
#endif /* LWIP_LOOPBACK_MAX_PBUFS */
  
#endif /* ENABLE_LOOPBACK */
  
}; // END struct netif  

/** Generic data structure used for all lwIP network interfaces.
* The following fields should be filled in by the initialization
* function for the device driver: hwaddr_len, hwaddr[], mtu, flags
* 用于所有lwIP网络接口的通用数据结构体
* 设备驱动函数在初始化时应该填充以下数据域:hwaddr_len,hwaddr[],mtu,flags.*/

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

/** IP address configuration in network byte order */
struct ip_addr ip_addr; //IP地址
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.
这个函数由网络设备驱动所调用,从网卡中接收数据包并传递给TCP/IP协议栈*/
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.
当IP模块向接口发送一个数据包时调用此函数。这个函数通常首先解析
硬件地址,然后发送数据包*/
err_t (* output)(struct netif *netif, struct pbuf *p, //IP层调用此函数向网卡发送一包数据
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.
当ARP模块向接口发送一个数据包时调用此函数。这个函数向链路输出一个pbuf。*/
err_t (* linkoutput)(struct netif *netif, struct pbuf *p); //ARP模块调用这个函数向网卡发送一包数据

#if LWIP_NETIF_STATUS_CALLBACK //netif状态被改变时该函数被调用,未使用
/** This function is called when the netif state is set to up or down
*/
void (* status_callback)(struct netif *netif);
#endif /* LWIP_NETIF_STATUS_CALLBACK */

#if LWIP_NETIF_LINK_CALLBACK //未使用
/** This function is called when the netif link is set to up or down
netif连接改变时调用
*/
void (* link_callback)(struct netif *netif);
#endif /* LWIP_NETIF_LINK_CALLBACK */

/** This field can be set by the device driver and could point
* to state information for the device.
* 这个域能够通过设备驱动设置并且能够指向设备的信息状态,供用户自行使用*/
void *state;

#if LWIP_DHCP //使能DHCP
/** the DHCP client state information for this netif */
struct dhcp *dhcp;
#endif /* LWIP_DHCP */

#if LWIP_AUTOIP
/** the AutoIP client state information for this netif */
struct autoip *autoip;
#endif

#if LWIP_NETIF_HOSTNAME
/* the hostname for this netif, NULL is a valid value NULL也是一个有效值*/
char* hostname;
#endif /* LWIP_NETIF_HOSTNAME */

/** maximum transfer unit (in bytes) 最大发送单元(单位:字节)以太网为1500*/
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]; //=6
/** flags (see NETIF_FLAG_ above) 标志位*/
u8_t flags;
/** descriptive abbreviation 描述缩写*/
char name[2];
/** number of this interface 接口号,如果两个网络接口具有相同的描述缩写(即上面的name字段),就用num字段来区分相同类型的不同网络接口*/
u8_t num;

#if LWIP_SNMP
/** link type (from "snmp_ifType" enum from snmp.h) 连接类型*/
u8_t link_type;
/** (estimate) link speed (预计)连接速度*/
u32_t link_speed;
/** timestamp at last change made (up/down) 最后up/down变化时间戳*/
u32_t ts;
/** counters 计数器*/
u32_t ifinoctets;
u32_t ifinucastpkts;
u32_t ifinnucastpkts;
u32_t ifindiscards;
u32_t ifoutoctets;
u32_t ifoutucastpkts;
u32_t ifoutnucastpkts;
u32_t ifoutdiscards;
#endif /* LWIP_SNMP */

#if LWIP_IGMP
/* This function could be called to add or delete a entry in the multicast filter table of the ethernet MAC.*/
err_t (*igmp_mac_filter)( struct netif *netif, struct ip_addr *group, u8_t action);
#endif /* LWIP_IGMP */
#if LWIP_NETIF_HWADDRHINT
u8_t *addr_hint;
#endif /* LWIP_NETIF_HWADDRHINT */
#if ENABLE_LOOPBACK
/* List of packets to be queued for ourselves. */
struct pbuf *loop_first;
struct pbuf *loop_last;
#if LWIP_LOOPBACK_MAX_PBUFS
u16_t loop_cnt_current;
#endif /* LWIP_LOOPBACK_MAX_PBUFS */
#endif /* ENABLE_LOOPBACK */
}; // END struct netif

1. next字段指向下一个netif结构体指针.只有一个产品有多个网卡时,才使用该字段.lwIP会把所有 网卡的结构体组成一个链表来进行管理. 

2. ip_addr、netmask、gw分别表示IP地址、子网掩码和网关地址。前两个在发送和处理数据时有重要作用,第三个字段目前保留。在同一个局域网内通信时也不用使用,它是一个网络与另一个网络进行通信的交换点。不懂的可查看有关计算机网络书籍。

3. input字段指向一个函数,该函数将网卡设备接收到的数据提交给IP层。使用时,将input指针指向该函数即可。参数为pbuf和netif类型,其中pbuf为接收到的数据包。

4. output字段指向一个函数,该函数和具体的网络接口设备密切相关,它用于IP层将一包数据发送到网络接口上。用户需要编写此函数并使output指向它。参数为pbuf、netif和ip_addr类型,其中,ipaddr代表要将该数据包发送到的地址,但不一定是数据包最终到到达的IP地址。比如,要发送IP数据包到一个并不在本网络的主机上,该数据包要被发送到一个路由器上,这里的ipaddr就是路由器IP地址。

5. linkoutput字段和output类似,但只有两个参数,它是由ARP模块调用的,用于将一包数据发送到网络接口上。实际上output最终还是调用linkoutput字段函数完成数据包的发送。

6. flag字段是网卡状态标志位,是很重要的字段,包括网卡功能使能,广播使能,ARP使能等等
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息