Search in sources :

Example 1 with Network

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

the class CitrixResourceBase method setupLinkLocalNetwork.

public void setupLinkLocalNetwork(final Connection conn) {
    try {
        final Network.Record rec = new Network.Record();
        final Set<Network> networks = Network.getByNameLabel(conn, _linkLocalPrivateNetworkName);
        Network linkLocal = null;
        if (networks.size() == 0) {
            rec.nameDescription = "link local network used by system vms";
            rec.nameLabel = _linkLocalPrivateNetworkName;
            final Map<String, String> configs = new HashMap<String, String>();
            configs.put("ip_begin", NetUtils.getLinkLocalGateway());
            configs.put("ip_end", NetUtils.getLinkLocalIpEnd());
            configs.put("netmask", NetUtils.getLinkLocalNetMask());
            configs.put("vswitch-disable-in-band", "true");
            rec.otherConfig = configs;
            linkLocal = Network.create(conn, rec);
        } else {
            linkLocal = networks.iterator().next();
            if (!linkLocal.getOtherConfig(conn).containsKey("vswitch-disable-in-band")) {
                linkLocal.addToOtherConfig(conn, "vswitch-disable-in-band", "true");
            }
        }
        /* Make sure there is a physical bridge on this network */
        VIF dom0vif = null;
        final Pair<VM, VM.Record> vm = getControlDomain(conn);
        final VM dom0 = vm.first();
        final Set<VIF> vifs = dom0.getVIFs(conn);
        if (vifs.size() != 0) {
            for (final VIF vif : vifs) {
                final Map<String, String> otherConfig = vif.getOtherConfig(conn);
                if (otherConfig != null) {
                    final String nameLabel = otherConfig.get("nameLabel");
                    if (nameLabel != null && nameLabel.equalsIgnoreCase("link_local_network_vif")) {
                        dom0vif = vif;
                    }
                }
            }
        }
        /* create temp VIF0 */
        if (dom0vif == null) {
            s_logger.debug("Can't find a vif on dom0 for link local, creating a new one");
            final VIF.Record vifr = new VIF.Record();
            vifr.VM = dom0;
            vifr.device = getLowestAvailableVIFDeviceNum(conn, dom0);
            if (vifr.device == null) {
                s_logger.debug("Failed to create link local network, no vif available");
                return;
            }
            final Map<String, String> config = new HashMap<String, String>();
            config.put("nameLabel", "link_local_network_vif");
            vifr.otherConfig = config;
            vifr.MAC = "FE:FF:FF:FF:FF:FF";
            vifr.network = linkLocal;
            vifr.lockingMode = Types.VifLockingMode.NETWORK_DEFAULT;
            dom0vif = VIF.create(conn, vifr);
            plugDom0Vif(conn, dom0vif);
        } else {
            s_logger.debug("already have a vif on dom0 for link local network");
            if (!dom0vif.getCurrentlyAttached(conn)) {
                plugDom0Vif(conn, dom0vif);
            }
        }
        final String brName = linkLocal.getBridge(conn);
        callHostPlugin(conn, "vmops", "setLinkLocalIP", "brName", brName);
        _host.setLinkLocalNetwork(linkLocal.getUuid(conn));
    } catch (final XenAPIException e) {
        s_logger.warn("Unable to create local link network", e);
        throw new CloudRuntimeException("Unable to create local link network due to " + e.toString(), e);
    } catch (final XmlRpcException e) {
        s_logger.warn("Unable to create local link network", e);
        throw new CloudRuntimeException("Unable to create local link network due to " + e.toString(), e);
    }
}
Also used : HashMap(java.util.HashMap) XenAPIException(com.xensource.xenapi.Types.XenAPIException) VIF(com.xensource.xenapi.VIF) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.xensource.xenapi.Network) VM(com.xensource.xenapi.VM) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 2 with Network

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

the class CitrixResourceBase method handleVmStartFailure.

