diff --git a/api/src/com/cloud/vm/NicProfile.java b/api/src/com/cloud/vm/NicProfile.java index 599b58b7ffe..1786e2b2235 100644 --- a/api/src/com/cloud/vm/NicProfile.java +++ b/api/src/com/cloud/vm/NicProfile.java @@ -5,11 +5,11 @@ package com.cloud.vm; import java.net.URI; +import com.cloud.network.Network; import com.cloud.network.Networks.AddressFormat; import com.cloud.network.Networks.BroadcastDomainType; import com.cloud.network.Networks.Mode; import com.cloud.network.Networks.TrafficType; -import com.cloud.network.Network; import com.cloud.resource.Resource; import com.cloud.resource.Resource.ReservationStrategy; @@ -235,4 +235,23 @@ public class NicProfile { public void setReservationId(String reservationId) { this.reservationId = reservationId; } + + public void deallocate() { + this.gateway = null; + this.mode = null; + this.format = null; + this.broadcastType = null; + this.trafficType = null; + this.ip4Address = null; + this.ip6Address = null; + this.macAddress = null; + this.reservationId = null; + this.strategy = null; + this.deviceId = null; + this.broadcastUri = null; + this.isolationUri = null; + this.netmask = null; + this.dns1 = null; + this.dns2 = null; + } } diff --git a/server/src/com/cloud/network/NetworkManager.java b/server/src/com/cloud/network/NetworkManager.java index 3623f7453b8..052c34af623 100644 --- a/server/src/com/cloud/network/NetworkManager.java +++ b/server/src/com/cloud/network/NetworkManager.java @@ -51,6 +51,18 @@ import com.cloud.vm.VirtualMachineProfile; public interface NetworkManager extends NetworkService { public static final boolean USE_POD_VLAN = false; + /** + * Assigns a new public ip address. + * + * @param dcId + * @param owner + * @param type + * @param networkId + * @return + * @throws InsufficientAddressCapacityException + */ + PublicIp assignPublicIpAddress(long dcId, Account owner, VlanType type, Long networkId) throws InsufficientAddressCapacityException; + /** * assigns a source nat ip address to an account within a network. * @@ -115,8 +127,6 @@ public interface NetworkManager extends NetworkService { boolean applyRules(Ip ip, List rules, boolean continueOnError) throws ResourceUnavailableException; - PublicIp fetchNewPublicIp(long dcId, VlanType vlanUse, Account owner, Long networkId, boolean sourceNat) throws InsufficientAddressCapacityException; - Commands getAssociateIPCommands(DomainRouterVO router, List ipAddrList, boolean add, long vmId, Commands cmds); } diff --git a/server/src/com/cloud/network/NetworkManagerImpl.java b/server/src/com/cloud/network/NetworkManagerImpl.java index bd14abe5f28..d1723ef6eb1 100755 --- a/server/src/com/cloud/network/NetworkManagerImpl.java +++ b/server/src/com/cloud/network/NetworkManagerImpl.java @@ -26,6 +26,7 @@ import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Random; import java.util.concurrent.ScheduledExecutorService; import javax.ejb.Local; @@ -183,6 +184,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag Adapters _networkElements; private HashMap _systemNetworks = new HashMap(5); + Random _rand = new Random(System.currentTimeMillis()); ScheduledExecutorService _executor; @@ -194,8 +196,13 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag HashMap _lastNetworkIdsToFree = new HashMap(); - @Override @DB - public PublicIp fetchNewPublicIp(long dcId, VlanType vlanUse, Account owner, Long networkId, boolean sourceNat) throws InsufficientAddressCapacityException { + @Override + public PublicIp assignPublicIpAddress(long dcId, Account owner, VlanType type, Long networkId) throws InsufficientAddressCapacityException { + return fetchNewPublicIp(dcId, owner, type, networkId, false, true); + } + + @DB + public PublicIp fetchNewPublicIp(long dcId, Account owner, VlanType vlanUse, Long networkId, boolean sourceNat, boolean assign) throws InsufficientAddressCapacityException { Transaction txn = Transaction.currentTxn(); txn.start(); SearchCriteria sc = AssignIpAddressSearch.create(); @@ -221,7 +228,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag addr.setAllocatedTime(new Date()); addr.setAllocatedInDomainId(owner.getDomainId()); addr.setAllocatedToAccountId(owner.getId()); - addr.setState(IpAddress.State.Allocating); + addr.setState(assign ? IpAddress.State.Allocated : IpAddress.State.Allocating); if (vlanUse == VlanType.DirectAttached) { addr.setState(IpAddress.State.Allocated); @@ -234,7 +241,9 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } txn.commit(); - return new PublicIp(addr, _vlanDao.findById(addr.getVlanId())); + long macAddress = addr.getMacAddress() | 0x060000000000l | (((long)_rand.nextInt(32768) << 25) & 0x00fffe000000l); + + return new PublicIp(addr, _vlanDao.findById(addr.getVlanId()), macAddress); } @Override @@ -275,7 +284,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag s_logger.debug("assigning a new ip address in " + dcId + " to " + owner); } - ip = fetchNewPublicIp(dcId, VlanType.VirtualNetwork, owner, network.getId(), true); + ip = fetchNewPublicIp(dcId, owner, VlanType.VirtualNetwork, network.getId(), true, false); sourceNat = ip.ip(); sourceNat.setState(IpAddress.State.Allocated); _ipAddressDao.update(sourceNat.getAddress(), sourceNat); @@ -296,7 +305,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } assert(sourceNat != null) : "How do we get a bunch of ip addresses but none of them are source nat? account=" + ownerId + "; dc=" + dcId; - ip = new PublicIp(sourceNat, _vlanDao.findById(sourceNat.getVlanId())); + ip = new PublicIp(sourceNat, _vlanDao.findById(sourceNat.getVlanId()), sourceNat.getMacAddress() | 0x060000000000l | (((long)_rand.nextInt(32768) << 25) & 0x00fffe000000l)); } UserStatisticsVO stats = _userStatsDao.findBy(ownerId, dcId); @@ -586,7 +595,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } txn.start(); - ip = fetchNewPublicIp(zoneId, VlanType.VirtualNetwork, owner, network.getId(), false); + ip = fetchNewPublicIp(zoneId, owner, VlanType.VirtualNetwork, network.getId(), false, false); if (ip == null) { throw new InsufficientAddressCapacityException("Unable to find available public IP addresses", DataCenter.class, zoneId); @@ -739,7 +748,6 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag Integer rateMbps = getIntegerConfigValue(Config.NetworkThrottlingRate.key(), null); Integer multicastRateMbps = getIntegerConfigValue(Config.MulticastThrottlingRate.key(), null); - NetworkOfferingVO publicNetworkOffering = new NetworkOfferingVO(NetworkOfferingVO.SystemVmPublicNetwork, TrafficType.Public, null); publicNetworkOffering = _networkOfferingDao.persistDefaultNetworkOffering(publicNetworkOffering); _systemNetworks.put(NetworkOfferingVO.SystemVmPublicNetwork, publicNetworkOffering); @@ -1031,51 +1039,51 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag Transaction.currentTxn(); Pair implemented = new Pair(null, null); - NetworkVO config = _networksDao.acquireInLockTable(networkId); - if (config == null) { + NetworkVO network = _networksDao.acquireInLockTable(networkId); + if (network == null) { throw new ConcurrentOperationException("Unable to acquire network configuration: " + networkId); } try { - NetworkGuru guru = _networkGurus.get(config.getGuruName()); - Network.State state = config.getState(); + NetworkGuru guru = _networkGurus.get(network.getGuruName()); + Network.State state = network.getState(); if (state == Network.State.Implemented || state == Network.State.Setup) { - implemented.set(guru, config); + implemented.set(guru, network); return implemented; } if (s_logger.isDebugEnabled()) { - s_logger.debug("Asking " + guru + " to implement " + config); + s_logger.debug("Asking " + guru + " to implement " + network); } - NetworkOfferingVO offering = _networkOfferingDao.findById(config.getNetworkOfferingId()); + NetworkOfferingVO offering = _networkOfferingDao.findById(network.getNetworkOfferingId()); - Network result = guru.implement(config, offering, dest, context); - config.setCidr(result.getCidr()); - config.setBroadcastUri(result.getBroadcastUri()); - config.setGateway(result.getGateway()); - config.setDns1(result.getDns1()); - config.setDns2(result.getDns2()); - config.setMode(result.getMode()); - config.setState(Network.State.Implemented); - _networksDao.update(networkId, config); + Network result = guru.implement(network, offering, dest, context); + network.setCidr(result.getCidr()); + network.setBroadcastUri(result.getBroadcastUri()); + network.setGateway(result.getGateway()); + network.setDns1(result.getDns1()); + network.setDns2(result.getDns2()); + network.setMode(result.getMode()); + network.setState(Network.State.Implemented); + _networksDao.update(networkId, network); for (NetworkElement element : _networkElements) { if (s_logger.isDebugEnabled()) { - s_logger.debug("Asking " + element.getName() + " to implmenet " + config); + s_logger.debug("Asking " + element.getName() + " to implmenet " + network); } try { - element.implement(config, offering, dest, context); + element.implement(network, offering, dest, context); } catch (InsufficientCapacityException e) { throw new ResourceUnavailableException("Unable to start domain router for this VM", e); } } - implemented.set(guru, config); + implemented.set(guru, network); return implemented; } finally { if (implemented.first() == null) { - s_logger.debug("Cleaning up because we're unable to implement network " + config); + s_logger.debug("Cleaning up because we're unable to implement network " + network); } _networksDao.releaseFromLockTable(networkId); } @@ -1087,20 +1095,20 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag for (NicVO nic : nics) { Pair implemented = implementNetwork(nic.getNetworkId(), dest, context); NetworkGuru concierge = implemented.first(); - NetworkVO config = implemented.second(); + NetworkVO network = implemented.second(); NicProfile profile = null; if (nic.getReservationStrategy() == ReservationStrategy.Start) { nic.setState(Resource.State.Reserving); _nicDao.update(nic.getId(), nic); URI broadcastUri = nic.getBroadcastUri(); if (broadcastUri == null) { - config.getBroadcastUri(); + network.getBroadcastUri(); } URI isolationUri = nic.getIsolationUri(); - profile = new NicProfile(nic, config, broadcastUri, isolationUri); - concierge.reserve(profile, config, vmProfile, dest, context); + profile = new NicProfile(nic, network, broadcastUri, isolationUri); + concierge.reserve(profile, network, vmProfile, dest, context); nic.setIp4Address(profile.getIp4Address()); nic.setIp6Address(profile.getIp6Address()); nic.setMacAddress(profile.getMacAddress()); @@ -1113,17 +1121,18 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag nic.setAddressFormat(profile.getFormat()); _nicDao.update(nic.getId(), nic); } else { - profile = new NicProfile(nic, config, nic.getBroadcastUri(), nic.getIsolationUri()); + profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri()); } for (NetworkElement element : _networkElements) { if (s_logger.isDebugEnabled()) { s_logger.debug("Asking " + element.getName() + " to prepare for " + nic); } - element.prepare(config, profile, vmProfile, dest, context); + element.prepare(network, profile, vmProfile, dest, context); } vmProfile.addNic(profile); + _networksDao.changeActiveNicsBy(network.getId(), 1); } } @@ -1131,17 +1140,18 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag public void release(VirtualMachineProfile vmProfile) { List nics = _nicDao.listBy(vmProfile.getId()); for (NicVO nic : nics) { - NetworkVO config = _networksDao.findById(nic.getNetworkId()); + NetworkVO network = _networksDao.findById(nic.getNetworkId()); if (nic.getReservationStrategy() == ReservationStrategy.Start) { - NetworkGuru concierge = _networkGurus.get(config.getGuruName()); + NetworkGuru concierge = _networkGurus.get(network.getGuruName()); nic.setState(Resource.State.Releasing); _nicDao.update(nic.getId(), nic); - NicProfile profile = new NicProfile(nic, config, null, null); + NicProfile profile = new NicProfile(nic, network, null, null); if (!concierge.release(profile, vmProfile, nic.getReservationId())) { nic.setState(Resource.State.Allocated); _nicDao.update(nic.getId(), nic); } } + _networksDao.changeActiveNicsBy(network.getId(), -1); } } @@ -1150,8 +1160,6 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag return _nicDao.listBy(vm.getId()); } - - public static boolean isAdmin(short accountType) { return ((accountType == Account.ACCOUNT_TYPE_ADMIN) || (accountType == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) || @@ -1177,7 +1185,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag // Verify input parameters Account accountByIp = findAccountByIpAddress(ipAddress); - if(accountByIp == null) { + if(accountByIp == null) { throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find account owner for ip " + ipAddress); } @@ -1622,6 +1630,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (mac == null) { throw new InsufficientAddressCapacityException("Unable to create another mac address", Network.class, networkId); } + return mac; } @@ -1760,6 +1769,16 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } else { networkId = networks.get(0).getId(); } + + for (Network network : networks) { + if (network.getGuestType() == GuestIpType.Virtual) { + s_logger.debug("Creating a source natp ip for " + network); + PublicIp ip = assignSourceNatIpAddress(owner, network, userId); + if (ip == null) { + throw new InsufficientAddressCapacityException("Unable to assign source nat ip address to owner for this network", DataCenter.class, zoneId); + } + } + } //Don't pass owner to create vlan when network offering is of type Direct if (networkOffering.getGuestIpType() == GuestIpType.Direct) { diff --git a/server/src/com/cloud/network/NetworkVO.java b/server/src/com/cloud/network/NetworkVO.java index d180c837cc4..5556fa6b262 100644 --- a/server/src/com/cloud/network/NetworkVO.java +++ b/server/src/com/cloud/network/NetworkVO.java @@ -410,6 +410,4 @@ public class NetworkVO implements Network { buf.append(id).append("|").append(trafficType.toString()).append("|").append(networkOfferingId).append("]"); return buf.toString(); } - - } diff --git a/server/src/com/cloud/network/addr/PublicIp.java b/server/src/com/cloud/network/addr/PublicIp.java index 6553e1c1f08..9d0e726b6e0 100644 --- a/server/src/com/cloud/network/addr/PublicIp.java +++ b/server/src/com/cloud/network/addr/PublicIp.java @@ -22,6 +22,7 @@ import java.util.Date; import com.cloud.dc.VlanVO; import com.cloud.network.IPAddressVO; import com.cloud.network.IpAddress; +import com.cloud.utils.net.NetUtils; /** * PublicIp is a combo object of IPAddressVO and VLAN information. @@ -29,10 +30,12 @@ import com.cloud.network.IpAddress; public class PublicIp implements IpAddress { IPAddressVO _addr; VlanVO _vlan; + String macAddress; - public PublicIp(IPAddressVO addr, VlanVO vlan) { + public PublicIp(IPAddressVO addr, VlanVO vlan, long macAddress) { _addr = addr; _vlan = vlan; + this.macAddress = NetUtils.long2Mac(macAddress); } @Override @@ -40,10 +43,6 @@ public class PublicIp implements IpAddress { return _addr.getAddress(); } - public long getMacAddress() { - return _addr.getMacAddress(); - } - public String getNetmask() { return _vlan.getVlanNetmask(); } @@ -118,4 +117,8 @@ public class PublicIp implements IpAddress { public VlanVO vlan() { return _vlan; } + + public String getMacAddress() { + return macAddress; + } } diff --git a/server/src/com/cloud/network/dao/NetworkDao.java b/server/src/com/cloud/network/dao/NetworkDao.java index abfec4f3491..89eb9039943 100644 --- a/server/src/com/cloud/network/dao/NetworkDao.java +++ b/server/src/com/cloud/network/dao/NetworkDao.java @@ -29,8 +29,7 @@ public interface NetworkDao extends GenericDao { List listBy(long accountId); List listBy(long accountId, long offeringId, long dataCenterId); List listBy(long accountId, long dataCenterId, GuestIpType type); - @Override - NetworkVO persist(NetworkVO network); + NetworkVO persist(NetworkVO network, boolean gc); void addAccountToNetwork(long networkId, long accountId); SearchBuilder createSearchBuilderForAccount(); List getNetworksForOffering(long offeringId, long dataCenterId, long accountId); @@ -46,4 +45,8 @@ public interface NetworkDao extends GenericDao { List listBy(long accountId, long networkId); List listBy(long zoneId, String broadcastUri); + + void changeActiveNicsBy(long networkId, int nicsCount); + + int getActiveNicsIn(long networkId); } diff --git a/server/src/com/cloud/network/dao/NetworkDaoImpl.java b/server/src/com/cloud/network/dao/NetworkDaoImpl.java index 9c51e46ebcd..5ced8ef191d 100644 --- a/server/src/com/cloud/network/dao/NetworkDaoImpl.java +++ b/server/src/com/cloud/network/dao/NetworkDaoImpl.java @@ -17,8 +17,6 @@ */ package com.cloud.network.dao; -import java.sql.PreparedStatement; -import java.sql.SQLException; import java.util.List; import java.util.Random; @@ -41,7 +39,6 @@ import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.SequenceFetcher; import com.cloud.utils.db.Transaction; -import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.NetUtils; @Local(value=NetworkDao.class) @DB(txn=false) @@ -53,6 +50,7 @@ public class NetworkDaoImpl extends GenericDaoBase implements N final SearchBuilder ZoneBroadcastUriSearch; NetworkAccountDaoImpl _accountsDao = new NetworkAccountDaoImpl(); + NetworkOpDaoImpl _opDao = new NetworkOpDaoImpl(); final TableGenerator _tgMacAddress; Random _rand = new Random(System.currentTimeMillis()); long _prefix = 0x2; @@ -144,18 +142,13 @@ public class NetworkDaoImpl extends GenericDaoBase implements N } @Override @DB - public NetworkVO persist(NetworkVO config) { + public NetworkVO persist(NetworkVO config, boolean gc) { Transaction txn = Transaction.currentTxn(); txn.start(); config = super.persist(config); addAccountToNetworkConfiguration(config.getId(), config.getAccountId(), true); - try { - PreparedStatement pstmt = txn.prepareAutoCloseStatement("INSERT INTO op_networks (id) VALUES(?)"); - pstmt.setLong(1, config.getId()); - pstmt.executeUpdate(); - } catch (SQLException e) { - throw new CloudRuntimeException("Problem inserting into the op_network_configurations"); - } + NetworkOpVO op = new NetworkOpVO(config.getId(), gc); + _opDao.persist(op); txn.commit(); return config; } @@ -208,10 +201,21 @@ public class NetworkDaoImpl extends GenericDaoBase implements N return listBy(sc); } + @Override public List listBy(long zoneId, String broadcastUri) { SearchCriteria sc = ZoneBroadcastUriSearch.create(); sc.setParameters("dataCenterId", zoneId); sc.setParameters("broadcastUri", broadcastUri); return search(sc, null); } + + @Override + public void changeActiveNicsBy(long networkId, int count) { + _opDao.changeActiveNicsBy(networkId, count); + } + + @Override + public int getActiveNicsIn(long networkId) { + return _opDao.getActiveNics(networkId); + } } diff --git a/server/src/com/cloud/network/guru/DirectNetworkGuru.java b/server/src/com/cloud/network/guru/DirectNetworkGuru.java index e62fcff36a4..a9ff7489a6d 100644 --- a/server/src/com/cloud/network/guru/DirectNetworkGuru.java +++ b/server/src/com/cloud/network/guru/DirectNetworkGuru.java @@ -3,8 +3,6 @@ */ package com.cloud.network.guru; -import java.util.Random; - import javax.ejb.Local; import org.apache.log4j.Logger; @@ -21,9 +19,9 @@ import com.cloud.exception.InsufficientAddressCapacityException; import com.cloud.exception.InsufficientVirtualNetworkCapcityException; import com.cloud.exception.InvalidParameterValueException; import com.cloud.network.Network; +import com.cloud.network.Network.State; import com.cloud.network.NetworkManager; import com.cloud.network.NetworkVO; -import com.cloud.network.Network.State; import com.cloud.network.Networks.AddressFormat; import com.cloud.network.Networks.BroadcastDomainType; import com.cloud.network.Networks.IsolationType; @@ -37,7 +35,6 @@ import com.cloud.resource.Resource.ReservationStrategy; import com.cloud.user.Account; import com.cloud.utils.component.AdapterBase; import com.cloud.utils.component.Inject; -import com.cloud.utils.net.NetUtils; import com.cloud.vm.NicProfile; import com.cloud.vm.ReservationContext; import com.cloud.vm.VirtualMachine; @@ -51,7 +48,6 @@ public class DirectNetworkGuru extends AdapterBase implements NetworkGuru { @Inject VlanDao _vlanDao; @Inject NetworkManager _networkMgr; @Inject IPAddressDao _ipAddressDao; - Random _rand = new Random(System.currentTimeMillis()); @Override public Network design(NetworkOffering offering, DeploymentPlan plan, Network userSpecified, Account owner) { @@ -92,7 +88,7 @@ public class DirectNetworkGuru extends AdapterBase implements NetworkGuru { protected void getIp(NicProfile nic, DataCenter dc, VirtualMachineProfile vm, Network network) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException, ConcurrentOperationException { if (nic.getIp4Address() == null) { - PublicIp ip = _networkMgr.fetchNewPublicIp(dc.getId(), VlanType.DirectAttached, vm.getOwner(), network.getId(), false); + PublicIp ip = _networkMgr.assignPublicIpAddress(dc.getId(), vm.getOwner(), VlanType.DirectAttached, network.getId()); nic.setIp4Address(ip.getAddress()); nic.setGateway(ip.getGateway()); nic.setNetmask(ip.getNetmask()); @@ -101,7 +97,7 @@ public class DirectNetworkGuru extends AdapterBase implements NetworkGuru { nic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(ip.getVlanTag())); nic.setFormat(AddressFormat.Ip4); nic.setReservationId(ip.getVlanTag()); - nic.setMacAddress(NetUtils.long2Mac(ip.getMacAddress() | 0x060000000000l | (((long)_rand.nextInt(32768) << 25) & 0x00fffe000000l))); + nic.setMacAddress(ip.getMacAddress()); } nic.setDns1(dc.getDns1()); nic.setDns2(dc.getDns2()); diff --git a/server/src/com/cloud/network/guru/PublicNetworkGuru.java b/server/src/com/cloud/network/guru/PublicNetworkGuru.java index e83c66a1a6b..403dd78fbcb 100644 --- a/server/src/com/cloud/network/guru/PublicNetworkGuru.java +++ b/server/src/com/cloud/network/guru/PublicNetworkGuru.java @@ -3,14 +3,13 @@ */ package com.cloud.network.guru; -import java.util.Random; - import javax.ejb.Local; import org.apache.log4j.Logger; import com.cloud.dc.DataCenter; import com.cloud.dc.DataCenterVO; +import com.cloud.dc.Vlan.VlanType; import com.cloud.dc.dao.DataCenterDao; import com.cloud.dc.dao.VlanDao; import com.cloud.deploy.DeployDestination; @@ -32,10 +31,8 @@ import com.cloud.offering.NetworkOffering; import com.cloud.offering.NetworkOffering.GuestIpType; import com.cloud.resource.Resource.ReservationStrategy; import com.cloud.user.Account; -import com.cloud.user.UserContext; import com.cloud.utils.component.AdapterBase; import com.cloud.utils.component.Inject; -import com.cloud.utils.net.NetUtils; import com.cloud.vm.NicProfile; import com.cloud.vm.ReservationContext; import com.cloud.vm.VirtualMachine; @@ -49,7 +46,6 @@ public class PublicNetworkGuru extends AdapterBase implements NetworkGuru { @Inject VlanDao _vlanDao; @Inject NetworkManager _networkMgr; @Inject IPAddressDao _ipAddressDao; - Random _rand = new Random(System.currentTimeMillis()); @Override public Network design(NetworkOffering offering, DeploymentPlan plan, Network network, Account owner) { @@ -75,7 +71,7 @@ public class PublicNetworkGuru extends AdapterBase implements NetworkGuru { protected void getIp(NicProfile nic, DataCenter dc, VirtualMachineProfile vm, Network network) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException, ConcurrentOperationException { if (nic.getIp4Address() == null) { - PublicIp ip = _networkMgr.assignSourceNatIpAddress(vm.getOwner(), network, UserContext.current().getUserId()); + PublicIp ip = _networkMgr.assignPublicIpAddress(dc.getId(), vm.getOwner(), VlanType.VirtualNetwork, null); nic.setIp4Address(ip.getAddress()); nic.setGateway(ip.getGateway()); nic.setNetmask(ip.getNetmask()); @@ -84,7 +80,7 @@ public class PublicNetworkGuru extends AdapterBase implements NetworkGuru { nic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(ip.getVlanTag())); nic.setFormat(AddressFormat.Ip4); nic.setReservationId(ip.getVlanTag()); - nic.setMacAddress(NetUtils.long2Mac(ip.getMacAddress() | 0x060000000000l | (((long)_rand.nextInt(32768) << 25) & 0x00fffe000000l))); + nic.setMacAddress(ip.getMacAddress()); } nic.setDns1(dc.getDns1()); nic.setDns2(dc.getDns2()); @@ -122,7 +118,6 @@ public class PublicNetworkGuru extends AdapterBase implements NetworkGuru { @Override public boolean release(NicProfile nic, VirtualMachineProfile vm, String reservationId) { - _ipAddressDao.unassignIpAddress(reservationId); return true; } @@ -133,6 +128,8 @@ public class PublicNetworkGuru extends AdapterBase implements NetworkGuru { @Override public void deallocate(Network network, NicProfile nic, VirtualMachineProfile vm) { + _ipAddressDao.unassignIpAddress(nic.getIp4Address()); + nic.deallocate(); } @Override diff --git a/server/src/com/cloud/network/router/DomainRouterManagerImpl.java b/server/src/com/cloud/network/router/DomainRouterManagerImpl.java index 39d06d50a59..ba18d9e3810 100644 --- a/server/src/com/cloud/network/router/DomainRouterManagerImpl.java +++ b/server/src/com/cloud/network/router/DomainRouterManagerImpl.java @@ -115,10 +115,13 @@ import com.cloud.network.IpAddress; import com.cloud.network.Network; import com.cloud.network.NetworkManager; import com.cloud.network.NetworkVO; +import com.cloud.network.Networks.BroadcastDomainType; +import com.cloud.network.Networks.IsolationType; import com.cloud.network.Networks.TrafficType; import com.cloud.network.RemoteAccessVpnVO; import com.cloud.network.SshKeysDistriMonitor; import com.cloud.network.VpnUserVO; +import com.cloud.network.addr.PublicIp; import com.cloud.network.dao.FirewallRulesDao; import com.cloud.network.dao.IPAddressDao; import com.cloud.network.dao.LoadBalancerDao; @@ -2091,7 +2094,7 @@ public class DomainRouterManagerImpl implements DomainRouterManager, DomainRoute s_logger.debug("Creating the router " + id); } - //String sourceNatIp = _networkMgr.assignSourceNatIpAddress(owner, dest.getDataCenter()); + PublicIp sourceNatIp = _networkMgr.assignSourceNatIpAddress(owner, guestConfig, _accountService.getSystemUser().getId()); List offerings = _networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemVmControlNetwork); NetworkOfferingVO controlOffering = offerings.get(0); @@ -2102,7 +2105,18 @@ public class DomainRouterManagerImpl implements DomainRouterManager, DomainRoute List publicConfigs = _networkMgr.setupNetwork(_systemAcct, publicOffering, plan, null, null, false); NicProfile defaultNic = new NicProfile(); defaultNic.setDefaultNic(true); - //defaultNic.setIp4Address(sourceNatIp); + defaultNic.setIp4Address(sourceNatIp.getAddress()); + defaultNic.setGateway(sourceNatIp.getGateway()); + defaultNic.setNetmask(sourceNatIp.getNetmask()); + defaultNic.setTrafficType(TrafficType.Public); + defaultNic.setMacAddress(sourceNatIp.getMacAddress()); + if (sourceNatIp.getVlanTag().equals("untagged")) { + defaultNic.setBroadcastType(BroadcastDomainType.Native); + } else { + defaultNic.setBroadcastType(BroadcastDomainType.Vlan); + defaultNic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(sourceNatIp.getVlanTag())); + defaultNic.setIsolationUri(IsolationType.Vlan.toUri(sourceNatIp.getVlanTag())); + } defaultNic.setDeviceId(2); networks.add(new Pair(publicConfigs.get(0), defaultNic)); NicProfile gatewayNic = new NicProfile(); diff --git a/setup/db/create-schema.sql b/setup/db/create-schema.sql index 9262741d7cf..df1647a78c2 100755 --- a/setup/db/create-schema.sql +++ b/setup/db/create-schema.sql @@ -113,6 +113,8 @@ CREATE TABLE `cloud`.`hypervsior_properties` ( CREATE TABLE `cloud`.`op_networks`( `id` bigint unsigned NOT NULL UNIQUE KEY, `mac_address_seq` bigint unsigned NOT NULL DEFAULT 1 COMMENT 'mac address', + `nics_count` int unsigned NOT NULL DEFAULT 0 COMMENT '# of nics', + `gc` tinyint unsigned NOT NULL DEFAULT 1 COMMENT 'gc this network or not', PRIMARY KEY(`id`), CONSTRAINT `fk_op_networks__id` FOREIGN KEY (`id`) REFERENCES `networks`(`id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8;