Skip to content

Commit

Permalink
dhcp: timeout value is set in DHCP daemon
Browse files Browse the repository at this point in the history
Eventually the timeout value will become a CLI argument

The default timeout was nestled all the way in the lease constructor

This commit is the first step in making the timeout configurable by
moving it to the DHCPLease constructor

Signed-off-by: toby lorne <[email protected]>
  • Loading branch information
tlwr committed Jan 11, 2021
1 parent 48a97a7 commit 3161bb5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
3 changes: 2 additions & 1 deletion 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 Down Expand Up @@ -63,7 +64,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, 5*time.Second)
if err != nil {
return err
}
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

0 comments on commit 3161bb5

Please sign in to comment.