net/netmon: report disappeared interface IPs (#20621)

InterfaceIPDisappeared is documented as true when an address existed in the
old network state and is absent from the new network state. The
non-nil-state predicate was reversed, so it reported addresses that
appeared instead.

Fix the predicate and add table coverage for disappeared, appeared,
unchanged, and unknown-state cases.

Fixes #20614

Change-Id: Ieb7a8c5e13d23e868e3ffe0b99e20fb14ba2245e

Signed-off-by: cyphercodes <[email protected]>
Co-authored-by: cyphercodes <[email protected]>
(cherry picked from commit 5927c1864f)
This commit is contained in:
Rayan Salhab authored and Brad Fitzpatrick committed 2026-07-30 10:02:56 -07:00
1 parent d9946f5742
commit d718795f81
2 files changed
+63 -1

No files matched your search

+1 -1
View File
@@ -254,7 +254,7 @@ func (cd *ChangeDelta) InterfaceIPDisappeared(ip netip.Addr) bool {
if cd.new == nil && cd.old.HasIP(ip) {
return true
}
return cd.new.HasIP(ip) && !cd.old.HasIP(ip)
return cd.old.HasIP(ip) && !cd.new.HasIP(ip)
}
// AnyInterfaceUp reports whether any interfaces are up in the new state.
+62
View File
@@ -157,6 +157,68 @@ func TestMonitorMode(t *testing.T) {
}
}
func TestInterfaceIPDisappeared(t *testing.T) {
ip := netip.MustParseAddr("192.0.2.1")
stateWithIP := func(ip netip.Addr) *State {
return &State{InterfaceIPs: map[string][]netip.Prefix{
"eth0": {netip.PrefixFrom(ip, ip.BitLen())},
}}
}
stateWithoutIP := func() *State {
return &State{InterfaceIPs: map[string][]netip.Prefix{
"eth0": {netip.MustParsePrefix("198.51.100.1/32")},
}}
}
tests := []struct {
name string
old *State
new *State
want bool
}{
{
name: "initial_state",
new: stateWithIP(ip),
},
{
name: "disappeared",
old: stateWithIP(ip),
new: stateWithoutIP(),
want: true,
},
{
name: "unchanged_present",
old: stateWithIP(ip),
new: stateWithIP(ip),
},
{
name: "appeared",
old: stateWithoutIP(),
new: stateWithIP(ip),
},
{
name: "unchanged_absent",
old: stateWithoutIP(),
new: stateWithoutIP(),
},
{
name: "new_unknown",
old: stateWithIP(ip),
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cd := &ChangeDelta{old: tt.old, new: tt.new}
if got := cd.InterfaceIPDisappeared(ip); got != tt.want {
t.Fatalf("InterfaceIPDisappeared(%v) = %v; want %v", ip, got, tt.want)
}
})
}
}
// tests (*ChangeDelta).RebindRequired
func TestRebindRequired(t *testing.T) {
// s1 must not be nil by definition