Skip to content

Commit

Permalink
change vlanid to vni and add vni test and disable consul update
Browse files Browse the repository at this point in the history
  • Loading branch information
WIZARD-CXY committed Jan 10, 2016
1 parent 38ebc8c commit a96923a
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 56 deletions.
4 changes: 2 additions & 2 deletions server/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ func TestDeleteConn(t *testing.T) {
}

// Qos test
func TestCreateQosApi(t *testing.T) {
/*func TestCreateQosApi(t *testing.T) {
d := NewDaemon()
connection := &Connection{
ContainerID: "abc123",
Expand Down Expand Up @@ -615,4 +615,4 @@ func TestUpdateQos(t *testing.T) {
if response.Code != http.StatusOK {
t.Fatalf("Expected %v:\n\tReceived: %v", "200", response.Code)
}
}
}*/
2 changes: 1 addition & 1 deletion server/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (m *SafeMap) Get(k string) interface{} {
return nil
}

// returns false if k is already in the sm and v is same with the old value
// returns false if k is already in the rm and v is same with the old value
func (m *SafeMap) Set(k string, v interface{}) bool {
m.Lock()
defer m.Unlock()
Expand Down
60 changes: 30 additions & 30 deletions server/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func CreateNetwork(name string, subnet *net.IPNet) (*Network, error) {
}

// get the smallest unused vlan id from data store
vlanID, err := allocateVlan()
VNI, err := allocateVNI()

if err != nil {
return nil, err
Expand All @@ -112,11 +112,11 @@ func CreateNetwork(name string, subnet *net.IPNet) (*Network, error) {
if err != nil {
fmt.Printf("Interface with name %s does not exist, Creating it\n", name)

gateway = RequestIP(fmt.Sprint(vlanID), *subnet)
gateway = RequestIP(fmt.Sprint(VNI), *subnet)

network = &Network{name, subnet.String(), gateway.String(), vlanID}
network = &Network{name, subnet.String(), gateway.String(), VNI}

if err = AddInternalPort(ovsClient, bridgeName, name, vlanID); err != nil {
if err = AddInternalPort(ovsClient, bridgeName, name, VNI); err != nil {
return network, err
}
time.Sleep(1 * time.Second)
Expand All @@ -139,16 +139,16 @@ func CreateNetwork(name string, subnet *net.IPNet) (*Network, error) {
ifaceAddr := addr.String()
// even though the interface exist, mark its IP as used by using RequestIP to
// let the ipam and I happy
RequestIP(fmt.Sprint(vlanID), *subnet)
RequestIP(fmt.Sprint(VNI), *subnet)

fmt.Printf("Interface %s already exists with IP %s, subnet %s, in %d vlan\n", name, ifaceAddr, subnet.String(), vlanID)
fmt.Printf("Interface %s already exists with IP %s, subnet %s, in %d vlan\n", name, ifaceAddr, subnet.String(), VNI)

gateway, subnet, err = net.ParseCIDR(ifaceAddr)

if err != nil {
return nil, err
}
network = &Network{name, subnet.String(), gateway.String(), vlanID}
network = &Network{name, subnet.String(), gateway.String(), VNI}
}

netBytes, _ := json.Marshal(network)
Expand All @@ -160,8 +160,8 @@ func CreateNetwork(name string, subnet *net.IPNet) (*Network, error) {
err2 := netAgent.Put(networkStore, name, netBytes, nil)

if err2 == netAgent.OUTDATED {
releaseVlan(vlanID)
ReleaseIP(gateway, *subnet, fmt.Sprint(vlanID))
releaseVNI(VNI)
ReleaseIP(gateway, *subnet, fmt.Sprint(VNI))
return CreateNetwork(name, subnet)
}

Expand Down Expand Up @@ -240,7 +240,7 @@ func DeleteNetwork(name string) error {
if errcode != netAgent.OK {
return errors.New("Error deleting network")
}
releaseVlan(network.VNI)
releaseVNI(network.VNI)

if ovsClient == nil {
return errors.New("OVS not connected")
Expand Down Expand Up @@ -322,7 +322,7 @@ func syncNetwork(d *Daemon) {
}
}

func allocateVlan() (uint, error) {
func allocateVNI() (uint, error) {
vlanBytes, _, ok := netAgent.Get(vlanStore, "vlan")

// not put the key already
Expand All @@ -333,19 +333,19 @@ func allocateVlan() (uint, error) {
curVal := make([]byte, vlanCount/8)
copy(curVal, vlanBytes)

vlanID := util.TestAndSet(vlanBytes)
VNI := util.TestAndSet(vlanBytes)

if vlanID > vlanCount {
return uint(vlanID), errors.New("All vlanID have been used")
if VNI > vlanCount {
return uint(VNI), errors.New("All VNI have been used")
}

netAgent.Put(vlanStore, "vlan", vlanBytes, curVal)

return uint(vlanID), nil
return uint(VNI), nil

}

func releaseVlan(vlanID uint) {
func releaseVNI(VNI uint) {
oldVal, _, ok := netAgent.Get(vlanStore, "vlan")

if !ok {
Expand All @@ -354,10 +354,10 @@ func releaseVlan(vlanID uint) {
curVal := make([]byte, vlanCount/8)
copy(curVal, oldVal)

util.Clear(curVal, vlanID-1)
util.Clear(curVal, VNI-1)
err := netAgent.Put(vlanStore, "vlan", curVal, oldVal)
if err == netAgent.OUTDATED {
releaseVlan(vlanID)
releaseVNI(VNI)
}
}
func GetAvailableGwAddress(bridgeIP string) (gwaddr string, err error) {
Expand Down Expand Up @@ -403,7 +403,7 @@ func GetAvailableSubnet() (subnet *net.IPNet, err error) {
// ipStore manage the cluster ip resource
// key is the vlan/subnet, value is the available ip address bytes
// Get an IP from the unused subnet and mark it as used
func RequestIP(vlanID string, subnet net.IPNet) net.IP {
func RequestIP(VNI string, subnet net.IPNet) net.IP {
ipCount := util.IPCount(subnet)
bc := int(ipCount / 8)
partial := int(math.Mod(ipCount, float64(8)))
Expand All @@ -412,7 +412,7 @@ func RequestIP(vlanID string, subnet net.IPNet) net.IP {
bc += 1
}

oldArray, _, ok := netAgent.Get(ipStore, vlanID+"-"+subnet.String())
oldArray, _, ok := netAgent.Get(ipStore, VNI+"-"+subnet.String())

if !ok {
oldArray = make([]byte, bc)
Expand All @@ -424,10 +424,10 @@ func RequestIP(vlanID string, subnet net.IPNet) net.IP {

pos := util.TestAndSet(newArray)

err := netAgent.Put(ipStore, vlanID+"-"+subnet.String(), newArray, oldArray)
err := netAgent.Put(ipStore, VNI+"-"+subnet.String(), newArray, oldArray)

if err == netAgent.OUTDATED {
return RequestIP(vlanID, subnet)
return RequestIP(VNI, subnet)
}

var num uint32
Expand All @@ -454,8 +454,8 @@ func RequestIP(vlanID string, subnet net.IPNet) net.IP {
}

// Mark a specified ip as used, return true as success
func MarkUsed(vlanID string, addr net.IP, subnet net.IPNet) bool {
oldArray, _, ok := netAgent.Get(ipStore, vlanID+"-"+subnet.String())
func MarkUsed(VNI string, addr net.IP, subnet net.IPNet) bool {
oldArray, _, ok := netAgent.Get(ipStore, VNI+"-"+subnet.String())

if !ok {
// the kv pair not exist yet
Expand All @@ -478,19 +478,19 @@ func MarkUsed(vlanID string, addr net.IP, subnet net.IPNet) bool {

util.Set(newArray, pos)

err2 := netAgent.Put(ipStore, vlanID+"-"+subnet.String(), newArray, oldArray)
err2 := netAgent.Put(ipStore, VNI+"-"+subnet.String(), newArray, oldArray)

if err2 == netAgent.OUTDATED {
MarkUsed(vlanID, addr, subnet)
MarkUsed(VNI, addr, subnet)
}

return true

}

// Release the given IP from the subnet of vlan
func ReleaseIP(addr net.IP, subnet net.IPNet, vlanID string) bool {
oldArray, _, ok := netAgent.Get(ipStore, vlanID+"-"+subnet.String())
func ReleaseIP(addr net.IP, subnet net.IPNet, VNI string) bool {
oldArray, _, ok := netAgent.Get(ipStore, VNI+"-"+subnet.String())

if !ok {
return false
Expand All @@ -512,10 +512,10 @@ func ReleaseIP(addr net.IP, subnet net.IPNet, vlanID string) bool {

util.Clear(newArray, pos)

err2 := netAgent.Put(ipStore, vlanID+"-"+subnet.String(), newArray, oldArray)
err2 := netAgent.Put(ipStore, VNI+"-"+subnet.String(), newArray, oldArray)

if err2 == netAgent.OUTDATED {
return ReleaseIP(addr, subnet, vlanID)
return ReleaseIP(addr, subnet, VNI)
}

return true
Expand Down
60 changes: 40 additions & 20 deletions server/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var subnetArray []*net.IPNet
var bridgeUUID string

func TestStartAgent(t *testing.T) {
err := InitAgent("eth1", true)
err := InitAgent("eth0", true)

if err != nil {
t.Errorf("Error starting agent")
Expand Down Expand Up @@ -81,6 +81,45 @@ func TestGetNetwork(t *testing.T) {
}
}

func TestNetworkCleanup(t *testing.T) {
if os.Getuid() != 0 {
msg := "Skipped TestNetworkCleanup test because it requires root privileges."
fmt.Println(msg)
t.Skip(msg)
}
for i := 0; i < len(subnetArray); i++ {
err := DeleteNetwork(fmt.Sprintf("Network-%d", i+1))
if err != nil {
t.Error("Error Deleting Network", err)
}
}

// delete the ovs bridge
if err := DeleteBridge(); err != nil {
t.Error("Delete ovs bridge failed", err)
}
}

func TestAllocateandReleaseVNI(t *testing.T) {
for i := 1; i <= 10; i++ {
vni, err := allocateVNI()

if err != nil || vni != uint(i) {
t.Error("1.allocate vni wrong at", vni, i)
}
}
for i := 1; i <= 5; i++ {
releaseVNI(uint(2 * i))
}

for i := 1; i <= 5; i++ {
vni, err := allocateVNI()
if err != nil || vni != uint(2*i) {
t.Error("2.allocate vni wrong at", vni, i)
}
}
}

func TestRequestandReleaseIP(t *testing.T) {
TestCount := 5

Expand Down Expand Up @@ -118,25 +157,6 @@ func TestRequestandReleaseIP(t *testing.T) {
}
}

func TestNetworkCleanup(t *testing.T) {
if os.Getuid() != 0 {
msg := "Skipped TestNetworkCleanup test because it requires root privileges."
fmt.Println(msg)
t.Skip(msg)
}
for i := 0; i < len(subnetArray); i++ {
err := DeleteNetwork(fmt.Sprintf("Network-%d", i+1))
if err != nil {
t.Error("Error Deleting Network", err)
}
}

// delete the ovs bridge
if err := DeleteBridge(bridgeUUID); err != nil {
t.Error("Delete ovs bridge failed", err)
}
}

func TestLeaveCluster(t *testing.T) {
if err := leave(); err != nil {
t.Error("Error leaving the cluster")
Expand Down
4 changes: 2 additions & 2 deletions server/ovs_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ func CreateOVSBridge(ovsClient *libovsdb.OvsdbClient, bridgeName string) (string
mutateUuid := []libovsdb.UUID{libovsdb.UUID{namedBridgeUuid}}
mutateSet, _ := libovsdb.NewOvsSet(mutateUuid)
mutation := libovsdb.NewMutation("bridges", "insert", mutateSet)
fmt.Println()
fmt.Println("+++++++++++++++", getRootUuid())
/* fmt.Println()
fmt.Println("+++++++++++++++", getRootUuid())*/

condition := libovsdb.NewCondition("_uuid", "==", libovsdb.UUID{getRootUuid()})

Expand Down
3 changes: 2 additions & 1 deletion vendor/github.com/hashicorp/consul/command/agent/command.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a96923a

Please sign in to comment.