Search in sources :

Example 21 with VIF

use of com.xensource.xenapi.VIF in project cloudstack by apache.

the class CitrixResourceBase method prepareNetworkElementCommand.

protected ExecutionResult prepareNetworkElementCommand(final IpAssocCommand cmd) {
    final Connection conn = getConnection();
    final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    final String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
    try {
        final IpAddressTO[] ips = cmd.getIpAddresses();
        for (final IpAddressTO ip : ips) {
            final VM router = getVM(conn, routerName);
            final NicTO nic = new NicTO();
            nic.setMac(ip.getVifMacAddress());
            nic.setType(ip.getTrafficType());
            if (ip.getBroadcastUri() == null) {
                nic.setBroadcastType(BroadcastDomainType.Native);
            } else {
                final URI uri = BroadcastDomainType.fromString(ip.getBroadcastUri());
                nic.setBroadcastType(BroadcastDomainType.getSchemeValue(uri));
                nic.setBroadcastUri(uri);
            }
            nic.setDeviceId(0);
            nic.setNetworkRateMbps(ip.getNetworkRate());
            nic.setName(ip.getNetworkName());
            final Network network = getNetwork(conn, nic);
            // Determine the correct VIF on DomR to associate/disassociate
            // the
            // IP address with
            VIF correctVif = getCorrectVif(conn, router, network);
            // If we are associating an IP address and DomR doesn't have a
            // VIF
            // for the specified vlan ID, we need to add a VIF
            // If we are disassociating the last IP address in the VLAN, we
            // need
            // to remove a VIF
            boolean addVif = false;
            if (ip.isAdd() && correctVif == null) {
                addVif = true;
            }
            if (addVif) {
                // Add a new VIF to DomR
                final String vifDeviceNum = getLowestAvailableVIFDeviceNum(conn, router);
                if (vifDeviceNum == null) {
                    throw new InternalErrorException("There were no more available slots for a new VIF on router: " + router.getNameLabel(conn));
                }
                nic.setDeviceId(Integer.parseInt(vifDeviceNum));
                correctVif = createVif(conn, routerName, router, null, nic);
                correctVif.plug(conn);
                // Add iptables rule for network usage
                networkUsage(conn, routerIp, "addVif", "eth" + correctVif.getDevice(conn));
            }
            if (ip.isAdd() && correctVif == null) {
                throw new InternalErrorException("Failed to find DomR VIF to associate/disassociate IP with.");
            }
            if (correctVif != null) {
                ip.setNicDevId(Integer.valueOf(correctVif.getDevice(conn)));
                ip.setNewNic(addVif);
            }
        }
    } catch (final InternalErrorException e) {
        s_logger.error("Ip Assoc failure on applying one ip due to exception:  ", e);
        return new ExecutionResult(false, e.getMessage());
    } catch (final Exception e) {
        return new ExecutionResult(false, e.getMessage());
    }
    return new ExecutionResult(true, null);
}
Also used : IpAddressTO(com.cloud.agent.api.to.IpAddressTO) Connection(com.xensource.xenapi.Connection) URLConnection(java.net.URLConnection) ExecutionResult(com.cloud.utils.ExecutionResult) InternalErrorException(com.cloud.exception.InternalErrorException) URI(java.net.URI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM) Network(com.xensource.xenapi.Network) NicTO(com.cloud.agent.api.to.NicTO)

Example 22 with VIF

use of com.xensource.xenapi.VIF in project cloudstack by apache.

the class CitrixResourceBase method prepareNetworkElementCommand.

protected ExecutionResult prepareNetworkElementCommand(final SetNetworkACLCommand cmd) {
    final Connection conn = getConnection();
    final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    try {
        final VM router = getVM(conn, routerName);
        final NicTO nic = cmd.getNic();
        if (nic != null) {
            final VIF vif = getVifByMac(conn, router, nic.getMac());
            if (vif == null) {
                final String msg = "Prepare SetNetworkACL failed due to VIF is null for : " + nic.getMac() + " with routername: " + routerName;
                s_logger.error(msg);
                return new ExecutionResult(false, msg);
            }
            nic.setDeviceId(Integer.parseInt(vif.getDevice(conn)));
        } else {
            final String msg = "Prepare SetNetworkACL failed due to nic is null for : " + routerName;
            s_logger.error(msg);
            return new ExecutionResult(false, msg);
        }
    } catch (final Exception e) {
        final String msg = "Prepare SetNetworkACL failed due to " + e.toString();
        s_logger.error(msg, e);
        return new ExecutionResult(false, msg);
    }
    return new ExecutionResult(true, null);
}
Also used : VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM) Connection(com.xensource.xenapi.Connection) URLConnection(java.net.URLConnection) ExecutionResult(com.cloud.utils.ExecutionResult) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) NicTO(com.cloud.agent.api.to.NicTO)

Example 23 with VIF

use of com.xensource.xenapi.VIF in project cloudstack by apache.

the class CitrixResourceBase method createVif.

