Skip to content

Commit

Permalink
ipam/dhcp: Add broadcast flag
Browse files Browse the repository at this point in the history
Signed-off-by: Micah Hausler <[email protected]>
  • Loading branch information
micahhausler committed Jan 27, 2021
1 parent 8feef71 commit 55e6447
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
14 changes: 14 additions & 0 deletions plugins/ipam/dhcp/client.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2015 CNI authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
Expand Down
6 changes: 4 additions & 2 deletions plugins/ipam/dhcp/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type DHCP struct {
leases map[string]*DHCPLease
hostNetnsPrefix string
clientTimeout time.Duration
broadcast bool
}

func newDHCP(clientTimeout time.Duration) *DHCP {
Expand All @@ -66,7 +67,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, d.clientTimeout)
l, err := AcquireLease(clientID, hostNetns, args.IfName, d.clientTimeout, d.broadcast)
if err != nil {
return err
}
Expand Down Expand Up @@ -161,7 +162,7 @@ func getListener(socketPath string) (net.Listener, error) {

func runDaemon(
pidfilePath, hostPrefix, socketPath string,
dhcpClientTimeout time.Duration,
dhcpClientTimeout time.Duration, broadcast bool,
) error {
// since other goroutines (on separate threads) will change namespaces,
// ensure the RPC server does not get scheduled onto those
Expand All @@ -184,6 +185,7 @@ func runDaemon(

dhcp := newDHCP(dhcpClientTimeout)
dhcp.hostNetnsPrefix = hostPrefix
dhcp.broadcast = broadcast
rpc.Register(dhcp)
rpc.HandleHTTP()
http.Serve(l, nil)
Expand Down
20 changes: 12 additions & 8 deletions plugins/ipam/dhcp/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type DHCPLease struct {
rebindingTime time.Time
expireTime time.Time
timeout time.Duration
broadcast bool
stopping uint32
stop chan struct{}
wg sync.WaitGroup
Expand All @@ -67,13 +68,14 @@ type DHCPLease struct {
// calling DHCPLease.Stop()
func AcquireLease(
clientID, netns, ifName string,
timeout time.Duration,
timeout time.Duration, broadcast bool,
) (*DHCPLease, error) {
errCh := make(chan error, 1)
l := &DHCPLease{
clientID: clientID,
stop: make(chan struct{}),
timeout: timeout,
clientID: clientID,
stop: make(chan struct{}),
timeout: timeout,
broadcast: broadcast,
}

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

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

func (l *DHCPLease) renew() error {
c, err := newDHCPClient(l.link, l.clientID, l.timeout)
c, err := newDHCPClient(l.link, l.clientID, l.timeout, l.broadcast)
if err != nil {
return err
}
Expand Down Expand Up @@ -278,7 +280,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, l.timeout)
c, err := newDHCPClient(l.link, l.clientID, l.timeout, l.broadcast)
if err != nil {
return err
}
Expand Down Expand Up @@ -369,16 +371,18 @@ func backoffRetry(f func() (*dhcp4.Packet, error)) (*dhcp4.Packet, error) {
func newDHCPClient(
link netlink.Link, clientID string,
timeout time.Duration,
broadcast bool,
) (*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(timeout),
dhcp4client.Broadcast(false),
dhcp4client.Broadcast(broadcast),
dhcp4client.Connection(pktsock),
)
}
4 changes: 3 additions & 1 deletion plugins/ipam/dhcp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,21 @@ func main() {
var pidfilePath string
var hostPrefix string
var socketPath string
var broadcast bool
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.BoolVar(&broadcast, "broadcast", false, "broadcast DHCP leases")
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, timeout); err != nil {
if err := runDaemon(pidfilePath, hostPrefix, socketPath, timeout, broadcast); err != nil {
log.Printf(err.Error())
os.Exit(1)
}
Expand Down

0 comments on commit 55e6447

Please sign in to comment.