@Override public NicProfile allocate( Network config, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException { if (_networkModel.networkIsConfiguredForExternalNetworking( config.getDataCenterId(), config.getId()) && nic != null && nic.getRequestedIpv4() != null) { throw new CloudRuntimeException("Does not support custom ip allocation at this time: " + nic); } NicProfile profile = super.allocate(config, nic, vm); boolean _isEnabled = Boolean.parseBoolean(_configDao.getValue(Config.OvsTunnelNetwork.key())); if (_isEnabled) { return null; } if (_networkModel.networkIsConfiguredForExternalNetworking( config.getDataCenterId(), config.getId())) { profile.setStrategy(ReservationStrategy.Start); /* We won't clear IP address, because router may set gateway as it IP, and it would be updated properly later */ // profile.setIp4Address(null); profile.setGateway(null); profile.setNetmask(null); } return profile; }
@Override public boolean prepareMigration( final NicProfile nic, final Network network, final VirtualMachineProfile vm, final DeployDestination dest, final ReservationContext context) { if (!canHandle(network, Service.Connectivity)) { return false; } if (nic.getBroadcastType() != Networks.BroadcastDomainType.Vswitch) { return false; } if (nic.getTrafficType() != Networks.TrafficType.Guest) { return false; } if (vm.getType() != VirtualMachine.Type.User && vm.getType() != VirtualMachine.Type.DomainRouter) { return false; } // prepare the tunnel network on the host, in order for VM to get launched _ovsTunnelMgr.checkAndPrepareHostForTunnelNetwork(network, dest.getHost()); return true; }
@Override public void updateNicProfile(NicProfile profile, Network network) { DataCenter dc = _dcDao.findById(network.getDataCenterId()); if (profile != null) { profile.setDns1(dc.getDns1()); profile.setDns2(dc.getDns2()); } }
@Override public boolean finalizeDeployment( Commands cmds, VirtualMachineProfile<SecondaryStorageVmVO> profile, DeployDestination dest, ReservationContext context) { finalizeCommandsOnStart(cmds, profile); SecondaryStorageVmVO secVm = profile.getVirtualMachine(); DataCenter dc = dest.getDataCenter(); List<NicProfile> nics = profile.getNics(); for (NicProfile nic : nics) { if ((nic.getTrafficType() == TrafficType.Public && dc.getNetworkType() == NetworkType.Advanced) || (nic.getTrafficType() == TrafficType.Guest && (dc.getNetworkType() == NetworkType.Basic || dc.isSecurityGroupEnabled()))) { secVm.setPublicIpAddress(nic.getIp4Address()); secVm.setPublicNetmask(nic.getNetmask()); secVm.setPublicMacAddress(nic.getMacAddress()); } else if (nic.getTrafficType() == TrafficType.Management) { secVm.setPrivateIpAddress(nic.getIp4Address()); secVm.setPrivateMacAddress(nic.getMacAddress()); } } _secStorageVmDao.update(secVm.getId(), secVm); return true; }
// we use context.reservationId for dedup of guru & element operations. public boolean createNicEnv( Network network, NicProfile nic, DeployDestination dest, ReservationContext context) { String tenantNetworkUuid = _sspUuidDao.findUuidByNetwork(network); if (tenantNetworkUuid == null) { s_logger.debug("Skipping #createNicEnv() for nic on " + network.toString()); return true; } String reservationId = context.getReservationId(); List<SspUuidVO> tenantPortUuidVos = _sspUuidDao.listUUidVoByNicProfile(nic); for (SspUuidVO tenantPortUuidVo : tenantPortUuidVos) { if (reservationId.equals(tenantPortUuidVo.getReservationId())) { s_logger.info("Skipping because reservation found " + reservationId); return true; } } String tenantPortUuid = null; for (SspClient client : fetchSspClients(network.getPhysicalNetworkId(), network.getDataCenterId(), true)) { SspClient.TenantPort sspPort = client.createTenantPort(tenantNetworkUuid); if (sspPort != null) { tenantPortUuid = sspPort.uuid; nic.setReservationId(reservationId); SspUuidVO uuid = new SspUuidVO(); uuid.setUuid(tenantPortUuid); uuid.setObjClass(SspUuidVO.objClassNicProfile); uuid.setObjId(nic.getId()); uuid.setReservationId(reservationId); _sspUuidDao.persist(uuid); break; } } if (tenantPortUuid == null) { s_logger.debug("#createNicEnv() failed for nic on " + network.toString()); return false; } for (SspClient client : fetchSspClients(network.getPhysicalNetworkId(), network.getDataCenterId(), true)) { SspClient.TenantPort sspPort = client.updateTenantVifBinding(tenantPortUuid, dest.getHost().getPrivateIpAddress()); if (sspPort != null) { if (sspPort.vlanId != null) { nic.setBroadcastType(BroadcastDomainType.Vlan); nic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(String.valueOf(sspPort.vlanId))); } return true; } } s_logger.error("Updating vif failed " + nic.toString()); return false; }
@Override public boolean isVmSecurityGroupEnabled(Long vmId) { VirtualMachine vm = _vmDao.findByIdIncludingRemoved(vmId); List<NicProfile> nics = _networkMgr.getNicProfiles(vm); for (NicProfile nic : nics) { if (nic.isSecurityGroupEnabled() && vm.getHypervisorType() != HypervisorType.VMware) { return true; } } return false; }
public boolean deleteNicEnv(Network network, NicProfile nic, ReservationContext context) { if (context == null) { s_logger.error("ReservationContext was null for " + nic + " " + network); return false; } String reservationId = context.getReservationId(); SspUuidVO deleteTarget = null; SspUuidVO remainingTarget = null; List<SspUuidVO> tenantPortUuidVos = _sspUuidDao.listUUidVoByNicProfile(nic); for (SspUuidVO tenantPortUuidVo : tenantPortUuidVos) { if (reservationId.equals(tenantPortUuidVo.getReservationId())) { deleteTarget = tenantPortUuidVo; } else { remainingTarget = tenantPortUuidVo; } } if (deleteTarget != null) { // delete the target ssp uuid (tenant-port) String tenantPortUuid = deleteTarget.getUuid(); boolean processed = false; for (SspClient client : fetchSspClients(network.getPhysicalNetworkId(), network.getDataCenterId(), true)) { SspClient.TenantPort sspPort = client.updateTenantVifBinding(tenantPortUuid, null); if (sspPort != null) { processed = true; break; } } if (!processed) { s_logger.warn("Ssp api nic detach failed " + nic.toString()); } processed = false; for (SspClient client : fetchSspClients(network.getPhysicalNetworkId(), network.getDataCenterId(), true)) { if (client.deleteTenantPort(tenantPortUuid)) { _sspUuidDao.removeUuid(tenantPortUuid); processed = true; break; } } if (!processed) { s_logger.warn("Ssp api tenant port deletion failed " + nic.toString()); } _sspUuidDao.removeUuid(tenantPortUuid); } if (remainingTarget != null) { NicVO nicVo = _nicDao.findById(nic.getId()); nicVo.setReservationId(remainingTarget.getReservationId()); _nicDao.persist(nicVo); // persist the new reservationId } return true; }
@Override public void reserve( NicProfile nic, Network network, VirtualMachineProfile<? extends VirtualMachine> vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException, ConcurrentOperationException { if (nic.getIp4Address() == null) { _networkMgr.allocateDirectIp(nic, dest.getDataCenter(), vm, network, null); nic.setStrategy(ReservationStrategy.Create); } }
@Override public void reserve( NicProfile nic, Network config, VirtualMachineProfile<? extends VirtualMachine> vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException { assert nic.getTrafficType() == TrafficType.Control; if (dest.getHost().getHypervisorType() == HypervisorType.VmWare && vm.getType() == VirtualMachine.Type.DomainRouter) { super.reserve(nic, config, vm, dest, context); String mac = _networkMgr.getNextAvailableMacAddressInNetwork(config.getId()); nic.setMacAddress(mac); return; } String ip = _dcDao.allocateLinkLocalIpAddress( dest.getDataCenter().getId(), dest.getPod().getId(), nic.getId(), context.getReservationId()); nic.setIp4Address(ip); nic.setMacAddress(NetUtils.long2Mac(NetUtils.ip2Long(ip) | (14l << 40))); nic.setNetmask("255.255.0.0"); nic.setFormat(AddressFormat.Ip4); nic.setGateway(NetUtils.getLinkLocalGateway()); }
@Override public boolean finalizeDeployment( Commands cmds, VirtualMachineProfile<DomainRouterVO> profile, DeployDestination dest, ReservationContext context) throws ResourceUnavailableException { DomainRouterVO elbVm = profile.getVirtualMachine(); List<NicProfile> nics = profile.getNics(); for (NicProfile nic : nics) { if (nic.getTrafficType() == TrafficType.Public) { elbVm.setPublicIpAddress(nic.getIp4Address()); elbVm.setPublicNetmask(nic.getNetmask()); elbVm.setPublicMacAddress(nic.getMacAddress()); } else if (nic.getTrafficType() == TrafficType.Control) { elbVm.setPrivateIpAddress(nic.getIp4Address()); elbVm.setPrivateMacAddress(nic.getMacAddress()); } } _routerDao.update(elbVm.getId(), elbVm); finalizeCommandsOnStart(cmds, profile); return true; }
@Override @DB public void deallocate( Network config, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm) { super.deallocate(config, nic, vm); if (Boolean.parseBoolean(_configDao.getValue(Config.OvsTunnelNetwork.key()))) { return; } if (_networkModel.networkIsConfiguredForExternalNetworking( config.getDataCenterId(), config.getId())) { nic.setIp4Address(null); nic.setGateway(null); nic.setNetmask(null); nic.setBroadcastUri(null); nic.setIsolationUri(null); } }
private boolean preparePxeInBasicZone( VirtualMachineProfile profile, NicProfile nic, DeployDestination dest, ReservationContext context) throws AgentUnavailableException, OperationTimedoutException { NetworkVO nwVO = _nwDao.findById(nic.getNetworkId()); QueryBuilder<BaremetalPxeVO> sc = QueryBuilder.create(BaremetalPxeVO.class); sc.and(sc.entity().getDeviceType(), Op.EQ, BaremetalPxeType.KICK_START.toString()); sc.and(sc.entity().getPhysicalNetworkId(), Op.EQ, nwVO.getPhysicalNetworkId()); BaremetalPxeVO pxeVo = sc.find(); if (pxeVo == null) { throw new CloudRuntimeException( "No kickstart PXE server found in pod: " + dest.getPod().getId() + ", you need to add it before starting VM"); } VMTemplateVO template = _tmpDao.findById(profile.getTemplateId()); List<String> tuple = parseKickstartUrl(profile); String ks = tuple.get(0); String kernel = tuple.get(1); String initrd = tuple.get(2); PrepareKickstartPxeServerCommand cmd = new PrepareKickstartPxeServerCommand(); cmd.setKsFile(ks); cmd.setInitrd(initrd); cmd.setKernel(kernel); cmd.setMac(nic.getMacAddress()); cmd.setTemplateUuid(template.getUuid()); Answer aws = _agentMgr.send(pxeVo.getHostId(), cmd); if (!aws.getResult()) { s_logger.warn( "Unable to set host: " + dest.getHost().getId() + " to PXE boot because " + aws.getDetails()); return false; } return true; }
@Override @DB public void deallocate( Network network, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm) { if (s_logger.isDebugEnabled()) { s_logger.debug( "Deallocate network: networkId: " + nic.getNetworkId() + ", ip: " + nic.getIp4Address()); } IPAddressVO ip = _ipAddressDao.findByIpAndSourceNetworkId(nic.getNetworkId(), nic.getIp4Address()); if (ip != null) { Transaction txn = Transaction.currentTxn(); txn.start(); _networkMgr.markIpAsUnavailable(ip.getId()); _ipAddressDao.unassignIpAddress(ip.getId()); txn.commit(); } nic.deallocate(); }
@Override public NicProfile allocate( Network network, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException, ConcurrentOperationException { DataCenter dc = _dcDao.findById(network.getDataCenterId()); if (nic == null) { nic = new NicProfile(ReservationStrategy.Create, null, null, null, null); } else if (nic.getIp4Address() == null) { nic.setStrategy(ReservationStrategy.Start); } else { nic.setStrategy(ReservationStrategy.Create); } _networkMgr.allocateDirectIp(nic, dc, vm, network, nic.getRequestedIp()); nic.setStrategy(ReservationStrategy.Create); return nic; }
@Override public boolean finalizeCommandsOnStart( Commands cmds, VirtualMachineProfile<SecondaryStorageVmVO> profile) { NicProfile managementNic = null; NicProfile controlNic = null; for (NicProfile nic : profile.getNics()) { if (nic.getTrafficType() == TrafficType.Management) { managementNic = nic; } else if (nic.getTrafficType() == TrafficType.Control && nic.getIp4Address() != null) { controlNic = nic; } } if (controlNic == null) { if (managementNic == null) { s_logger.error( "Management network doesn't exist for the secondaryStorageVm " + profile.getVirtualMachine()); return false; } controlNic = managementNic; } CheckSshCommand check = new CheckSshCommand(profile.getInstanceName(), controlNic.getIp4Address(), 3922, 5, 20); cmds.addCommand("checkSsh", check); return true; }
@Override @DB public boolean prepare( Network network, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { Host host = dest.getHost(); if (host == null || host.getHypervisorType() != HypervisorType.BareMetal) { return true; } Transaction txn = Transaction.currentTxn(); txn.start(); nic.setMacAddress(host.getPrivateMacAddress()); NicVO vo = _nicDao.findById(nic.getId()); assert vo != null : "Where ths nic " + nic.getId() + " going???"; vo.setMacAddress(nic.getMacAddress()); _nicDao.update(vo.getId(), vo); txn.commit(); s_logger.debug( "Bare Metal changes mac address of nic " + nic.getId() + " to " + nic.getMacAddress()); return _dhcpMgr.addVirtualMachineIntoNetwork(network, nic, vm, dest, context); }
@Override public boolean release( final Network network, final NicProfile nic, final VirtualMachineProfile vm, final ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException { if (!canHandle(network, Service.Connectivity)) { return false; } if (nic.getBroadcastType() != Networks.BroadcastDomainType.Vswitch) { return false; } if (nic.getTrafficType() != Networks.TrafficType.Guest) { return false; } final HostVO host = _hostDao.findById(vm.getVirtualMachine().getHostId()); _ovsTunnelMgr.checkAndRemoveHostFromTunnelNetwork(network, host); return true; }
@Override public NicProfile allocate( Network config, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException { if (config.getTrafficType() != TrafficType.Control) { return null; } if (vm.getHypervisorType() == HypervisorType.VmWare && vm.getType() != VirtualMachine.Type.DomainRouter) { NicProfile nicProf = new NicProfile(ReservationStrategy.Create, null, null, null, null); String mac = _networkMgr.getNextAvailableMacAddressInNetwork(config.getId()); nicProf.setMacAddress(mac); return nicProf; } if (nic != null) { throw new CloudRuntimeException("Does not support nic specification at this time: " + nic); } return new NicProfile(ReservationStrategy.Start, null, null, null, null); }
@Override public NicProfile allocate( Network network, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException, ConcurrentOperationException { if (network.getTrafficType() != TrafficType.Public) { return null; } if (nic == null) { nic = new NicProfile(ReservationStrategy.Create, null, null, null, null); } DataCenter dc = _dcDao.findById(network.getDataCenterId()); getIp(nic, dc, vm, network); if (nic.getIp4Address() == null) { nic.setStrategy(ReservationStrategy.Start); } else { nic.setStrategy(ReservationStrategy.Create); } return nic; }
@Override public boolean finalizeVirtualMachineProfile( final VirtualMachineProfile profile, final DeployDestination dest, final ReservationContext context) { final DomainRouterVO domainRouterVO = _routerDao.findById(profile.getId()); final Long vpcId = domainRouterVO.getVpcId(); if (vpcId != null) { if (domainRouterVO.getState() == State.Starting || domainRouterVO.getState() == State.Running) { String defaultDns1 = null; String defaultDns2 = null; // remove public and guest nics as we will plug them later final Iterator<NicProfile> it = profile.getNics().iterator(); while (it.hasNext()) { final NicProfile nic = it.next(); if (nic.getTrafficType() == TrafficType.Public || nic.getTrafficType() == TrafficType.Guest) { // save dns information if (nic.getTrafficType() == TrafficType.Public) { defaultDns1 = nic.getIPv4Dns1(); defaultDns2 = nic.getIPv4Dns2(); } s_logger.debug( "Removing nic " + nic + " of type " + nic.getTrafficType() + " from the nics passed on vm start. " + "The nic will be plugged later"); it.remove(); } } // add vpc cidr/dns/networkdomain to the boot load args final StringBuilder buf = profile.getBootArgsBuilder(); final Vpc vpc = _entityMgr.findById(Vpc.class, vpcId); buf.append(" vpccidr=" + vpc.getCidr() + " domain=" + vpc.getNetworkDomain()); buf.append(" dns1=").append(defaultDns1); if (defaultDns2 != null) { buf.append(" dns2=").append(defaultDns2); } } } return super.finalizeVirtualMachineProfile(profile, dest, context); }
@Override public boolean setupDhcpForPvlan( final boolean isAddPvlan, final DomainRouterVO router, final Long hostId, final NicProfile nic) throws ResourceUnavailableException { s_logger.debug("SETUP DHCP PVLAN RULES"); if (!nic.getBroadCastUri().getScheme().equals("pvlan")) { return false; } final DhcpPvlanRules pvlanRules = new DhcpPvlanRules(isAddPvlan, nic); return pvlanRules.accept(_advancedVisitor, router); }
@Override public boolean release( NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm, String reservationId) { if (Boolean.parseBoolean(_configDao.getValue(Config.OvsTunnelNetwork.key()))) { return true; } NetworkVO network = _networkDao.findById(nic.getNetworkId()); if (network != null && _networkModel.networkIsConfiguredForExternalNetworking( network.getDataCenterId(), network.getId())) { return true; } else { return super.release(nic, vm, reservationId); } }
@Override public NicProfile createGuestNicProfileForVpcRouter( final RouterDeploymentDefinition vpcRouterDeploymentDefinition, final Network guestNetwork) { final NicProfile guestNic = new NicProfile(); if (vpcRouterDeploymentDefinition.isRedundant()) { guestNic.setIPv4Address(_ipAddrMgr.acquireGuestIpAddress(guestNetwork, null)); } else { guestNic.setIPv4Address(guestNetwork.getGateway()); } guestNic.setBroadcastUri(guestNetwork.getBroadcastUri()); guestNic.setBroadcastType(guestNetwork.getBroadcastDomainType()); guestNic.setIsolationUri(guestNetwork.getBroadcastUri()); guestNic.setMode(guestNetwork.getMode()); final String gatewayCidr = guestNetwork.getCidr(); guestNic.setIPv4Netmask(NetUtils.getCidrNetmask(gatewayCidr)); return guestNic; }
@Override public boolean release( NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm, String reservationId) { assert nic.getTrafficType() == TrafficType.Control; if (vm.getHypervisorType() == HypervisorType.VmWare && vm.getType() == VirtualMachine.Type.DomainRouter) { super.release(nic, vm, reservationId); return true; } _dcDao.releaseLinkLocalIpAddress(nic.getId(), reservationId); nic.setIp4Address(null); nic.setMacAddress(null); nic.setNetmask(null); nic.setFormat(null); nic.setGateway(null); return true; }
@Override public LinkedHashMap<Network, List<? extends NicProfile>> configureGuestNic( final RouterDeploymentDefinition routerDeploymentDefinition) throws ConcurrentOperationException, InsufficientAddressCapacityException { // Form networks final LinkedHashMap<Network, List<? extends NicProfile>> networks = new LinkedHashMap<Network, List<? extends NicProfile>>(3); // 1) Guest network final Network guestNetwork = routerDeploymentDefinition.getGuestNetwork(); if (guestNetwork != null) { s_logger.debug("Adding nic for Virtual Router in Guest network " + guestNetwork); String defaultNetworkStartIp = null, defaultNetworkStartIpv6 = null; if (!routerDeploymentDefinition.isPublicNetwork()) { final Nic placeholder = _networkModel.getPlaceholderNicForRouter( guestNetwork, routerDeploymentDefinition.getPodId()); if (guestNetwork.getCidr() != null) { if (placeholder != null && placeholder.getIPv4Address() != null) { s_logger.debug( "Requesting ipv4 address " + placeholder.getIPv4Address() + " stored in placeholder nic for the network " + guestNetwork); defaultNetworkStartIp = placeholder.getIPv4Address(); } else { final String startIp = _networkModel.getStartIpAddress(guestNetwork.getId()); if (startIp != null && _ipAddressDao .findByIpAndSourceNetworkId(guestNetwork.getId(), startIp) .getAllocatedTime() == null) { defaultNetworkStartIp = startIp; } else if (s_logger.isDebugEnabled()) { s_logger.debug( "First ipv4 " + startIp + " in network id=" + guestNetwork.getId() + " is already allocated, can't use it for domain router; will get random ip address from the range"); } } } if (guestNetwork.getIp6Cidr() != null) { if (placeholder != null && placeholder.getIPv6Address() != null) { s_logger.debug( "Requesting ipv6 address " + placeholder.getIPv6Address() + " stored in placeholder nic for the network " + guestNetwork); defaultNetworkStartIpv6 = placeholder.getIPv6Address(); } else { final String startIpv6 = _networkModel.getStartIpv6Address(guestNetwork.getId()); if (startIpv6 != null && _ipv6Dao.findByNetworkIdAndIp(guestNetwork.getId(), startIpv6) == null) { defaultNetworkStartIpv6 = startIpv6; } else if (s_logger.isDebugEnabled()) { s_logger.debug( "First ipv6 " + startIpv6 + " in network id=" + guestNetwork.getId() + " is already allocated, can't use it for domain router; will get random ipv6 address from the range"); } } } } final NicProfile gatewayNic = new NicProfile(defaultNetworkStartIp, defaultNetworkStartIpv6); if (routerDeploymentDefinition.isPublicNetwork()) { if (routerDeploymentDefinition.isRedundant()) { gatewayNic.setIPv4Address(_ipAddrMgr.acquireGuestIpAddress(guestNetwork, null)); } else { gatewayNic.setIPv4Address(guestNetwork.getGateway()); } gatewayNic.setBroadcastUri(guestNetwork.getBroadcastUri()); gatewayNic.setBroadcastType(guestNetwork.getBroadcastDomainType()); gatewayNic.setIsolationUri(guestNetwork.getBroadcastUri()); gatewayNic.setMode(guestNetwork.getMode()); final String gatewayCidr = guestNetwork.getCidr(); gatewayNic.setIPv4Netmask(NetUtils.getCidrNetmask(gatewayCidr)); } else { gatewayNic.setDefaultNic(true); } networks.put(guestNetwork, new ArrayList<NicProfile>(Arrays.asList(gatewayNic))); } return networks; }
protected LinkedHashMap<Network, List<? extends NicProfile>> configurePublicNic( final RouterDeploymentDefinition routerDeploymentDefinition, final boolean hasGuestNic) { final LinkedHashMap<Network, List<? extends NicProfile>> publicConfig = new LinkedHashMap<Network, List<? extends NicProfile>>(3); if (routerDeploymentDefinition.isPublicNetwork()) { s_logger.debug("Adding nic for Virtual Router in Public network "); // if source nat service is supported by the network, get the source // nat ip address final NicProfile defaultNic = new NicProfile(); defaultNic.setDefaultNic(true); final PublicIp sourceNatIp = routerDeploymentDefinition.getSourceNatIP(); defaultNic.setIPv4Address(sourceNatIp.getAddress().addr()); defaultNic.setIPv4Gateway(sourceNatIp.getGateway()); defaultNic.setIPv4Netmask(sourceNatIp.getNetmask()); defaultNic.setMacAddress(sourceNatIp.getMacAddress()); // get broadcast from public network final Network pubNet = _networkDao.findById(sourceNatIp.getNetworkId()); if (pubNet.getBroadcastDomainType() == BroadcastDomainType.Vxlan) { defaultNic.setBroadcastType(BroadcastDomainType.Vxlan); defaultNic.setBroadcastUri(BroadcastDomainType.Vxlan.toUri(sourceNatIp.getVlanTag())); defaultNic.setIsolationUri(BroadcastDomainType.Vxlan.toUri(sourceNatIp.getVlanTag())); } else { defaultNic.setBroadcastType(BroadcastDomainType.Vlan); defaultNic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(sourceNatIp.getVlanTag())); defaultNic.setIsolationUri(IsolationType.Vlan.toUri(sourceNatIp.getVlanTag())); } // If guest nic has already been added we will have 2 devices in the list. if (hasGuestNic) { defaultNic.setDeviceId(2); } final NetworkOffering publicOffering = _networkModel .getSystemAccountNetworkOfferings(NetworkOffering.SystemPublicNetwork) .get(0); final List<? extends Network> publicNetworks = _networkMgr.setupNetwork( s_systemAccount, publicOffering, routerDeploymentDefinition.getPlan(), null, null, false); final String publicIp = defaultNic.getIPv4Address(); // We want to use the identical MAC address for RvR on public // interface if possible final NicVO peerNic = _nicDao.findByIp4AddressAndNetworkId(publicIp, publicNetworks.get(0).getId()); if (peerNic != null) { s_logger.info("Use same MAC as previous RvR, the MAC is " + peerNic.getMacAddress()); defaultNic.setMacAddress(peerNic.getMacAddress()); } publicConfig.put(publicNetworks.get(0), new ArrayList<NicProfile>(Arrays.asList(defaultNic))); } return publicConfig; }
@Override @DB public NicProfile createPrivateNicProfileForGateway( final VpcGateway privateGateway, final VirtualRouter router) { final Network privateNetwork = _networkModel.getNetwork(privateGateway.getNetworkId()); PrivateIpVO ipVO = _privateIpDao.allocateIpAddress( privateNetwork.getDataCenterId(), privateNetwork.getId(), privateGateway.getIp4Address()); final Long vpcId = privateGateway.getVpcId(); final Vpc activeVpc = _vpcMgr.getActiveVpc(vpcId); if (activeVpc.isRedundant() && ipVO == null) { ipVO = _privateIpDao.findByIpAndVpcId(vpcId, privateGateway.getIp4Address()); } Nic privateNic = null; if (ipVO != null) { privateNic = _nicDao.findByIp4AddressAndNetworkId(ipVO.getIpAddress(), privateNetwork.getId()); } NicProfile privateNicProfile = new NicProfile(); if (privateNic != null) { privateNicProfile = new NicProfile( privateNic, privateNetwork, privateNic.getBroadcastUri(), privateNic.getIsolationUri(), _networkModel.getNetworkRate(privateNetwork.getId(), router.getId()), _networkModel.isSecurityGroupSupportedInNetwork(privateNetwork), _networkModel.getNetworkTag(router.getHypervisorType(), privateNetwork)); if (router.getIsRedundantRouter()) { String newMacAddress = NetUtils.long2Mac(NetUtils.createSequenceBasedMacAddress(ipVO.getMacAddress())); privateNicProfile.setMacAddress(newMacAddress); } } else { final String netmask = NetUtils.getCidrNetmask(privateNetwork.getCidr()); final PrivateIpAddress ip = new PrivateIpAddress( ipVO, privateNetwork.getBroadcastUri().toString(), privateNetwork.getGateway(), netmask, NetUtils.long2Mac(NetUtils.createSequenceBasedMacAddress(ipVO.getMacAddress()))); final URI netUri = BroadcastDomainType.fromString(ip.getBroadcastUri()); privateNicProfile.setIPv4Address(ip.getIpAddress()); privateNicProfile.setIPv4Gateway(ip.getGateway()); privateNicProfile.setIPv4Netmask(ip.getNetmask()); privateNicProfile.setIsolationUri(netUri); privateNicProfile.setBroadcastUri(netUri); // can we solve this in setBroadcastUri()??? // or more plugable construct is desirable privateNicProfile.setBroadcastType(BroadcastDomainType.getSchemeValue(netUri)); privateNicProfile.setFormat(AddressFormat.Ip4); privateNicProfile.setReservationId(String.valueOf(ip.getBroadcastUri())); privateNicProfile.setMacAddress(ip.getMacAddress()); } return privateNicProfile; }
@Override public boolean finalizeCommandsOnStart( Commands cmds, VirtualMachineProfile<DomainRouterVO> profile) { DomainRouterVO elbVm = profile.getVirtualMachine(); DataCenterVO dcVo = _dcDao.findById(elbVm.getDataCenterId()); NicProfile controlNic = null; Long guestNetworkId = null; if (profile.getHypervisorType() == HypervisorType.VMware && dcVo.getNetworkType() == NetworkType.Basic) { // TODO this is a ugly to test hypervisor type here // for basic network mode, we will use the guest NIC for control NIC for (NicProfile nic : profile.getNics()) { if (nic.getTrafficType() == TrafficType.Guest && nic.getIp4Address() != null) { controlNic = nic; guestNetworkId = nic.getNetworkId(); } } } else { for (NicProfile nic : profile.getNics()) { if (nic.getTrafficType() == TrafficType.Control && nic.getIp4Address() != null) { controlNic = nic; } else if (nic.getTrafficType() == TrafficType.Guest) { guestNetworkId = nic.getNetworkId(); } } } if (controlNic == null) { s_logger.error("Control network doesn't exist for the ELB vm " + elbVm); return false; } cmds.addCommand( "checkSsh", new CheckSshCommand(profile.getInstanceName(), controlNic.getIp4Address(), 3922)); // Re-apply load balancing rules List<LoadBalancerVO> lbs = _elbVmMapDao.listLbsForElbVm(elbVm.getId()); List<LoadBalancingRule> lbRules = new ArrayList<LoadBalancingRule>(); for (LoadBalancerVO lb : lbs) { List<LbDestination> dstList = _lbMgr.getExistingDestinations(lb.getId()); List<LbStickinessPolicy> policyList = _lbMgr.getStickinessPolicies(lb.getId()); List<LbHealthCheckPolicy> hcPolicyList = _lbMgr.getHealthCheckPolicies(lb.getId()); LoadBalancingRule loadBalancing = new LoadBalancingRule(lb, dstList, policyList, hcPolicyList); lbRules.add(loadBalancing); } s_logger.debug( "Found " + lbRules.size() + " load balancing rule(s) to apply as a part of ELB vm " + elbVm + " start."); if (!lbRules.isEmpty()) { createApplyLoadBalancingRulesCommands(lbRules, elbVm, cmds, guestNetworkId); } return true; }
@Override public boolean finalizeVirtualMachineProfile( VirtualMachineProfile<DomainRouterVO> profile, DeployDestination dest, ReservationContext context) { DomainRouterVO elbVm = profile.getVirtualMachine(); List<NicProfile> elbNics = profile.getNics(); Long guestNtwkId = null; for (NicProfile routerNic : elbNics) { if (routerNic.getTrafficType() == TrafficType.Guest) { guestNtwkId = routerNic.getNetworkId(); break; } } NetworkVO guestNetwork = _networkDao.findById(guestNtwkId); DataCenter dc = dest.getDataCenter(); StringBuilder buf = profile.getBootArgsBuilder(); buf.append(" template=domP type=" + _systemVmType); buf.append(" name=").append(profile.getHostName()); NicProfile controlNic = null; String defaultDns1 = null; String defaultDns2 = null; for (NicProfile nic : profile.getNics()) { int deviceId = nic.getDeviceId(); buf.append(" eth").append(deviceId).append("ip=").append(nic.getIp4Address()); buf.append(" eth").append(deviceId).append("mask=").append(nic.getNetmask()); if (nic.isDefaultNic()) { buf.append(" gateway=").append(nic.getGateway()); defaultDns1 = nic.getDns1(); defaultDns2 = nic.getDns2(); } if (nic.getTrafficType() == TrafficType.Management) { buf.append(" localgw=").append(dest.getPod().getGateway()); } else if (nic.getTrafficType() == TrafficType.Control) { // control command is sent over management network in VMware if (dest.getHost().getHypervisorType() == HypervisorType.VMware) { if (s_logger.isInfoEnabled()) { s_logger.info( "Check if we need to add management server explicit route to ELB vm. pod cidr: " + dest.getPod().getCidrAddress() + "/" + dest.getPod().getCidrSize() + ", pod gateway: " + dest.getPod().getGateway() + ", management host: " + _mgmtHost); } if (s_logger.isDebugEnabled()) { s_logger.debug("Added management server explicit route to ELB vm."); } // always add management explicit route, for basic networking setup buf.append(" mgmtcidr=").append(_mgmtCidr); buf.append(" localgw=").append(dest.getPod().getGateway()); if (dc.getNetworkType() == NetworkType.Basic) { // ask elb vm to setup SSH on guest network buf.append(" sshonguest=true"); } } controlNic = nic; } } String domain = guestNetwork.getNetworkDomain(); if (domain != null) { buf.append(" domain=" + domain); } buf.append(" dns1=").append(defaultDns1); if (defaultDns2 != null) { buf.append(" dns2=").append(defaultDns2); } if (s_logger.isDebugEnabled()) { s_logger.debug("Boot Args for " + profile + ": " + buf.toString()); } if (controlNic == null) { throw new CloudRuntimeException("Didn't start a control port"); } return true; }
public DomainRouterVO deployELBVm( Network guestNetwork, DeployDestination dest, Account owner, Map<Param, Object> params) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { long dcId = dest.getDataCenter().getId(); // lock guest network Long guestNetworkId = guestNetwork.getId(); guestNetwork = _networkDao.acquireInLockTable(guestNetworkId); if (guestNetwork == null) { throw new ConcurrentOperationException("Unable to acquire network lock: " + guestNetworkId); } try { if (_networkModel.isNetworkSystem(guestNetwork) || guestNetwork.getGuestType() == Network.GuestType.Shared) { owner = _accountService.getSystemAccount(); } if (s_logger.isDebugEnabled()) { s_logger.debug( "Starting a ELB vm for network configurations: " + guestNetwork + " in " + dest); } assert guestNetwork.getState() == Network.State.Implemented || guestNetwork.getState() == Network.State.Setup || guestNetwork.getState() == Network.State.Implementing : "Network is not yet fully implemented: " + guestNetwork; DataCenterDeployment plan = null; DomainRouterVO elbVm = null; plan = new DataCenterDeployment(dcId, dest.getPod().getId(), null, null, null, null); if (elbVm == null) { long id = _routerDao.getNextInSequence(Long.class, "id"); if (s_logger.isDebugEnabled()) { s_logger.debug("Creating the ELB vm " + id); } List<? extends NetworkOffering> offerings = _networkModel.getSystemAccountNetworkOfferings(NetworkOffering.SystemControlNetwork); NetworkOffering controlOffering = offerings.get(0); NetworkVO controlConfig = _networkMgr.setupNetwork(_systemAcct, controlOffering, plan, null, null, false).get(0); List<Pair<NetworkVO, NicProfile>> networks = new ArrayList<Pair<NetworkVO, NicProfile>>(2); NicProfile guestNic = new NicProfile(); guestNic.setDefaultNic(true); networks.add(new Pair<NetworkVO, NicProfile>(controlConfig, null)); networks.add(new Pair<NetworkVO, NicProfile>((NetworkVO) guestNetwork, guestNic)); VMTemplateVO template = _templateDao.findSystemVMTemplate(dcId); String typeString = "ElasticLoadBalancerVm"; Long physicalNetworkId = _networkModel.getPhysicalNetworkId(guestNetwork); PhysicalNetworkServiceProvider provider = _physicalProviderDao.findByServiceProvider(physicalNetworkId, typeString); if (provider == null) { throw new CloudRuntimeException( "Cannot find service provider " + typeString + " in physical network " + physicalNetworkId); } VirtualRouterProvider vrProvider = _vrProviderDao.findByNspIdAndType( provider.getId(), VirtualRouterProviderType.ElasticLoadBalancerVm); if (vrProvider == null) { throw new CloudRuntimeException( "Cannot find virtual router provider " + typeString + " as service provider " + provider.getId()); } elbVm = new DomainRouterVO( id, _elasticLbVmOffering.getId(), vrProvider.getId(), VirtualMachineName.getSystemVmName(id, _instance, _elbVmNamePrefix), template.getId(), template.getHypervisorType(), template.getGuestOSId(), owner.getDomainId(), owner.getId(), false, 0, false, RedundantState.UNKNOWN, _elasticLbVmOffering.getOfferHA(), false, VirtualMachine.Type.ElasticLoadBalancerVm, null); elbVm.setRole(Role.LB); elbVm = _itMgr.allocate(elbVm, template, _elasticLbVmOffering, networks, plan, null, owner); // TODO: create usage stats } State state = elbVm.getState(); if (state != State.Running) { elbVm = this.start( elbVm, _accountService.getSystemUser(), _accountService.getSystemAccount(), params); } return elbVm; } finally { _networkDao.releaseFromLockTable(guestNetworkId); } }