Search in sources :

Example 6 with Network

use of com.vmware.vim25.mo.Network in project CloudStack-archive by CloudStack-extras.

the class HypervisorHostHelper method prepareNetwork.

public static Pair<ManagedObjectReference, String> prepareNetwork(String vSwitchName, String namePrefix, HostMO hostMo, String vlanId, Integer networkRateMbps, Integer networkRateMulticastMbps, long timeOutMs, boolean syncPeerHosts) throws Exception {
    HostVirtualSwitch vSwitch;
    vSwitch = hostMo.getHostVirtualSwitchByName(vSwitchName);
    if (vSwitch == null) {
        String msg = "Unable to find vSwitch" + vSwitchName;
        s_logger.error(msg);
        throw new Exception(msg);
    }
    boolean createGCTag = false;
    String networkName;
    Integer vid = null;
    if (vlanId != null && !UNTAGGED_VLAN_NAME.equalsIgnoreCase(vlanId)) {
        createGCTag = true;
        vid = Integer.parseInt(vlanId);
    }
    networkName = composeCloudNetworkName(namePrefix, vlanId, networkRateMbps, vSwitchName);
    HostNetworkTrafficShapingPolicy shapingPolicy = null;
    if (networkRateMbps != null && networkRateMbps.intValue() > 0) {
        shapingPolicy = new HostNetworkTrafficShapingPolicy();
        shapingPolicy.setEnabled(true);
        shapingPolicy.setAverageBandwidth((long) networkRateMbps.intValue() * 1024L * 1024L);
        // 
        // TODO : people may have different opinion on how to set the following
        //
        // give 50% premium to peek
        shapingPolicy.setPeakBandwidth((long) (shapingPolicy.getAverageBandwidth() * 1.5));
        // allow 5 seconds of burst transfer
        shapingPolicy.setBurstSize(5 * shapingPolicy.getAverageBandwidth() / 8);
    }
    boolean bWaitPortGroupReady = false;
    if (!hostMo.hasPortGroup(vSwitch, networkName)) {
        hostMo.createPortGroup(vSwitch, networkName, vid, shapingPolicy);
        bWaitPortGroupReady = true;
    } else {
        HostPortGroupSpec spec = hostMo.getPortGroupSpec(networkName);
        if (!isSpecMatch(spec, vid, shapingPolicy)) {
            hostMo.updatePortGroup(vSwitch, networkName, vid, shapingPolicy);
            bWaitPortGroupReady = true;
        }
    }
    ManagedObjectReference morNetwork;
    if (bWaitPortGroupReady)
        morNetwork = waitForNetworkReady(hostMo, networkName, timeOutMs);
    else
        morNetwork = hostMo.getNetworkMor(networkName);
    if (morNetwork == null) {
        String msg = "Failed to create guest network " + networkName;
        s_logger.error(msg);
        throw new Exception(msg);
    }
    if (createGCTag) {
        NetworkMO networkMo = new NetworkMO(hostMo.getContext(), morNetwork);
        networkMo.setCustomFieldValue(CustomFieldConstants.CLOUD_GC, "true");
    }
    if (syncPeerHosts) {
        ManagedObjectReference morParent = hostMo.getParentMor();
        if (morParent != null && morParent.getType().equals("ClusterComputeResource")) {
            // to be conservative, lock cluster
            GlobalLock lock = GlobalLock.getInternLock("ClusterLock." + morParent.get_value());
            try {
                if (lock.lock(DEFAULT_LOCK_TIMEOUT_SECONDS)) {
                    try {
                        ManagedObjectReference[] hosts = (ManagedObjectReference[]) hostMo.getContext().getServiceUtil().getDynamicProperty(morParent, "host");
                        if (hosts != null) {
                            for (ManagedObjectReference otherHost : hosts) {
                                if (!otherHost.get_value().equals(hostMo.getMor().get_value())) {
                                    HostMO otherHostMo = new HostMO(hostMo.getContext(), otherHost);
                                    try {
                                        if (s_logger.isDebugEnabled())
                                            s_logger.debug("Prepare network on other host, vlan: " + vlanId + ", host: " + otherHostMo.getHostName());
                                        prepareNetwork(vSwitchName, namePrefix, otherHostMo, vlanId, networkRateMbps, networkRateMulticastMbps, timeOutMs, false);
                                    } catch (Exception e) {
                                        s_logger.warn("Unable to prepare network on other host, vlan: " + vlanId + ", host: " + otherHostMo.getHostName());
                                    }
                                }
                            }
                        }
                    } finally {
                        lock.unlock();
                    }
                } else {
                    s_logger.warn("Unable to lock cluster to prepare guest network, vlan: " + vlanId);
                }
            } finally {
                lock.releaseRef();
            }
        }
    }
    s_logger.info("Network " + networkName + " is ready on vSwitch " + vSwitchName);
    return new Pair<ManagedObjectReference, String>(morNetwork, networkName);
}
Also used : HostPortGroupSpec(com.vmware.vim25.HostPortGroupSpec) URISyntaxException(java.net.URISyntaxException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) GlobalLock(com.cloud.utils.db.GlobalLock) HostVirtualSwitch(com.vmware.vim25.HostVirtualSwitch) HostNetworkTrafficShapingPolicy(com.vmware.vim25.HostNetworkTrafficShapingPolicy) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Pair(com.cloud.utils.Pair)

