|
| 1 | +use wire::EthernetAddress; |
| 2 | +use super::ProtocolAddress; |
| 3 | + |
| 4 | +/// An Address Resolution Protocol cache. |
| 5 | +/// |
| 6 | +/// This cache maps protocol addresses to hardware addresses. |
| 7 | +pub trait Cache { |
| 8 | + /// Update the cache to map given protocol address to given hardware address. |
| 9 | + fn fill(&mut self, protocol_addr: ProtocolAddress, hardware_addr: EthernetAddress); |
| 10 | + |
| 11 | + /// Look up the hardware address corresponding for the given protocol address. |
| 12 | + fn lookup(&mut self, protocol_addr: ProtocolAddress) -> Option<EthernetAddress>; |
| 13 | +} |
| 14 | + |
| 15 | +/// An Address Resolution Protocol cache backed by a slice. |
| 16 | +/// |
| 17 | +/// This cache uses a fixed-size storage, binary search, and a least recently used |
| 18 | +/// eviction strategy. |
| 19 | +/// |
| 20 | +/// # Examples |
| 21 | +/// This cache can be created as: |
| 22 | +/// |
| 23 | +/// ```rust |
| 24 | +/// use smoltcp::iface::SliceArpCache; |
| 25 | +/// let mut arp_cache_storage = [Default::default(); 8]; |
| 26 | +/// let mut arp_cache = SliceArpCache::new(&mut arp_cache_storage); |
| 27 | +/// ``` |
| 28 | +pub struct SliceCache<'a> { |
| 29 | + storage: &'a mut [(ProtocolAddress, EthernetAddress, usize)], |
| 30 | + counter: usize |
| 31 | +} |
| 32 | + |
| 33 | +impl<'a> SliceCache<'a> { |
| 34 | + /// Create a cache. The backing storage is cleared upon creation. |
| 35 | + /// |
| 36 | + /// # Panics |
| 37 | + /// This function panics if `storage.len() == 0`. |
| 38 | + pub fn new(storage: &'a mut [(ProtocolAddress, EthernetAddress, usize)]) -> SliceCache<'a> { |
| 39 | + if storage.len() == 0 { |
| 40 | + panic!("ARP slice cache created with empty storage") |
| 41 | + } |
| 42 | + |
| 43 | + for elem in storage.iter_mut() { |
| 44 | + *elem = Default::default() |
| 45 | + } |
| 46 | + SliceCache { |
| 47 | + storage: storage, |
| 48 | + counter: 0 |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /// Find an entry for the given protocol address, if any. |
| 53 | + fn find(&self, protocol_addr: ProtocolAddress) -> Option<usize> { |
| 54 | + // The order of comparison is important: any valid ProtocolAddress should |
| 55 | + // sort before ProtocolAddress::Invalid. |
| 56 | + self.storage.binary_search_by_key(&protocol_addr, |&(key, _, _)| key).ok() |
| 57 | + } |
| 58 | + |
| 59 | + /// Sort entries in an order suitable for `find`. |
| 60 | + fn sort(&mut self) { |
| 61 | + self.storage.sort_by_key(|&(key, _, _)| key) |
| 62 | + } |
| 63 | + |
| 64 | + /// Find the least recently used entry. |
| 65 | + fn lru(&self) -> usize { |
| 66 | + self.storage.iter().enumerate().min_by_key(|&(_, &(_, _, counter))| counter).unwrap().0 |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl<'a> Cache for SliceCache<'a> { |
| 71 | + fn fill(&mut self, protocol_addr: ProtocolAddress, hardware_addr: EthernetAddress) { |
| 72 | + if let None = self.find(protocol_addr) { |
| 73 | + self.storage[self.lru()] = (protocol_addr, hardware_addr, self.counter); |
| 74 | + self.sort() |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + fn lookup(&mut self, protocol_addr: ProtocolAddress) -> Option<EthernetAddress> { |
| 79 | + if let Some(index) = self.find(protocol_addr) { |
| 80 | + let (_protocol_addr, hardware_addr, ref mut counter) = self.storage[index]; |
| 81 | + self.counter += 1; |
| 82 | + *counter = self.counter; |
| 83 | + Some(hardware_addr) |
| 84 | + } else { |
| 85 | + None |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +#[cfg(test)] |
| 91 | +mod test { |
| 92 | + use super::*; |
| 93 | + |
| 94 | + const HADDR_A: EthernetAddress = EthernetAddress([0, 0, 0, 0, 0, 1]); |
| 95 | + const HADDR_B: EthernetAddress = EthernetAddress([0, 0, 0, 0, 0, 2]); |
| 96 | + const HADDR_C: EthernetAddress = EthernetAddress([0, 0, 0, 0, 0, 3]); |
| 97 | + const HADDR_D: EthernetAddress = EthernetAddress([0, 0, 0, 0, 0, 4]); |
| 98 | + |
| 99 | + const PADDR_A: ProtocolAddress = ProtocolAddress::ipv4([0, 0, 0, 0]); |
| 100 | + const PADDR_B: ProtocolAddress = ProtocolAddress::ipv4([0, 0, 0, 1]); |
| 101 | + const PADDR_C: ProtocolAddress = ProtocolAddress::ipv4([0, 0, 0, 2]); |
| 102 | + const PADDR_D: ProtocolAddress = ProtocolAddress::ipv4([0, 0, 0, 3]); |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn test_slice_cache() { |
| 106 | + let mut cache_storage = [Default::default(); 3]; |
| 107 | + let mut cache = SliceCache::new(&mut cache_storage); |
| 108 | + |
| 109 | + cache.fill(PADDR_A, HADDR_A); |
| 110 | + assert_eq!(cache.lookup(PADDR_A), Some(HADDR_A)); |
| 111 | + assert_eq!(cache.lookup(PADDR_B), None); |
| 112 | + |
| 113 | + cache.fill(PADDR_B, HADDR_B); |
| 114 | + cache.fill(PADDR_C, HADDR_C); |
| 115 | + assert_eq!(cache.lookup(PADDR_A), Some(HADDR_A)); |
| 116 | + assert_eq!(cache.lookup(PADDR_B), Some(HADDR_B)); |
| 117 | + assert_eq!(cache.lookup(PADDR_C), Some(HADDR_C)); |
| 118 | + |
| 119 | + cache.lookup(PADDR_B); |
| 120 | + cache.lookup(PADDR_A); |
| 121 | + cache.lookup(PADDR_C); |
| 122 | + cache.fill(PADDR_D, HADDR_D); |
| 123 | + assert_eq!(cache.lookup(PADDR_A), Some(HADDR_A)); |
| 124 | + assert_eq!(cache.lookup(PADDR_B), None); |
| 125 | + assert_eq!(cache.lookup(PADDR_C), Some(HADDR_C)); |
| 126 | + assert_eq!(cache.lookup(PADDR_D), Some(HADDR_D)); |
| 127 | + } |
| 128 | +} |
0 commit comments