public VIF createVif(final Connection conn, final String vmName, final VM vm, final VirtualMachineTO vmSpec, final NicTO nic) throws XmlRpcException, XenAPIException {
    assert nic.getUuid() != null : "Nic should have a uuid value";
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Creating VIF for " + vmName + " on nic " + nic);
    }
    VIF.Record vifr = new VIF.Record();
    vifr.VM = vm;
    vifr.device = Integer.toString(nic.getDeviceId());
    vifr.MAC = nic.getMac();
    // Nicira needs these IDs to find the NIC
    vifr.otherConfig = new HashMap<String, String>();
    vifr.otherConfig.put("nicira-iface-id", nic.getUuid());
    vifr.otherConfig.put("nicira-vm-id", vm.getUuid(conn));
    // Provide XAPI with the cloudstack vm and nic uids.
    vifr.otherConfig.put("cloudstack-nic-id", nic.getUuid());
    if (vmSpec != null) {
        vifr.otherConfig.put("cloudstack-vm-id", vmSpec.getUuid());
    }
    // OVS plugin looks at network UUID in the vif 'otherconfig' details to
    // group VIF's & tunnel ports as part of tier
    // when bridge is setup for distributed routing
    vifr.otherConfig.put("cloudstack-network-id", nic.getNetworkUuid());
    vifr.network = getNetwork(conn, nic);
    if (nic.getNetworkRateMbps() != null && nic.getNetworkRateMbps().intValue() != -1) {
        vifr.qosAlgorithmType = "ratelimit";
        vifr.qosAlgorithmParams = new HashMap<String, String>();
        // convert mbs to kilobyte per second
        vifr.qosAlgorithmParams.put("kbps", Integer.toString(nic.getNetworkRateMbps() * 128));
    }
    vifr.lockingMode = Types.VifLockingMode.NETWORK_DEFAULT;
    final VIF vif = VIF.create(conn, vifr);
    if (s_logger.isDebugEnabled()) {
        vifr = vif.getRecord(conn);
        if (vifr != null) {
            s_logger.debug("Created a vif " + vifr.uuid + " on " + nic.getDeviceId());
        }
    }
    return vif;
}
Also used : VIF(com.xensource.xenapi.VIF)

Example 24 with VIF

use of com.xensource.xenapi.VIF in project cloudstack by apache.

the class CitrixResourceBase method getLowestAvailableVIFDeviceNum.

public String getLowestAvailableVIFDeviceNum(final Connection conn, final VM vm) {
    String vmName = "";
    try {
        vmName = vm.getNameLabel(conn);
        final List<Integer> usedDeviceNums = new ArrayList<Integer>();
        final Set<VIF> vifs = vm.getVIFs(conn);
        final Iterator<VIF> vifIter = vifs.iterator();
        while (vifIter.hasNext()) {
            final VIF vif = vifIter.next();
            try {
                final String deviceId = vif.getDevice(conn);
                if (vm.getIsControlDomain(conn) || vif.getCurrentlyAttached(conn)) {
                    usedDeviceNums.add(Integer.valueOf(deviceId));
                } else {
                    s_logger.debug("Found unplugged VIF " + deviceId + " in VM " + vmName + " destroy it");
                    vif.destroy(conn);
                }
            } catch (final NumberFormatException e) {
                final String msg = "Obtained an invalid value for an allocated VIF device number for VM: " + vmName;
                s_logger.debug(msg, e);
                throw new CloudRuntimeException(msg);
            }
        }
        for (Integer i = 0; i < _maxNics; i++) {
            if (!usedDeviceNums.contains(i)) {
                s_logger.debug("Lowest available Vif device number: " + i + " for VM: " + vmName);
                return i.toString();
            }
        }
    } catch (final XmlRpcException e) {
        final String msg = "Caught XmlRpcException: " + e.getMessage();
        s_logger.warn(msg, e);
    } catch (final XenAPIException e) {
        final String msg = "Caught XenAPIException: " + e.toString();
        s_logger.warn(msg, e);
    }
    throw new CloudRuntimeException("Could not find available VIF slot in VM with name: " + vmName);
}
Also used : VIF(com.xensource.xenapi.VIF) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ArrayList(java.util.ArrayList) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 25 with VIF

use of com.xensource.xenapi.VIF in project cloudstack by apache.

the class XenServer610MigrateWithStorageSendCommandWrapper method execute.

