tstest/natlab/vnet: rename some things for clarity

The bad naming (which had only been half updated with the IPv6
changes) tripped me up in the earlier change.

Updates #13038

Change-Id: I65ce07c167e8219d35b87e1f4bf61aab4cac31ff
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2024-08-25 20:50:22 -07:00 committed by Brad Fitzpatrick
parent 0157000cab
commit f99f970dc1
2 changed files with 17 additions and 15 deletions

View File

@ -328,7 +328,7 @@ func (s *Server) initFromConfig(c *Config) error {
v6: conf.wanIP6.IsValid(), v6: conf.wanIP6.IsValid(),
wanIP4: conf.wanIP4, wanIP4: conf.wanIP4,
lanIP4: conf.lanIP4, lanIP4: conf.lanIP4,
nodesByIP: map[netip.Addr]*node{}, nodesByIP4: map[netip.Addr]*node{},
nodesByMAC: map[MAC]*node{}, nodesByMAC: map[MAC]*node{},
logf: logger.WithPrefix(log.Printf, fmt.Sprintf("[net-%v] ", conf.mac)), logf: logger.WithPrefix(log.Printf, fmt.Sprintf("[net-%v] ", conf.mac)),
} }
@ -389,7 +389,7 @@ func (s *Server) initFromConfig(c *Config) error {
ip4 := n.net.lanIP4.Addr().As4() ip4 := n.net.lanIP4.Addr().As4()
ip4[3] = 100 + n.mac[5] ip4[3] = 100 + n.mac[5]
n.lanIP = netip.AddrFrom4(ip4) n.lanIP = netip.AddrFrom4(ip4)
n.net.nodesByIP[n.lanIP] = n n.net.nodesByIP4[n.lanIP] = n
} }
n.net.nodesByMAC[n.mac] = n n.net.nodesByMAC[n.mac] = n
} }

View File

