tstest/natlab: make Machine constructible directly.
This is a prelude to adding more fields, which would otherwise become more unnamed function params. Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:

committed by
Dave Anderson

parent
d94593e884
commit
73db7e99ab
@ -141,7 +141,7 @@ func (n *Network) write(p []byte, dst, src netaddr.IPPort) (num int, err error)
|
|||||||
|
|
||||||
// Pretend it went across the network. Make a copy so nobody
|
// Pretend it went across the network. Make a copy so nobody
|
||||||
// can later mess with caller's memory.
|
// can later mess with caller's memory.
|
||||||
trace(p, "net=%s src=%v dst=%v -> mach=%s", n.Name, src, dst, m.name)
|
trace(p, "net=%s src=%v dst=%v -> mach=%s", n.Name, src, dst, m.Name)
|
||||||
pcopy := append([]byte(nil), p...)
|
pcopy := append([]byte(nil), p...)
|
||||||
go m.deliverIncomingPacket(pcopy, dst, src)
|
go m.deliverIncomingPacket(pcopy, dst, src)
|
||||||
return len(p), nil
|
return len(p), nil
|
||||||
@ -191,17 +191,12 @@ type routeEntry struct {
|
|||||||
iface *Interface
|
iface *Interface
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMachine returns a new Machine without any network connection.
|
|
||||||
// The name is just for debugging and need not be globally unique.
|
|
||||||
// Use Attach to add networks.
|
|
||||||
func NewMachine(name string) *Machine {
|
|
||||||
return &Machine{name: name}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A Machine is a representation of an operating system's network stack.
|
// A Machine is a representation of an operating system's network stack.
|
||||||
// It has a network routing table and can have multiple attached networks.
|
// It has a network routing table and can have multiple attached networks.
|
||||||
type Machine struct {
|
type Machine struct {
|
||||||
name string
|
// Name is a pretty name for debugging and packet tracing. It need
|
||||||
|
// not be globally unique.
|
||||||
|
Name string
|
||||||
|
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
interfaces []*Interface
|
interfaces []*Interface
|
||||||
@ -231,14 +226,14 @@ func (m *Machine) deliverIncomingPacket(p []byte, dst, src netaddr.IPPort) {
|
|||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
case c.in <- incomingPacket{src: src, p: p}:
|
case c.in <- incomingPacket{src: src, p: p}:
|
||||||
trace(p, "mach=%s src=%v dst=%v queued to conn", m.name, src, dst)
|
trace(p, "mach=%s src=%v dst=%v queued to conn", m.Name, src, dst)
|
||||||
default:
|
default:
|
||||||
trace(p, "mach=%s src=%v dst=%v dropped, queue overflow", m.name, src, dst)
|
trace(p, "mach=%s src=%v dst=%v dropped, queue overflow", m.Name, src, dst)
|
||||||
// Queue overflow. Just drop it.
|
// Queue overflow. Just drop it.
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
trace(p, "mach=%s src=%v dst=%v dropped, no listening conn", m.name, src, dst)
|
trace(p, "mach=%s src=%v dst=%v dropped, no listening conn", m.Name, src, dst)
|
||||||
}
|
}
|
||||||
|
|
||||||
func unspecOf(ip netaddr.IP) netaddr.IP {
|
func unspecOf(ip netaddr.IP) netaddr.IP {
|
||||||
@ -337,7 +332,7 @@ func (m *Machine) writePacket(p []byte, dst, src netaddr.IPPort) (n int, err err
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
trace(p, "mach=%s src=%s dst=%s -> net=%s", m.name, src, dst, iface.net.Name)
|
trace(p, "mach=%s src=%s dst=%s -> net=%s", m.Name, src, dst, iface.net.Name)
|
||||||
return iface.net.write(p, dst, src)
|
return iface.net.write(p, dst, src)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,8 +41,8 @@ func TestAllocIPs(t *testing.T) {
|
|||||||
func TestSendPacket(t *testing.T) {
|
func TestSendPacket(t *testing.T) {
|
||||||
internet := NewInternet()
|
internet := NewInternet()
|
||||||
|
|
||||||
foo := NewMachine("foo")
|
foo := &Machine{Name: "foo"}
|
||||||
bar := NewMachine("bar")
|
bar := &Machine{Name: "bar"}
|
||||||
ifFoo := foo.Attach("eth0", internet)
|
ifFoo := foo.Attach("eth0", internet)
|
||||||
ifBar := bar.Attach("enp0s1", internet)
|
ifBar := bar.Attach("enp0s1", internet)
|
||||||
|
|
||||||
@ -84,9 +84,9 @@ func TestMultiNetwork(t *testing.T) {
|
|||||||
}
|
}
|
||||||
internet := NewInternet()
|
internet := NewInternet()
|
||||||
|
|
||||||
client := NewMachine("client")
|
client := &Machine{Name: "client"}
|
||||||
nat := NewMachine("nat")
|
nat := &Machine{Name: "nat"}
|
||||||
server := NewMachine("server")
|
server := &Machine{Name: "server"}
|
||||||
|
|
||||||
ifClient := client.Attach("eth0", lan)
|
ifClient := client.Attach("eth0", lan)
|
||||||
ifNATWAN := nat.Attach("ethwan", internet)
|
ifNATWAN := nat.Attach("ethwan", internet)
|
||||||
|
Reference in New Issue
Block a user