/** Clean up ARP table entries */ static void free_entry(int i) { /* remove from SNMP ARP index tree */ snmp_delete_arpidx_tree(arp_table[i].netif, &arp_table[i].ipaddr); #if ARP_QUEUEING /* and empty packet queue */ if (arp_table[i].q != NULL) { /* remove all queued packets */ LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: freeing entry %"U16_F", packet queue %p.\n", (u16_t)i, (void *)(arp_table[i].q))); free_etharp_q(arp_table[i].q); arp_table[i].q = NULL; } #endif /* ARP_QUEUEING */ /* recycle entry for re-use */ arp_table[i].state = ETHARP_STATE_EMPTY; #if ETHARP_SUPPORT_STATIC_ENTRIES arp_table[i].static_entry = 0; #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ #ifdef LWIP_DEBUG /* for debugging, clean out the complete entry */ arp_table[i].ctime = 0; #if LWIP_SNMP arp_table[i].netif = NULL; #endif /* LWIP_SNMP */ ip_addr_set_zero(&arp_table[i].ipaddr); arp_table[i].ethaddr = ethzero; #endif /* LWIP_DEBUG */ }
/** Clean up ARP table entries */ static void etharp_free_entry(int i) { /* remove from SNMP ARP index tree */ mib2_remove_arp_entry(arp_table[i].netif, &arp_table[i].ipaddr); /* and empty packet queue */ if (arp_table[i].q != NULL) { /* remove all queued packets */ LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_free_entry: freeing entry %"U16_F", packet queue %p.\n", (u16_t)i, (void *)(arp_table[i].q))); free_etharp_q(arp_table[i].q); arp_table[i].q = NULL; } /* recycle entry for re-use */ arp_table[i].state = ETHARP_STATE_EMPTY; #ifdef LWIP_DEBUG /* for debugging, clean out the complete entry */ arp_table[i].ctime = 0; arp_table[i].netif = NULL; ip4_addr_set_zero(&arp_table[i].ipaddr); arp_table[i].ethaddr = ethzero; #endif /* LWIP_DEBUG */ }
/** * Clears expired entries in the ARP table. * * This function should be called every ETHARP_TMR_INTERVAL microseconds (5 seconds), * in order to expire entries in the ARP table. */ void etharp_tmr(void) { u8_t i; LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer\n")); /* remove expired entries from the ARP table */ for (i = 0; i < ARP_TABLE_SIZE; ++i) { arp_table[i].ctime++; if (((arp_table[i].state == ETHARP_STATE_STABLE) && (arp_table[i].ctime >= ARP_MAXAGE)) || ((arp_table[i].state == ETHARP_STATE_PENDING) && (arp_table[i].ctime >= ARP_MAXPENDING))) { /* pending or stable entry has become old! */ LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: expired %s entry %"U16_F".\n", arp_table[i].state == ETHARP_STATE_STABLE ? "stable" : "pending", (u16_t)i)); /* clean up entries that have just been expired */ /* remove from SNMP ARP index tree */ snmp_delete_arpidx_tree(arp_table[i].netif, &arp_table[i].ipaddr); #if ARP_QUEUEING /* and empty packet queue */ if (arp_table[i].q != NULL) { /* remove all queued packets */ LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: freeing entry %"U16_F", packet queue %p.\n", (u16_t)i, (void *)(arp_table[i].q))); free_etharp_q(arp_table[i].q); arp_table[i].q = NULL; } #endif /* recycle entry for re-use */ arp_table[i].state = ETHARP_STATE_EMPTY; } #if ARP_QUEUEING /* still pending entry? (not expired) */ if (arp_table[i].state == ETHARP_STATE_PENDING) { /* resend an ARP query here? */ } #endif } }
/** * Search the ARP table for a matching or new entry. * * If an IP address is given, return a pending or stable ARP entry that matches * the address. If no match is found, create a new entry with this address set, * but in state ETHARP_EMPTY. The caller must check and possibly change the * state of the returned entry. * * If ipaddr is NULL, return a initialized new entry in state ETHARP_EMPTY. * * In all cases, attempt to create new entries from an empty entry. If no * empty entries are available and ETHARP_TRY_HARD flag is set, recycle * old entries. Heuristic choose the least important entry for recycling. * * @param ipaddr IP address to find in ARP cache, or to add if not found. * @param flags * - ETHARP_TRY_HARD: Try hard to create a entry by allowing recycling of * active (stable or pending) entries. * * @return The ARP entry index that matched or is created, ERR_MEM if no * entry is found or could be recycled. */ static s8_t #if LWIP_NETIF_HWADDRHINT find_entry(struct ip_addr *ipaddr, u8_t flags, struct netif *netif) #else /* LWIP_NETIF_HWADDRHINT */ find_entry(struct ip_addr *ipaddr, u8_t flags) #endif /* LWIP_NETIF_HWADDRHINT */ { s8_t old_pending = ARP_TABLE_SIZE, old_stable = ARP_TABLE_SIZE; s8_t empty = ARP_TABLE_SIZE; u8_t i = 0, age_pending = 0, age_stable = 0; #if ARP_QUEUEING /* oldest entry with packets on queue */ s8_t old_queue = ARP_TABLE_SIZE; /* its age */ u8_t age_queue = 0; #endif /* First, test if the last call to this function asked for the * same address. If so, we're really fast! */ if (ipaddr) { /* ipaddr to search for was given */ #if LWIP_NETIF_HWADDRHINT if ((netif != NULL) && (netif->addr_hint != NULL)) { /* per-pcb cached entry was given */ u8_t per_pcb_cache = *(netif->addr_hint); if ((per_pcb_cache < ARP_TABLE_SIZE) && arp_table[per_pcb_cache].state == ETHARP_STATE_STABLE) { /* the per-pcb-cached entry is stable */ if (ip_addr_cmp(ipaddr, &arp_table[per_pcb_cache].ipaddr)) { /* per-pcb cached entry was the right one! */ ETHARP_STATS_INC(etharp.cachehit); return per_pcb_cache; } } } #else /* #if LWIP_NETIF_HWADDRHINT */ if (arp_table[etharp_cached_entry].state == ETHARP_STATE_STABLE) { /* the cached entry is stable */ if (ip_addr_cmp(ipaddr, &arp_table[etharp_cached_entry].ipaddr)) { /* cached entry was the right one! */ ETHARP_STATS_INC(etharp.cachehit); return etharp_cached_entry; } } #endif /* #if LWIP_NETIF_HWADDRHINT */ } /** * a) do a search through the cache, remember candidates * b) select candidate entry * c) create new entry */ /* a) in a single search sweep, do all of this * 1) remember the first empty entry (if any) * 2) remember the oldest stable entry (if any) * 3) remember the oldest pending entry without queued packets (if any) * 4) remember the oldest pending entry with queued packets (if any) * 5) search for a matching IP entry, either pending or stable * until 5 matches, or all entries are searched for. */ for (i = 0; i < ARP_TABLE_SIZE; ++i) { /* no empty entry found yet and now we do find one? */ if ((empty == ARP_TABLE_SIZE) && (arp_table[i].state == ETHARP_STATE_EMPTY)) { LWIP_DEBUGF(ETHARP_DEBUG, ("find_entry: found empty entry %"U16_F"\n", (u16_t)i)); /* remember first empty entry */ empty = i; } /* pending entry? */ else if (arp_table[i].state == ETHARP_STATE_PENDING) { /* if given, does IP address match IP address in ARP entry? */ if (ipaddr && ip_addr_cmp(ipaddr, &arp_table[i].ipaddr)) { LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: found matching pending entry %"U16_F"\n", (u16_t)i)); /* found exact IP address match, simply bail out */ #if LWIP_NETIF_HWADDRHINT NETIF_SET_HINT(netif, i); #else /* #if LWIP_NETIF_HWADDRHINT */ etharp_cached_entry = i; #endif /* #if LWIP_NETIF_HWADDRHINT */ return i; #if ARP_QUEUEING /* pending with queued packets? */ } else if (arp_table[i].q != NULL) { if (arp_table[i].ctime >= age_queue) { old_queue = i; age_queue = arp_table[i].ctime; } #endif /* pending without queued packets? */ } else { if (arp_table[i].ctime >= age_pending) { old_pending = i; age_pending = arp_table[i].ctime; } } } /* stable entry? */ else if (arp_table[i].state == ETHARP_STATE_STABLE) { /* if given, does IP address match IP address in ARP entry? */ if (ipaddr && ip_addr_cmp(ipaddr, &arp_table[i].ipaddr)) { LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: found matching stable entry %"U16_F"\n", (u16_t)i)); /* found exact IP address match, simply bail out */ #if LWIP_NETIF_HWADDRHINT NETIF_SET_HINT(netif, i); #else /* #if LWIP_NETIF_HWADDRHINT */ etharp_cached_entry = i; #endif /* #if LWIP_NETIF_HWADDRHINT */ return i; /* remember entry with oldest stable entry in oldest, its age in maxtime */ } else if (arp_table[i].ctime >= age_stable) { old_stable = i; age_stable = arp_table[i].ctime; } } } /* { we have no match } => try to create a new entry */ /* no empty entry found and not allowed to recycle? */ if (((empty == ARP_TABLE_SIZE) && ((flags & ETHARP_TRY_HARD) == 0)) /* or don't create new entry, only search? */ || ((flags & ETHARP_FIND_ONLY) != 0)) { LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: no empty entry found and not allowed to recycle\n")); return (s8_t)ERR_MEM; } /* b) choose the least destructive entry to recycle: * 1) empty entry * 2) oldest stable entry * 3) oldest pending entry without queued packets * 4) oldest pending entry with queued packets * * { ETHARP_TRY_HARD is set at this point } */ /* 1) empty entry available? */ if (empty < ARP_TABLE_SIZE) { i = empty; LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: selecting empty entry %"U16_F"\n", (u16_t)i)); } /* 2) found recyclable stable entry? */ else if (old_stable < ARP_TABLE_SIZE) { /* recycle oldest stable*/ i = old_stable; LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: selecting oldest stable entry %"U16_F"\n", (u16_t)i)); #if ARP_QUEUEING /* no queued packets should exist on stable entries */ LWIP_ASSERT("arp_table[i].q == NULL", arp_table[i].q == NULL); #endif /* 3) found recyclable pending entry without queued packets? */ } else if (old_pending < ARP_TABLE_SIZE) { /* recycle oldest pending */ i = old_pending; LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: selecting oldest pending entry %"U16_F" (without queue)\n", (u16_t)i)); #if ARP_QUEUEING /* 4) found recyclable pending entry with queued packets? */ } else if (old_queue < ARP_TABLE_SIZE) { /* recycle oldest pending */ i = old_queue; LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: selecting oldest pending entry %"U16_F", freeing packet queue %p\n", (u16_t)i, (void *)(arp_table[i].q))); free_etharp_q(arp_table[i].q); arp_table[i].q = NULL; #endif /* no empty or recyclable entries found */ } else { return (s8_t)ERR_MEM; } /* { empty or recyclable entry found } */ LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE); if (arp_table[i].state != ETHARP_STATE_EMPTY) { snmp_delete_arpidx_tree(arp_table[i].netif, &arp_table[i].ipaddr); } /* recycle entry (no-op for an already empty entry) */ arp_table[i].state = ETHARP_STATE_EMPTY; /* IP address given? */ if (ipaddr != NULL) { /* set IP address */ ip_addr_set(&arp_table[i].ipaddr, ipaddr); } arp_table[i].ctime = 0; #if LWIP_NETIF_HWADDRHINT NETIF_SET_HINT(netif, i); #else /* #if LWIP_NETIF_HWADDRHINT */ etharp_cached_entry = i; #endif /* #if LWIP_NETIF_HWADDRHINT */ return (err_t)i; }