Search in sources :

Example 1 with VirtualEthernetCardNetworkBackingInfo

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

the class VmwareHelper method prepareNicDevice.

public static VirtualDevice prepareNicDevice(VirtualMachineMO vmMo, ManagedObjectReference morNetwork, VirtualEthernetCardType deviceType, String portGroupName, String macAddress, int deviceNumber, int contextNumber, boolean conntected, boolean connectOnStart) throws Exception {
    VirtualEthernetCard nic;
    switch(deviceType) {
        case E1000:
            nic = new VirtualE1000();
            break;
        case PCNet32:
            nic = new VirtualPCNet32();
            break;
        case Vmxnet2:
            nic = new VirtualVmxnet2();
            break;
        case Vmxnet3:
            nic = new VirtualVmxnet3();
            break;
        default:
            assert (false);
            nic = new VirtualE1000();
    }
    VirtualEthernetCardNetworkBackingInfo nicBacking = new VirtualEthernetCardNetworkBackingInfo();
    nicBacking.setDeviceName(portGroupName);
    nicBacking.setNetwork(morNetwork);
    nic.setBacking(nicBacking);
    VirtualDeviceConnectInfo connectInfo = new VirtualDeviceConnectInfo();
    connectInfo.setAllowGuestControl(true);
    connectInfo.setConnected(conntected);
    connectInfo.setStartConnected(connectOnStart);
    nic.setAddressType("Manual");
    nic.setConnectable(connectInfo);
    nic.setMacAddress(macAddress);
    nic.setUnitNumber(deviceNumber);
    nic.setKey(-contextNumber);
    return nic;
}
Also used : VirtualEthernetCardNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo) VirtualPCNet32(com.vmware.vim25.VirtualPCNet32) VirtualVmxnet2(com.vmware.vim25.VirtualVmxnet2) VirtualVmxnet3(com.vmware.vim25.VirtualVmxnet3) VirtualEthernetCard(com.vmware.vim25.VirtualEthernetCard) VirtualDeviceConnectInfo(com.vmware.vim25.VirtualDeviceConnectInfo) VirtualE1000(com.vmware.vim25.VirtualE1000)

Example 2 with VirtualEthernetCardNetworkBackingInfo

use of com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo 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)

Example 3 with VirtualEthernetCardNetworkBackingInfo

use of com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo in project cloudstack by apache.

the class VmwareResource method postNvpConfigBeforeStart.

