{"id":1007,"date":"2013-10-04T15:58:05","date_gmt":"2013-10-04T15:58:05","guid":{"rendered":"http:\/\/blog.bachi.net\/?p=1007"},"modified":"2014-06-13T08:07:06","modified_gmt":"2014-06-13T08:07:06","slug":"lwip-internals","status":"publish","type":"post","link":"https:\/\/blog.bachi.net\/?p=1007","title":{"rendered":"lwIP internals"},"content":{"rendered":"<p><a href=\"http:\/\/lwip.wikia.com\/wiki\/Lwipopts.h\">lwipopts.h<\/a><br \/>\n<a href=\"http:\/\/lwip.wikia.com\/wiki\/Raw\/native_API\">Native API (RAW)<\/a> (see also lwip-1.4.1\/doc\/rawapi.txt)<br \/>\n<a href=\"http:\/\/lwip.wikia.com\/wiki\/IPv6\">IPv6<\/a><br \/>\n<a href=\"http:\/\/lwip.wikia.com\/wiki\/LwIP_IPv4\/IPv6_stacks\">LwIP IPv4\/IPv6 stacks<\/a><br \/>\n<a href=\"http:\/\/lwip.wikia.com\/wiki\/Available_device_drivers\">Available device drivers<\/a><br \/>\n<a href=\"http:\/\/lwip.wikia.com\/wiki\/LwIP_with_or_without_an_operating_system\">LwIP with or without an operating system<\/a><br \/>\n<a href=\"http:\/\/lwip.wikia.com\/wiki\/Porting_For_Bare_Metal\">Porting For Bare Metal<\/a><br \/>\n<a href=\"http:\/\/lwip.wikia.com\/wiki\/Porting_for_an_OS\">Porting for an OS<\/a><\/p>\n<p><a href=\"http:\/\/www.freertos.org\/Interactive_Frames\/Open_Frames.html?http:\/\/interactive.freertos.org\/forums\/216314-altera\">Nios updated port and demo for NiosEDS 11.0 and up (includes LwIP)<\/a><br \/>\n<a href=\"http:\/\/www.altera.co.jp\/literature\/tt\/tt_nios2_lwip_tutorial.pdf\">Using Lightweight IP with the Nios II Processor Tutorial<\/a><\/p>\n<h3>Commiter<\/h3>\n<p><span style=\"font-family: monospace\">http:\/\/git.savannah.gnu.org\/cgit\/lwip.git<br \/>http:\/\/git.savannah.gnu.org\/cgit\/lwip\/lwip-contrib.git<\/span><br \/>\n<a href=\"http:\/\/thebigecommerceconference.co.uk\/speaker2013\/simon-goldschmidt\/\">The Big eCommerce Conference: Simon Goldschmidt<\/a><br \/>\n<a href=\"https:\/\/www.facebook.com\/simon.goldschmidt\">Facebook: Simon Goldschmidt<\/a><\/p>\n<h3>VLAN<\/h3>\n<h4>Mailinglist<\/h4>\n<p>Add support for outgoing VLAN tags?<br \/>\n<a href=\"http:\/\/lists.gnu.org\/archive\/html\/lwip-users\/2011-11\/msg00000.html\">from Simon Goldschmidt<\/a><br \/>\n<a href=\"http:\/\/lists.gnu.org\/archive\/html\/lwip-users\/2011-11\/msg00056.html\">from address@hidden<\/a><br \/>\n<a href=\"http:\/\/lists.gnu.org\/archive\/html\/lwip-users\/2011-11\/msg00058.html\">from Dance, Brian<\/a><br \/>\n<a href=\"http:\/\/lists.gnu.org\/archive\/html\/lwip-users\/2011-11\/msg00012.html\">from web<\/a><\/p>\n<h4>Tasks<\/h4>\n<p><a href=\"http:\/\/savannah.nongnu.org\/task\/?9033\">task #9033 Support IEEE 802.1q tagged frame (VLAN)<\/a> (Commited: 21.012.2009, Done: 25.08.2009)<br \/>\n<a href=\"http:\/\/savannah.nongnu.org\/task\/?11620\">task #11620 Add outgoing VLAN PCP support for Ethernet level QoS<\/a> (Commited: 01.12.2011, Done: 01.12.2011)<\/p>\n<h4>Patches<\/h4>\n<p><a href=\"http:\/\/savannah.nongnu.org\/patch\/?7993\">patch #7993 Add support for transmitting packets with VLAN headers<\/a> (Commited: 03.04.2013, Done: 20.02.2014)<\/p>\n<h4>Code<\/h4>\n<pre class=\"brush: cpp; collapse: true; light: false; title: lwip-enet\/lwipopts.h; toolbar: true; notranslate\" title=\"lwip-enet\/lwipopts.h\">\r\n\/**\r\n * ETHARP_SUPPORT_VLAN==1: support receiving and sending ethernet packets with\r\n * VLAN header. See the description of LWIP_HOOK_VLAN_CHECK and\r\n * LWIP_HOOK_VLAN_SET hooks to check\/set VLAN headers.\r\n * Additionally, you can define ETHARP_VLAN_CHECK to an u16_t VLAN ID to check.\r\n * If ETHARP_VLAN_CHECK is defined, only VLAN-traffic for this VLAN is accepted.\r\n * If ETHARP_VLAN_CHECK is not defined, all traffic is accepted.\r\n * Alternatively, define a function\/define ETHARP_VLAN_CHECK_FN(eth_hdr, vlan)\r\n * that returns 1 to accept a packet or 0 to drop a packet.\r\n *\/\r\n#ifndef ETHARP_SUPPORT_VLAN\r\n#define ETHARP_SUPPORT_VLAN             1\r\n#endif\r\n\r\n\/**\r\n * LWIP_HOOK_VLAN_CHECK(netif, eth_hdr, vlan_hdr):\r\n * - called from ethernet_input() if VLAN support is enabled\r\n * - netif: struct netif on which the packet has been received\r\n * - eth_hdr: struct eth_hdr of the packet\r\n * - vlan_hdr: struct eth_vlan_hdr of the packet\r\n * Return values:\r\n * - 0: Packet must be dropped.\r\n * - != 0: Packet must be accepted.\r\n *\/\r\n\r\n\/**\r\n * LWIP_HOOK_VLAN_SET(netif, eth_hdr, vlan_hdr):\r\n * - called from etharp_raw() and etharp_send_ip() if VLAN support is enabled\r\n * - netif: struct netif that the packet will be sent through\r\n * - eth_hdr: struct eth_hdr of the packet\r\n * - vlan_hdr: struct eth_vlan_hdr of the packet\r\n * Return values:\r\n * - 0: Packet shall not contain VLAN header.\r\n * - != 0: Packet shall contain VLAN header.\r\n * Hook can be used to set prio_vid field of vlan_hdr.\r\n *\/\r\n<\/pre>\n<pre class=\"brush: cpp; collapse: true; light: false; title: lwip-1.5.0\/netif\/etharp.c; toolbar: true; notranslate\" title=\"lwip-1.5.0\/netif\/etharp.c\">\r\n#if ETHARP_SUPPORT_VLAN\r\n  if (type == PP_HTONS(ETHTYPE_VLAN)) {\r\n    struct eth_vlan_hdr *vlan = (struct eth_vlan_hdr*)(((char*)ethhdr) + SIZEOF_ETH_HDR);\r\n    if (p-&gt;len &lt;= SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR) {\r\n      \/* a packet with only an ethernet\/vlan header (or less) is not valid for us *\/\r\n      ETHARP_STATS_INC(etharp.proterr);\r\n      ETHARP_STATS_INC(etharp.drop);\r\n      goto free_and_return;\r\n    }\r\n#if defined(LWIP_HOOK_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK_FN) \/* if not, allow all VLANs *\/\r\n#ifdef LWIP_HOOK_VLAN_CHECK\r\n    if (!LWIP_HOOK_VLAN_CHECK(netif, ethhdr, vlan)) {\r\n#elif defined(ETHARP_VLAN_CHECK_FN)\r\n    if (!ETHARP_VLAN_CHECK_FN(ethhdr, vlan)) {\r\n#elif defined(ETHARP_VLAN_CHECK)\r\n    if (VLAN_ID(vlan) != ETHARP_VLAN_CHECK) {\r\n#endif\r\n      \/* silently ignore this packet: not for our VLAN *\/\r\n      pbuf_free(p);\r\n      return ERR_OK;\r\n    }\r\n#endif \/* defined(LWIP_HOOK_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK_FN) *\/\r\n    type = vlan-&gt;tpid;\r\n    ip_hdr_offset = SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR;\r\n  }\r\n#endif \/* ETHARP_SUPPORT_VLAN *\/\r\n<\/pre>\n<h3>STM32 &#038; FreeRTOS<\/h3>\n<p><a href=\"http:\/\/lists.gnu.org\/archive\/html\/lwip-users\/2013-09\/msg00025.html\">lwIP replies ping request with reply of an earlier request (packages out off sync)<\/a><\/p>\n<h3>Nios II<\/h3>\n<p><a href=\"http:\/\/comments.gmane.org\/gmane.network.lwip.general\/12431\">Nios II + LWIP_RAND() + IGMP<\/a><\/p>\n<h3>IPv4 (deprecated)<\/h3>\n<p><a href=\"http:\/\/lwip.wikia.com\/wiki\/IPv4\">IPv4<\/a><\/p>\n<pre class=\"brush: cpp; collapse: true; light: false; title: lwip-1.4.1\/ipv4\/lwip\/ip_addr.h; toolbar: true; notranslate\" title=\"lwip-1.4.1\/ipv4\/lwip\/ip_addr.h\">\r\n\/* This is the aligned version of ip_addr_t,\r\n   used as local variable, on the stack, etc. *\/\r\nstruct ip_addr {\r\n  u32_t addr;\r\n};\r\n\r\n\/* This is the packed version of ip_addr_t,\r\n   used in network headers that are itself packed *\/\r\nstruct ip_addr_packed {\r\n  PACK_STRUCT_FIELD(u32_t addr);\r\n};\r\n\r\n\/*\r\n * struct ipaddr2 is used in the definition of the ARP packet format in\r\n * order to support compilers that don't have structure packing.\r\n *\/\r\nstruct ip_addr2 {\r\n  PACK_STRUCT_FIELD(u16_t addrw&#x5B;2]);\r\n};\r\n\r\n\/** ip_addr_t uses a struct for convenience only, so that the same defines can\r\n * operate both on ip_addr_t as well as on ip_addr_p_t. *\/\r\ntypedef struct ip_addr ip_addr_t;\r\ntypedef struct ip_addr_packed ip_addr_p_t;\r\n<\/pre>\n<pre class=\"brush: cpp; collapse: true; light: false; title: lwip-1.4.1\/core\/ipv4\/ip.c; toolbar: true; notranslate\" title=\"lwip-1.4.1\/core\/ipv4\/ip.c\">\r\n\/**\r\n * This function is called by the network interface device driver when\r\n * an IP packet is received. The function does the basic checks of the\r\n * IP header such as packet size being at least larger than the header\r\n * size etc. If the packet was not destined for us, the packet is\r\n * forwarded (using ip_forward). The IP checksum is always checked.\r\n *\r\n * Finally, the packet is sent to the upper layer protocol input function.\r\n * \r\n * @param p the received IP packet (p-&gt;payload points to IP header)\r\n * @param inp the netif on which this packet was received\r\n * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't\r\n *         processed, but currently always returns ERR_OK)\r\n *\/\r\nerr_t\r\nip_input(struct pbuf *p, struct netif *inp)\r\n{\r\n    &#x5B;...]\r\n}\r\n<\/pre>\n<h3>IPv6<\/h3>\n<h4>Neighbor Discovery Protocol (NDP)<\/h4>\n<pre class=\"brush: cpp; collapse: true; light: false; title: IPv6 structures and defines; toolbar: true; notranslate\" title=\"IPv6 structures and defines\">\r\n\/** Router advertisement message header. *\/\r\nstruct ra_header {\r\n  PACK_STRUCT_FIELD(u8_t         type);\r\n  PACK_STRUCT_FIELD(u8_t         code);\r\n  PACK_STRUCT_FIELD(u16_t        chksum);\r\n  PACK_STRUCT_FIELD(u8_t         current_hop_limit);\r\n  PACK_STRUCT_FIELD(u8_t         flags);\r\n  PACK_STRUCT_FIELD(u16_t        router_lifetime);\r\n  PACK_STRUCT_FIELD(u32_t        reachable_time);\r\n  PACK_STRUCT_FIELD(u32_t        retrans_timer);\r\n  \/* Options follow. *\/\r\n} PACK_STRUCT_STRUCT;\r\n\r\n#define ND6_OPTION_TYPE_SOURCE_LLADDR &lt;--\r\n#define ND6_OPTION_TYPE_MTU\r\n#define ND6_OPTION_TYPE_PREFIX_INFO   &lt;--\r\n#define ND6_OPTION_TYPE_ROUTE_INFO\r\n\r\n#define ND6_PREFIX_FLAG_ON_LINK        (0x80)\r\n#define ND6_PREFIX_FLAG_AUTONOMOUS     (0x40)\r\n#define ND6_PREFIX_FLAG_ROUTER_ADDRESS (0x20)\r\n#define ND6_PREFIX_FLAG_SITE_PREFIX    (0x10)\r\n\r\nstruct prefix_option {\r\n  PACK_STRUCT_FIELD(u8_t         type);\r\n  PACK_STRUCT_FIELD(u8_t         length);\r\n  PACK_STRUCT_FIELD(u8_t         prefix_length);\r\n  PACK_STRUCT_FIELD(u8_t         flags);\r\n  PACK_STRUCT_FIELD(u32_t        valid_lifetime);\r\n  PACK_STRUCT_FIELD(u32_t        preferred_lifetime);\r\n  PACK_STRUCT_FIELD(u8_t         reserved2&#x5B;3]);\r\n  PACK_STRUCT_FIELD(u8_t         site_prefix_length);\r\n  PACK_STRUCT_FIELD(ip6_addr_p_t prefix);\r\n} PACK_STRUCT_STRUCT;\r\n\r\nstruct lladdr_option {\r\n  PACK_STRUCT_FIELD(u8_t         type);\r\n  PACK_STRUCT_FIELD(u8_t         length);\r\n  PACK_STRUCT_FIELD(u8_t         addr&#x5B;NETIF_MAX_HWADDR_LEN]);\r\n} PACK_STRUCT_STRUCT;\r\n\r\nenum nd6_neighbor_cache_entry_state {\r\n  ND6_NO_ENTRY = 0,\r\n  ND6_INCOMPLETE,\r\n  ND6_REACHABLE,\r\n  ND6_STALE,\r\n  ND6_DELAY,\r\n  ND6_PROBE\r\n};\r\n\r\n\/* Struct for tables. *\/\r\nstruct nd6_neighbor_cache_entry {\r\n  ip6_addr_t           next_hop_address;\r\n  struct netif        *netif;\r\n  u8_t                 lladdr&#x5B;NETIF_MAX_HWADDR_LEN];\r\n  \/** Pointer to queue of pending outgoing packets on this entry. *\/\r\n  struct nd6_q_entry  *q;\r\n  u8_t                 state;\r\n  u8_t                 isrouter;\r\n  union {\r\n    u32_t              reachable_time;\r\n    u32_t              delay_time;\r\n    u32_t              probes_sent;\r\n    u32_t              stale_time;\r\n  }                    counter;\r\n};\r\n\r\ndefault_router_list&#x5B;i];\r\nprefix_list&#x5B;i];\r\nneighbor_cache&#x5B;i];\r\nneighbor_cache&#x5B;neighbor_index];\r\n\r\nstatic void nd6_send_q(s8_t i)\r\n<\/pre>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nThread &#x5B;1] (Suspended)\t\r\n\t12 nd6_new_onlink_prefix() nd6.c:1401 0x100277c8\t\r\n\t11 nd6_input() nd6.c:466 0x10024570\r\n           &#x5B;...]\r\n\r\nThread &#x5B;1] (Suspended)\t\r\n\t12 nd6_new_router() nd6.c:1333 0x10027464\t\r\n\t11 nd6_input() nd6.c:386 0x1002413c\t\r\n\t10 icmp6_input() icmp6.c:116 0x1001dd40\t\r\n\t9 ip6_input() ip6.c:689 0x1001f9b0\t\r\n\t8 ethernet_input() etharp.c:1465 0x1003da38\t\r\n\t7 lwip_enet_input() lwip_enet.c:74 0x10015d48\t\r\n\t6 ptp2_port_receive() ptp2_port.c:315 0x10011f90\t\r\n\t5 ptp2_port_task_rx() ptp2_port.c:239 0x10011bf8\t\r\n\t4 ptp2_master_mainloop() ptp2_master.c:142 0x1000d6b4\t\r\n\t3 main() main.c:73 0x1003dbfc\t\r\n\t2 alt_main() alt_main.c:154 0x1004f77c\t\r\n\t1 _start() crt0.S:437 0x10000204\t\r\n<\/pre>\n<h3>Ping \/ ICMP<\/h3>\n<p><a href=\"http:\/\/lwip.wikia.com\/wiki\/ICMP\">ICMP<\/a><br \/>\n<a href=\"https:\/\/lists.gnu.org\/archive\/html\/lwip-users\/2011-04\/msg00087.html\">Is there and &#8216;ping&#8217; function?<\/a><br \/>\n<a href=\"http:\/\/lists.gnu.org\/archive\/html\/lwip-users\/2007-10\/msg00063.html\">ICMP ping send\/receive<\/a><\/p>\n<h3>ARP<\/h3>\n<p><a href=\"http:\/\/lwip.wikia.com\/wiki\/ARP\">ARP<\/a><br \/>\n<a href=\"http:\/\/tools.ietf.org\/html\/rfc826\">RFC 826: An Ethernet Address Resolution Protocol<\/a><\/p>\n<pre class=\"brush: cpp; collapse: true; light: false; title: lwip-enet\/lwipopts.h; toolbar: true; notranslate\" title=\"lwip-enet\/lwipopts.h\">\r\n\/**\r\n * LWIP_ARP==1: Enable ARP functionality.\r\n *\/\r\n#ifndef LWIP_ARP\r\n#define LWIP_ARP                        1\r\n#endif\r\n\r\n\/**\r\n * ARP_TABLE_SIZE: Number of active MAC-IP address pairs cached.\r\n *\/\r\n#ifndef ARP_TABLE_SIZE\r\n#define ARP_TABLE_SIZE                  10\r\n#endif\r\n\r\n\/**\r\n * ARP_QUEUEING==1: Multiple outgoing packets are queued during hardware address\r\n * resolution. By default, only the most recent packet is queued per IP address.\r\n * This is sufficient for most protocols and mainly reduces TCP connection\r\n * startup time. Set this to 1 if you know your application sends more than one\r\n * packet in a row to an IP address that is not in the ARP cache.\r\n *\/\r\n#ifndef ARP_QUEUEING\r\n#define ARP_QUEUEING                    1\r\n#endif\r\n\r\n\/**\r\n * ETHARP_TRUST_IP_MAC==1: Incoming IP packets cause the ARP table to be\r\n * updated with the source MAC and IP addresses supplied in the packet.\r\n * You may want to disable this if you do not trust LAN peers to have the\r\n * correct addresses, or as a limited approach to attempt to handle\r\n * spoofing. If disabled, lwIP will need to make a new ARP request if\r\n * the peer is not already in the ARP table, adding a little latency.\r\n * The peer *is* in the ARP table if it requested our address before.\r\n * Also notice that this slows down input processing of every IP packet!\r\n *\/\r\n#ifndef ETHARP_TRUST_IP_MAC\r\n#define ETHARP_TRUST_IP_MAC             1\r\n#endif\r\n\r\n\/**\r\n * ETHARP_SUPPORT_VLAN==1: support receiving ethernet packets with VLAN header.\r\n * Additionally, you can define ETHARP_VLAN_CHECK to an u16_t VLAN ID to check.\r\n * If ETHARP_VLAN_CHECK is defined, only VLAN-traffic for this VLAN is accepted.\r\n * If ETHARP_VLAN_CHECK is not defined, all traffic is accepted.\r\n * Alternatively, define a function\/define ETHARP_VLAN_CHECK_FN(eth_hdr, vlan)\r\n * that returns 1 to accept a packet or 0 to drop a packet.\r\n *\/\r\n#ifndef ETHARP_SUPPORT_VLAN\r\n#define ETHARP_SUPPORT_VLAN             0\r\n#endif\r\n<\/pre>\n<pre class=\"brush: cpp; collapse: true; light: false; title: lwip-1.4.1\/netif\/etharp.h; toolbar: true; notranslate\" title=\"lwip-1.4.1\/netif\/etharp.h\">\r\n\/** the ARP message, see RFC 826 (&quot;Packet format&quot;) *\/\r\nstruct etharp_hdr {\r\n  PACK_STRUCT_FIELD(u16_t hwtype);\r\n  PACK_STRUCT_FIELD(u16_t proto);\r\n  PACK_STRUCT_FIELD(u8_t  hwlen);\r\n  PACK_STRUCT_FIELD(u8_t  protolen);\r\n  PACK_STRUCT_FIELD(u16_t opcode);\r\n  PACK_STRUCT_FIELD(struct eth_addr shwaddr);\r\n  PACK_STRUCT_FIELD(struct ip_addr2 sipaddr);\r\n  PACK_STRUCT_FIELD(struct eth_addr dhwaddr);\r\n  PACK_STRUCT_FIELD(struct ip_addr2 dipaddr);\r\n} PACK_STRUCT_STRUCT;\r\n<\/pre>\n<pre class=\"brush: cpp; collapse: true; light: false; title: lwip-1.4.1\/netif\/etharp.c; toolbar: true; notranslate\" title=\"lwip-1.4.1\/netif\/etharp.c\">\r\n\/**\r\n * Update (or insert) a IP\/MAC address pair in the ARP cache.\r\n *\r\n * If a pending entry is resolved, any queued packets will be sent\r\n * at this point.\r\n * \r\n * @param netif netif related to this entry (used for NETIF_ADDRHINT)\r\n * @param ipaddr IP address of the inserted ARP entry.\r\n * @param ethaddr Ethernet address of the inserted ARP entry.\r\n * @param flags @see definition of ETHARP_FLAG_*\r\n *\r\n * @return\r\n * - ERR_OK Succesfully updated ARP cache.\r\n * - ERR_MEM If we could not add a new ARP entry when ETHARP_FLAG_TRY_HARD was set.\r\n * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.\r\n *\r\n * @see pbuf_free()\r\n *\/\r\nstatic err_t\r\netharp_update_arp_entry(struct netif *netif, ip_addr_t *ipaddr, struct eth_addr *ethaddr, u8_t flags)\r\n{\r\n    &#x5B;...]\r\n}\r\n<\/pre>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n  \/* ARP message directed to us?\r\n      -&gt; add IP address in ARP cache; assume requester wants to talk to us,\r\n         can result in directly sending the queued packets for this host.\r\n     ARP message not directed to us?\r\n      -&gt;  update the source IP address in the cache, if present *\/\r\n  etharp_update_arp_entry(netif, &amp;sipaddr, &amp;(hdr-&gt;shwaddr),\r\n                   for_us ? ETHARP_FLAG_TRY_HARD : ETHARP_FLAG_FIND_ONLY);\r\n<\/pre>\n<pre class=\"brush: plain; title: Stacktrace; notranslate\" title=\"Stacktrace\">\r\nThread &#x5B;1] (Suspended)\t\r\n\t10 lwip_enet_low_level_output() lwip_enet.c:131 0x1002e640 &#x5B;via netif-&gt;linkoutput()]\r\n\t9 etharp_arp_input() etharp.c:807 0x1002d374\t\r\n\t8 ethernet_input() etharp.c:1372 0x1002e38c &#x5B;via netif-&gt;input()]\r\n\t7 lwip_enet_input() lwip_enet.c:74 0x1002e4d0\t\r\n\t6 ptp2_port_receive() ptp2_port.c:301 0x10011f48\t\r\n\t5 ptp2_port_task_rx() ptp2_port.c:214 0x10011bac\t\r\n\t4 ptp2_master_mainloop() ptp2_master.c:142 0x1000d6f8\t\r\n\t3 main() main.c:73 0x1002e92c\t\r\n\t2 alt_main() alt_main.c:154 0x10040340\t\r\n\t1 _start() crt0.S:437 0x10000204\t\r\n<\/pre>\n<h3>Memory Pools &#8211; Dynamic pool memory manager<\/h3>\n<p>lwIP has dedicated pools for many structures (netconn, protocol control blocks, packet buffers, &#8230;). All these pools are managed here.<\/p>\n<pre class=\"brush: cpp; collapse: true; light: false; title: lwip-enet\/lwipopts.h; toolbar: true; notranslate\" title=\"lwip-enet\/lwipopts.h\">\r\n\/**\r\n * MEM_LIBC_MALLOC==1: Use malloc\/free\/realloc provided by your C-library\r\n * instead of the lwip internal allocator. Can save code size if you\r\n * already use it.\r\n *\/\r\n#ifndef MEM_LIBC_MALLOC\r\n#define MEM_LIBC_MALLOC                 0\r\n#endif\r\n\r\n\/**\r\n* MEMP_MEM_MALLOC==1: Use mem_malloc\/mem_free instead of the lwip pool allocator.\r\n* Especially useful with MEM_LIBC_MALLOC but handle with care regarding execution\r\n* speed and usage from interrupts!\r\n*\/\r\n#ifndef MEMP_MEM_MALLOC\r\n#define MEMP_MEM_MALLOC                 0\r\n#endif\r\n\r\n\/**\r\n * MEMP_SEPARATE_POOLS: if defined to 1, each pool is placed in its own array.\r\n * This can be used to individually change the location of each pool.\r\n * Default is one big array for all pools\r\n *\/\r\n#ifndef MEMP_SEPARATE_POOLS\r\n#define MEMP_SEPARATE_POOLS             0\r\n#endif\r\n\/**\r\n * MEM_USE_POOLS==1: Use an alternative to malloc() by allocating from a set\r\n * of memory pools of various sizes. When mem_malloc is called, an element of\r\n * the smallest pool that can provide the length needed is returned.\r\n * To use this, MEMP_USE_CUSTOM_POOLS also has to be enabled.\r\n *\/\r\n#ifndef MEM_USE_POOLS\r\n#define MEM_USE_POOLS                   0\r\n#endif\r\n<\/pre>\n<p>On the fly <span style=\"font-family: monospace\">enum memp_t { MEMP_ARP_QUEUE, [&#8230;] }<\/span><\/p>\n<pre class=\"brush: cpp; collapse: true; light: false; title: lwip-1.4.1\/lwip\/memp_std.h; toolbar: true; notranslate\" title=\"lwip-1.4.1\/lwip\/memp_std.h\">\r\n\r\n\/* Create the list of all memory pools managed by memp. MEMP_MAX represents a NULL pool at the end *\/\r\ntypedef enum {\r\n#define LWIP_MEMPOOL(name,num,size,desc)  MEMP_##name,\r\n#include &quot;lwip\/memp_std.h&quot;\r\n  MEMP_MAX\r\n} memp_t;\r\n\r\n\/* it not defines macros, but uses it *\/\r\nLWIP_MEMPOOL(RAW_PCB,        MEMP_NUM_RAW_PCB,         sizeof(struct raw_pcb),        &quot;RAW_PCB&quot;)\r\nLWIP_MEMPOOL(UDP_PCB,        MEMP_NUM_UDP_PCB,         sizeof(struct udp_pcb),        &quot;UDP_PCB&quot;)\r\nLWIP_MEMPOOL(TCP_PCB,        MEMP_NUM_TCP_PCB,         sizeof(struct tcp_pcb),        &quot;TCP_PCB&quot;)\r\nLWIP_MEMPOOL(TCP_PCB_LISTEN, MEMP_NUM_TCP_PCB_LISTEN,  sizeof(struct tcp_pcb_listen), &quot;TCP_PCB_LISTEN&quot;)\r\nLWIP_MEMPOOL(TCP_SEG,        MEMP_NUM_TCP_SEG,         sizeof(struct tcp_seg),        &quot;TCP_SEG&quot;)\r\nLWIP_MEMPOOL(REASSDATA,      MEMP_NUM_REASSDATA,       sizeof(struct ip_reassdata),   &quot;REASSDATA&quot;)\r\nLWIP_MEMPOOL(ARP_QUEUE,      MEMP_NUM_ARP_QUEUE,       sizeof(struct etharp_q_entry), &quot;ARP_QUEUE&quot;)\r\nLWIP_MEMPOOL(SYS_TIMEOUT,    MEMP_NUM_SYS_TIMEOUT,     sizeof(struct sys_timeo),      &quot;SYS_TIMEOUT&quot;)\r\n\/*\r\n * A list of pools of pbuf's used by LWIP.\r\n *\r\n * LWIP_PBUF_MEMPOOL(pool_name, number_elements, pbuf_payload_size, pool_description)\r\n *     creates a pool name MEMP_pool_name. description is used in stats.c\r\n *     This allocates enough space for the pbuf struct and a payload.\r\n *     (Example: pbuf_payload_size=0 allocates only size for the struct)\r\n *\/\r\nLWIP_PBUF_MEMPOOL(PBUF,      MEMP_NUM_PBUF,            0,                             &quot;PBUF_REF\/ROM&quot;)\r\nLWIP_PBUF_MEMPOOL(PBUF_POOL, PBUF_POOL_SIZE,           PBUF_POOL_BUFSIZE,             &quot;PBUF_POOL&quot;)\r\n<\/pre>\n<pre class=\"brush: cpp; collapse: true; light: false; title: lwip-1.4.1\/core\/memp.c; toolbar: true; notranslate\" title=\"lwip-1.4.1\/core\/memp.c\">\r\n\r\n#if !MEMP_MEM_MALLOC \/* don't build if not configured for use in lwipopts.h *\/\r\n\r\nstruct memp {\r\n  struct memp *next;\r\n};\r\n\r\n\/*====== re-defines LWIP_MEMPOOL each time  ======*\/\r\n\r\n\/** This array holds the first free element of each pool.\r\n *  Elements form a linked list. *\/\r\nstatic struct memp *memp_tab&#x5B;MEMP_MAX];\r\n\r\n\/** This array holds the element sizes of each pool. *\/\r\nstatic const u16_t memp_sizes&#x5B;MEMP_MAX] = {\r\n#define LWIP_MEMPOOL(name,num,size,desc)  LWIP_MEM_ALIGN_SIZE(size),\r\n#include &quot;lwip\/memp_std.h&quot;\r\n};\r\n\r\n\/** This array holds the number of elements in each pool. *\/\r\nstatic const u16_t memp_num&#x5B;MEMP_MAX] = {\r\n#define LWIP_MEMPOOL(name,num,size,desc)  (num),\r\n#include &quot;lwip\/memp_std.h&quot;\r\n};\r\n\r\n\/** This is the actual memory used by the pools (all pools in one big block). *\/\r\nstatic u8_t memp_memory&#x5B;MEM_ALIGNMENT - 1 \r\n#define LWIP_MEMPOOL(name,num,size,desc) + ( (num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size) ) )\r\n#include &quot;lwip\/memp_std.h&quot;\r\n];\r\n\r\n\/**\r\n * Get an element from a specific pool.\r\n *\r\n * @param type the pool to get an element from\r\n *\r\n * @return a pointer to the allocated memory or a NULL pointer on error\r\n *\/\r\nvoid *\r\nmemp_malloc(memp_t type)\r\n{\r\n    &#x5B;...]\r\n}\r\n<\/pre>\n<h3>Mutexes and Semaphores<\/h3>\n<pre class=\"brush: cpp; collapse: true; light: false; title: lwip-enet\/lwipopts.h; toolbar: true; notranslate\" title=\"lwip-enet\/lwipopts.h\">\r\n\/**\r\n * SYS_LIGHTWEIGHT_PROT==1: if you want inter-task protection for certain\r\n * critical regions during buffer allocation, deallocation and memory\r\n * allocation and deallocation.\r\n *\/\r\n#ifndef SYS_LIGHTWEIGHT_PROT\r\n#define SYS_LIGHTWEIGHT_PROT            0\r\n#endif\r\n<\/pre>\n<pre class=\"brush: cpp; collapse: true; light: false; title: lwip-1.4.1\/lwip\/sys.h; toolbar: true; notranslate\" title=\"lwip-1.4.1\/lwip\/sys.h\">\r\n#if SYS_LIGHTWEIGHT_PROT\r\n\r\n\/** SYS_ARCH_DECL_PROTECT\r\n * declare a protection variable. This macro will default to defining a variable of\r\n * type sys_prot_t. If a particular port needs a different implementation, then\r\n * this macro may be defined in sys_arch.h.\r\n *\/\r\n#define SYS_ARCH_DECL_PROTECT(lev) sys_prot_t lev\r\n\/** SYS_ARCH_PROTECT\r\n * Perform a &quot;fast&quot; protect. This could be implemented by\r\n * disabling interrupts for an embedded system or by using a semaphore or\r\n * mutex. The implementation should allow calling SYS_ARCH_PROTECT when\r\n * already protected. The old protection level is returned in the variable\r\n * &quot;lev&quot;. This macro will default to calling the sys_arch_protect() function\r\n * which should be implemented in sys_arch.c. If a particular port needs a\r\n * different implementation, then this macro may be defined in sys_arch.h\r\n *\/\r\n#define SYS_ARCH_PROTECT(lev) lev = sys_arch_protect()\r\n\/** SYS_ARCH_UNPROTECT\r\n * Perform a &quot;fast&quot; set of the protection level to &quot;lev&quot;. This could be\r\n * implemented by setting the interrupt level to &quot;lev&quot; within the MACRO or by\r\n * using a semaphore or mutex.  This macro will default to calling the\r\n * sys_arch_unprotect() function which should be implemented in\r\n * sys_arch.c. If a particular port needs a different implementation, then\r\n * this macro may be defined in sys_arch.h\r\n *\/\r\n#define SYS_ARCH_UNPROTECT(lev) sys_arch_unprotect(lev)\r\n\r\n#else\r\n\r\n#define SYS_ARCH_DECL_PROTECT(lev)\r\n#define SYS_ARCH_PROTECT(lev)\r\n#define SYS_ARCH_UNPROTECT(lev)\r\n\r\n#endif \/* SYS_LIGHTWEIGHT_PROT *\/\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>lwipopts.h Native API (RAW) (see also lwip-1.4.1\/doc\/rawapi.txt) IPv6 LwIP IPv4\/IPv6 stacks Available device drivers LwIP with or without an operating system Porting For Bare Metal Porting for an OS Nios updated port and demo for NiosEDS 11.0 and up (includes LwIP) Using Lightweight IP with the Nios II Processor Tutorial Commiter http:\/\/git.savannah.gnu.org\/cgit\/lwip.githttp:\/\/git.savannah.gnu.org\/cgit\/lwip\/lwip-contrib.git The Big eCommerce [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1007","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/1007","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1007"}],"version-history":[{"count":39,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/1007\/revisions"}],"predecessor-version":[{"id":2519,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/1007\/revisions\/2519"}],"wp:attachment":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}