@Override
public Answer execute(final MigrateWithStorageSendCommand command, final XenServer610Resource xenServer610Resource) {
    final Connection connection = xenServer610Resource.getConnection();
    final VirtualMachineTO vmSpec = command.getVirtualMachine();
    final List<Pair<VolumeTO, Object>> volumeToSr = command.getVolumeToSr();
    final List<Pair<NicTO, Object>> nicToNetwork = command.getNicToNetwork();
    final Map<String, String> token = command.getToken();
    final String vmName = vmSpec.getName();
    Task task = null;
    try {
        // In a cluster management server setup, the migrate with storage receive and send
        // commands and answers may have to be forwarded to another management server. This
        // happens when the host/resource on which the command has to be executed is owned
        // by the second management server. The serialization/deserialization of the command
        // and answers fails as the xapi SR and Network class type isn't understand by the
        // agent attache. Seriliaze the SR and Network objects here to a string and pass in
        // the answer object. It'll be deserialzed and object created in migrate with
        // storage send command execution.
        final Map<String, String> other = new HashMap<String, String>();
        other.put("live", "true");
        // Create the vdi map which tells what volumes of the vm need to go
        // on which sr on the destination.
        final Map<VDI, SR> vdiMap = new HashMap<VDI, SR>();
        for (final Pair<VolumeTO, Object> entry : volumeToSr) {
            if (entry.second() instanceof SR) {
                final SR sr = (SR) entry.second();
                final VDI vdi = xenServer610Resource.getVDIbyUuid(connection, entry.first().getPath());
                vdiMap.put(vdi, sr);
            } else {
                throw new CloudRuntimeException("The object " + entry.second() + " passed is not of type SR.");
            }
        }
        final Set<VM> vms = VM.getByNameLabel(connection, vmSpec.getName());
        VM vmToMigrate = null;
        if (vms != null) {
            vmToMigrate = vms.iterator().next();
        }
        // Create the vif map.
        final Map<VIF, Network> vifMap = new HashMap<VIF, Network>();
        for (final Pair<NicTO, Object> entry : nicToNetwork) {
            if (entry.second() instanceof Network) {
                final Network network = (Network) entry.second();
                final VIF vif = xenServer610Resource.getVifByMac(connection, vmToMigrate, entry.first().getMac());
                vifMap.put(vif, network);
            } else {
                throw new CloudRuntimeException("The object " + entry.second() + " passed is not of type Network.");
            }
        }
        // Check migration with storage is possible.
        task = vmToMigrate.assertCanMigrateAsync(connection, token, true, vdiMap, vifMap, other);
        try {
            // poll every 1 seconds.
            final long timeout = xenServer610Resource.getMigrateWait() * 1000L;
            xenServer610Resource.waitForTask(connection, task, 1000, timeout);
            xenServer610Resource.checkForSuccess(connection, task);
        } catch (final Types.HandleInvalid e) {
            s_logger.error("Error while checking if vm " + vmName + " can be migrated.", e);
            throw new CloudRuntimeException("Error while checking if vm " + vmName + " can be migrated.", e);
        }
        // Migrate now.
        task = vmToMigrate.migrateSendAsync(connection, token, true, vdiMap, vifMap, other);
        try {
            // poll every 1 seconds.
            final long timeout = xenServer610Resource.getMigrateWait() * 1000L;
            xenServer610Resource.waitForTask(connection, task, 1000, timeout);
            xenServer610Resource.checkForSuccess(connection, task);
        } catch (final Types.HandleInvalid e) {
            s_logger.error("Error while migrating vm " + vmName, e);
            throw new CloudRuntimeException("Error while migrating vm " + vmName, e);
        }
        final Set<VolumeTO> volumeToSet = null;
        return new MigrateWithStorageSendAnswer(command, volumeToSet);
    } catch (final CloudRuntimeException e) {
        s_logger.error("Migration of vm " + vmName + " with storage failed due to " + e.toString(), e);
        return new MigrateWithStorageSendAnswer(command, e);
    } catch (final Exception e) {
        s_logger.error("Migration of vm " + vmName + " with storage failed due to " + e.toString(), e);
        return new MigrateWithStorageSendAnswer(command, e);
    } finally {
        if (task != null) {
            try {
                task.destroy(connection);
            } catch (final Exception e) {
                s_logger.debug("Unable to destroy task " + task.toString() + " on host " + xenServer610Resource.getHost().getUuid() + " due to " + e.toString());
            }
        }
    }
}
Also used : Types(com.xensource.xenapi.Types) Task(com.xensource.xenapi.Task) MigrateWithStorageSendAnswer(com.cloud.agent.api.MigrateWithStorageSendAnswer) HashMap(java.util.HashMap) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) VolumeTO(com.cloud.agent.api.to.VolumeTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.xensource.xenapi.Network) VDI(com.xensource.xenapi.VDI) Pair(com.cloud.utils.Pair) SR(com.xensource.xenapi.SR) NicTO(com.cloud.agent.api.to.NicTO) Connection(com.xensource.xenapi.Connection) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM)

Aggregations

VIF (com.xensource.xenapi.VIF)31 VM (com.xensource.xenapi.VM)22 Connection (com.xensource.xenapi.Connection)19 XenAPIException (com.xensource.xenapi.Types.XenAPIException)18 Network (com.xensource.xenapi.Network)17 XmlRpcException (org.apache.xmlrpc.XmlRpcException)17 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)12 NicTO (com.cloud.agent.api.to.NicTO)10 IOException (java.io.IOException)10 MalformedURLException (java.net.MalformedURLException)10 URISyntaxException (java.net.URISyntaxException)10 HashMap (java.util.HashMap)10 TimeoutException (java.util.concurrent.TimeoutException)10 ConfigurationException (javax.naming.ConfigurationException)10 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)10 SAXException (org.xml.sax.SAXException)10 InternalErrorException (com.cloud.exception.InternalErrorException)8 SR (com.xensource.xenapi.SR)8 ArrayList (java.util.ArrayList)8 URLConnection (java.net.URLConnection)7