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"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// MinDynamicPort is the smallest dynamic port generated
|
|
|
|
MinDynamicPort = 20000
|
|
|
|
|
|
|
|
// MaxDynamicPort is the largest dynamic port generated
|
|
|
|
MaxDynamicPort = 60000
|
|
|
|
|
|
|
|
// maxRandPortAttempts is the maximum number of attempt
|
|
|
|
// to assign a random port
|
|
|
|
maxRandPortAttempts = 20
|
|
|
|
)
|
|
|
|
|
|
|
|
// NetworkIndex is used to index the available network resources
|
|
|
|
// and the used network resources on a machine given allocations
|
|
|
|
type NetworkIndex struct {
|
2015-09-13 21:35:28 +00:00
|
|
|
AvailNetworks []*NetworkResource // List of available networks
|
2015-09-13 02:34:46 +00:00
|
|
|
AvailBandwidth map[string]int // Bandwidth by device
|
|
|
|
UsedPorts map[string]map[int]struct{} // Ports by IP
|
|
|
|
UsedBandwidth map[string]int // Bandwidth by device
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNetworkIndex is used to construct a new network index
|
|
|
|
func NewNetworkIndex() *NetworkIndex {
|
|
|
|
return &NetworkIndex{
|
|
|
|
AvailBandwidth: make(map[string]int),
|
|
|
|
UsedPorts: make(map[string]map[int]struct{}),
|
|
|
|
UsedBandwidth: make(map[string]int),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-13 21:56:51 +00:00
|
|
|
// Overcommitted checks if the network is overcommitted
|
|
|
|
func (idx *NetworkIndex) Overcommitted() bool {
|
|
|
|
for device, used := range idx.UsedBandwidth {
|
|
|
|
avail := idx.AvailBandwidth[device]
|
|
|
|
if used > avail {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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) {
|
2015-09-13 02:34:46 +00:00
|
|
|
// Add the available CIDR blocks
|
|
|
|
for _, n := range node.Resources.Networks {
|
|
|
|
if n.CIDR != "" {
|
|
|
|
idx.AvailNetworks = append(idx.AvailNetworks, n)
|
|
|
|
idx.AvailBandwidth[n.Device] = n.MBits
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the reserved resources
|
|
|
|
if r := node.Reserved; r != nil {
|
|
|
|
for _, n := range r.Networks {
|
2015-09-13 21:56:51 +00:00
|
|
|
if idx.AddReserved(n) {
|
|
|
|
collide = true
|
|
|
|
}
|
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
|
|
|
// 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 {
|
|
|
|
for _, task := range alloc.TaskResources {
|
|
|
|
if len(task.Networks) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
n := task.Networks[0]
|
2015-09-13 21:56:51 +00:00
|
|
|
if idx.AddReserved(n) {
|
|
|
|
collide = true
|
|
|
|
}
|
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
|
|
|
|
used := idx.UsedPorts[n.IP]
|
|
|
|
if used == nil {
|
|
|
|
used = make(map[int]struct{})
|
|
|
|
idx.UsedPorts[n.IP] = used
|
|
|
|
}
|
|
|
|
for _, port := range n.ReservedPorts {
|
2015-09-13 21:56:51 +00:00
|
|
|
if _, ok := used[port]; ok {
|
|
|
|
collide = true
|
|
|
|
} else {
|
|
|
|
used[port] = struct{}{}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if any of the reserved ports are in use
|
|
|
|
for _, port := range ask.ReservedPorts {
|
|
|
|
if _, ok := idx.UsedPorts[ipStr][port]; ok {
|
2015-09-13 23:40:53 +00:00
|
|
|
err = fmt.Errorf("reserved port collision")
|
|
|
|
return
|
2015-09-13 02:34:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the offer
|
2015-09-13 21:35:28 +00:00
|
|
|
offer := &NetworkResource{
|
2015-09-13 02:34:46 +00:00
|
|
|
Device: n.Device,
|
|
|
|
IP: ipStr,
|
|
|
|
ReservedPorts: ask.ReservedPorts,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we need to generate any ports
|
|
|
|
for i := 0; i < ask.DynamicPorts; i++ {
|
|
|
|
attempts := 0
|
|
|
|
PICK:
|
|
|
|
attempts++
|
|
|
|
if attempts > maxRandPortAttempts {
|
2015-09-13 23:40:53 +00:00
|
|
|
err = fmt.Errorf("dynamic port selection failed")
|
2015-09-13 02:34:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
randPort := MinDynamicPort + rand.Intn(MaxDynamicPort-MinDynamicPort)
|
|
|
|
if _, ok := idx.UsedPorts[ipStr][randPort]; ok {
|
|
|
|
goto PICK
|
|
|
|
}
|
2015-09-13 21:36:33 +00:00
|
|
|
if IntContains(offer.ReservedPorts, randPort) {
|
2015-09-13 02:34:46 +00:00
|
|
|
goto PICK
|
|
|
|
}
|
|
|
|
offer.ReservedPorts = append(offer.ReservedPorts, randPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2015-09-13 21:36:33 +00:00
|
|
|
// IntContains scans an integer slice for a value
|
|
|
|
func IntContains(haystack []int, needle int) bool {
|
2015-09-13 02:34:46 +00:00
|
|
|
for _, item := range haystack {
|
|
|
|
if item == needle {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|