Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DHCP timeout is configurable #565

Merged
merged 3 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions plugins/ipam/dhcp/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"path/filepath"
"runtime"
"sync"
"time"

"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types"
Expand All @@ -41,11 +42,13 @@ type DHCP struct {
mux sync.Mutex
leases map[string]*DHCPLease
hostNetnsPrefix string
clientTimeout time.Duration
}

func newDHCP() *DHCP {
func newDHCP(clientTimeout time.Duration) *DHCP {
return &DHCP{
leases: make(map[string]*DHCPLease),
leases: make(map[string]*DHCPLease),
clientTimeout: clientTimeout,
}
}

Expand All @@ -63,7 +66,7 @@ func (d *DHCP) Allocate(args *skel.CmdArgs, result *current.Result) error {

clientID := generateClientID(args.ContainerID, conf.Name, args.IfName)
hostNetns := d.hostNetnsPrefix + args.Netns
l, err := AcquireLease(clientID, hostNetns, args.IfName)
l, err := AcquireLease(clientID, hostNetns, args.IfName, d.clientTimeout)
if err != nil {
return err
}
Expand Down Expand Up @@ -156,7 +159,10 @@ func getListener(socketPath string) (net.Listener, error) {
}
}

func runDaemon(pidfilePath string, hostPrefix string, socketPath string) error {
func runDaemon(
pidfilePath, hostPrefix, socketPath string,
dhcpClientTimeout time.Duration,
) error {
// since other goroutines (on separate threads) will change namespaces,
// ensure the RPC server does not get scheduled onto those
runtime.LockOSThread()
Expand All @@ -176,7 +182,7 @@ func runDaemon(pidfilePath string, hostPrefix string, socketPath string) error {
return fmt.Errorf("Error getting listener: %v", err)
}

dhcp := newDHCP()
dhcp := newDHCP(dhcpClientTimeout)
dhcp.hostNetnsPrefix = hostPrefix
rpc.Register(dhcp)
rpc.HandleHTTP()
Expand Down
20 changes: 14 additions & 6 deletions plugins/ipam/dhcp/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type DHCPLease struct {
renewalTime time.Time
rebindingTime time.Time
expireTime time.Time
timeout time.Duration
stopping uint32
stop chan struct{}
wg sync.WaitGroup
Expand All @@ -64,11 +65,15 @@ type DHCPLease struct {
// AcquireLease gets an DHCP lease and then maintains it in the background
// by periodically renewing it. The acquired lease can be released by
// calling DHCPLease.Stop()
func AcquireLease(clientID, netns, ifName string) (*DHCPLease, error) {
func AcquireLease(
clientID, netns, ifName string,
timeout time.Duration,
) (*DHCPLease, error) {
errCh := make(chan error, 1)
l := &DHCPLease{
clientID: clientID,
stop: make(chan struct{}),
timeout: timeout,
}

log.Printf("%v: acquiring lease", clientID)
Expand Down Expand Up @@ -115,7 +120,7 @@ func (l *DHCPLease) Stop() {
}

func (l *DHCPLease) acquire() error {
c, err := newDHCPClient(l.link, l.clientID)
c, err := newDHCPClient(l.link, l.clientID, l.timeout)
if err != nil {
return err
}
Expand Down Expand Up @@ -242,7 +247,7 @@ func (l *DHCPLease) downIface() {
}

func (l *DHCPLease) renew() error {
c, err := newDHCPClient(l.link, l.clientID)
c, err := newDHCPClient(l.link, l.clientID, l.timeout)
if err != nil {
return err
}
Expand Down Expand Up @@ -273,7 +278,7 @@ func (l *DHCPLease) renew() error {
func (l *DHCPLease) release() error {
log.Printf("%v: releasing lease", l.clientID)

c, err := newDHCPClient(l.link, l.clientID)
c, err := newDHCPClient(l.link, l.clientID, l.timeout)
if err != nil {
return err
}
Expand Down Expand Up @@ -361,15 +366,18 @@ func backoffRetry(f func() (*dhcp4.Packet, error)) (*dhcp4.Packet, error) {
return nil, errNoMoreTries
}

func newDHCPClient(link netlink.Link, clientID string) (*dhcp4client.Client, error) {
func newDHCPClient(
link netlink.Link, clientID string,
timeout time.Duration,
) (*dhcp4client.Client, error) {
pktsock, err := dhcp4client.NewPacketSock(link.Attrs().Index)
if err != nil {
return nil, err
}

return dhcp4client.New(
dhcp4client.HardwareAddr(link.Attrs().HardwareAddr),
dhcp4client.Timeout(5*time.Second),
dhcp4client.Timeout(timeout),
dhcp4client.Broadcast(false),
dhcp4client.Connection(pktsock),
)
Expand Down
5 changes: 4 additions & 1 deletion plugins/ipam/dhcp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/rpc"
"os"
"path/filepath"
"time"

"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types"
Expand All @@ -37,17 +38,19 @@ func main() {
var pidfilePath string
var hostPrefix string
var socketPath string
var timeout time.Duration
daemonFlags := flag.NewFlagSet("daemon", flag.ExitOnError)
daemonFlags.StringVar(&pidfilePath, "pidfile", "", "optional path to write daemon PID to")
daemonFlags.StringVar(&hostPrefix, "hostprefix", "", "optional prefix to host root")
daemonFlags.StringVar(&socketPath, "socketpath", "", "optional dhcp server socketpath")
daemonFlags.DurationVar(&timeout, "timeout", 10*time.Second, "optional dhcp client timeout duration")
daemonFlags.Parse(os.Args[2:])

if socketPath == "" {
socketPath = defaultSocketPath
}

if err := runDaemon(pidfilePath, hostPrefix, socketPath); err != nil {
if err := runDaemon(pidfilePath, hostPrefix, socketPath, timeout); err != nil {
log.Printf(err.Error())
os.Exit(1)
}
Expand Down