Search in sources :

Example 1 with VirtualVmxnet2

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

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

the class VmwareHelper method prepareDvNicDevice.

public static VirtualDevice prepareDvNicDevice(VirtualMachineMO vmMo, ManagedObjectReference morNetwork, VirtualEthernetCardType deviceType, String dvPortGroupName, String dvSwitchUuid, 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();
    }
    final VirtualEthernetCardDistributedVirtualPortBackingInfo dvPortBacking = new VirtualEthernetCardDistributedVirtualPortBackingInfo();
    final DistributedVirtualSwitchPortConnection dvPortConnection = new DistributedVirtualSwitchPortConnection();
    final VirtualDeviceConnectInfo connectInfo = new VirtualDeviceConnectInfo();
    dvPortConnection.setSwitchUuid(dvSwitchUuid);
    dvPortConnection.setPortgroupKey(morNetwork.get_value());
    dvPortBacking.setPort(dvPortConnection);
    nic.setBacking(dvPortBacking);
    nic.setKey(30);
    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 : VirtualEthernetCardDistributedVirtualPortBackingInfo(com.vmware.vim25.VirtualEthernetCardDistributedVirtualPortBackingInfo) VirtualPCNet32(com.vmware.vim25.VirtualPCNet32) VirtualVmxnet2(com.vmware.vim25.VirtualVmxnet2) VirtualVmxnet3(com.vmware.vim25.VirtualVmxnet3) DistributedVirtualSwitchPortConnection(com.vmware.vim25.DistributedVirtualSwitchPortConnection) VirtualEthernetCard(com.vmware.vim25.VirtualEthernetCard) VirtualDeviceConnectInfo(com.vmware.vim25.VirtualDeviceConnectInfo) VirtualE1000(com.vmware.vim25.VirtualE1000)

Example 3 with VirtualVmxnet2

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

the class VmwareResource method getUnmanageInstanceNics.

