Revamp DHCP, add basic context management
This started off as a small change to make the network service DHCP client a bit nicer, and ended up basically me half-assedly starting to add context within Smalltown.
In my opionion a simple OnStart/OnStop lifecycle management for services will stop working once we have to start handling failing services. I think taking inspiration from Erlang's OTP and implementing some sort of supervision tree is the way to go. I think this also ties nicely together with Go's context system, at least partially. Implementing the full supervision tree system is out of scope for this change, but at least this introduces .Context() on the base service struct that service implementations can use. Currently each service has its own background context, but again, this should tie into some sort of supervision tree in the future. There will be a design document for this.
I also rejigger the init code to have a context available immediately, and use that to acquire (with timeout) information about DHCP addresses from the network service.
I also fix a bug where the network service is started twice (once by init, once by the smalltown node code; now the smalltown node code takes in a dependency injected network service instead).
I also fix a bug where OnStop would call OnStart. Whoops.
Test Plan: no new functionality, covered by current tests
Bug: T561
X-Origin-Diff: phab/D396
GitOrigin-RevId: adddf3dd2f140b6ea64eb034ff19533d32c4ef23
diff --git a/core/internal/network/main.go b/core/internal/network/main.go
index 04ab159..7c22249 100644
--- a/core/internal/network/main.go
+++ b/core/internal/network/main.go
@@ -21,13 +21,9 @@
"fmt"
"net"
"os"
- "sync"
- "time"
"git.monogon.dev/source/nexantic.git/core/internal/common/service"
- "github.com/insomniacslk/dhcp/dhcpv4"
- "github.com/insomniacslk/dhcp/dhcpv4/nclient4"
"github.com/vishvananda/netlink"
"go.uber.org/zap"
"golang.org/x/sys/unix"
@@ -40,11 +36,9 @@
type Service struct {
*service.BaseService
- config Config
- dhcp4Client *nclient4.Client
- ip *net.IP
- ipNotify chan struct{}
- lock sync.Mutex
+ config Config
+
+ dhcp *dhcpClient
}
type Config struct {
@@ -52,8 +46,8 @@
func NewNetworkService(config Config, logger *zap.Logger) (*Service, error) {
s := &Service{
- config: config,
- ipNotify: make(chan struct{}),
+ config: config,
+ dhcp: newDHCPClient(logger),
}
s.BaseService = service.NewBaseService("network", logger, s)
return s, nil
@@ -91,79 +85,36 @@
Gw: gw,
Scope: netlink.SCOPE_UNIVERSE,
}); err != nil {
- return fmt.Errorf("Failed to add default route: %w", err)
+ return fmt.Errorf("failed to add default route: %w", err)
}
return nil
}
-const (
- stateInitialize = 1
- stateSelect = 2
- stateBound = 3
- stateRenew = 4
- stateRebind = 5
-)
+func (s *Service) useInterface(iface netlink.Link) error {
+ go s.dhcp.run(s.Context(), iface)
-var dhcpBroadcastAddr = &net.UDPAddr{IP: net.IP{255, 255, 255, 255}, Port: 67}
-
-// TODO(lorenz): This is a super terrible DHCP client, but it works for QEMU slirp
-func (s *Service) dhcpClient(iface netlink.Link) error {
- client, err := nclient4.New(iface.Attrs().Name)
+ status, err := s.dhcp.status(s.Context(), true)
if err != nil {
- panic(err)
- }
- var ack *dhcpv4.DHCPv4
- for {
- dhcpCtx, dhcpCtxCancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer dhcpCtxCancel()
- _, ack, err = client.Request(dhcpCtx)
- if err == nil {
- break
- }
- s.Logger.Info("DHCP request failed", zap.Error(err))
- }
- s.Logger.Info("Network service got IP", zap.String("ip", ack.YourIPAddr.String()))
- if err := setResolvconf(ack.DNS(), []string{}); err != nil {
- s.Logger.Warn("Failed to set resolvconf", zap.Error(err))
+ return fmt.Errorf("could not get DHCP status: %v", err)
}
- s.lock.Lock()
- s.ip = &ack.YourIPAddr
- s.lock.Unlock()
-loop:
- for {
- select {
- case s.ipNotify <- struct{}{}:
- default:
- break loop
- }
+ if err := setResolvconf(status.DNS(), []string{}); err != nil {
+ s.Logger.Warn("failed to set resolvconf", zap.Error(err))
}
- if err := addNetworkRoutes(iface, net.IPNet{IP: ack.YourIPAddr, Mask: ack.SubnetMask()}, ack.GatewayIPAddr); err != nil {
- s.Logger.Warn("Failed to add routes", zap.Error(err))
+ if err := addNetworkRoutes(iface, net.IPNet{IP: status.YourIPAddr, Mask: status.SubnetMask()}, status.GatewayIPAddr); err != nil {
+ s.Logger.Warn("failed to add routes", zap.Error(err))
}
return nil
}
// GetIP returns the current IP (and optionally waits for one to be assigned)
-func (s *Service) GetIP(wait bool) *net.IP {
- s.lock.Lock()
- if !wait {
- ip := s.ip
- s.lock.Unlock()
- return ip
+func (s *Service) GetIP(ctx context.Context, wait bool) (*net.IP, error) {
+ status, err := s.dhcp.status(ctx, wait)
+ if err != nil {
+ return nil, err
}
-
- for {
- if s.ip != nil {
- ip := s.ip
- s.lock.Unlock()
- return ip
- }
- s.lock.Unlock()
- <-s.ipNotify
- s.lock.Lock()
- }
+ return &status.YourIPAddr, nil
}
func (s *Service) OnStart() error {
@@ -190,13 +141,14 @@
}
}
}
- if len(ethernetLinks) == 1 {
- link := ethernetLinks[0]
- go s.dhcpClient(link)
-
- } else {
- s.Logger.Warn("Network service cannot yet handle more than one interface :(")
+ if len(ethernetLinks) != 1 {
+ s.Logger.Warn("Network service needs exactly one link, bailing")
+ return nil
}
+
+ link := ethernetLinks[0]
+ go s.useInterface(link)
+
return nil
}