netif.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  19. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  21. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  24. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  25. * OF SUCH DAMAGE.
  26. *
  27. * This file is part of the lwIP TCP/IP stack.
  28. *
  29. * Author: Adam Dunkels <adam@sics.se>
  30. *
  31. */
  32. #ifndef __LWIP_NETIF_H__
  33. #define __LWIP_NETIF_H__
  34. #include "lwip/opt.h"
  35. #define ENABLE_LOOPBACK (LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF)
  36. #include "lwip/err.h"
  37. #include "lwip/ip_addr.h"
  38. #include "lwip/def.h"
  39. #include "lwip/pbuf.h"
  40. #if LWIP_DHCP
  41. struct dhcp;
  42. #endif
  43. #if LWIP_AUTOIP
  44. struct autoip;
  45. #endif
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. /* Throughout this file, IP addresses are expected to be in
  50. * the same byte order as in IP_PCB. */
  51. /** must be the maximum of all used hardware address lengths
  52. across all types of interfaces in use */
  53. #define NETIF_MAX_HWADDR_LEN 6U
  54. /** Whether the network interface is 'up'. This is
  55. * a software flag used to control whether this network
  56. * interface is enabled and processes traffic.
  57. * It is set by the startup code (for static IP configuration) or
  58. * by dhcp/autoip when an address has been assigned.
  59. */
  60. #define NETIF_FLAG_UP 0x01U
  61. /** If set, the netif has broadcast capability.
  62. * Set by the netif driver in its init function. */
  63. #define NETIF_FLAG_BROADCAST 0x02U
  64. /** If set, the netif is one end of a point-to-point connection.
  65. * Set by the netif driver in its init function. */
  66. #define NETIF_FLAG_POINTTOPOINT 0x04U
  67. /** If set, the interface is configured using DHCP.
  68. * Set by the DHCP code when starting or stopping DHCP. */
  69. #define NETIF_FLAG_DHCP 0x08U
  70. /** If set, the interface has an active link
  71. * (set by the network interface driver).
  72. * Either set by the netif driver in its init function (if the link
  73. * is up at that time) or at a later point once the link comes up
  74. * (if link detection is supported by the hardware). */
  75. #define NETIF_FLAG_LINK_UP 0x10U
  76. /** If set, the netif is an ethernet device using ARP.
  77. * Set by the netif driver in its init function.
  78. * Used to check input packet types and use of DHCP. */
  79. #define NETIF_FLAG_ETHARP 0x20U
  80. /** If set, the netif is an ethernet device. It might not use
  81. * ARP or TCP/IP if it is used for PPPoE only.
  82. */
  83. #define NETIF_FLAG_ETHERNET 0x40U
  84. /** If set, the netif has IGMP capability.
  85. * Set by the netif driver in its init function. */
  86. #define NETIF_FLAG_IGMP 0x80U
  87. /** Function prototype for netif init functions. Set up flags and output/linkoutput
  88. * callback functions in this function.
  89. *
  90. * @param netif The netif to initialize
  91. */
  92. typedef err_t (*netif_init_fn)(struct netif *netif);
  93. /** Function prototype for netif->input functions. This function is saved as 'input'
  94. * callback function in the netif struct. Call it when a packet has been received.
  95. *
  96. * @param p The received packet, copied into a pbuf
  97. * @param inp The netif which received the packet
  98. */
  99. typedef err_t (*netif_input_fn)(struct pbuf *p, struct netif *inp);
  100. /** Function prototype for netif->output functions. Called by lwIP when a packet
  101. * shall be sent. For ethernet netif, set this to 'etharp_output' and set
  102. * 'linkoutput'.
  103. *
  104. * @param netif The netif which shall send a packet
  105. * @param p The packet to send (p->payload points to IP header)
  106. * @param ipaddr The IP address to which the packet shall be sent
  107. */
  108. typedef err_t (*netif_output_fn)(struct netif *netif, struct pbuf *p,
  109. ip_addr_t *ipaddr);
  110. /** Function prototype for netif->linkoutput functions. Only used for ethernet
  111. * netifs. This function is called by ARP when a packet shall be sent.
  112. *
  113. * @param netif The netif which shall send a packet
  114. * @param p The packet to send (raw ethernet packet)
  115. */
  116. typedef err_t (*netif_linkoutput_fn)(struct netif *netif, struct pbuf *p);
  117. /** Function prototype for netif status- or link-callback functions. */
  118. typedef void (*netif_status_callback_fn)(struct netif *netif);
  119. /** Function prototype for netif igmp_mac_filter functions */
  120. typedef err_t (*netif_igmp_mac_filter_fn)(struct netif *netif,
  121. ip_addr_t *group, u8_t action);
  122. /*add DHCP event processing by LiuHan*/
  123. typedef void (*dhcp_event_fn)(void);
  124. /** Generic data structure used for all lwIP network interfaces.
  125. * The following fields should be filled in by the initialization
  126. * function for the device driver: hwaddr_len, hwaddr[], mtu, flags */
  127. struct netif {
  128. /** pointer to next in linked list */
  129. struct netif *next;
  130. /** IP address configuration in network byte order */
  131. ip_addr_t ip_addr;
  132. ip_addr_t netmask;
  133. ip_addr_t gw;
  134. /** This function is called by the network device driver
  135. * to pass a packet up the TCP/IP stack. 向IP层输入数据包*/
  136. netif_input_fn input;
  137. /** This function is called by the IP module when it wants
  138. * to send a packet on the interface. This function typically
  139. * first resolves the hardware address, then sends the packet. 发送IP数据包*/
  140. netif_output_fn output;
  141. /** This function is called by the ARP module when it wants
  142. * to send a packet on the interface. This function outputs
  143. * the pbuf as-is on the link medium. 底层数据包发送*/
  144. netif_linkoutput_fn linkoutput;
  145. #if LWIP_NETIF_STATUS_CALLBACK
  146. /** This function is called when the netif state is set to up or down
  147. */
  148. netif_status_callback_fn status_callback;
  149. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  150. #if LWIP_NETIF_LINK_CALLBACK
  151. /** This function is called when the netif link is set to up or down
  152. */
  153. netif_status_callback_fn link_callback;
  154. #endif /* LWIP_NETIF_LINK_CALLBACK */
  155. /** This field can be set by the device driver and could point
  156. * to state information for the device. 自由设置字段,比如指向底层设备相关信息*/
  157. void *state;
  158. #if LWIP_DHCP
  159. /** the DHCP client state information for this netif */
  160. struct dhcp *dhcp;
  161. struct udp_pcb *dhcps_pcb; //dhcps
  162. dhcp_event_fn dhcp_event;
  163. #endif /* LWIP_DHCP */
  164. #if LWIP_AUTOIP
  165. /** the AutoIP client state information for this netif */
  166. struct autoip *autoip;
  167. #endif
  168. #if LWIP_NETIF_HOSTNAME
  169. /* the hostname for this netif, NULL is a valid value */
  170. char* hostname;
  171. #endif /* LWIP_NETIF_HOSTNAME */
  172. /** maximum transfer unit (in bytes) 该接口允许的最大数据包长度,多是1500*/
  173. u16_t mtu;
  174. /** number of bytes used in hwaddr该接口物理地址长度 */
  175. u8_t hwaddr_len;
  176. /** link level hardware address of this interface 该接口物理地址*/
  177. u8_t hwaddr[NETIF_MAX_HWADDR_LEN];
  178. /** flags (see NETIF_FLAG_ above) 该接口状态、属性字段*/
  179. u8_t flags;
  180. /** descriptive abbreviation 该接口的名字*/
  181. char name[2];
  182. /** number of this interface 该接口的编号*/
  183. u8_t num;
  184. #if LWIP_SNMP
  185. /** link type (from "snmp_ifType" enum from snmp.h) */
  186. u8_t link_type;
  187. /** (estimate) link speed */
  188. u32_t link_speed;
  189. /** timestamp at last change made (up/down) */
  190. u32_t ts;
  191. /** counters */
  192. u32_t ifinoctets;
  193. u32_t ifinucastpkts;
  194. u32_t ifinnucastpkts;
  195. u32_t ifindiscards;
  196. u32_t ifoutoctets;
  197. u32_t ifoutucastpkts;
  198. u32_t ifoutnucastpkts;
  199. u32_t ifoutdiscards;
  200. #endif /* LWIP_SNMP */
  201. #if LWIP_IGMP
  202. /** This function could be called to add or delete a entry in the multicast
  203. filter table of the ethernet MAC.*/
  204. netif_igmp_mac_filter_fn igmp_mac_filter;
  205. #endif /* LWIP_IGMP */
  206. #if LWIP_NETIF_HWADDRHINT
  207. u8_t *addr_hint;
  208. #endif /* LWIP_NETIF_HWADDRHINT */
  209. #if ENABLE_LOOPBACK
  210. /* List of packets to be queued for ourselves. 指向发送给自己的数据包的pbuf*/
  211. struct pbuf *loop_first;//第一个
  212. struct pbuf *loop_last;//最后一个
  213. #if LWIP_LOOPBACK_MAX_PBUFS
  214. u16_t loop_cnt_current;
  215. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  216. #endif /* ENABLE_LOOPBACK */
  217. };
  218. #if LWIP_SNMP
  219. #define NETIF_INIT_SNMP(netif, type, speed) \
  220. /* use "snmp_ifType" enum from snmp.h for "type", snmp_ifType_ethernet_csmacd by example */ \
  221. (netif)->link_type = (type); \
  222. /* your link speed here (units: bits per second) */ \
  223. (netif)->link_speed = (speed); \
  224. (netif)->ts = 0; \
  225. (netif)->ifinoctets = 0; \
  226. (netif)->ifinucastpkts = 0; \
  227. (netif)->ifinnucastpkts = 0; \
  228. (netif)->ifindiscards = 0; \
  229. (netif)->ifoutoctets = 0; \
  230. (netif)->ifoutucastpkts = 0; \
  231. (netif)->ifoutnucastpkts = 0; \
  232. (netif)->ifoutdiscards = 0
  233. #else /* LWIP_SNMP */
  234. #define NETIF_INIT_SNMP(netif, type, speed)
  235. #endif /* LWIP_SNMP */
  236. /** The list of network interfaces. */
  237. extern struct netif *netif_list;
  238. /** The default network interface. */
  239. extern struct netif *netif_default;
  240. void netif_init(void)ICACHE_FLASH_ATTR;
  241. struct netif *netif_add(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
  242. ip_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input)ICACHE_FLASH_ATTR;
  243. void
  244. netif_set_addr(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
  245. ip_addr_t *gw)ICACHE_FLASH_ATTR;
  246. void netif_remove(struct netif * netif)ICACHE_FLASH_ATTR;
  247. /* Returns a network interface given its name. The name is of the form
  248. "et0", where the first two letters are the "name" field in the
  249. netif structure, and the digit is in the num field in the same
  250. structure. */
  251. struct netif *netif_find(char *name)ICACHE_FLASH_ATTR;
  252. void netif_set_default(struct netif *netif)ICACHE_FLASH_ATTR;
  253. void netif_set_ipaddr(struct netif *netif, ip_addr_t *ipaddr)ICACHE_FLASH_ATTR;
  254. void netif_set_netmask(struct netif *netif, ip_addr_t *netmask)ICACHE_FLASH_ATTR;
  255. void netif_set_gw(struct netif *netif, ip_addr_t *gw)ICACHE_FLASH_ATTR;
  256. void netif_set_up(struct netif *netif)ICACHE_FLASH_ATTR;
  257. void netif_set_down(struct netif *netif)ICACHE_FLASH_ATTR;
  258. /** Ask if an interface is up */
  259. #define netif_is_up(netif) (((netif)->flags & NETIF_FLAG_UP) ? (u8_t)1 : (u8_t)0)
  260. #if LWIP_NETIF_STATUS_CALLBACK
  261. void netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback)ICACHE_FLASH_ATTR;
  262. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  263. void netif_set_link_up(struct netif *netif)ICACHE_FLASH_ATTR;
  264. void netif_set_link_down(struct netif *netif)ICACHE_FLASH_ATTR;
  265. /** Ask if a link is up */
  266. #define netif_is_link_up(netif) (((netif)->flags & NETIF_FLAG_LINK_UP) ? (u8_t)1 : (u8_t)0)
  267. #if LWIP_NETIF_LINK_CALLBACK
  268. void netif_set_link_callback(struct netif *netif, netif_status_callback_fn link_callback)ICACHE_FLASH_ATTR;
  269. #endif /* LWIP_NETIF_LINK_CALLBACK */
  270. #if LWIP_NETIF_HOSTNAME
  271. #define netif_set_hostname(netif, name) do { if((netif) != NULL) { (netif)->hostname = name; }}while(0)
  272. #define netif_get_hostname(netif) (((netif) != NULL) ? ((netif)->hostname) : NULL)
  273. #endif /* LWIP_NETIF_HOSTNAME */
  274. #if LWIP_IGMP
  275. #define netif_set_igmp_mac_filter(netif, function) do { if((netif) != NULL) { (netif)->igmp_mac_filter = function; }}while(0)
  276. #define netif_get_igmp_mac_filter(netif) (((netif) != NULL) ? ((netif)->igmp_mac_filter) : NULL)
  277. #endif /* LWIP_IGMP */
  278. #if ENABLE_LOOPBACK
  279. err_t netif_loop_output(struct netif *netif, struct pbuf *p, ip_addr_t *dest_ip)ICACHE_FLASH_ATTR;
  280. void netif_poll(struct netif *netif)ICACHE_FLASH_ATTR;
  281. #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
  282. void netif_poll_all(void)ICACHE_FLASH_ATTR;
  283. #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
  284. #endif /* ENABLE_LOOPBACK */
  285. #ifdef __cplusplus
  286. }
  287. #endif
  288. #endif /* __LWIP_NETIF_H__ */