private List<UnmanagedInstanceTO.Nic> getUnmanageInstanceNics(VmwareHypervisorHost hyperHost, VirtualMachineMO vmMo) {
    List<UnmanagedInstanceTO.Nic> instanceNics = new ArrayList<>();
    HashMap<String, List<String>> guestNicMacIPAddressMap = new HashMap<>();
    try {
        GuestInfo guestInfo = vmMo.getGuestInfo();
        if (guestInfo.getToolsStatus() == VirtualMachineToolsStatus.TOOLS_OK) {
            for (GuestNicInfo nicInfo : guestInfo.getNet()) {
                if (CollectionUtils.isNotEmpty(nicInfo.getIpAddress())) {
                    List<String> ipAddresses = new ArrayList<>();
                    for (String ipAddress : nicInfo.getIpAddress()) {
                        if (NetUtils.isValidIp4(ipAddress)) {
                            ipAddresses.add(ipAddress);
                        }
                    }
                    guestNicMacIPAddressMap.put(nicInfo.getMacAddress(), ipAddresses);
                }
            }
        } else {
            s_logger.info(String.format("Unable to retrieve guest nics for instance: %s from VMware tools as tools status: %s", vmMo.getName(), guestInfo.getToolsStatus().toString()));
        }
    } catch (Exception e) {
        s_logger.info("Unable to retrieve guest nics for instance from VMware tools. " + e.getMessage());
    }
    VirtualDevice[] nics = null;
    try {
        nics = vmMo.getNicDevices();
    } catch (Exception e) {
        s_logger.info("Unable to retrieve unmanaged instance nics. " + e.getMessage());
    }
    if (nics != null) {
        for (VirtualDevice nic : nics) {
            try {
                VirtualEthernetCard ethCardDevice = (VirtualEthernetCard) nic;
                s_logger.error(nic.getClass().getCanonicalName() + " " + nic.getBacking().getClass().getCanonicalName() + " " + ethCardDevice.getMacAddress());
                UnmanagedInstanceTO.Nic instanceNic = new UnmanagedInstanceTO.Nic();
                instanceNic.setNicId(ethCardDevice.getDeviceInfo().getLabel());
                if (ethCardDevice instanceof VirtualPCNet32) {
                    instanceNic.setAdapterType(VirtualEthernetCardType.PCNet32.toString());
                } else if (ethCardDevice instanceof VirtualVmxnet2) {
                    instanceNic.setAdapterType(VirtualEthernetCardType.Vmxnet2.toString());
                } else if (ethCardDevice instanceof VirtualVmxnet3) {
                    instanceNic.setAdapterType(VirtualEthernetCardType.Vmxnet3.toString());
                } else {
                    instanceNic.setAdapterType(VirtualEthernetCardType.E1000.toString());
                }
                instanceNic.setMacAddress(ethCardDevice.getMacAddress());
                if (guestNicMacIPAddressMap.containsKey(instanceNic.getMacAddress())) {
                    instanceNic.setIpAddress(guestNicMacIPAddressMap.get(instanceNic.getMacAddress()));
                }
                if (ethCardDevice.getSlotInfo() != null) {
                    instanceNic.setPciSlot(ethCardDevice.getSlotInfo().toString());
                }
                VirtualDeviceBackingInfo backing = ethCardDevice.getBacking();
                if (backing instanceof VirtualEthernetCardDistributedVirtualPortBackingInfo) {
                    VirtualEthernetCardDistributedVirtualPortBackingInfo backingInfo = (VirtualEthernetCardDistributedVirtualPortBackingInfo) backing;
                    DistributedVirtualSwitchPortConnection port = backingInfo.getPort();
                    String portKey = port.getPortKey();
                    String portGroupKey = port.getPortgroupKey();
                    String dvSwitchUuid = port.getSwitchUuid();
                    s_logger.debug("NIC " + nic.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);
                    for (DistributedVirtualPort dvPort : dvPorts) {
                        // Find the port for this NIC by portkey
                        if (portKey.equals(dvPort.getKey())) {
                            VMwareDVSPortSetting settings = (VMwareDVSPortSetting) dvPort.getConfig().getSetting();
                            if (settings.getVlan() instanceof VmwareDistributedVirtualSwitchVlanIdSpec) {
                                VmwareDistributedVirtualSwitchVlanIdSpec vlanId = (VmwareDistributedVirtualSwitchVlanIdSpec) settings.getVlan();
                                s_logger.trace("Found port " + dvPort.getKey() + " with vlan " + vlanId.getVlanId());
                                if (vlanId.getVlanId() > 0 && vlanId.getVlanId() < 4095) {
                                    instanceNic.setVlan(vlanId.getVlanId());
                                }
                            } else if (settings.getVlan() instanceof VmwareDistributedVirtualSwitchPvlanSpec) {
                                VmwareDistributedVirtualSwitchPvlanSpec pvlanSpec = (VmwareDistributedVirtualSwitchPvlanSpec) settings.getVlan();
                                s_logger.trace("Found port " + dvPort.getKey() + " with pvlan " + pvlanSpec.getPvlanId());
                                if (pvlanSpec.getPvlanId() > 0 && pvlanSpec.getPvlanId() < 4095) {
                                    DistributedVirtualSwitchMO dvSwitchMo = new DistributedVirtualSwitchMO(vmMo.getContext(), dvSwitch);
                                    Pair<Integer, HypervisorHostHelper.PvlanType> vlanDetails = dvSwitchMo.retrieveVlanFromPvlan(pvlanSpec.getPvlanId(), dvSwitch);
                                    if (vlanDetails != null && vlanDetails.first() != null && vlanDetails.second() != null) {
                                        instanceNic.setVlan(vlanDetails.first());
                                        instanceNic.setPvlan(pvlanSpec.getPvlanId());
                                        instanceNic.setPvlanType(vlanDetails.second().toString());
                                    }
                                }
                            }
                            break;
                        }
                    }
                } else if (backing instanceof VirtualEthernetCardNetworkBackingInfo) {
                    VirtualEthernetCardNetworkBackingInfo backingInfo = (VirtualEthernetCardNetworkBackingInfo) backing;
                    instanceNic.setNetwork(backingInfo.getDeviceName());
                    if (hyperHost instanceof HostMO) {
                        HostMO hostMo = (HostMO) hyperHost;
                        HostPortGroupSpec portGroupSpec = hostMo.getHostPortGroupSpec(backingInfo.getDeviceName());
                        instanceNic.setVlan(portGroupSpec.getVlanId());
                    }
                }
                instanceNics.add(instanceNic);
            } catch (Exception e) {
                s_logger.info("Unable to retrieve unmanaged instance nic info. " + e.getMessage());
            }
        }
        Collections.sort(instanceNics, new Comparator<UnmanagedInstanceTO.Nic>() {

            @Override
            public int compare(final UnmanagedInstanceTO.Nic nic1, final UnmanagedInstanceTO.Nic nic2) {
                return extractInt(nic1) - extractInt(nic2);
            }

            int extractInt(UnmanagedInstanceTO.Nic nic) {
                String num = nic.getNicId().replaceAll("\\D", "");
                // return 0 if no digits found
                return num.isEmpty() ? 0 : Integer.parseInt(num);
            }
        });
    }
    return instanceNics;
}
Also used : VMwareDVSPortSetting(com.vmware.vim25.VMwareDVSPortSetting) VirtualEthernetCardDistributedVirtualPortBackingInfo(com.vmware.vim25.VirtualEthernetCardDistributedVirtualPortBackingInfo) VirtualEthernetCardNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo) HashMap(java.util.HashMap) VirtualPCNet32(com.vmware.vim25.VirtualPCNet32) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) ArrayList(java.util.ArrayList) VirtualVmxnet2(com.vmware.vim25.VirtualVmxnet2) HostPortGroupSpec(com.vmware.vim25.HostPortGroupSpec) VirtualVmxnet3(com.vmware.vim25.VirtualVmxnet3) DistributedVirtualSwitchPortConnection(com.vmware.vim25.DistributedVirtualSwitchPortConnection) GuestNicInfo(com.vmware.vim25.GuestNicInfo) DistributedVirtualSwitchMO(com.cloud.hypervisor.vmware.mo.DistributedVirtualSwitchMO) UnmanagedInstanceTO(org.apache.cloudstack.vm.UnmanagedInstanceTO) HypervisorHostHelper(com.cloud.hypervisor.vmware.mo.HypervisorHostHelper) DistributedVirtualSwitchPortCriteria(com.vmware.vim25.DistributedVirtualSwitchPortCriteria) ArrayList(java.util.ArrayList) List(java.util.List) VmwareDistributedVirtualSwitchPvlanSpec(com.vmware.vim25.VmwareDistributedVirtualSwitchPvlanSpec) VirtualEthernetCard(com.vmware.vim25.VirtualEthernetCard) VmwareDistributedVirtualSwitchVlanIdSpec(com.vmware.vim25.VmwareDistributedVirtualSwitchVlanIdSpec) Pair(com.cloud.utils.Pair) DistributedVirtualPort(com.vmware.vim25.DistributedVirtualPort) VirtualDevice(com.vmware.vim25.VirtualDevice) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) 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) GuestInfo(com.vmware.vim25.GuestInfo) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

