ipam/dhcp: Add broadcast flag
Signed-off-by: Micah Hausler <hausler.m@gmail.com>
This commit is contained in:
parent
74a6b28a2c
commit
fa48f7515b
@ -1,3 +1,17 @@
|
|||||||
|
// Copyright 2021 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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -43,6 +43,7 @@ type DHCP struct {
|
|||||||
leases map[string]*DHCPLease
|
leases map[string]*DHCPLease
|
||||||
hostNetnsPrefix string
|
hostNetnsPrefix string
|
||||||
clientTimeout time.Duration
|
clientTimeout time.Duration
|
||||||
|
broadcast bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDHCP(clientTimeout time.Duration) *DHCP {
|
func newDHCP(clientTimeout time.Duration) *DHCP {
|
||||||
@ -66,7 +67,7 @@ func (d *DHCP) Allocate(args *skel.CmdArgs, result *current.Result) error {
|
|||||||
|
|
||||||
clientID := generateClientID(args.ContainerID, conf.Name, args.IfName)
|
clientID := generateClientID(args.ContainerID, conf.Name, args.IfName)
|
||||||
hostNetns := d.hostNetnsPrefix + args.Netns
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -161,7 +162,7 @@ func getListener(socketPath string) (net.Listener, error) {
|
|||||||
|
|
||||||
func runDaemon(
|
func runDaemon(
|
||||||
pidfilePath, hostPrefix, socketPath string,
|
pidfilePath, hostPrefix, socketPath string,
|
||||||
dhcpClientTimeout time.Duration,
|
dhcpClientTimeout time.Duration, broadcast bool,
|
||||||
) error {
|
) error {
|
||||||
// since other goroutines (on separate threads) will change namespaces,
|
// since other goroutines (on separate threads) will change namespaces,
|
||||||
// ensure the RPC server does not get scheduled onto those
|
// ensure the RPC server does not get scheduled onto those
|
||||||
@ -184,6 +185,7 @@ func runDaemon(
|
|||||||
|
|
||||||
dhcp := newDHCP(dhcpClientTimeout)
|
dhcp := newDHCP(dhcpClientTimeout)
|
||||||
dhcp.hostNetnsPrefix = hostPrefix
|
dhcp.hostNetnsPrefix = hostPrefix
|
||||||
|
dhcp.broadcast = broadcast
|
||||||
rpc.Register(dhcp)
|
rpc.Register(dhcp)
|
||||||
rpc.HandleHTTP()
|
rpc.HandleHTTP()
|
||||||
http.Serve(l, nil)
|
http.Serve(l, nil)
|
||||||
|
@ -57,6 +57,7 @@ type DHCPLease struct {
|
|||||||
rebindingTime time.Time
|
rebindingTime time.Time
|
||||||
expireTime time.Time
|
expireTime time.Time
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
|
broadcast bool
|
||||||
stopping uint32
|
stopping uint32
|
||||||
stop chan struct{}
|
stop chan struct{}
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
@ -67,13 +68,14 @@ type DHCPLease struct {
|
|||||||
// calling DHCPLease.Stop()
|
// calling DHCPLease.Stop()
|
||||||
func AcquireLease(
|
func AcquireLease(
|
||||||
clientID, netns, ifName string,
|
clientID, netns, ifName string,
|
||||||
timeout time.Duration,
|
timeout time.Duration, broadcast bool,
|
||||||
) (*DHCPLease, error) {
|
) (*DHCPLease, error) {
|
||||||
errCh := make(chan error, 1)
|
errCh := make(chan error, 1)
|
||||||
l := &DHCPLease{
|
l := &DHCPLease{
|
||||||
clientID: clientID,
|
clientID: clientID,
|
||||||
stop: make(chan struct{}),
|
stop: make(chan struct{}),
|
||||||
timeout: timeout,
|
timeout: timeout,
|
||||||
|
broadcast: broadcast,
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("%v: acquiring lease", clientID)
|
log.Printf("%v: acquiring lease", clientID)
|
||||||
@ -120,7 +122,7 @@ func (l *DHCPLease) Stop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *DHCPLease) acquire() error {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -247,7 +249,7 @@ func (l *DHCPLease) downIface() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *DHCPLease) renew() error {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -278,7 +280,7 @@ func (l *DHCPLease) renew() error {
|
|||||||
func (l *DHCPLease) release() error {
|
func (l *DHCPLease) release() error {
|
||||||
log.Printf("%v: releasing lease", l.clientID)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -369,6 +371,7 @@ func backoffRetry(f func() (*dhcp4.Packet, error)) (*dhcp4.Packet, error) {
|
|||||||
func newDHCPClient(
|
func newDHCPClient(
|
||||||
link netlink.Link, clientID string,
|
link netlink.Link, clientID string,
|
||||||
timeout time.Duration,
|
timeout time.Duration,
|
||||||
|
broadcast bool,
|
||||||
) (*dhcp4client.Client, error) {
|
) (*dhcp4client.Client, error) {
|
||||||
pktsock, err := dhcp4client.NewPacketSock(link.Attrs().Index)
|
pktsock, err := dhcp4client.NewPacketSock(link.Attrs().Index)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -378,7 +381,7 @@ func newDHCPClient(
|
|||||||
return dhcp4client.New(
|
return dhcp4client.New(
|
||||||
dhcp4client.HardwareAddr(link.Attrs().HardwareAddr),
|
dhcp4client.HardwareAddr(link.Attrs().HardwareAddr),
|
||||||
dhcp4client.Timeout(timeout),
|
dhcp4client.Timeout(timeout),
|
||||||
dhcp4client.Broadcast(false),
|
dhcp4client.Broadcast(broadcast),
|
||||||
dhcp4client.Connection(pktsock),
|
dhcp4client.Connection(pktsock),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -38,11 +38,13 @@ func main() {
|
|||||||
var pidfilePath string
|
var pidfilePath string
|
||||||
var hostPrefix string
|
var hostPrefix string
|
||||||
var socketPath string
|
var socketPath string
|
||||||
|
var broadcast bool
|
||||||
var timeout time.Duration
|
var timeout time.Duration
|
||||||
daemonFlags := flag.NewFlagSet("daemon", flag.ExitOnError)
|
daemonFlags := flag.NewFlagSet("daemon", flag.ExitOnError)
|
||||||
daemonFlags.StringVar(&pidfilePath, "pidfile", "", "optional path to write daemon PID to")
|
daemonFlags.StringVar(&pidfilePath, "pidfile", "", "optional path to write daemon PID to")
|
||||||
daemonFlags.StringVar(&hostPrefix, "hostprefix", "", "optional prefix to host root")
|
daemonFlags.StringVar(&hostPrefix, "hostprefix", "", "optional prefix to host root")
|
||||||
daemonFlags.StringVar(&socketPath, "socketpath", "", "optional dhcp server socketpath")
|
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.DurationVar(&timeout, "timeout", 10*time.Second, "optional dhcp client timeout duration")
|
||||||
daemonFlags.Parse(os.Args[2:])
|
daemonFlags.Parse(os.Args[2:])
|
||||||
|
|
||||||
@ -50,7 +52,7 @@ func main() {
|
|||||||
socketPath = defaultSocketPath
|
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())
|
log.Printf(err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user