public String handleVmStartFailure(final Connection conn, final String vmName, final VM vm, final String message, final Throwable th) {
    final String msg = "Unable to start " + vmName + " due to " + message;
    s_logger.warn(msg, th);
    if (vm == null) {
        return msg;
    }
    try {
        final VM.Record vmr = vm.getRecord(conn);
        final List<Network> networks = new ArrayList<Network>();
        for (final VIF vif : vmr.VIFs) {
            try {
                final VIF.Record rec = vif.getRecord(conn);
                if (rec != null) {
                    networks.add(rec.network);
                } else {
                    s_logger.warn("Unable to cleanup VIF: " + vif.toWireString() + " As vif record is null");
                }
            } catch (final Exception e) {
                s_logger.warn("Unable to cleanup VIF", e);
            }
        }
        if (vmr.powerState == VmPowerState.RUNNING) {
            try {
                vm.hardShutdown(conn);
            } catch (final Exception e) {
                s_logger.warn("VM hardshutdown failed due to ", e);
            }
        }
        if (vm.getPowerState(conn) == VmPowerState.HALTED) {
            try {
                vm.destroy(conn);
            } catch (final Exception e) {
                s_logger.warn("VM destroy failed due to ", e);
            }
        }
        for (final VBD vbd : vmr.VBDs) {
            try {
                vbd.unplug(conn);
                vbd.destroy(conn);
            } catch (final Exception e) {
                s_logger.warn("Unable to clean up VBD due to ", e);
            }
        }
        for (final VIF vif : vmr.VIFs) {
            try {
                vif.unplug(conn);
                vif.destroy(conn);
            } catch (final Exception e) {
                s_logger.warn("Unable to cleanup VIF", e);
            }
        }
        for (final Network network : networks) {
            if (network.getNameLabel(conn).startsWith("VLAN")) {
                disableVlanNetwork(conn, network, true);
            }
        }
    } catch (final Exception e) {
        s_logger.warn("VM getRecord failed due to ", e);
    }
    return msg;
}
Also used : VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM) Network(com.xensource.xenapi.Network) ArrayList(java.util.ArrayList) VBD(com.xensource.xenapi.VBD) 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)

Example 3 with Network

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

the class CitrixResourceBase method getCorrectVif.

protected VIF getCorrectVif(final Connection conn, final VM router, final IpAddressTO ip) throws XmlRpcException, XenAPIException {
    final NicTO nic = new NicTO();
    nic.setType(ip.getTrafficType());
    nic.setName(ip.getNetworkName());
    if (ip.getBroadcastUri() == null) {
        nic.setBroadcastType(BroadcastDomainType.Native);
    } else {
        final URI uri = BroadcastDomainType.fromString(ip.getBroadcastUri());
        nic.setBroadcastType(BroadcastDomainType.getSchemeValue(uri));
        nic.setBroadcastUri(uri);
    }
    final Network network = getNetwork(conn, nic);
    // Determine the correct VIF on DomR to associate/disassociate the
    // IP address with
    final Set<VIF> routerVIFs = router.getVIFs(conn);
    for (final VIF vif : routerVIFs) {
        final Network vifNetwork = vif.getNetwork(conn);
        if (vifNetwork.getUuid(conn).equals(network.getUuid(conn))) {
            return vif;
        }
    }
    return null;
}
Also used : VIF(com.xensource.xenapi.VIF) Network(com.xensource.xenapi.Network) URI(java.net.URI) NicTO(com.cloud.agent.api.to.NicTO)

Example 4 with Network

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

the class CitrixResourceBase method enableVlanNetwork.

/**
 * enableVlanNetwork creates a Network object, Vlan object, and thereby a
 * tagged PIF object in Xapi.
 *
 * In XenServer, VLAN is added by - Create a network, which is unique
 * cluster wide. - Find the PIF that you want to create the VLAN on. -
 * Create a VLAN using the network and the PIF. As a result of this
 * operation, a tagged PIF object is also created.
 *
 * Here is a list of problems with clustered Xapi implementation that we are
 * trying to circumvent. - There can be multiple Networks with the same
 * name-label so searching using name-label is not unique. - There are no
 * other ways to search for Networks other than listing all of them which is
 * not efficient in our implementation because we can have over 4000 VLAN
 * networks. - In a clustered situation, it's possible for both hosts to
 * detect that the Network is missing and both creates it. This causes a lot
 * of problems as one host may be using one Network and another may be using
 * a different network for their VMs. This causes problems in migration
 * because the VMs are logically attached to different networks in Xapi's
 * database but in reality, they are attached to the same network.
 *
 * To work around these problems, we do the following.
 *
 * - When creating the VLAN network, we name it as VLAN-UUID of the Network
 * it is created on-VLAN Tag. Because VLAN tags is unique with one
 * particular network, this is a unique name-label to quickly retrieve the
 * the VLAN network with when we need it again. - When we create the VLAN
 * network, we add a timestamp and a random number as a tag into the
 * network. Then instead of creating VLAN on that network, we actually
 * retrieve the Network again and this time uses the VLAN network with
 * lowest timestamp or lowest random number as the VLAN network. This allows
 * VLAN creation to happen on multiple hosts concurrently but even if two
 * VLAN networks were created with the same name, only one of them is used.
 *
 * One cavaet about this approach is that it relies on the timestamp to be
 * relatively accurate among different hosts.
 *
 * @param conn
 *            Xapi Connection
 * @param tag
 *            VLAN tag
 * @param network
 *            network on this host to create the VLAN on.
 * @return VLAN Network created.
 * @throws XenAPIException
 * @throws XmlRpcException
 */