VirtualEthernetCard (com.vmware.vim25.VirtualEthernetCard)3 VirtualPCNet32 (com.vmware.vim25.VirtualPCNet32)3 VirtualVmxnet2 (com.vmware.vim25.VirtualVmxnet2)3 VirtualVmxnet3 (com.vmware.vim25.VirtualVmxnet3)3 DistributedVirtualSwitchPortConnection (com.vmware.vim25.DistributedVirtualSwitchPortConnection)2 VirtualDeviceConnectInfo (com.vmware.vim25.VirtualDeviceConnectInfo)2 VirtualE1000 (com.vmware.vim25.VirtualE1000)2 VirtualEthernetCardDistributedVirtualPortBackingInfo (com.vmware.vim25.VirtualEthernetCardDistributedVirtualPortBackingInfo)2 VirtualEthernetCardNetworkBackingInfo (com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo)2 CloudException (com.cloud.exception.CloudException)1 InternalErrorException (com.cloud.exception.InternalErrorException)1 DistributedVirtualSwitchMO (com.cloud.hypervisor.vmware.mo.DistributedVirtualSwitchMO)1 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)1 HypervisorHostHelper (com.cloud.hypervisor.vmware.mo.HypervisorHostHelper)1 Pair (com.cloud.utils.Pair)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 DistributedVirtualPort (com.vmware.vim25.DistributedVirtualPort)1 DistributedVirtualSwitchPortCriteria (com.vmware.vim25.DistributedVirtualSwitchPortCriteria)1 GuestInfo (com.vmware.vim25.GuestInfo)1 GuestNicInfo (com.vmware.vim25.GuestNicInfo)1