Search in sources :

Example 6 with OvsTunnelNetworkVO

use of com.cloud.network.ovs.dao.OvsTunnelNetworkVO in project cloudstack by apache.

the class OvsTunnelManagerImpl method checkAndCreateVpcTunnelNetworks.

@DB
protected void checkAndCreateVpcTunnelNetworks(Host host, long vpcId) {
    long hostId = host.getId();
    String bridgeName = generateBridgeNameForVpc(vpcId);
    List<Long> vmIds = _ovsNetworkToplogyGuru.getActiveVmsInVpcOnHost(vpcId, hostId);
    if (vmIds == null || vmIds.isEmpty()) {
        // since this is the first VM from the VPC being launched on the host, first setup the bridge
        try {
            Commands cmds = new Commands(new OvsSetupBridgeCommand(bridgeName, hostId, null));
            s_logger.debug("Ask host " + hostId + " to create bridge for vpc " + vpcId + " and configure the " + " bridge for distributed routing.");
            Answer[] answers = _agentMgr.send(hostId, cmds);
            handleSetupBridgeAnswer(answers);
        } catch (OperationTimedoutException | AgentUnavailableException e) {
            s_logger.warn("Ovs Tunnel network created tunnel failed", e);
        }
        // now that bridge is setup, populate network acl's before the VM gets created
        OvsVpcRoutingPolicyConfigCommand cmd = prepareVpcRoutingPolicyUpdate(vpcId);
        cmd.setSequenceNumber(getNextRoutingPolicyUpdateSequenceNumber(vpcId));
        if (!sendVpcRoutingPolicyChangeUpdate(cmd, hostId, bridgeName)) {
            s_logger.debug("Failed to send VPC routing policy change update to host : " + hostId + ". But moving on with sending the updates to the rest of the hosts.");
        }
    }
    List<? extends Network> vpcNetworks = _vpcMgr.getVpcNetworks(vpcId);
    List<Long> vpcSpannedHostIds = _ovsNetworkToplogyGuru.getVpcSpannedHosts(vpcId);
    for (Network vpcNetwork : vpcNetworks) {
        if (vpcNetwork.getState() != Network.State.Implemented && vpcNetwork.getState() != Network.State.Implementing && vpcNetwork.getState() != Network.State.Setup)
            continue;
        int key = getGreKey(vpcNetwork);
        List<Long> toHostIds = new ArrayList<Long>();
        List<Long> fromHostIds = new ArrayList<Long>();
        OvsTunnelNetworkVO tunnelRecord = null;
        for (Long rh : vpcSpannedHostIds) {
            if (rh == hostId) {
                continue;
            }
            tunnelRecord = _tunnelNetworkDao.getByFromToNetwork(hostId, rh.longValue(), vpcNetwork.getId());
            // Try and create the tunnel if does not exit or previous attempt failed
            if (tunnelRecord == null || tunnelRecord.getState().equals(OvsTunnel.State.Failed.name())) {
                s_logger.debug("Attempting to create tunnel from:" + hostId + " to:" + rh.longValue());
                if (tunnelRecord == null) {
                    createTunnelRecord(hostId, rh.longValue(), vpcNetwork.getId(), key);
                }
                if (!toHostIds.contains(rh)) {
                    toHostIds.add(rh);
                }
            }
            tunnelRecord = _tunnelNetworkDao.getByFromToNetwork(rh.longValue(), hostId, vpcNetwork.getId());
            // Try and create the tunnel if does not exit or previous attempt failed
            if (tunnelRecord == null || tunnelRecord.getState().equals(OvsTunnel.State.Failed.name())) {
                s_logger.debug("Attempting to create tunnel from:" + rh.longValue() + " to:" + hostId);
                if (tunnelRecord == null) {
                    createTunnelRecord(rh.longValue(), hostId, vpcNetwork.getId(), key);
                }
                if (!fromHostIds.contains(rh)) {
                    fromHostIds.add(rh);
                }
            }
        }
        try {
            String myIp = getGreEndpointIP(host, vpcNetwork);
            if (myIp == null)
                throw new GreTunnelException("Unable to retrieve the source " + "endpoint for the GRE tunnel." + "Failure is on host:" + host.getId());
            boolean noHost = true;
            for (Long i : toHostIds) {
                HostVO rHost = _hostDao.findById(i);
                String otherIp = getGreEndpointIP(rHost, vpcNetwork);
                if (otherIp == null)
                    throw new GreTunnelException("Unable to retrieve the remote endpoint for the GRE tunnel." + "Failure is on host:" + rHost.getId());
                Commands cmds = new Commands(new OvsCreateTunnelCommand(otherIp, key, Long.valueOf(hostId), i, vpcNetwork.getId(), myIp, bridgeName, vpcNetwork.getUuid()));
                s_logger.debug("Attempting to create tunnel from:" + hostId + " to:" + i + " for the network " + vpcNetwork.getId());
                s_logger.debug("Ask host " + hostId + " to create gre tunnel to " + i);
                Answer[] answers = _agentMgr.send(hostId, cmds);
                handleCreateTunnelAnswer(answers);
            }
            for (Long i : fromHostIds) {
                HostVO rHost = _hostDao.findById(i);
                String otherIp = getGreEndpointIP(rHost, vpcNetwork);
                Commands cmds = new Commands(new OvsCreateTunnelCommand(myIp, key, i, Long.valueOf(hostId), vpcNetwork.getId(), otherIp, bridgeName, vpcNetwork.getUuid()));
                s_logger.debug("Ask host " + i + " to create gre tunnel to " + hostId);
                Answer[] answers = _agentMgr.send(i, cmds);
                handleCreateTunnelAnswer(answers);
            }
        } catch (GreTunnelException | OperationTimedoutException | AgentUnavailableException e) {
            // I really thing we should do a better handling of these exceptions
            s_logger.warn("Ovs Tunnel network created tunnel failed", e);
        }
    }
}
Also used : OperationTimedoutException(com.cloud.exception.OperationTimedoutException) OvsTunnelNetworkVO(com.cloud.network.ovs.dao.OvsTunnelNetworkVO) OvsCreateTunnelCommand(com.cloud.agent.api.OvsCreateTunnelCommand) ArrayList(java.util.ArrayList) OvsVpcRoutingPolicyConfigCommand(com.cloud.agent.api.OvsVpcRoutingPolicyConfigCommand) HostVO(com.cloud.host.HostVO) OvsFetchInterfaceAnswer(com.cloud.agent.api.OvsFetchInterfaceAnswer) Answer(com.cloud.agent.api.Answer) OvsCreateTunnelAnswer(com.cloud.agent.api.OvsCreateTunnelAnswer) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) Network(com.cloud.network.Network) Commands(com.cloud.agent.manager.Commands) OvsSetupBridgeCommand(com.cloud.agent.api.OvsSetupBridgeCommand) DB(com.cloud.utils.db.DB)

