| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package stack |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
|
| 20 |
"inet.af/netstack/sync" |
| 21 |
"inet.af/netstack/tcpip" |
| 22 |
) |
| 23 |
|
| 24 |
const neighborCacheSize = 512 |
| 25 |
|
| 26 |
|
| 27 |
type NeighborStats struct { |
| 28 |
|
| 29 |
|
| 30 |
UnreachableEntryLookups *tcpip.StatCounter |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
type neighborCache struct { |
| 46 |
nic *nic |
| 47 |
state *NUDState |
| 48 |
linkRes LinkAddressResolver |
| 49 |
|
| 50 |
mu struct { |
| 51 |
sync.RWMutex |
| 52 |
|
| 53 |
cache map[tcpip.Address]*neighborEntry |
| 54 |
dynamic struct { |
| 55 |
lru neighborEntryList |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
count uint16 |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
func (n *neighborCache) getOrCreateEntry(remoteAddr tcpip.Address) *neighborEntry { |
| 75 |
n.mu.Lock() |
| 76 |
defer n.mu.Unlock() |
| 77 |
|
| 78 |
if entry, ok := n.mu.cache[remoteAddr]; ok { |
| 79 |
entry.mu.RLock() |
| 80 |
if entry.mu.neigh.State != Static { |
| 81 |
n.mu.dynamic.lru.Remove(entry) |
| 82 |
n.mu.dynamic.lru.PushFront(entry) |
| 83 |
} |
| 84 |
entry.mu.RUnlock() |
| 85 |
return entry |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
entry := newNeighborEntry(n, remoteAddr, n.state) |
| 91 |
if n.mu.dynamic.count == neighborCacheSize { |
| 92 |
e := n.mu.dynamic.lru.Back() |
| 93 |
e.mu.Lock() |
| 94 |
|
| 95 |
delete(n.mu.cache, e.mu.neigh.Addr) |
| 96 |
n.mu.dynamic.lru.Remove(e) |
| 97 |
n.mu.dynamic.count-- |
| 98 |
|
| 99 |
e.removeLocked() |
| 100 |
e.mu.Unlock() |
| 101 |
} |
| 102 |
n.mu.cache[remoteAddr] = entry |
| 103 |
n.mu.dynamic.lru.PushFront(entry) |
| 104 |
n.mu.dynamic.count++ |
| 105 |
return entry |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
func (n *neighborCache) entry(remoteAddr, localAddr tcpip.Address, onResolve func(LinkResolutionResult)) (NeighborEntry, <-chan struct{}, tcpip.Error) { |
| 128 |
entry := n.getOrCreateEntry(remoteAddr) |
| 129 |
entry.mu.Lock() |
| 130 |
defer entry.mu.Unlock() |
| 131 |
|
| 132 |
switch s := entry.mu.neigh.State; s { |
| 133 |
case Stale: |
| 134 |
entry.handlePacketQueuedLocked(localAddr) |
| 135 |
fallthrough |
| 136 |
case Reachable, Static, Delay, Probe: |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
if onResolve != nil { |
| 143 |
onResolve(LinkResolutionResult{LinkAddress: entry.mu.neigh.LinkAddr, Err: nil}) |
| 144 |
} |
| 145 |
return entry.mu.neigh, nil, nil |
| 146 |
case Unknown, Incomplete, Unreachable: |
| 147 |
if onResolve != nil { |
| 148 |
entry.mu.onResolve = append(entry.mu.onResolve, onResolve) |
| 149 |
} |
| 150 |
if entry.mu.done == nil { |
| 151 |
|
| 152 |
entry.mu.done = make(chan struct{}) |
| 153 |
} |
| 154 |
entry.handlePacketQueuedLocked(localAddr) |
| 155 |
return entry.mu.neigh, entry.mu.done, &tcpip.ErrWouldBlock{} |
| 156 |
default: |
| 157 |
panic(fmt.Sprintf("Invalid cache entry state: %s", s)) |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
func (n *neighborCache) entries() []NeighborEntry { |
| 163 |
n.mu.RLock() |
| 164 |
defer n.mu.RUnlock() |
| 165 |
|
| 166 |
entries := make([]NeighborEntry, 0, len(n.mu.cache)) |
| 167 |
for _, entry := range n.mu.cache { |
| 168 |
entry.mu.RLock() |
| 169 |
entries = append(entries, entry.mu.neigh) |
| 170 |
entry.mu.RUnlock() |
| 171 |
} |
| 172 |
return entries |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
func (n *neighborCache) addStaticEntry(addr tcpip.Address, linkAddr tcpip.LinkAddress) { |
| 182 |
n.mu.Lock() |
| 183 |
defer n.mu.Unlock() |
| 184 |
|
| 185 |
if entry, ok := n.mu.cache[addr]; ok { |
| 186 |
entry.mu.Lock() |
| 187 |
if entry.mu.neigh.State != Static { |
| 188 |
|
| 189 |
n.mu.dynamic.lru.Remove(entry) |
| 190 |
n.mu.dynamic.count-- |
| 191 |
} else if entry.mu.neigh.LinkAddr == linkAddr { |
| 192 |
|
| 193 |
entry.mu.Unlock() |
| 194 |
return |
| 195 |
} else { |
| 196 |
|
| 197 |
entry.mu.neigh.LinkAddr = linkAddr |
| 198 |
entry.dispatchChangeEventLocked() |
| 199 |
entry.mu.Unlock() |
| 200 |
return |
| 201 |
} |
| 202 |
|
| 203 |
entry.removeLocked() |
| 204 |
entry.mu.Unlock() |
| 205 |
} |
| 206 |
|
| 207 |
entry := newStaticNeighborEntry(n, addr, linkAddr, n.state) |
| 208 |
n.mu.cache[addr] = entry |
| 209 |
|
| 210 |
entry.mu.Lock() |
| 211 |
defer entry.mu.Unlock() |
| 212 |
entry.dispatchAddEventLocked() |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
func (n *neighborCache) removeEntry(addr tcpip.Address) bool { |
| 218 |
n.mu.Lock() |
| 219 |
defer n.mu.Unlock() |
| 220 |
|
| 221 |
entry, ok := n.mu.cache[addr] |
| 222 |
if !ok { |
| 223 |
return false |
| 224 |
} |
| 225 |
|
| 226 |
entry.mu.Lock() |
| 227 |
defer entry.mu.Unlock() |
| 228 |
|
| 229 |
if entry.mu.neigh.State != Static { |
| 230 |
n.mu.dynamic.lru.Remove(entry) |
| 231 |
n.mu.dynamic.count-- |
| 232 |
} |
| 233 |
|
| 234 |
entry.removeLocked() |
| 235 |
delete(n.mu.cache, entry.mu.neigh.Addr) |
| 236 |
return true |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
func (n *neighborCache) clear() { |
| 241 |
n.mu.Lock() |
| 242 |
defer n.mu.Unlock() |
| 243 |
|
| 244 |
for _, entry := range n.mu.cache { |
| 245 |
entry.mu.Lock() |
| 246 |
entry.removeLocked() |
| 247 |
entry.mu.Unlock() |
| 248 |
} |
| 249 |
|
| 250 |
n.mu.dynamic.lru = neighborEntryList{} |
| 251 |
n.mu.cache = make(map[tcpip.Address]*neighborEntry) |
| 252 |
n.mu.dynamic.count = 0 |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
func (n *neighborCache) config() NUDConfigurations { |
| 257 |
return n.state.Config() |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
func (n *neighborCache) setConfig(config NUDConfigurations) { |
| 265 |
config.resetInvalidFields() |
| 266 |
n.state.SetConfig(config) |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
func (n *neighborCache) handleProbe(remoteAddr tcpip.Address, remoteLinkAddr tcpip.LinkAddress) { |
| 273 |
entry := n.getOrCreateEntry(remoteAddr) |
| 274 |
entry.mu.Lock() |
| 275 |
entry.handleProbeLocked(remoteLinkAddr) |
| 276 |
entry.mu.Unlock() |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
func (n *neighborCache) handleConfirmation(addr tcpip.Address, linkAddr tcpip.LinkAddress, flags ReachabilityConfirmationFlags) { |
| 284 |
n.mu.RLock() |
| 285 |
entry, ok := n.mu.cache[addr] |
| 286 |
n.mu.RUnlock() |
| 287 |
if ok { |
| 288 |
entry.mu.Lock() |
| 289 |
entry.handleConfirmationLocked(linkAddr, flags) |
| 290 |
entry.mu.Unlock() |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
func (n *neighborCache) handleUpperLevelConfirmation(addr tcpip.Address) { |
| 300 |
n.mu.RLock() |
| 301 |
entry, ok := n.mu.cache[addr] |
| 302 |
n.mu.RUnlock() |
| 303 |
if ok { |
| 304 |
entry.mu.Lock() |
| 305 |
entry.handleUpperLevelConfirmationLocked() |
| 306 |
entry.mu.Unlock() |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
func (n *neighborCache) init(nic *nic, r LinkAddressResolver) { |
| 311 |
*n = neighborCache{ |
| 312 |
nic: nic, |
| 313 |
state: NewNUDState(nic.stack.nudConfigs, nic.stack.clock, nic.stack.randomGenerator), |
| 314 |
linkRes: r, |
| 315 |
} |
| 316 |
n.mu.Lock() |
| 317 |
n.mu.cache = make(map[tcpip.Address]*neighborEntry, neighborCacheSize) |
| 318 |
n.mu.Unlock() |
| 319 |
} |