//Random Key updates func keyUpdates(key string) { valArray, _, _ := ecc.Get("network", key) currVal := make([]byte, len(valArray)) copy(currVal, valArray) valArray = []byte("value1") ecc.Put("network", key, valArray, currVal) time.Sleep(time.Second * 2) updArray := []byte("value2") ecc.Put("network", key, updArray, valArray) time.Sleep(time.Second * 2) }
func releaseVlan(vlan uint) { vlanArray, _, ok := ecc.Get(vlanStore, "vlan") currVal := make([]byte, vlanCount/8) copy(currVal, vlanArray) if !ok { vlanArray = make([]byte, vlanCount/8) } clearBit(vlanArray, vlan-1) eccerr := ecc.Put(vlanStore, "vlan", vlanArray, currVal) if eccerr == ecc.OUTDATED { releaseVlan(vlan) } }
func IPAMRelease(address net.IP, subnet net.IPNet) bool { addrArray, _, ok := ecc.Get(dataStore, subnet.String()) currVal := make([]byte, len(addrArray)) copy(currVal, addrArray) if !ok { return false } pos := getBitPosition(address, subnet) clearBit(addrArray, pos-1) eccerr := ecc.Put(dataStore, subnet.String(), addrArray, currVal) if eccerr == ecc.OUTDATED { return IPAMRelease(address, subnet) } return true }
func allocateVlan() (uint, error) { vlanArray, _, ok := ecc.Get(vlanStore, "vlan") currVal := make([]byte, vlanCount/8) copy(currVal, vlanArray) if !ok { vlanArray = make([]byte, vlanCount/8) } vlan := testAndSetBit(vlanArray) if vlan >= vlanCount { return vlanCount, errors.New("Vlan unavailable") } eccerr := ecc.Put(vlanStore, "vlan", vlanArray, currVal) if eccerr == ecc.OUTDATED { return allocateVlan() } return vlan, nil }
func IPAMRequest(subnet net.IPNet) net.IP { bits := bitCount(subnet) bc := int(bits / 8) partial := int(math.Mod(bits, float64(8))) if partial != 0 { bc += 1 } addrArray, _, ok := ecc.Get(dataStore, subnet.String()) currVal := make([]byte, len(addrArray)) copy(currVal, addrArray) if !ok { addrArray = make([]byte, bc) } pos := testAndSetBit(addrArray) eccerr := ecc.Put(dataStore, subnet.String(), addrArray, currVal) if eccerr == ecc.OUTDATED { return IPAMRequest(subnet) } return getIP(subnet, pos) }
func CreateNetwork(id string, subnet *net.IPNet) (*Network, error) { network, err := GetNetwork(id) if err == nil { log.Debugf("Network '%s' found", id) return network, nil } vlan, err := allocateVlan() if err != nil { log.Debugf("Unable to allocate VLAN for Network '%s'. Error: %v", id, err.Error()) return nil, err } var gateway net.IP addr, err := GetIfaceAddr(id) if err != nil { log.Debugf("Interface with name %s does not exist. Creating it.", id) if ovs == nil { return nil, errors.New("OVS not connected") } // Interface does not exist, use the generated subnet gateway = IPAMRequest(*subnet) network = &Network{id, subnet.String(), gateway.String(), vlan} if err = AddInternalPort(ovs, defaultBridgeName, network.ID, vlan); err != nil { return network, err } // TODO : Lame. Remove the sleep. This is required now to keep netlink happy // in the next step to find the created interface. time.Sleep(time.Second * 1) gatewayNet := &net.IPNet{gateway, subnet.Mask} log.Debugf("Setting address %s on %s", gatewayNet.String(), network.ID) if err = SetMtu(network.ID, mtu); err != nil { return network, err } if err = SetInterfaceIp(network.ID, gatewayNet.String()); err != nil { return network, err } if err = InterfaceUp(network.ID); err != nil { return network, err } } else { log.Debugf("Interface with name %s already exists", id) ifaceAddr := addr.String() gateway, subnet, err = net.ParseCIDR(ifaceAddr) if err != nil { return nil, err } network = &Network{id, subnet.String(), gateway.String(), vlan} } data, err := json.Marshal(network) if err != nil { return nil, err } eccerr := ecc.Put(networkStore, id, data, nil) if eccerr == ecc.OUTDATED { releaseVlan(vlan) IPAMRelease(gateway, *subnet) return CreateNetwork(id, subnet) } if err = setupIPTables(network.ID, network.Subnet); err != nil { return network, err } return network, nil }