Example 7 with OvsTunnelNetworkVO

use of com.cloud.network.ovs.dao.OvsTunnelNetworkVO in project cloudstack by apache.

the class OvsTunnelManagerImpl method handleCreateTunnelAnswer.

private void handleCreateTunnelAnswer(Answer[] answers) {
    OvsCreateTunnelAnswer r = (OvsCreateTunnelAnswer) answers[0];
    String s = String.format("(hostIP:%1$s, remoteIP:%2$s, bridge:%3$s," + "greKey:%4$s, portName:%5$s)", r.getFromIp(), r.getToIp(), r.getBridge(), r.getKey(), r.getInPortName());
    Long from = r.getFrom();
    Long to = r.getTo();
    long networkId = r.getNetworkId();
    OvsTunnelNetworkVO tunnel = _tunnelNetworkDao.getByFromToNetwork(from, to, networkId);
    if (tunnel == null) {
        throw new CloudRuntimeException(String.format("Unable find tunnelNetwork record" + "(from=%1$s,to=%2$s, account=%3$s", from, to, networkId));
    }
    if (!r.getResult()) {
        tunnel.setState(OvsTunnel.State.Failed.name());
        s_logger.warn("Create GRE tunnel from " + from + " to " + to + " failed due to " + r.getDetails() + s);
    } else {
        tunnel.setState(OvsTunnel.State.Established.name());
        tunnel.setPortName(r.getInPortName());
        s_logger.info("Create GRE tunnel from " + from + " to " + to + " succeeded." + r.getDetails() + s);
    }
    _tunnelNetworkDao.update(tunnel.getId(), tunnel);
}
Also used : OvsTunnelNetworkVO(com.cloud.network.ovs.dao.OvsTunnelNetworkVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) OvsCreateTunnelAnswer(com.cloud.agent.api.OvsCreateTunnelAnswer)

Aggregations

OvsTunnelNetworkVO (com.cloud.network.ovs.dao.OvsTunnelNetworkVO)7 DB (com.cloud.utils.db.DB)5 OvsCreateTunnelAnswer (com.cloud.agent.api.OvsCreateTunnelAnswer)4 Answer (com.cloud.agent.api.Answer)3 OvsCreateTunnelCommand (com.cloud.agent.api.OvsCreateTunnelCommand)3 OvsFetchInterfaceAnswer (com.cloud.agent.api.OvsFetchInterfaceAnswer)3 OvsSetupBridgeCommand (com.cloud.agent.api.OvsSetupBridgeCommand)3 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)3 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)3 OvsVpcRoutingPolicyConfigCommand (com.cloud.agent.api.OvsVpcRoutingPolicyConfigCommand)2 Commands (com.cloud.agent.manager.Commands)2 HostVO (com.cloud.host.HostVO)2 Network (com.cloud.network.Network)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 ArrayList (java.util.ArrayList)2 EntityExistsException (javax.persistence.EntityExistsException)2 Command (com.cloud.agent.api.Command)1 OvsDestroyBridgeCommand (com.cloud.agent.api.OvsDestroyBridgeCommand)1 OvsDestroyTunnelCommand (com.cloud.agent.api.OvsDestroyTunnelCommand)1 OvsFetchInterfaceCommand (com.cloud.agent.api.OvsFetchInterfaceCommand)1