Example 7 with Network

use of com.vmware.vim25.mo.Network in project CloudStack-archive by CloudStack-extras.

the class HypervisorHostHelper method waitForNetworkReady.

public static ManagedObjectReference waitForNetworkReady(HostMO hostMo, String networkName, long timeOutMs) throws Exception {
    ManagedObjectReference morNetwork = null;
    // if portGroup is just created, getNetwork may fail to retrieve it, we
    // need to retry
    long startTick = System.currentTimeMillis();
    while (System.currentTimeMillis() - startTick <= timeOutMs) {
        morNetwork = hostMo.getNetworkMor(networkName);
        if (morNetwork != null) {
            break;
        }
        s_logger.info("Waiting for network " + networkName + " to be ready");
        Thread.sleep(1000);
    }
    return morNetwork;
}
Also used : ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 8 with Network

use of com.vmware.vim25.mo.Network in project cloudstack by apache.

the class HypervisorHostHelperTest method testIsSpecMatchConfigSpecWithHighBandwidthShapingPolicy.

@Test
public void testIsSpecMatchConfigSpecWithHighBandwidthShapingPolicy() throws Exception {
    // Tests case of network offering upgrade in terms of bandwidth
    int currentNumPorts = 256;
    int currentvlanId = 100;
    boolean currentAutoExpand = true;
    DVSTrafficShapingPolicy currentTrafficShapingPolicy = new DVSTrafficShapingPolicy();
    BoolPolicy currentIsEnabled = new BoolPolicy();
    currentIsEnabled.setValue(true);
    LongPolicy currentAvgBw = new LongPolicy();
    currentAvgBw.setValue(200L);
    LongPolicy currentBurstSize = new LongPolicy();
    currentBurstSize.setValue(400L);
    LongPolicy currentPeakBw = new LongPolicy();
    currentPeakBw.setValue(2000L);
    VMwareDVSPortSetting currentVmwareDvsPortSetting = new VMwareDVSPortSetting();
    VmwareDistributedVirtualSwitchVlanIdSpec currentVlanIdSpec = new VmwareDistributedVirtualSwitchVlanIdSpec();
    currentVlanIdSpec.setVlanId(currentvlanId);
    currentVmwareDvsPortSetting.setVlan(currentVlanIdSpec);
    currentTrafficShapingPolicy.setAverageBandwidth(currentAvgBw);
    currentTrafficShapingPolicy.setBurstSize(currentBurstSize);
    currentTrafficShapingPolicy.setPeakBandwidth(currentPeakBw);
    currentTrafficShapingPolicy.setEnabled(currentIsEnabled);
    currentVmwareDvsPortSetting.setInShapingPolicy(currentTrafficShapingPolicy);
    when(currentDvPortgroupInfo.getNumPorts()).thenReturn(currentNumPorts);
    when(currentDvPortgroupInfo.isAutoExpand()).thenReturn(currentAutoExpand);
    when(currentDvPortgroupInfo.getDefaultPortConfig()).thenReturn(currentVmwareDvsPortSetting);
    int newNumPorts = 256;
    int newvlanId = 100;
    boolean newAutoExpand = true;
    DVSTrafficShapingPolicy newTrafficShapingPolicy = new DVSTrafficShapingPolicy();
    BoolPolicy newIsEnabled = new BoolPolicy();
    newIsEnabled.setValue(true);
    LongPolicy newAvgBw = new LongPolicy();
    newAvgBw.setValue(400L);
    LongPolicy newBurstSize = new LongPolicy();
    newBurstSize.setValue(800L);
    LongPolicy newPeakBw = new LongPolicy();
    newPeakBw.setValue(4000L);
    VMwareDVSPortSetting newVmwareDvsPortSetting = new VMwareDVSPortSetting();
    VmwareDistributedVirtualSwitchVlanIdSpec newVlanIdSpec = new VmwareDistributedVirtualSwitchVlanIdSpec();
    newVlanIdSpec.setVlanId(newvlanId);
    newVmwareDvsPortSetting.setVlan(newVlanIdSpec);
    newTrafficShapingPolicy.setAverageBandwidth(newAvgBw);
    newTrafficShapingPolicy.setBurstSize(newBurstSize);
    newTrafficShapingPolicy.setPeakBandwidth(newPeakBw);
    newTrafficShapingPolicy.setEnabled(newIsEnabled);
    newVmwareDvsPortSetting.setInShapingPolicy(newTrafficShapingPolicy);
    when(dvPortgroupConfigSpec.getNumPorts()).thenReturn(newNumPorts);
    when(dvPortgroupConfigSpec.isAutoExpand()).thenReturn(newAutoExpand);
    when(dvPortgroupConfigSpec.getDefaultPortConfig()).thenReturn(newVmwareDvsPortSetting);
    boolean specCompareResult = HypervisorHostHelper.isSpecMatch(currentDvPortgroupInfo, dvPortgroupConfigSpec);
    assertFalse(specCompareResult);
}
Also used : LongPolicy(com.vmware.vim25.LongPolicy) VMwareDVSPortSetting(com.vmware.vim25.VMwareDVSPortSetting) DVSTrafficShapingPolicy(com.vmware.vim25.DVSTrafficShapingPolicy) BoolPolicy(com.vmware.vim25.BoolPolicy) VmwareDistributedVirtualSwitchVlanIdSpec(com.vmware.vim25.VmwareDistributedVirtualSwitchVlanIdSpec) Test(org.junit.Test)