private static void postNvpConfigBeforeStart(VirtualMachineMO vmMo, VirtualMachineTO vmSpec) throws Exception {
    /**
         * We need to configure the port on the DV switch after the host is
         * connected. So make this happen between the configure and start of
         * the VM
         */
    int nicIndex = 0;
    for (NicTO nicTo : sortNicsByDeviceId(vmSpec.getNics())) {
        if (nicTo.getBroadcastType() == BroadcastDomainType.Lswitch) {
            // We need to create a port with a unique vlan and pass the key to the nic device
            s_logger.trace("Nic " + nicTo.toString() + " is connected to an NVP logicalswitch");
            VirtualDevice nicVirtualDevice = vmMo.getNicDeviceByIndex(nicIndex);
            if (nicVirtualDevice == null) {
                //FIXME Generic exceptions are bad
                throw new Exception("Failed to find a VirtualDevice for nic " + nicIndex);
            }
            VirtualDeviceBackingInfo backing = nicVirtualDevice.getBacking();
            if (backing instanceof VirtualEthernetCardDistributedVirtualPortBackingInfo) {
                // This NIC is connected to a Distributed Virtual Switch
                VirtualEthernetCardDistributedVirtualPortBackingInfo portInfo = (VirtualEthernetCardDistributedVirtualPortBackingInfo) backing;
                DistributedVirtualSwitchPortConnection port = portInfo.getPort();
                String portKey = port.getPortKey();
                String portGroupKey = port.getPortgroupKey();
                String dvSwitchUuid = port.getSwitchUuid();
                s_logger.debug("NIC " + nicTo.toString() + " is connected to dvSwitch " + dvSwitchUuid + " pg " + portGroupKey + " port " + portKey);
                ManagedObjectReference dvSwitchManager = vmMo.getContext().getVimClient().getServiceContent().getDvSwitchManager();
                ManagedObjectReference dvSwitch = vmMo.getContext().getVimClient().getService().queryDvsByUuid(dvSwitchManager, dvSwitchUuid);
                // Get all ports
                DistributedVirtualSwitchPortCriteria criteria = new DistributedVirtualSwitchPortCriteria();
                criteria.setInside(true);
                criteria.getPortgroupKey().add(portGroupKey);
                List<DistributedVirtualPort> dvPorts = vmMo.getContext().getVimClient().getService().fetchDVPorts(dvSwitch, criteria);
                DistributedVirtualPort vmDvPort = null;
                List<Integer> usedVlans = new ArrayList<Integer>();
                for (DistributedVirtualPort dvPort : dvPorts) {
                    // Find the port for this NIC by portkey
                    if (portKey.equals(dvPort.getKey())) {
                        vmDvPort = dvPort;
                    }
                    VMwareDVSPortSetting settings = (VMwareDVSPortSetting) dvPort.getConfig().getSetting();
                    VmwareDistributedVirtualSwitchVlanIdSpec vlanId = (VmwareDistributedVirtualSwitchVlanIdSpec) settings.getVlan();
                    s_logger.trace("Found port " + dvPort.getKey() + " with vlan " + vlanId.getVlanId());
                    if (vlanId.getVlanId() > 0 && vlanId.getVlanId() < 4095) {
                        usedVlans.add(vlanId.getVlanId());
                    }
                }
                if (vmDvPort == null) {
                    throw new Exception("Empty port list from dvSwitch for nic " + nicTo.toString());
                }
                DVPortConfigInfo dvPortConfigInfo = vmDvPort.getConfig();
                VMwareDVSPortSetting settings = (VMwareDVSPortSetting) dvPortConfigInfo.getSetting();
                VmwareDistributedVirtualSwitchVlanIdSpec vlanId = (VmwareDistributedVirtualSwitchVlanIdSpec) settings.getVlan();
                BoolPolicy blocked = settings.getBlocked();
                if (blocked.isValue() == Boolean.TRUE) {
                    s_logger.trace("Port is blocked, set a vlanid and unblock");
                    DVPortConfigSpec dvPortConfigSpec = new DVPortConfigSpec();
                    VMwareDVSPortSetting edittedSettings = new VMwareDVSPortSetting();
                    // Unblock
                    blocked.setValue(Boolean.FALSE);
                    blocked.setInherited(Boolean.FALSE);
                    edittedSettings.setBlocked(blocked);
                    // Set vlan
                    int i;
                    for (i = 1; i < 4095; i++) {
                        if (!usedVlans.contains(i))
                            break;
                    }
                    // FIXME should be a determined
                    vlanId.setVlanId(i);
                    // based on usage
                    vlanId.setInherited(false);
                    edittedSettings.setVlan(vlanId);
                    dvPortConfigSpec.setSetting(edittedSettings);
                    dvPortConfigSpec.setOperation("edit");
                    dvPortConfigSpec.setKey(portKey);
                    List<DVPortConfigSpec> dvPortConfigSpecs = new ArrayList<DVPortConfigSpec>();
                    dvPortConfigSpecs.add(dvPortConfigSpec);
                    ManagedObjectReference task = vmMo.getContext().getVimClient().getService().reconfigureDVPortTask(dvSwitch, dvPortConfigSpecs);
                    if (!vmMo.getContext().getVimClient().waitForTask(task)) {
                        throw new Exception("Failed to configure the dvSwitch port for nic " + nicTo.toString());
                    }
                    s_logger.debug("NIC " + nicTo.toString() + " connected to vlan " + i);
                } else {
                    s_logger.trace("Port already configured and set to vlan " + vlanId.getVlanId());
                }
            } else if (backing instanceof VirtualEthernetCardNetworkBackingInfo) {
            // This NIC is connected to a Virtual Switch
            // Nothing to do
            } else if (backing instanceof VirtualEthernetCardOpaqueNetworkBackingInfo) {
            //if NSX API VERSION >= 4.2, connect to br-int (nsx.network), do not create portgroup else previous behaviour
            //OK, connected to OpaqueNetwork
            } else {
                s_logger.error("nic device backing is of type " + backing.getClass().getName());
                //FIXME Generic exceptions are bad
                throw new Exception("Incompatible backing for a VirtualDevice for nic " + nicIndex);
            }
        }
        nicIndex++;
    }
}
Also used : DVPortConfigSpec(com.vmware.vim25.DVPortConfigSpec) VMwareDVSPortSetting(com.vmware.vim25.VMwareDVSPortSetting) VirtualEthernetCardDistributedVirtualPortBackingInfo(com.vmware.vim25.VirtualEthernetCardDistributedVirtualPortBackingInfo) VirtualEthernetCardNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo) VirtualEthernetCardOpaqueNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardOpaqueNetworkBackingInfo) DistributedVirtualPort(com.vmware.vim25.DistributedVirtualPort) VirtualDevice(com.vmware.vim25.VirtualDevice) ArrayList(java.util.ArrayList) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) DistributedVirtualSwitchPortConnection(com.vmware.vim25.DistributedVirtualSwitchPortConnection) BoolPolicy(com.vmware.vim25.BoolPolicy) 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) DVPortConfigInfo(com.vmware.vim25.DVPortConfigInfo) DistributedVirtualSwitchPortCriteria(com.vmware.vim25.DistributedVirtualSwitchPortCriteria) VmwareDistributedVirtualSwitchVlanIdSpec(com.vmware.vim25.VmwareDistributedVirtualSwitchVlanIdSpec) NicTO(com.cloud.agent.api.to.NicTO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 4 with VirtualEthernetCardNetworkBackingInfo

use of com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo in project cloudstack by apache.

the class VmwareHelper method prepareNicDevice.

public static VirtualDevice prepareNicDevice(VirtualMachineMO vmMo, ManagedObjectReference morNetwork, VirtualEthernetCardType deviceType, String portGroupName, String macAddress, int deviceNumber, int contextNumber, boolean conntected, boolean connectOnStart) throws Exception {
    VirtualEthernetCard nic;
    switch(deviceType) {
        case E1000:
            nic = new VirtualE1000();
            break;
        case PCNet32:
            nic = new VirtualPCNet32();
            break;
        case Vmxnet2:
            nic = new VirtualVmxnet2();
            break;
        case Vmxnet3:
            nic = new VirtualVmxnet3();
            break;
        default:
            assert (false);
            nic = new VirtualE1000();
    }
    VirtualEthernetCardNetworkBackingInfo nicBacking = new VirtualEthernetCardNetworkBackingInfo();
    nicBacking.setDeviceName(portGroupName);
    nicBacking.setNetwork(morNetwork);
    nic.setBacking(nicBacking);
    VirtualDeviceConnectInfo connectInfo = new VirtualDeviceConnectInfo();
    connectInfo.setAllowGuestControl(true);
    connectInfo.setConnected(conntected);
    connectInfo.setStartConnected(connectOnStart);
    nic.setAddressType("Manual");
    nic.setConnectable(connectInfo);
    nic.setMacAddress(macAddress);
    nic.setUnitNumber(deviceNumber);
    nic.setKey(-contextNumber);
    return nic;
}
Also used : VirtualEthernetCardNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo) VirtualPCNet32(com.vmware.vim25.VirtualPCNet32) VirtualVmxnet2(com.vmware.vim25.VirtualVmxnet2) VirtualVmxnet3(com.vmware.vim25.VirtualVmxnet3) VirtualEthernetCard(com.vmware.vim25.VirtualEthernetCard) VirtualDeviceConnectInfo(com.vmware.vim25.VirtualDeviceConnectInfo) VirtualE1000(com.vmware.vim25.VirtualE1000)

Aggregations

VirtualEthernetCardNetworkBackingInfo (com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo)4 VirtualEthernetCard (com.vmware.vim25.VirtualEthernetCard)3 CloudException (com.cloud.exception.CloudException)2 InternalErrorException (com.cloud.exception.InternalErrorException)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)2 VirtualDevice (com.vmware.vim25.VirtualDevice)2 VirtualDeviceConnectInfo (com.vmware.vim25.VirtualDeviceConnectInfo)2 VirtualE1000 (com.vmware.vim25.VirtualE1000)2 VirtualPCNet32 (com.vmware.vim25.VirtualPCNet32)2 VirtualVmxnet2 (com.vmware.vim25.VirtualVmxnet2)2 VirtualVmxnet3 (com.vmware.vim25.VirtualVmxnet3)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ConnectException (java.net.ConnectException)2 RemoteException (java.rmi.RemoteException)2 ConfigurationException (javax.naming.ConfigurationException)2 NicTO (com.cloud.agent.api.to.NicTO)1 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)1 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)1