@ -116,10 +116,10 @@ func (n *network) setNATTable(nt NATTable) {
// SoleLANIP implements [IPPool]. // SoleLANIP implements [IPPool].
func (n *network) SoleLANIP() (netip.Addr, bool) { func (n *network) SoleLANIP() (netip.Addr, bool) {
if len(n.nodesByIP) != 1 { if len(n.nodesByIP4) != 1 {
return netip.Addr{}, false return netip.Addr{}, false
} }
for ip := range n.nodesByIP { for ip := range n.nodesByIP4 {
return ip, true return ip, true
} }
return netip.Addr{}, false return netip.Addr{}, false
@ -240,7 +240,7 @@ func (n *network) handleIPPacketFromGvisor(ipRaw []byte) {
if !ok { if !ok {
panic("unexpected gvisor packet") panic("unexpected gvisor packet")
} }
node, ok := n.nodeForDestIP(flow.dst) node, ok := n.nodeByIP(flow.dst)
if !ok { if !ok {
n.logf("no node for netstack dest IP %v", flow.dst) n.logf("no node for netstack dest IP %v", flow.dst)
return return
@ -327,7 +327,7 @@ func (n *network) acceptTCP(r *tcp.ForwarderRequest) {
} }
if destPort == 8008 && fakeTestAgent.Match(destIP) { if destPort == 8008 && fakeTestAgent.Match(destIP) {
node, ok := n.nodeForDestIP(clientRemoteIP) node, ok := n.nodeByIP(clientRemoteIP)
if !ok { if !ok {
n.logf("unknown client IP %v trying to connect to test driver", clientRemoteIP) n.logf("unknown client IP %v trying to connect to test driver", clientRemoteIP)
r.Complete(true) r.Complete(true)
@ -433,7 +433,7 @@ func (n *network) serveLogCatcherConn(clientRemoteIP netip.Addr, c net.Conn) {
log.Printf("Logs decode error: %v", err) log.Printf("Logs decode error: %v", err)
return return
} }
node := n.nodesByIP[clientRemoteIP] node := n.nodesByIP4[clientRemoteIP]
if node != nil { if node != nil {
node.logMu.Lock() node.logMu.Lock()
defer node.logMu.Unlock() defer node.logMu.Unlock()
@ -519,7 +519,7 @@ type network struct {
wanIP6 netip.Prefix // router's WAN IPv6, if any, as a /64. wanIP6 netip.Prefix // router's WAN IPv6, if any, as a /64.
wanIP4 netip.Addr // router's LAN IPv4, if any wanIP4 netip.Addr // router's LAN IPv4, if any
lanIP4 netip.Prefix // router's LAN IP + CIDR (e.g. 192.168.2.1/24) lanIP4 netip.Prefix // router's LAN IP + CIDR (e.g. 192.168.2.1/24)
nodesByIP map[netip.Addr]*node // by LAN IPv4 nodesByIP4 map[netip.Addr]*node // by LAN IPv4
nodesByMAC map[MAC]*node nodesByMAC map[MAC]*node
logf func(format string, args ...any) logf func(format string, args ...any)
@ -559,7 +559,7 @@ func (n *network) MACOfIP(ip netip.Addr) (_ MAC, ok bool) {
if n.lanIP4.Addr() == ip { if n.lanIP4.Addr() == ip {
return n.mac, true return n.mac, true
} }
if n, ok := n.nodesByIP[ip]; ok { if n, ok := n.nodesByIP4[ip]; ok {
return n.mac, true return n.mac, true
} }
return MAC{}, false return MAC{}, false
@ -1036,20 +1036,22 @@ func (n *network) HandleUDPPacket(p UDPPacket) {
n.WriteUDPPacketNoNAT(p) n.WriteUDPPacketNoNAT(p)
} }
func (n *network) nodeForDestIP(ip netip.Addr) (node *node, ok bool) { func (n *network) nodeByIP(ip netip.Addr) (node *node, ok bool) {
node, ok = n.nodesByIP[ip] if ip.Is4() {
node, ok = n.nodesByIP4[ip]
}
if !ok && ip.Is6() { if !ok && ip.Is6() {
var mac MAC var mac MAC
n.macMu.Lock() n.macMu.Lock()
mac, ok = n.macOfIPv6[ip] mac, ok = n.macOfIPv6[ip]
n.macMu.Unlock() n.macMu.Unlock()
if !ok { if !ok {
log.Printf("XXX no MAC for IPv6 %v", ip) log.Printf("warning: no known MAC for IPv6 %v", ip)
return nil, false return nil, false
} }
node, ok = n.nodesByMAC[mac] node, ok = n.nodesByMAC[mac]
if !ok { if !ok {
log.Printf("XXX no node for MAC %v", mac) log.Printf("warning: no known node for MAC %v (IP %v)", mac, ip)
} }
} }
return node, ok return node, ok
@ -1063,7 +1065,7 @@ func (n *network) nodeForDestIP(ip netip.Addr) (node *node, ok bool) {
// same ethernet segment. // same ethernet segment.
func (n *network) WriteUDPPacketNoNAT(p UDPPacket) { func (n *network) WriteUDPPacketNoNAT(p UDPPacket) {
src, dst := p.Src, p.Dst src, dst := p.Src, p.Dst
node, ok := n.nodeForDestIP(dst.Addr()) node, ok := n.nodeByIP(dst.Addr())
if !ok { if !ok {
n.logf("no node for dest IP %v in UDP packet %v=>%v", dst.Addr(), p.Src, p.Dst) n.logf("no node for dest IP %v in UDP packet %v=>%v", dst.Addr(), p.Src, p.Dst)
return return
@ -1220,7 +1222,7 @@ func (n *network) handleUDPPacketForRouter(ep EthernetPacket, udp *layers.UDP, t
} }
if fakeSyslog.Match(dstIP) { if fakeSyslog.Match(dstIP) {
node, ok := n.nodeForDestIP(srcIP) node, ok := n.nodeByIP(srcIP)
if !ok { if !ok {
return return
} }