Example 9 with Network

use of com.vmware.vim25.mo.Network in project cloudstack by apache.

the class DatacenterMO method getDvSwitchMor.

public ManagedObjectReference getDvSwitchMor(ManagedObjectReference dvPortGroupMor) throws Exception {
    String dvPortGroupKey = null;
    ManagedObjectReference dvSwitchMor = null;
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("DistributedVirtualPortgroup");
    pSpec.getPathSet().add("key");
    pSpec.getPathSet().add("config.distributedVirtualSwitch");
    TraversalSpec datacenter2DvPortGroupTraversal = new TraversalSpec();
    datacenter2DvPortGroupTraversal.setType("Datacenter");
    datacenter2DvPortGroupTraversal.setPath("network");
    datacenter2DvPortGroupTraversal.setName("datacenter2DvPortgroupTraversal");
    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(_mor);
    oSpec.setSkip(Boolean.TRUE);
    oSpec.getSelectSet().add(datacenter2DvPortGroupTraversal);
    PropertyFilterSpec pfSpec = new PropertyFilterSpec();
    pfSpec.getPropSet().add(pSpec);
    pfSpec.getObjectSet().add(oSpec);
    List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
    pfSpecArr.add(pfSpec);
    List<ObjectContent> ocs = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);
    if (ocs != null) {
        for (ObjectContent oc : ocs) {
            List<DynamicProperty> props = oc.getPropSet();
            if (props != null) {
                assert (props.size() == 2);
                for (DynamicProperty prop : props) {
                    if (prop.getName().equals("key")) {
                        dvPortGroupKey = (String) prop.getVal();
                    } else {
                        dvSwitchMor = (ManagedObjectReference) prop.getVal();
                    }
                }
                if ((dvPortGroupKey != null) && dvPortGroupKey.equals(dvPortGroupMor.getValue())) {
                    return dvSwitchMor;
                }
            }
        }
    }
    return null;
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) ObjectContent(com.vmware.vim25.ObjectContent) ObjectSpec(com.vmware.vim25.ObjectSpec) PropertySpec(com.vmware.vim25.PropertySpec) DynamicProperty(com.vmware.vim25.DynamicProperty) TraversalSpec(com.vmware.vim25.TraversalSpec) ArrayList(java.util.ArrayList) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 10 with Network

