From 771eb05bcb53b27fdcb8882dd462989053162e60 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Fri, 3 Jul 2020 01:02:38 +0000 Subject: [PATCH] tstest/natlab: first network attached becomes the default route. Signed-off-by: David Anderson --- tstest/natlab/natlab.go | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/tstest/natlab/natlab.go b/tstest/natlab/natlab.go index 73f9ec400..a377c3bae 100644 --- a/tstest/natlab/natlab.go +++ b/tstest/natlab/natlab.go @@ -37,10 +37,6 @@ func NewInternet() *Network { Name: "internet", Prefix4: mustPrefix("203.0.113.0/24"), // documentation netblock that looks Internet-y Prefix6: mustPrefix("fc00:52::/64"), - pushRoutes: []netaddr.IPPrefix{ - mustPrefix("0.0.0.0/0"), - mustPrefix("::/0"), - }, } } @@ -49,8 +45,6 @@ type Network struct { Prefix4 netaddr.IPPrefix Prefix6 netaddr.IPPrefix - pushRoutes []netaddr.IPPrefix - mu sync.Mutex machine map[netaddr.IP]*Machine lastV4 netaddr.IP @@ -225,6 +219,9 @@ func unspecOf(ip netaddr.IP) netaddr.IP { } // Attach adds an interface to a machine. +// +// The first interface added to a Machine becomes that machine's +// default route. func (m *Machine) Attach(interfaceName string, n *Network) *Interface { f := &Interface{ net: n, @@ -241,22 +238,30 @@ func (m *Machine) Attach(interfaceName string, n *Network) *Interface { defer m.mu.Unlock() m.interfaces = append(m.interfaces, f) - - if n.pushRoutes == nil { + if len(m.interfaces) == 1 { + m.routes = append(m.routes, + routeEntry{ + prefix: mustPrefix("0.0.0.0/0"), + iface: f, + }, + routeEntry{ + prefix: mustPrefix("::/0"), + iface: f, + }) + } else { if !n.Prefix4.IsZero() { - n.pushRoutes = append(n.pushRoutes, n.Prefix4) + m.routes = append(m.routes, routeEntry{ + prefix: n.Prefix4, + iface: f, + }) } if !n.Prefix6.IsZero() { - n.pushRoutes = append(n.pushRoutes, n.Prefix6) + m.routes = append(m.routes, routeEntry{ + prefix: n.Prefix6, + iface: f, + }) } } - - for _, pfx := range n.pushRoutes { - m.routes = append(m.routes, routeEntry{ - prefix: pfx, - iface: f, - }) - } sort.Slice(m.routes, func(i, j int) bool { return m.routes[i].prefix.Bits > m.routes[j].prefix.Bits })