protected Network enableVlanNetwork(final Connection conn, final long tag, final XsLocalNetwork network) throws XenAPIException, XmlRpcException {
    Network vlanNetwork = null;
    final String oldName = "VLAN" + Long.toString(tag);
    final String newName = "VLAN-" + network.getNetworkRecord(conn).uuid + "-" + tag;
    XsLocalNetwork vlanNic = getNetworkByName(conn, newName);
    if (vlanNic == null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Couldn't find vlan network with the new name so trying old name: " + oldName);
        }
        vlanNic = getNetworkByName(conn, oldName);
        if (vlanNic != null) {
            s_logger.info("Renaming VLAN with old name " + oldName + " to " + newName);
            vlanNic.getNetwork().setNameLabel(conn, newName);
        }
    }
    if (vlanNic == null) {
        // Can't find it, then create it.
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Creating VLAN network for " + tag + " on host " + _host.getIp());
        }
        final Network.Record nwr = new Network.Record();
        nwr.nameLabel = newName;
        nwr.tags = new HashSet<String>();
        nwr.tags.add(generateTimeStamp());
        vlanNetwork = Network.create(conn, nwr);
        vlanNic = getNetworkByName(conn, newName);
        if (vlanNic == null) {
            // capture happened.
            throw new CloudRuntimeException("Could not find/create vlan network with name: " + newName);
        }
    }
    final PIF nPif = network.getPif(conn);
    final PIF.Record nPifr = network.getPifRecord(conn);
    vlanNetwork = vlanNic.getNetwork();
    if (vlanNic.getPif(conn) != null) {
        return vlanNetwork;
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Creating VLAN " + tag + " on host " + _host.getIp() + " on device " + nPifr.device);
    }
    final VLAN vlan = VLAN.create(conn, nPif, tag, vlanNetwork);
    if (vlan != null) {
        final VLAN.Record vlanr = vlan.getRecord(conn);
        if (vlanr != null) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("VLAN is created for " + tag + ".  The uuid is " + vlanr.uuid);
            }
        }
    }
    return vlanNetwork;
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.xensource.xenapi.Network) VLAN(com.xensource.xenapi.VLAN) PIF(com.xensource.xenapi.PIF)

Example 5 with Network

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

the class CitrixResourceBase method getManagementNetwork.

protected XsLocalNetwork getManagementNetwork(final Connection conn) throws XmlRpcException, XenAPIException {
    PIF mgmtPif = null;
    PIF.Record mgmtPifRec = null;
    final Host host = Host.getByUuid(conn, _host.getUuid());
    final Set<PIF> hostPifs = host.getPIFs(conn);
    for (final PIF pif : hostPifs) {
        final PIF.Record rec = pif.getRecord(conn);
        if (rec.management) {
            if (rec.VLAN != null && rec.VLAN != -1) {
                final String msg = new StringBuilder("Unsupported configuration.  Management network is on a VLAN.  host=").append(_host.getUuid()).append("; pif=").append(rec.uuid).append("; vlan=").append(rec.VLAN).toString();
                s_logger.warn(msg);
                throw new CloudRuntimeException(msg);
            }
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Management network is on pif=" + rec.uuid);
            }
            mgmtPif = pif;
            mgmtPifRec = rec;
            break;
        }
    }
    if (mgmtPif == null) {
        final String msg = "Unable to find management network for " + _host.getUuid();
        s_logger.warn(msg);
        throw new CloudRuntimeException(msg);
    }
    final Bond bond = mgmtPifRec.bondSlaveOf;
    if (!isRefNull(bond)) {
        final String msg = "Management interface is on slave(" + mgmtPifRec.uuid + ") of bond(" + bond.getUuid(conn) + ") on host(" + _host.getUuid() + "), please move management interface to bond!";
        s_logger.warn(msg);
        throw new CloudRuntimeException(msg);
    }
    final Network nk = mgmtPifRec.network;
    final Network.Record nkRec = nk.getRecord(conn);
    return new XsLocalNetwork(this, nk, nkRec, mgmtPif, mgmtPifRec);
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.xensource.xenapi.Network) Host(com.xensource.xenapi.Host) PIF(com.xensource.xenapi.PIF) Bond(com.xensource.xenapi.Bond)

Aggregations

Network (com.xensource.xenapi.Network)69 Connection (com.xensource.xenapi.Connection)50 XenAPIException (com.xensource.xenapi.Types.XenAPIException)45 XmlRpcException (org.apache.xmlrpc.XmlRpcException)41 Answer (com.cloud.agent.api.Answer)31 XsLocalNetwork (com.cloud.hypervisor.xenserver.resource.XsLocalNetwork)30 Test (org.junit.Test)26 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)23 RebootAnswer (com.cloud.agent.api.RebootAnswer)20 CreateAnswer (com.cloud.agent.api.storage.CreateAnswer)20 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 NicTO (com.cloud.agent.api.to.NicTO)18 VIF (com.xensource.xenapi.VIF)17 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)16 HashMap (java.util.HashMap)15 SR (com.xensource.xenapi.SR)14 BadServerResponse (com.xensource.xenapi.Types.BadServerResponse)14 VM (com.xensource.xenapi.VM)14 XsHost (com.cloud.hypervisor.xenserver.resource.XsHost)13 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)12