use of com.vmware.vim25.mo.Network in project cloudstack by apache.

the class VmwareResource method plugPublicNic.

private void plugPublicNic(VirtualMachineMO vmMo, final String vlanId, final String vifMacAddress) throws Exception {
    // TODO : probably need to set traffic shaping
    Pair<ManagedObjectReference, String> networkInfo = null;
    VirtualSwitchType vSwitchType = VirtualSwitchType.StandardVirtualSwitch;
    if (_publicTrafficInfo != null) {
        vSwitchType = _publicTrafficInfo.getVirtualSwitchType();
    }
    /** FIXME We have no clue which network this nic is on and that means that we can't figure out the BroadcastDomainType
         *  so we assume that it's VLAN for now
         */
    if (VirtualSwitchType.StandardVirtualSwitch == vSwitchType) {
        networkInfo = HypervisorHostHelper.prepareNetwork(_publicTrafficInfo.getVirtualSwitchName(), "cloud.public", vmMo.getRunningHost(), vlanId, null, null, _opsTimeout, true, BroadcastDomainType.Vlan, null);
    } else {
        networkInfo = HypervisorHostHelper.prepareNetwork(_publicTrafficInfo.getVirtualSwitchName(), "cloud.public", vmMo.getRunningHost(), vlanId, null, null, null, _opsTimeout, vSwitchType, _portsPerDvPortGroup, null, false, BroadcastDomainType.Vlan, _vsmCredentials);
    }
    int nicIndex = allocPublicNicIndex(vmMo);
    try {
        VirtualDevice[] nicDevices = vmMo.getNicDevices();
        VirtualEthernetCard device = (VirtualEthernetCard) nicDevices[nicIndex];
        if (VirtualSwitchType.StandardVirtualSwitch == vSwitchType) {
            VirtualEthernetCardNetworkBackingInfo nicBacking = new VirtualEthernetCardNetworkBackingInfo();
            nicBacking.setDeviceName(networkInfo.second());
            nicBacking.setNetwork(networkInfo.first());
            device.setBacking(nicBacking);
        } else {
            HostMO hostMo = vmMo.getRunningHost();
            DatacenterMO dataCenterMo = new DatacenterMO(hostMo.getContext(), hostMo.getHyperHostDatacenter());
            device.setBacking(dataCenterMo.getDvPortBackingInfo(networkInfo));
        }
        VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
        //VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
        VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
        deviceConfigSpec.setDevice(device);
        deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.EDIT);
        vmConfigSpec.getDeviceChange().add(deviceConfigSpec);
        if (!vmMo.configureVm(vmConfigSpec)) {
            throw new Exception("Failed to configure devices when plugPublicNic");
        }
    } catch (Exception e) {
        // restore allocation mask in case of exceptions
        String nicMasksStr = vmMo.getCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK);
        int nicMasks = Integer.parseInt(nicMasksStr);
        nicMasks &= ~(1 << nicIndex);
        vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK, String.valueOf(nicMasks));
        throw e;
    }
}
Also used : VirtualEthernetCardNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualDevice(com.vmware.vim25.VirtualDevice) ConnectException(java.net.ConnectException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) CloudException(com.cloud.exception.CloudException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualSwitchType(com.cloud.hypervisor.vmware.mo.VirtualSwitchType) VirtualEthernetCard(com.vmware.vim25.VirtualEthernetCard) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO)

Aggregations

ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)23 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)12 ObjectContent (com.vmware.vim25.ObjectContent)12 ObjectSpec (com.vmware.vim25.ObjectSpec)12 PropertyFilterSpec (com.vmware.vim25.PropertyFilterSpec)12 PropertySpec (com.vmware.vim25.PropertySpec)12 TraversalSpec (com.vmware.vim25.TraversalSpec)12 ArrayList (java.util.ArrayList)11 CloudException (com.cloud.exception.CloudException)10 DynamicProperty (com.vmware.vim25.DynamicProperty)10 IOException (java.io.IOException)10 RemoteException (java.rmi.RemoteException)10 UnsupportedEncodingException (java.io.UnsupportedEncodingException)9 Pair (com.cloud.utils.Pair)8 ConfigurationException (javax.naming.ConfigurationException)8 InternalErrorException (com.cloud.exception.InternalErrorException)7 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)7 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)7 ConnectException (java.net.ConnectException)7 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)6