2015-09-13 21:35:28 +00:00
|
|
|
package structs
|
2015-09-13 02:34:46 +00:00
|
|
|
|
|
|
|
import (
|
2015-09-13 23:40:53 +00:00
|
|
|
"fmt"
|
2015-09-13 02:34:46 +00:00
|
|
|
"math/rand"
|
|
|
|
"net"
|
2016-02-20 20:18:22 +00:00
|
|
|
"sync"
|
2015-09-13 02:34:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// MinDynamicPort is the smallest dynamic port generated
|
|
|
|
MinDynamicPort = 20000
|
|
|
|
|
|
|
|
// MaxDynamicPort is the largest dynamic port generated
|
2017-07-18 20:47:53 +00:00
|
|
|
MaxDynamicPort = 32000
|
2015-09-13 02:34:46 +00:00
|
|
|
|
|
|
|
// maxRandPortAttempts is the maximum number of attempt
|
|
|
|
// to assign a random port
|
|
|
|
maxRandPortAttempts = 20
|
2016-02-20 20:08:27 +00:00
|
|
|
|
|
|
|
// maxValidPort is the max valid port number
|
|
|
|
maxValidPort = 65536
|
2015-09-13 02:34:46 +00:00
|
|
|
)
|
|
|
|
|
2016-02-20 20:18:22 +00:00
|
|
|
var (
|
|
|
|
// bitmapPool is used to pool the bitmaps used for port collision
|
|
|
|
// checking. They are fairly large (8K) so we can re-use them to
|
|
|
|
// avoid GC pressure. Care should be taken to call Clear() on any
|
|
|
|
// bitmap coming from the pool.
|
|
|
|
bitmapPool = new(sync.Pool)
|
|
|
|
)
|
|
|
|
|
2015-09-13 02:34:46 +00:00
|
|
|
// NetworkIndex is used to index the available network resources
|
|
|
|
// and the used network resources on a machine given allocations
|
|
|
|
type NetworkIndex struct {
|
2020-06-16 15:53:10 +00:00
|
|
|
AvailNetworks []*NetworkResource // List of available networks
|
|
|
|
NodeNetworks []*NodeNetworkResource // List of available node networks
|
|
|
|
AvailAddresses map[string][]NodeNetworkAddress // Map of host network aliases to list of addresses
|
|
|
|
AvailBandwidth map[string]int // Bandwidth by device
|
|
|
|
UsedPorts map[string]Bitmap // Ports by IP
|
|
|
|
UsedBandwidth map[string]int // Bandwidth by device
|
2015-09-13 02:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewNetworkIndex is used to construct a new network index
|
|
|
|
func NewNetworkIndex() *NetworkIndex {
|
|
|
|
return &NetworkIndex{
|
2020-06-16 15:53:10 +00:00
|
|
|
AvailAddresses: make(map[string][]NodeNetworkAddress),
|
2015-09-13 02:34:46 +00:00
|
|
|
AvailBandwidth: make(map[string]int),
|
2016-02-20 20:08:27 +00:00
|
|
|
UsedPorts: make(map[string]Bitmap),
|
2015-09-13 02:34:46 +00:00
|
|
|
UsedBandwidth: make(map[string]int),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 15:53:10 +00:00
|
|
|
func (idx *NetworkIndex) getUsedPortsFor(ip string) Bitmap {
|
|
|
|
used := idx.UsedPorts[ip]
|
|
|
|
if used == nil {
|
|
|
|
// Try to get a bitmap from the pool, else create
|
|
|
|
raw := bitmapPool.Get()
|
|
|
|
if raw != nil {
|
|
|
|
used = raw.(Bitmap)
|
|
|
|
used.Clear()
|
|
|
|
} else {
|
|
|
|
used, _ = NewBitmap(maxValidPort)
|
|
|
|
}
|
|
|
|
idx.UsedPorts[ip] = used
|
|
|
|
}
|
|
|
|
return used
|
|
|
|
}
|
|
|
|
|
2016-02-20 20:18:22 +00:00
|
|
|
// Release is called when the network index is no longer needed
|
|
|
|
// to attempt to re-use some of the memory it has allocated
|
|
|
|
func (idx *NetworkIndex) Release() {
|
|
|
|
for _, b := range idx.UsedPorts {
|
|
|
|
bitmapPool.Put(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-13 21:56:51 +00:00
|
|
|
// Overcommitted checks if the network is overcommitted
|
|
|
|
func (idx *NetworkIndex) Overcommitted() bool {
|
2020-06-16 15:53:10 +00:00
|
|
|
// TODO remove since bandwidth is deprecated
|
|
|
|
/*for device, used := range idx.UsedBandwidth {
|
2015-09-13 21:56:51 +00:00
|
|
|
avail := idx.AvailBandwidth[device]
|
|
|
|
if used > avail {
|
|
|
|
return true
|
|
|
|
}
|
2020-06-16 15:53:10 +00:00
|
|
|
}*/
|
2015-09-13 21:56:51 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNode is used to setup the available network resources. Returns
|
|
|
|
// true if there is a collision
|
|
|
|
func (idx *NetworkIndex) SetNode(node *Node) (collide bool) {
|
2018-10-02 20:36:04 +00:00
|
|
|
|
|
|
|
// COMPAT(0.11): Remove in 0.11
|
|
|
|
// Grab the network resources, handling both new and old
|
|
|
|
var networks []*NetworkResource
|
|
|
|
if node.NodeResources != nil && len(node.NodeResources.Networks) != 0 {
|
|
|
|
networks = node.NodeResources.Networks
|
|
|
|
} else if node.Resources != nil {
|
|
|
|
networks = node.Resources.Networks
|
|
|
|
}
|
|
|
|
|
2020-06-16 15:53:10 +00:00
|
|
|
var nodeNetworks []*NodeNetworkResource
|
|
|
|
if node.NodeResources != nil && len(node.NodeResources.NodeNetworks) != 0 {
|
|
|
|
nodeNetworks = node.NodeResources.NodeNetworks
|
|
|
|
}
|
|
|
|
|
2015-09-13 02:34:46 +00:00
|
|
|
// Add the available CIDR blocks
|
2018-10-02 20:36:04 +00:00
|
|
|
for _, n := range networks {
|
2015-09-14 01:46:40 +00:00
|
|
|
if n.Device != "" {
|
2015-09-13 02:34:46 +00:00
|
|
|
idx.AvailNetworks = append(idx.AvailNetworks, n)
|
|
|
|
idx.AvailBandwidth[n.Device] = n.MBits
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 15:53:10 +00:00
|
|
|
// TODO: upgrade path?
|
|
|
|
// is it possible to get duplicates here?
|
|
|
|
for _, n := range nodeNetworks {
|
|
|
|
for _, a := range n.Addresses {
|
|
|
|
idx.AvailAddresses[a.Alias] = append(idx.AvailAddresses[a.Alias], a)
|
|
|
|
if idx.AddReservedPortsForIP(a.ReservedPorts, a.Address) {
|
|
|
|
collide = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 20:36:04 +00:00
|
|
|
// COMPAT(0.11): Remove in 0.11
|
|
|
|
// Handle reserving ports, handling both new and old
|
|
|
|
if node.ReservedResources != nil && node.ReservedResources.Networks.ReservedHostPorts != "" {
|
|
|
|
collide = idx.AddReservedPortRange(node.ReservedResources.Networks.ReservedHostPorts)
|
|
|
|
} else if node.Reserved != nil {
|
|
|
|
for _, n := range node.Reserved.Networks {
|
2015-09-13 21:56:51 +00:00
|
|
|
if idx.AddReserved(n) {
|
|
|
|
collide = true
|
|
|
|
}
|
2015-09-13 02:34:46 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-02 20:36:04 +00:00
|
|
|
|
2015-09-13 21:56:51 +00:00
|
|
|
return
|
2015-09-13 02:34:46 +00:00
|
|
|
}
|
|
|
|
|
2015-09-13 21:56:51 +00:00
|
|
|
// AddAllocs is used to add the used network resources. Returns
|
|
|
|
// true if there is a collision
|
|
|
|
func (idx *NetworkIndex) AddAllocs(allocs []*Allocation) (collide bool) {
|
2015-09-13 02:34:46 +00:00
|
|
|
for _, alloc := range allocs {
|
2018-10-02 20:36:04 +00:00
|
|
|
// Do not consider the resource impact of terminal allocations
|
|
|
|
if alloc.TerminalStatus() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if alloc.AllocatedResources != nil {
|
2020-07-06 22:51:46 +00:00
|
|
|
// Only look at AllocatedPorts if populated, otherwise use pre 0.12 logic
|
|
|
|
// COMPAT(1.0): Remove when network resources struct is removed.
|
|
|
|
if len(alloc.AllocatedResources.Shared.Ports) > 0 {
|
|
|
|
if idx.AddReservedPorts(alloc.AllocatedResources.Shared.Ports) {
|
|
|
|
collide = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Add network resources that are at the task group level
|
|
|
|
if len(alloc.AllocatedResources.Shared.Networks) > 0 {
|
|
|
|
for _, network := range alloc.AllocatedResources.Shared.Networks {
|
|
|
|
if idx.AddReserved(network) {
|
|
|
|
collide = true
|
|
|
|
}
|
2019-07-03 18:29:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 22:51:46 +00:00
|
|
|
for _, task := range alloc.AllocatedResources.Tasks {
|
|
|
|
if len(task.Networks) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
n := task.Networks[0]
|
|
|
|
if idx.AddReserved(n) {
|
|
|
|
collide = true
|
|
|
|
}
|
2018-10-02 20:36:04 +00:00
|
|
|
}
|
2015-09-13 02:34:46 +00:00
|
|
|
}
|
2018-10-02 20:36:04 +00:00
|
|
|
} else {
|
|
|
|
// COMPAT(0.11): Remove in 0.11
|
|
|
|
for _, task := range alloc.TaskResources {
|
|
|
|
if len(task.Networks) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
n := task.Networks[0]
|
|
|
|
if idx.AddReserved(n) {
|
|
|
|
collide = true
|
|
|
|
}
|
2015-09-13 21:56:51 +00:00
|
|
|
}
|
2015-09-13 02:34:46 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-13 21:56:51 +00:00
|
|
|
return
|
2015-09-13 02:34:46 +00:00
|
|
|
}
|
|
|
|
|
2015-09-13 21:56:51 +00:00
|
|
|
// AddReserved is used to add a reserved network usage, returns true
|
|
|
|
// if there is a port collision
|
|
|
|
func (idx *NetworkIndex) AddReserved(n *NetworkResource) (collide bool) {
|
2015-09-13 02:34:46 +00:00
|
|
|
// Add the port usage
|
2020-06-16 15:53:10 +00:00
|
|
|
used := idx.getUsedPortsFor(n.IP)
|
2015-11-24 00:32:30 +00:00
|
|
|
|
|
|
|
for _, ports := range [][]Port{n.ReservedPorts, n.DynamicPorts} {
|
|
|
|
for _, port := range ports {
|
2016-02-20 20:08:27 +00:00
|
|
|
// Guard against invalid port
|
|
|
|
if port.Value < 0 || port.Value >= maxValidPort {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if used.Check(uint(port.Value)) {
|
2015-11-24 00:32:30 +00:00
|
|
|
collide = true
|
|
|
|
} else {
|
2016-02-20 20:08:27 +00:00
|
|
|
used.Set(uint(port.Value))
|
2015-11-24 00:32:30 +00:00
|
|
|
}
|
2015-09-13 21:56:51 +00:00
|
|
|
}
|
2015-09-13 02:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the bandwidth
|
|
|
|
idx.UsedBandwidth[n.Device] += n.MBits
|
2015-09-13 21:56:51 +00:00
|
|
|
return
|
2015-09-13 02:34:46 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 15:53:10 +00:00
|
|
|
func (idx *NetworkIndex) AddReservedPorts(ports AllocatedPorts) (collide bool) {
|
|
|
|
for _, port := range ports {
|
|
|
|
used := idx.getUsedPortsFor(port.HostIP)
|
|
|
|
if port.Value < 0 || port.Value >= maxValidPort {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if used.Check(uint(port.Value)) {
|
|
|
|
collide = true
|
|
|
|
} else {
|
|
|
|
used.Set(uint(port.Value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-02 20:36:04 +00:00
|
|
|
// AddReservedPortRange marks the ports given as reserved on all network
|
|
|
|
// interfaces. The port format is comma delimited, with spans given as n1-n2
|
|
|
|
// (80,100-200,205)
|
|
|
|
func (idx *NetworkIndex) AddReservedPortRange(ports string) (collide bool) {
|
|
|
|
// Convert the ports into a slice of ints
|
|
|
|
resPorts, err := ParsePortRanges(ports)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we create a bitmap for each available network
|
|
|
|
for _, n := range idx.AvailNetworks {
|
2020-06-16 15:53:10 +00:00
|
|
|
idx.getUsedPortsFor(n.IP)
|
2018-10-02 20:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, used := range idx.UsedPorts {
|
|
|
|
for _, port := range resPorts {
|
|
|
|
// Guard against invalid port
|
2020-12-10 15:29:18 +00:00
|
|
|
if port >= maxValidPort {
|
2018-10-02 20:36:04 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
if used.Check(uint(port)) {
|
|
|
|
collide = true
|
|
|
|
} else {
|
|
|
|
used.Set(uint(port))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-30 09:08:12 +00:00
|
|
|
// AddReservedPortsForIP checks whether any reserved ports collide with those
|
|
|
|
// in use for the IP address.
|
2020-06-16 15:53:10 +00:00
|
|
|
func (idx *NetworkIndex) AddReservedPortsForIP(ports string, ip string) (collide bool) {
|
|
|
|
// Convert the ports into a slice of ints
|
|
|
|
resPorts, err := ParsePortRanges(ports)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
used := idx.getUsedPortsFor(ip)
|
|
|
|
for _, port := range resPorts {
|
|
|
|
// Guard against invalid port
|
2020-12-10 15:29:18 +00:00
|
|
|
if port >= maxValidPort {
|
2020-06-16 15:53:10 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
if used.Check(uint(port)) {
|
|
|
|
collide = true
|
|
|
|
} else {
|
|
|
|
used.Set(uint(port))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-13 02:34:46 +00:00
|
|
|
// yieldIP is used to iteratively invoke the callback with
|
|
|
|
// an available IP
|
2015-09-13 21:35:28 +00:00
|
|
|
func (idx *NetworkIndex) yieldIP(cb func(net *NetworkResource, ip net.IP) bool) {
|
2015-09-13 02:34:46 +00:00
|
|
|
inc := func(ip net.IP) {
|
|
|
|
for j := len(ip) - 1; j >= 0; j-- {
|
|
|
|
ip[j]++
|
|
|
|
if ip[j] > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, n := range idx.AvailNetworks {
|
|
|
|
ip, ipnet, err := net.ParseCIDR(n.CIDR)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
|
|
|
|
if cb(n, ip) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 15:53:10 +00:00
|
|
|
func (idx *NetworkIndex) AssignPorts(ask *NetworkResource) (AllocatedPorts, error) {
|
|
|
|
var offer AllocatedPorts
|
|
|
|
|
|
|
|
// index of host network name to slice of reserved ports, used during dynamic port assignment
|
|
|
|
reservedIdx := map[string][]Port{}
|
|
|
|
|
|
|
|
for _, port := range ask.ReservedPorts {
|
|
|
|
reservedIdx[port.HostNetwork] = append(reservedIdx[port.HostNetwork], port)
|
|
|
|
|
|
|
|
// allocPort is set in the inner for loop if a port mapping can be created
|
|
|
|
// if allocPort is still nil after the loop, the port wasn't available for reservation
|
|
|
|
var allocPort *AllocatedPortMapping
|
|
|
|
var addrErr error
|
|
|
|
for _, addr := range idx.AvailAddresses[port.HostNetwork] {
|
|
|
|
used := idx.getUsedPortsFor(addr.Address)
|
|
|
|
// Guard against invalid port
|
|
|
|
if port.Value < 0 || port.Value >= maxValidPort {
|
|
|
|
return nil, fmt.Errorf("invalid port %d (out of range)", port.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if in use
|
|
|
|
if used != nil && used.Check(uint(port.Value)) {
|
2020-07-06 22:51:46 +00:00
|
|
|
return nil, fmt.Errorf("reserved port collision %s=%d", port.Label, port.Value)
|
2020-06-16 15:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
allocPort = &AllocatedPortMapping{
|
|
|
|
Label: port.Label,
|
|
|
|
Value: port.Value,
|
|
|
|
To: port.To,
|
|
|
|
HostIP: addr.Address,
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if allocPort == nil {
|
|
|
|
if addrErr != nil {
|
|
|
|
return nil, addrErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("no addresses available for %q network", port.HostNetwork)
|
|
|
|
}
|
|
|
|
|
|
|
|
offer = append(offer, *allocPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, port := range ask.DynamicPorts {
|
|
|
|
var allocPort *AllocatedPortMapping
|
|
|
|
var addrErr error
|
|
|
|
for _, addr := range idx.AvailAddresses[port.HostNetwork] {
|
|
|
|
used := idx.getUsedPortsFor(addr.Address)
|
|
|
|
// Try to stochastically pick the dynamic ports as it is faster and
|
|
|
|
// lower memory usage.
|
|
|
|
var dynPorts []int
|
2020-06-19 15:29:41 +00:00
|
|
|
// TODO: its more efficient to find multiple dynamic ports at once
|
2020-06-16 15:53:10 +00:00
|
|
|
dynPorts, addrErr = getDynamicPortsStochastic(used, reservedIdx[port.HostNetwork], 1)
|
|
|
|
if addrErr != nil {
|
|
|
|
// Fall back to the precise method if the random sampling failed.
|
|
|
|
dynPorts, addrErr = getDynamicPortsPrecise(used, reservedIdx[port.HostNetwork], 1)
|
|
|
|
if addrErr != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
allocPort = &AllocatedPortMapping{
|
|
|
|
Label: port.Label,
|
|
|
|
Value: dynPorts[0],
|
|
|
|
To: port.To,
|
|
|
|
HostIP: addr.Address,
|
|
|
|
}
|
|
|
|
if allocPort.To == -1 {
|
|
|
|
allocPort.To = allocPort.Value
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if allocPort == nil {
|
|
|
|
if addrErr != nil {
|
|
|
|
return nil, addrErr
|
|
|
|
}
|
|
|
|
|
2020-07-14 16:33:17 +00:00
|
|
|
return nil, fmt.Errorf("no addresses available for %q network", port.HostNetwork)
|
2020-06-16 15:53:10 +00:00
|
|
|
}
|
|
|
|
offer = append(offer, *allocPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
return offer, nil
|
|
|
|
}
|
|
|
|
|
2015-09-13 21:56:51 +00:00
|
|
|
// AssignNetwork is used to assign network resources given an ask.
|
|
|
|
// If the ask cannot be satisfied, returns nil
|
2015-09-13 23:40:53 +00:00
|
|
|
func (idx *NetworkIndex) AssignNetwork(ask *NetworkResource) (out *NetworkResource, err error) {
|
2015-09-23 01:33:29 +00:00
|
|
|
err = fmt.Errorf("no networks available")
|
2015-09-13 21:35:28 +00:00
|
|
|
idx.yieldIP(func(n *NetworkResource, ip net.IP) (stop bool) {
|
2015-09-13 02:34:46 +00:00
|
|
|
// Convert the IP to a string
|
|
|
|
ipStr := ip.String()
|
|
|
|
|
|
|
|
// Check if we would exceed the bandwidth cap
|
|
|
|
availBandwidth := idx.AvailBandwidth[n.Device]
|
|
|
|
usedBandwidth := idx.UsedBandwidth[n.Device]
|
|
|
|
if usedBandwidth+ask.MBits > availBandwidth {
|
2015-09-13 23:40:53 +00:00
|
|
|
err = fmt.Errorf("bandwidth exceeded")
|
2015-09-13 02:34:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-05 23:08:35 +00:00
|
|
|
used := idx.UsedPorts[ipStr]
|
|
|
|
|
2015-09-13 02:34:46 +00:00
|
|
|
// Check if any of the reserved ports are in use
|
|
|
|
for _, port := range ask.ReservedPorts {
|
2016-02-20 20:08:27 +00:00
|
|
|
// Guard against invalid port
|
|
|
|
if port.Value < 0 || port.Value >= maxValidPort {
|
|
|
|
err = fmt.Errorf("invalid port %d (out of range)", port.Value)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if in use
|
|
|
|
if used != nil && used.Check(uint(port.Value)) {
|
2020-07-06 22:51:46 +00:00
|
|
|
err = fmt.Errorf("reserved port collision %s=%d", port.Label, port.Value)
|
2015-09-13 23:40:53 +00:00
|
|
|
return
|
2015-09-13 02:34:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the offer
|
2015-09-13 21:35:28 +00:00
|
|
|
offer := &NetworkResource{
|
2019-08-28 03:59:36 +00:00
|
|
|
Mode: ask.Mode,
|
2015-09-13 02:34:46 +00:00
|
|
|
Device: n.Device,
|
|
|
|
IP: ipStr,
|
2016-05-06 04:32:01 +00:00
|
|
|
MBits: ask.MBits,
|
2020-04-28 03:11:06 +00:00
|
|
|
DNS: ask.DNS,
|
2015-09-13 02:34:46 +00:00
|
|
|
ReservedPorts: ask.ReservedPorts,
|
2015-09-24 01:01:49 +00:00
|
|
|
DynamicPorts: ask.DynamicPorts,
|
2015-09-13 02:34:46 +00:00
|
|
|
}
|
|
|
|
|
2016-08-10 18:47:20 +00:00
|
|
|
// Try to stochastically pick the dynamic ports as it is faster and
|
|
|
|
// lower memory usage.
|
|
|
|
var dynPorts []int
|
|
|
|
var dynErr error
|
2020-06-16 15:53:10 +00:00
|
|
|
dynPorts, dynErr = getDynamicPortsStochastic(used, ask.ReservedPorts, len(ask.DynamicPorts))
|
2016-08-11 23:18:45 +00:00
|
|
|
if dynErr == nil {
|
2016-08-10 18:47:20 +00:00
|
|
|
goto BUILD_OFFER
|
2016-08-05 23:08:35 +00:00
|
|
|
}
|
|
|
|
|
2016-08-10 18:47:20 +00:00
|
|
|
// Fall back to the precise method if the random sampling failed.
|
2020-06-16 15:53:10 +00:00
|
|
|
dynPorts, dynErr = getDynamicPortsPrecise(used, ask.ReservedPorts, len(ask.DynamicPorts))
|
2016-08-10 23:37:26 +00:00
|
|
|
if dynErr != nil {
|
2016-08-10 18:47:20 +00:00
|
|
|
err = dynErr
|
|
|
|
return
|
2016-08-05 23:08:35 +00:00
|
|
|
}
|
|
|
|
|
2016-08-10 18:47:20 +00:00
|
|
|
BUILD_OFFER:
|
|
|
|
for i, port := range dynPorts {
|
|
|
|
offer.DynamicPorts[i].Value = port
|
2019-08-28 03:59:36 +00:00
|
|
|
|
|
|
|
// This syntax allows you to set the mapped to port to the same port
|
|
|
|
// allocated by the scheduler on the host.
|
|
|
|
if offer.DynamicPorts[i].To == -1 {
|
|
|
|
offer.DynamicPorts[i].To = port
|
|
|
|
}
|
2015-09-13 02:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop, we have an offer!
|
|
|
|
out = offer
|
2015-09-13 23:40:53 +00:00
|
|
|
err = nil
|
2015-09-13 02:34:46 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-10 18:47:20 +00:00
|
|
|
// getDynamicPortsPrecise takes the nodes used port bitmap which may be nil if
|
|
|
|
// no ports have been allocated yet, the network ask and returns a set of unused
|
2020-04-20 13:28:19 +00:00
|
|
|
// ports to fulfil the ask's DynamicPorts or an error if it failed. An error
|
2016-08-10 18:47:20 +00:00
|
|
|
// means the ask can not be satisfied as the method does a precise search.
|
2020-06-16 15:53:10 +00:00
|
|
|
func getDynamicPortsPrecise(nodeUsed Bitmap, reserved []Port, numDyn int) ([]int, error) {
|
|
|
|
// Create a copy of the used ports and apply the new reserves
|
|
|
|
var usedSet Bitmap
|
|
|
|
var err error
|
|
|
|
if nodeUsed != nil {
|
|
|
|
usedSet, err = nodeUsed.Copy()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
usedSet, err = NewBitmap(maxValidPort)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, port := range reserved {
|
|
|
|
usedSet.Set(uint(port.Value))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the indexes of the unset
|
|
|
|
availablePorts := usedSet.IndexesInRange(false, MinDynamicPort, MaxDynamicPort)
|
|
|
|
|
|
|
|
// Randomize the amount we need
|
|
|
|
if len(availablePorts) < numDyn {
|
|
|
|
return nil, fmt.Errorf("dynamic port selection failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
numAvailable := len(availablePorts)
|
|
|
|
for i := 0; i < numDyn; i++ {
|
|
|
|
j := rand.Intn(numAvailable)
|
|
|
|
availablePorts[i], availablePorts[j] = availablePorts[j], availablePorts[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
return availablePorts[:numDyn], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getDynamicPortsStochastic takes the nodes used port bitmap which may be nil if
|
|
|
|
// no ports have been allocated yet, the network ask and returns a set of unused
|
|
|
|
// ports to fulfil the ask's DynamicPorts or an error if it failed. An error
|
|
|
|
// does not mean the ask can not be satisfied as the method has a fixed amount
|
|
|
|
// of random probes and if these fail, the search is aborted.
|
|
|
|
func getDynamicPortsStochastic(nodeUsed Bitmap, reservedPorts []Port, count int) ([]int, error) {
|
|
|
|
var reserved, dynamic []int
|
|
|
|
for _, port := range reservedPorts {
|
|
|
|
reserved = append(reserved, port.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
attempts := 0
|
|
|
|
PICK:
|
|
|
|
attempts++
|
|
|
|
if attempts > maxRandPortAttempts {
|
|
|
|
return nil, fmt.Errorf("stochastic dynamic port selection failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
randPort := MinDynamicPort + rand.Intn(MaxDynamicPort-MinDynamicPort)
|
|
|
|
if nodeUsed != nil && nodeUsed.Check(uint(randPort)) {
|
2016-08-10 18:47:20 +00:00
|
|
|
goto PICK
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ports := range [][]int{reserved, dynamic} {
|
|
|
|
if isPortReserved(ports, randPort) {
|
|
|
|
goto PICK
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dynamic = append(dynamic, randPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dynamic, nil
|
|
|
|
}
|
|
|
|
|
2015-09-13 21:36:33 +00:00
|
|
|
// IntContains scans an integer slice for a value
|
2016-08-10 18:47:20 +00:00
|
|
|
func isPortReserved(haystack []int, needle int) bool {
|
2015-09-13 02:34:46 +00:00
|
|
|
for _, item := range haystack {
|
2016-08-10 18:47:20 +00:00
|
|
|
if item == needle {
|
2015-09-13 02:34:46 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2020-06-16 15:53:10 +00:00
|
|
|
|
2021-08-30 09:08:12 +00:00
|
|
|
// AllocatedPortsToNetworkResouce is a COMPAT(1.0) remove when NetworkResource
|
|
|
|
// is no longer used for materialized client view of ports.
|
2020-07-06 22:51:46 +00:00
|
|
|
func AllocatedPortsToNetworkResouce(ask *NetworkResource, ports AllocatedPorts, node *NodeResources) *NetworkResource {
|
2020-06-16 15:53:10 +00:00
|
|
|
out := ask.Copy()
|
|
|
|
|
|
|
|
for i, port := range ask.DynamicPorts {
|
|
|
|
if p, ok := ports.Get(port.Label); ok {
|
|
|
|
out.DynamicPorts[i].Value = p.Value
|
|
|
|
out.DynamicPorts[i].To = p.To
|
|
|
|
}
|
|
|
|
}
|
2020-07-06 22:51:46 +00:00
|
|
|
if len(node.NodeNetworks) > 0 {
|
|
|
|
for _, nw := range node.NodeNetworks {
|
|
|
|
if nw.Mode == "host" {
|
|
|
|
out.IP = nw.Addresses[0].Address
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, nw := range node.Networks {
|
|
|
|
if nw.Mode == "host" {
|
|
|
|
out.IP = nw.IP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-16 15:53:10 +00:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
type ClientHostNetworkConfig struct {
|
|
|
|
Name string `hcl:",key"`
|
|
|
|
CIDR string `hcl:"cidr"`
|
|
|
|
Interface string `hcl:"interface"`
|
|
|
|
ReservedPorts string `hcl:"reserved_ports"`
|
|
|
|
}
|