Added cli options for address pool configuration

This commit is contained in:
Dmitri Dolguikh 2017-10-30 15:14:32 -07:00 committed by Dave Anderson
parent 794097f861
commit b78d515ee1
6 changed files with 68 additions and 46 deletions

View File

@ -9,46 +9,42 @@ type PacketBuilder struct {
ServerDuid []byte ServerDuid []byte
PreferredLifetime uint32 PreferredLifetime uint32
ValidLifetime uint32 ValidLifetime uint32
Configuration BootConfiguration
Addresses AddressPool
} }
func MakePacketBuilder(serverDuid []byte, preferredLifetime, validLifetime uint32, bootFileUrl BootConfiguration, func MakePacketBuilder(serverDuid []byte, preferredLifetime, validLifetime uint32) *PacketBuilder {
addressPool AddressPool) *PacketBuilder { return &PacketBuilder{ServerDuid: serverDuid, PreferredLifetime: preferredLifetime, ValidLifetime: validLifetime}
return &PacketBuilder{ServerDuid: serverDuid, PreferredLifetime: preferredLifetime, ValidLifetime: validLifetime,
Configuration: bootFileUrl, Addresses: addressPool}
} }
func (b *PacketBuilder) BuildResponse(in *Packet) (*Packet, error) { func (b *PacketBuilder) BuildResponse(in *Packet, configuration BootConfiguration, addresses AddressPool) (*Packet, error) {
switch in.Type { switch in.Type {
case MsgSolicit: case MsgSolicit:
bootFileUrl, err := b.Configuration.GetBootUrl(b.ExtractLLAddressOrId(in.Options.ClientId()), in.Options.ClientArchType()) bootFileUrl, err := configuration.GetBootUrl(b.ExtractLLAddressOrId(in.Options.ClientId()), in.Options.ClientArchType())
if err != nil { if err != nil {
return nil, err return nil, err
} }
associations, err := b.Addresses.ReserveAddresses(in.Options.ClientId(), in.Options.IaNaIds()) associations, err := addresses.ReserveAddresses(in.Options.ClientId(), in.Options.IaNaIds())
if err != nil { if err != nil {
return b.MakeMsgAdvertiseWithNoAddrsAvailable(in.TransactionID, in.Options.ClientId(), err), err return b.MakeMsgAdvertiseWithNoAddrsAvailable(in.TransactionID, in.Options.ClientId(), err), err
} }
return b.MakeMsgAdvertise(in.TransactionID, in.Options.ClientId(), return b.MakeMsgAdvertise(in.TransactionID, in.Options.ClientId(),
in.Options.ClientArchType(), associations, bootFileUrl, b.Configuration.GetPreference()), nil in.Options.ClientArchType(), associations, bootFileUrl, configuration.GetPreference()), nil
case MsgRequest: case MsgRequest:
bootFileUrl, err := b.Configuration.GetBootUrl(b.ExtractLLAddressOrId(in.Options.ClientId()), in.Options.ClientArchType()) bootFileUrl, err := configuration.GetBootUrl(b.ExtractLLAddressOrId(in.Options.ClientId()), in.Options.ClientArchType())
if err != nil { if err != nil {
return nil, err return nil, err
} }
associations, err := b.Addresses.ReserveAddresses(in.Options.ClientId(), in.Options.IaNaIds()) associations, err := addresses.ReserveAddresses(in.Options.ClientId(), in.Options.IaNaIds())
return b.MakeMsgReply(in.TransactionID, in.Options.ClientId(), return b.MakeMsgReply(in.TransactionID, in.Options.ClientId(),
in.Options.ClientArchType(), associations, iasWithoutAddesses(associations, in.Options.IaNaIds()), bootFileUrl, err), err in.Options.ClientArchType(), associations, iasWithoutAddesses(associations, in.Options.IaNaIds()), bootFileUrl, err), err
case MsgInformationRequest: case MsgInformationRequest:
bootFileUrl, err := b.Configuration.GetBootUrl(b.ExtractLLAddressOrId(in.Options.ClientId()), in.Options.ClientArchType()) bootFileUrl, err := configuration.GetBootUrl(b.ExtractLLAddressOrId(in.Options.ClientId()), in.Options.ClientArchType())
if err != nil { if err != nil {
return nil, err return nil, err
} }
return b.MakeMsgInformationRequestReply(in.TransactionID, in.Options.ClientId(), return b.MakeMsgInformationRequestReply(in.TransactionID, in.Options.ClientId(),
in.Options.ClientArchType(), bootFileUrl), nil in.Options.ClientArchType(), bootFileUrl), nil
case MsgRelease: case MsgRelease:
b.Addresses.ReleaseAddresses(in.Options.ClientId(), in.Options.IaNaIds()) addresses.ReleaseAddresses(in.Options.ClientId(), in.Options.IaNaIds())
return b.MakeMsgReleaseReply(in.TransactionID, in.Options.ClientId()), nil return b.MakeMsgReleaseReply(in.TransactionID, in.Options.ClientId()), nil
default: default:
return nil, nil return nil, nil

View File

@ -16,8 +16,7 @@ func TestMakeMsgAdvertise(t *testing.T) {
expectedBootFileUrl := []byte("http://bootfileurl") expectedBootFileUrl := []byte("http://bootfileurl")
identityAssociation := &IdentityAssociation{IpAddress: expectedIp, InterfaceId: expectedInterfaceId} identityAssociation := &IdentityAssociation{IpAddress: expectedIp, InterfaceId: expectedInterfaceId}
builder := MakePacketBuilder(expectedServerId, 90, 100, nil, builder := MakePacketBuilder(expectedServerId, 90, 100)
NewRandomAddressPool(net.ParseIP("2001:db8:f00f:cafe::1"), 1, 100))
msg := builder.MakeMsgAdvertise(transactionId, expectedClientId, 0x11, []*IdentityAssociation{identityAssociation}, msg := builder.MakeMsgAdvertise(transactionId, expectedClientId, 0x11, []*IdentityAssociation{identityAssociation},
expectedBootFileUrl, nil) expectedBootFileUrl, nil)
@ -67,8 +66,7 @@ func TestMakeMsgAdvertise(t *testing.T) {
func TestShouldSetPreferenceOptionWhenSpecified(t *testing.T) { func TestShouldSetPreferenceOptionWhenSpecified(t *testing.T) {
identityAssociation := &IdentityAssociation{IpAddress: net.ParseIP("2001:db8:f00f:cafe::1"), InterfaceId: []byte("id-1")} identityAssociation := &IdentityAssociation{IpAddress: net.ParseIP("2001:db8:f00f:cafe::1"), InterfaceId: []byte("id-1")}
builder := MakePacketBuilder([]byte("serverid"), 90, 100, nil, builder := MakePacketBuilder([]byte("serverid"), 90, 100)
NewRandomAddressPool(net.ParseIP("2001:db8:f00f:cafe::1"), 1, 100))
expectedPreference := []byte{128} expectedPreference := []byte{128}
msg := builder.MakeMsgAdvertise([3]byte{'t', 'i', 'd'}, []byte("clientid"), 0x11, msg := builder.MakeMsgAdvertise([3]byte{'t', 'i', 'd'}, []byte("clientid"), 0x11,
@ -91,8 +89,7 @@ func TestMakeMsgAdvertiseWithHttpClientArch(t *testing.T) {
expectedBootFileUrl := []byte("http://bootfileurl") expectedBootFileUrl := []byte("http://bootfileurl")
identityAssociation := &IdentityAssociation{IpAddress: expectedIp, InterfaceId: []byte("id-1")} identityAssociation := &IdentityAssociation{IpAddress: expectedIp, InterfaceId: []byte("id-1")}
builder := MakePacketBuilder(expectedServerId, 90, 100, nil, builder := MakePacketBuilder(expectedServerId, 90, 100)
NewRandomAddressPool(net.ParseIP("2001:db8:f00f:cafe::1"), 1, 100))
msg := builder.MakeMsgAdvertise(transactionId, expectedClientId, 0x10, []*IdentityAssociation{identityAssociation}, msg := builder.MakeMsgAdvertise(transactionId, expectedClientId, 0x10, []*IdentityAssociation{identityAssociation},
expectedBootFileUrl, nil) expectedBootFileUrl, nil)
@ -116,8 +113,7 @@ func TestMakeNoAddrsAvailable(t *testing.T) {
transactionId := [3]byte{'1', '2', '3'} transactionId := [3]byte{'1', '2', '3'}
expectedMessage := "Boom!" expectedMessage := "Boom!"
builder := MakePacketBuilder(expectedServerId, 90, 100, nil, builder := MakePacketBuilder(expectedServerId, 90, 100)
NewRandomAddressPool(net.ParseIP("2001:db8:f00f:cafe::1"), 1, 100))
msg := builder.MakeMsgAdvertiseWithNoAddrsAvailable(transactionId, expectedClientId, fmt.Errorf(expectedMessage)) msg := builder.MakeMsgAdvertiseWithNoAddrsAvailable(transactionId, expectedClientId, fmt.Errorf(expectedMessage))
@ -164,8 +160,7 @@ func TestMakeMsgReply(t *testing.T) {
expectedBootFileUrl := []byte("http://bootfileurl") expectedBootFileUrl := []byte("http://bootfileurl")
identityAssociation := &IdentityAssociation{IpAddress: expectedIp, InterfaceId: []byte("id-1")} identityAssociation := &IdentityAssociation{IpAddress: expectedIp, InterfaceId: []byte("id-1")}
builder := MakePacketBuilder(expectedServerId, 90, 100, nil, builder := MakePacketBuilder(expectedServerId, 90, 100)
NewRandomAddressPool(net.ParseIP("2001:db8:f00f:cafe::1"), 1, 100))
msg := builder.MakeMsgReply(transactionId, expectedClientId, 0x11, []*IdentityAssociation{identityAssociation}, msg := builder.MakeMsgReply(transactionId, expectedClientId, 0x11, []*IdentityAssociation{identityAssociation},
make([][]byte, 0), expectedBootFileUrl, nil) make([][]byte, 0), expectedBootFileUrl, nil)
@ -221,8 +216,7 @@ func TestMakeMsgReplyWithHttpClientArch(t *testing.T) {
expectedBootFileUrl := []byte("http://bootfileurl") expectedBootFileUrl := []byte("http://bootfileurl")
identityAssociation := &IdentityAssociation{IpAddress: expectedIp, InterfaceId: []byte("id-1")} identityAssociation := &IdentityAssociation{IpAddress: expectedIp, InterfaceId: []byte("id-1")}
builder := MakePacketBuilder(expectedServerId, 90, 100, nil, builder := MakePacketBuilder(expectedServerId, 90, 100)
NewRandomAddressPool(net.ParseIP("2001:db8:f00f:cafe::1"), 1, 100))
msg := builder.MakeMsgReply(transactionId, expectedClientId, 0x10, msg := builder.MakeMsgReply(transactionId, expectedClientId, 0x10,
[]*IdentityAssociation{identityAssociation}, make([][]byte, 0), []*IdentityAssociation{identityAssociation}, make([][]byte, 0),
@ -251,8 +245,7 @@ func TestMakeMsgReplyWithNoAddrsAvailable(t *testing.T) {
identityAssociation := &IdentityAssociation{IpAddress: expectedIp, InterfaceId: []byte("id-1")} identityAssociation := &IdentityAssociation{IpAddress: expectedIp, InterfaceId: []byte("id-1")}
expectedErrorMessage := "Boom!" expectedErrorMessage := "Boom!"
builder := MakePacketBuilder(expectedServerId, 90, 100, nil, builder := MakePacketBuilder(expectedServerId, 90, 100)
NewRandomAddressPool(net.ParseIP("2001:db8:f00f:cafe::1"), 1, 100))
msg := builder.MakeMsgReply(transactionId, expectedClientId, 0x10, msg := builder.MakeMsgReply(transactionId, expectedClientId, 0x10,
[]*IdentityAssociation{identityAssociation}, [][]byte{[]byte("id-2")}, expectedBootFileUrl, []*IdentityAssociation{identityAssociation}, [][]byte{[]byte("id-2")}, expectedBootFileUrl,
@ -303,8 +296,7 @@ func TestMakeMsgInformationRequestReply(t *testing.T) {
transactionId := [3]byte{'1', '2', '3'} transactionId := [3]byte{'1', '2', '3'}
expectedBootFileUrl := []byte("http://bootfileurl") expectedBootFileUrl := []byte("http://bootfileurl")
builder := MakePacketBuilder(expectedServerId, 90, 100, nil, builder := MakePacketBuilder(expectedServerId, 90, 100)
NewRandomAddressPool(net.ParseIP("2001:db8:f00f:cafe::1"), 1, 100))
msg := builder.MakeMsgInformationRequestReply(transactionId, expectedClientId, 0x11, expectedBootFileUrl) msg := builder.MakeMsgInformationRequestReply(transactionId, expectedClientId, 0x11, expectedBootFileUrl)
@ -352,8 +344,7 @@ func TestMakeMsgInformationRequestReplyWithHttpClientArch(t *testing.T) {
transactionId := [3]byte{'1', '2', '3'} transactionId := [3]byte{'1', '2', '3'}
expectedBootFileUrl := []byte("http://bootfileurl") expectedBootFileUrl := []byte("http://bootfileurl")
builder := MakePacketBuilder(expectedServerId, 90, 100, nil, builder := MakePacketBuilder(expectedServerId, 90, 100)
NewRandomAddressPool(net.ParseIP("2001:db8:f00f:cafe::1"), 1, 100))
msg := builder.MakeMsgInformationRequestReply(transactionId, expectedClientId, 0x10, expectedBootFileUrl) msg := builder.MakeMsgInformationRequestReply(transactionId, expectedClientId, 0x10, expectedBootFileUrl)
@ -376,8 +367,7 @@ func TestMakeMsgReleaseReply(t *testing.T) {
expectedServerId := []byte("serverid") expectedServerId := []byte("serverid")
transactionId := [3]byte{'1', '2', '3'} transactionId := [3]byte{'1', '2', '3'}
builder := MakePacketBuilder(expectedServerId, 90, 100, nil, builder := MakePacketBuilder(expectedServerId, 90, 100)
NewRandomAddressPool(net.ParseIP("2001:db8:f00f:cafe::1"), 1, 100))
msg := builder.MakeMsgReleaseReply(transactionId, expectedClientId) msg := builder.MakeMsgReleaseReply(transactionId, expectedClientId)

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"go.universe.tf/netboot/pixiecorev6" "go.universe.tf/netboot/pixiecorev6"
"go.universe.tf/netboot/dhcp6" "go.universe.tf/netboot/dhcp6"
"net"
) )
var bootIPv6Cmd = &cobra.Command{ var bootIPv6Cmd = &cobra.Command{
@ -51,6 +52,21 @@ var bootIPv6Cmd = &cobra.Command{
} }
s.BootConfig = dhcp6.MakeStaticBootConfiguration(httpBootUrl, ipxeUrl, preference, cmd.Flags().Changed("preference")) s.BootConfig = dhcp6.MakeStaticBootConfiguration(httpBootUrl, ipxeUrl, preference, cmd.Flags().Changed("preference"))
addressPoolStart, err := cmd.Flags().GetString("address-pool-start")
if err != nil {
fatalf("Error reading flag: %s", err)
}
addressPoolSize, err := cmd.Flags().GetUint64("address-pool-size")
if err != nil {
fatalf("Error reading flag: %s", err)
}
addressPoolValidLifetime, err := cmd.Flags().GetUint32("address-pool-lifetime")
if err != nil {
fatalf("Error reading flag: %s", err)
}
s.AddressPool = dhcp6.NewRandomAddressPool(net.ParseIP(addressPoolStart), addressPoolSize, addressPoolValidLifetime)
s.PacketBuilder = dhcp6.MakePacketBuilder(s.Duid, addressPoolValidLifetime - addressPoolValidLifetime*3/100, addressPoolValidLifetime)
fmt.Println(s.Serve()) fmt.Println(s.Serve())
}, },
} }
@ -61,6 +77,9 @@ func serverv6ConfigFlags(cmd *cobra.Command) {
cmd.Flags().StringP("httpboot-url", "", "", "HTTPBoot url, e.g. http://[2001:db8:f00f:cafe::4]/bootx64.efi") cmd.Flags().StringP("httpboot-url", "", "", "HTTPBoot url, e.g. http://[2001:db8:f00f:cafe::4]/bootx64.efi")
cmd.Flags().Bool("debug", false, "Enable debug-level logging") cmd.Flags().Bool("debug", false, "Enable debug-level logging")
cmd.Flags().Uint8("preference", 255, "Set dhcp server preference value") cmd.Flags().Uint8("preference", 255, "Set dhcp server preference value")
cmd.Flags().StringP("address-pool-start", "", "2001:db8:f00f:cafe:ffff::100", "Starting ip of the address pool, e.g. 2001:db8:f00f:cafe:ffff::100")
cmd.Flags().Uint64("address-pool-size", 50, "Address pool size")
cmd.Flags().Uint32("address-pool-lifetime", 1850, "Address pool ip valid lifetime in seconds")
} }
func init() { func init() {

View File

@ -6,6 +6,7 @@ import (
"go.universe.tf/netboot/pixiecorev6" "go.universe.tf/netboot/pixiecorev6"
"go.universe.tf/netboot/dhcp6" "go.universe.tf/netboot/dhcp6"
"time" "time"
"net"
) )
var ipv6ApiCmd = &cobra.Command{ var ipv6ApiCmd = &cobra.Command{
@ -40,15 +41,28 @@ var ipv6ApiCmd = &cobra.Command{
if apiUrl == "" { if apiUrl == "" {
fatalf("Please specify ipxe config file url") fatalf("Please specify ipxe config file url")
} }
s.Address = addr s.Address = addr
preference, err := cmd.Flags().GetUint8("preference") preference, err := cmd.Flags().GetUint8("preference")
if err != nil { if err != nil {
fatalf("Error reading flag: %s", err) fatalf("Error reading flag: %s", err)
} }
s.BootConfig = dhcp6.MakeApiBootConfiguration(apiUrl, apiTimeout, preference, cmd.Flags().Changed("preference")) s.BootConfig = dhcp6.MakeApiBootConfiguration(apiUrl, apiTimeout, preference, cmd.Flags().Changed("preference"))
addressPoolStart, err := cmd.Flags().GetString("address-pool-start")
if err != nil {
fatalf("Error reading flag: %s", err)
}
addressPoolSize, err := cmd.Flags().GetUint64("address-pool-size")
if err != nil {
fatalf("Error reading flag: %s", err)
}
addressPoolValidLifetime, err := cmd.Flags().GetUint32("address-pool-lifetime")
if err != nil {
fatalf("Error reading flag: %s", err)
}
s.AddressPool = dhcp6.NewRandomAddressPool(net.ParseIP(addressPoolStart), addressPoolSize, addressPoolValidLifetime)
s.PacketBuilder = dhcp6.MakePacketBuilder(s.Duid, addressPoolValidLifetime - addressPoolValidLifetime*3/100, addressPoolValidLifetime)
fmt.Println(s.Serve()) fmt.Println(s.Serve())
}, },
} }
@ -59,6 +73,9 @@ func serverv6ApiConfigFlags(cmd *cobra.Command) {
cmd.Flags().Duration("api-request-timeout", 5*time.Second, "Timeout for request to the API server") cmd.Flags().Duration("api-request-timeout", 5*time.Second, "Timeout for request to the API server")
cmd.Flags().Bool("debug", false, "Enable debug-level logging") cmd.Flags().Bool("debug", false, "Enable debug-level logging")
cmd.Flags().Uint8("preference", 255, "Set dhcp server preference value") cmd.Flags().Uint8("preference", 255, "Set dhcp server preference value")
cmd.Flags().StringP("address-pool-start", "", "2001:db8:f00f:cafe:ffff::100", "Starting ip of the address pool, e.g. 2001:db8:f00f:cafe:ffff::100")
cmd.Flags().Uint64("address-pool-size", 50, "Address pool size")
cmd.Flags().Uint32("address-pool-lifetime", 1850, "Address pool ip address valid lifetime in seconds")
} }
func init() { func init() {

View File

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
) )
func (s *ServerV6) serveDHCP(conn *dhcp6.Conn, packetBuilder *dhcp6.PacketBuilder) error { func (s *ServerV6) serveDHCP(conn *dhcp6.Conn) error {
s.debug("dhcpv6", "Waiting for packets...\n") s.debug("dhcpv6", "Waiting for packets...\n")
for { for {
pkt, src, err := conn.RecvDHCP() pkt, src, err := conn.RecvDHCP()
@ -19,7 +19,7 @@ func (s *ServerV6) serveDHCP(conn *dhcp6.Conn, packetBuilder *dhcp6.PacketBuilde
s.debug("dhcpv6", fmt.Sprintf("Received (%d) packet (%d): %s\n", pkt.Type, pkt.TransactionID, pkt.Options.HumanReadable())) s.debug("dhcpv6", fmt.Sprintf("Received (%d) packet (%d): %s\n", pkt.Type, pkt.TransactionID, pkt.Options.HumanReadable()))
response, err := packetBuilder.BuildResponse(pkt) response, err := s.PacketBuilder.BuildResponse(pkt, s.BootConfig, s.AddressPool)
if err != nil { if err != nil {
s.log("dhcpv6", fmt.Sprintf("Error creating response for transaction: %d: %s", pkt.TransactionID, err)) s.log("dhcpv6", fmt.Sprintf("Error creating response for transaction: %d: %s", pkt.TransactionID, err))
if response == nil { if response == nil {

View File

@ -9,10 +9,13 @@ import (
) )
type ServerV6 struct { type ServerV6 struct {
Address string Address string
Port string Port string
Duid []byte Duid []byte
BootConfig dhcp6.BootConfiguration
BootConfig dhcp6.BootConfiguration
PacketBuilder *dhcp6.PacketBuilder
AddressPool dhcp6.AddressPool
errs chan error errs chan error
@ -46,10 +49,7 @@ func (s *ServerV6) Serve() error {
s.SetDUID(dhcp.SourceHardwareAddress()) s.SetDUID(dhcp.SourceHardwareAddress())
addressPool := dhcp6.NewRandomAddressPool(net.ParseIP("2001:db8:f00f:cafe::10"), 90, 1850) go func() { s.errs <- s.serveDHCP(dhcp) }()
packetBuilder := dhcp6.MakePacketBuilder(s.Duid, 1800, 1850, s.BootConfig, addressPool)
go func() { s.errs <- s.serveDHCP(dhcp, packetBuilder) }()
// Wait for either a fatal error, or Shutdown(). // Wait for either a fatal error, or Shutdown().
err = <-s.errs err = <-s.errs