-
Notifications
You must be signed in to change notification settings - Fork 38.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an IPAllocator type #1594
Merged
Merged
Add an IPAllocator type #1594
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
Copyright 2014 Google Inc. All rights reserved. | ||
|
||
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 service | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"sync" | ||
|
||
"github.com/golang/glog" | ||
) | ||
|
||
type ipAllocator struct { | ||
subnet *net.IPNet | ||
// TODO: This could be smarter, but for now a bitmap will suffice. | ||
lock sync.Mutex // protects 'used' | ||
used []byte // a bitmap of allocated IPs | ||
} | ||
|
||
// newIPAllocator creates and intializes a new ipAllocator object. | ||
// FIXME: resync from storage at startup. | ||
func newIPAllocator(subnet *net.IPNet) *ipAllocator { | ||
if subnet == nil || subnet.IP == nil || subnet.Mask == nil { | ||
return nil | ||
} | ||
|
||
ones, bits := subnet.Mask.Size() | ||
numIps := 1 << uint(bits-ones) | ||
ipa := &ipAllocator{ | ||
subnet: subnet, | ||
used: make([]byte, numIps/8), | ||
} | ||
ipa.used[0] = 0x01 // block the network addr | ||
ipa.used[(numIps/8)-1] = 0x80 // block the broadcast addr | ||
return ipa | ||
} | ||
|
||
// Allocate allocates a specific IP. This is useful when recovering saved state. | ||
func (ipa *ipAllocator) Allocate(ip net.IP) error { | ||
ipa.lock.Lock() | ||
defer ipa.lock.Unlock() | ||
|
||
if !ipa.subnet.Contains(ip) { | ||
return fmt.Errorf("IP %s does not fall within subnet %s", ip, ipa.subnet) | ||
} | ||
offset := ipSub(ip, ipa.subnet.IP) | ||
i := offset / 8 | ||
m := byte(1 << byte(offset%8)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as above wrt to spaces around shift operator. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. gofmt has an opinion on this, and AFAIK we take gofmt as gospel |
||
if ipa.used[i]&m != 0 { | ||
return fmt.Errorf("IP %s is already allocated", ip) | ||
} | ||
ipa.used[i] |= m | ||
return nil | ||
} | ||
|
||
// AllocateNext allocates and returns a new IP. | ||
func (ipa *ipAllocator) AllocateNext() (net.IP, error) { | ||
ipa.lock.Lock() | ||
defer ipa.lock.Unlock() | ||
|
||
for i := range ipa.used { | ||
if ipa.used[i] != 0xff { | ||
freeMask := ^ipa.used[i] | ||
nextBit, err := ffs(freeMask) | ||
if err != nil { | ||
// If this happens, something really weird is going on. | ||
glog.Errorf("ffs(%#x) had an unexpected error: %s", freeMask, err) | ||
return nil, err | ||
} | ||
ipa.used[i] |= 1 << nextBit | ||
offset := (i * 8) + int(nextBit) | ||
ip := ipAdd(copyIP(ipa.subnet.IP), offset) | ||
return ip, nil | ||
} | ||
} | ||
return nil, fmt.Errorf("can't find a free IP in %s", ipa.subnet) | ||
} | ||
|
||
// This is a really dumb implementation of find-first-set-bit. | ||
func ffs(val byte) (uint, error) { | ||
if val == 0 { | ||
return 0, fmt.Errorf("Can't find-first-set on 0") | ||
} | ||
i := uint(0) | ||
for ; i < 8 && (val&(1<<i) == 0); i++ { | ||
} | ||
return i, nil | ||
} | ||
|
||
// Add an offset to an IP address - used for joining network addr and host addr parts. | ||
func ipAdd(ip net.IP, offset int) net.IP { | ||
for i := 0; offset > 0; i++ { | ||
add := offset % 256 | ||
ip[len(ip)-1-i] += byte(add) | ||
offset >>= 8 | ||
} | ||
return ip | ||
} | ||
|
||
// Subtract two IPs, returning the difference as an offset - used or splitting an IP into | ||
// network addr and host addr parts. | ||
func ipSub(lhs, rhs net.IP) int { | ||
// If they are not the same length, normalize them. Make copies because net.IP is | ||
// a slice underneath. Sneaky sneaky. | ||
if len(lhs) != len(rhs) { | ||
lhs = copyIP(lhs).To16() | ||
rhs = copyIP(rhs).To16() | ||
} | ||
offset := 0 | ||
for i := range lhs { | ||
offset *= 256 | ||
offset += int(lhs[i] - rhs[i]) | ||
} | ||
return offset | ||
} | ||
|
||
// Make a copy of a net.IP. It appears to be a value type, but it is actually defined as a | ||
// slice, so value assignment is shallow. Why does a poor dumb user like me need to know | ||
// this sort of implementation detail? | ||
func copyIP(in net.IP) net.IP { | ||
out := make(net.IP, len(in)) | ||
copy(out, in) | ||
return out | ||
} | ||
|
||
// Release de-allocates an IP. | ||
func (ipa *ipAllocator) Release(ip net.IP) error { | ||
ipa.lock.Lock() | ||
defer ipa.lock.Unlock() | ||
|
||
if !ipa.subnet.Contains(ip) { | ||
return fmt.Errorf("IP %s does not fall within subnet %s", ip, ipa.subnet) | ||
} | ||
offset := ipSub(ip, ipa.subnet.IP) | ||
i := offset / 8 | ||
m := byte(1 << byte(offset%8)) | ||
ipa.used[i] &^= m | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inconsistent w/
numIps/8
(no spaces) aboveThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gofmt did this