Search in sources :

Example 6 with InterfaceDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef in project cloudstack by apache.

the class LibvirtComputingResource method defaultNetworkRules.

public boolean defaultNetworkRules(final Connect conn, final String vmName, final NicTO nic, final Long vmId, final String secIpStr) {
    if (!_canBridgeFirewall) {
        return false;
    }
    final List<InterfaceDef> intfs = getInterfaces(conn, vmName);
    if (intfs.size() == 0 || intfs.size() < nic.getDeviceId()) {
        return false;
    }
    final InterfaceDef intf = intfs.get(nic.getDeviceId());
    final String brname = intf.getBrName();
    final String vif = intf.getDevName();
    final Script cmd = new Script(_securityGroupPath, _timeout, s_logger);
    cmd.add("default_network_rules");
    cmd.add("--vmname", vmName);
    cmd.add("--vmid", vmId.toString());
    if (nic.getIp() != null) {
        cmd.add("--vmip", nic.getIp());
    }
    if (nic.getIp6Address() != null) {
        cmd.add("--vmip6", nic.getIp6Address());
    }
    cmd.add("--vmmac", nic.getMac());
    cmd.add("--vif", vif);
    cmd.add("--brname", brname);
    cmd.add("--nicsecips", secIpStr);
    final String result = cmd.execute();
    if (result != null) {
        return false;
    }
    return true;
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) Script(com.cloud.utils.script.Script)

Example 7 with InterfaceDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef in project cloudstack by apache.

the class LibvirtComputingResource method prepareNetworkElementCommand.

private ExecutionResult prepareNetworkElementCommand(final SetupGuestNetworkCommand cmd) {
    Connect conn;
    final NicTO nic = cmd.getNic();
    final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    try {
        conn = LibvirtConnection.getConnectionByVmName(routerName);
        final List<InterfaceDef> pluggedNics = getInterfaces(conn, routerName);
        InterfaceDef routerNic = null;
        for (final InterfaceDef pluggedNic : pluggedNics) {
            if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) {
                routerNic = pluggedNic;
                break;
            }
        }
        if (routerNic == null) {
            return new ExecutionResult(false, "Can not find nic with mac " + nic.getMac() + " for VM " + routerName);
        }
        return new ExecutionResult(true, null);
    } catch (final LibvirtException e) {
        final String msg = "Creating guest network failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new ExecutionResult(false, msg);
    }
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ExecutionResult(com.cloud.utils.ExecutionResult) NicTO(com.cloud.agent.api.to.NicTO)

Example 8 with InterfaceDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef in project cloudstack by apache.

the class IvsVifDriver method plug.

@Override
public InterfaceDef plug(NicTO nic, String guestOsType, String nicAdapter) throws InternalErrorException, LibvirtException {
    LibvirtVMDef.InterfaceDef intf = new LibvirtVMDef.InterfaceDef();
    String vNetId = null;
    String protocol = null;
    if (nic.getBroadcastType() == Networks.BroadcastDomainType.Vlan || nic.getBroadcastType() == Networks.BroadcastDomainType.Vxlan) {
        vNetId = Networks.BroadcastDomainType.getValue(nic.getBroadcastUri());
        protocol = Networks.BroadcastDomainType.getSchemeValue(nic.getBroadcastUri()).scheme();
    }
    String vlanId = null;
    String logicalSwitchUuid = null;
    if (nic.getBroadcastType() == Networks.BroadcastDomainType.Vlan) {
        vlanId = Networks.BroadcastDomainType.getValue(nic.getBroadcastUri());
    } else if (nic.getBroadcastType() == Networks.BroadcastDomainType.Lswitch) {
        logicalSwitchUuid = Networks.BroadcastDomainType.getValue(nic.getBroadcastUri());
    } else if (nic.getBroadcastType() == Networks.BroadcastDomainType.Pvlan) {
        // TODO consider moving some of this functionality from NetUtils to Networks....
        vlanId = NetUtils.getPrimaryPvlanFromUri(nic.getBroadcastUri());
    }
    String trafficLabel = nic.getName();
    Integer networkRateKBps = (nic.getNetworkRateMbps() != null && nic.getNetworkRateMbps().intValue() != -1) ? nic.getNetworkRateMbps().intValue() * 128 : 0;
    if (nic.getType() == Networks.TrafficType.Guest) {
        if ((nic.getBroadcastType() == Networks.BroadcastDomainType.Vlan || nic.getBroadcastType() == Networks.BroadcastDomainType.Pvlan) && !vlanId.equalsIgnoreCase("untagged")) {
            if (trafficLabel != null && !trafficLabel.isEmpty()) {
                s_logger.debug("creating a vlan dev and bridge for guest traffic per traffic label " + trafficLabel);
                intf.defEthernet("ivsnet-" + nic.getUuid().substring(0, 5), nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), _ivsIfUpPath, networkRateKBps);
            } else {
                throw new InternalErrorException("no traffic label ");
            }
        }
    } else if (nic.getType() == Networks.TrafficType.Control) {
        /* Make sure the network is still there */
        createControlNetwork();
        intf.defBridgeNet(_bridges.get("linklocal"), null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter));
    } else if (nic.getType() == Networks.TrafficType.Public) {
        if ((nic.getBroadcastType() == Networks.BroadcastDomainType.Vlan) && (vNetId != null) && (protocol != null) && (!vNetId.equalsIgnoreCase("untagged")) || (nic.getBroadcastType() == Networks.BroadcastDomainType.Vxlan)) {
            if (trafficLabel != null && !trafficLabel.isEmpty()) {
                s_logger.debug("creating a vNet dev and bridge for public traffic per traffic label " + trafficLabel);
                String brName = createVnetBr(vNetId, trafficLabel, protocol);
                intf.defBridgeNet(brName, null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), networkRateKBps);
            } else {
                String brName = createVnetBr(vNetId, "public", protocol);
                intf.defBridgeNet(brName, null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), networkRateKBps);
            }
        } else {
            intf.defBridgeNet(_bridges.get("public"), null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), networkRateKBps);
        }
    } else if (nic.getType() == Networks.TrafficType.Management) {
        intf.defBridgeNet(_bridges.get("private"), null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter));
    } else if (nic.getType() == Networks.TrafficType.Storage) {
        String storageBrName = nic.getName() == null ? _bridges.get("private") : nic.getName();
        intf.defBridgeNet(storageBrName, null, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter));
    }
    if (nic.getPxeDisable() == true) {
        intf.setPxeDisable(true);
    }
    return intf;
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) InternalErrorException(com.cloud.exception.InternalErrorException)

Example 9 with InterfaceDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef in project cloudstack by apache.

the class LibvirtComputingResource method post_default_network_rules.

protected boolean post_default_network_rules(final Connect conn, final String vmName, final NicTO nic, final Long vmId, final InetAddress dhcpServerIp, final String hostIp, final String hostMacAddr) {
    if (!_canBridgeFirewall) {
        return false;
    }
    final List<InterfaceDef> intfs = getInterfaces(conn, vmName);
    if (intfs.size() < nic.getDeviceId()) {
        return false;
    }
    final InterfaceDef intf = intfs.get(nic.getDeviceId());
    final String brname = intf.getBrName();
    final String vif = intf.getDevName();
    final Script cmd = new Script(_securityGroupPath, _timeout, s_logger);
    cmd.add("post_default_network_rules");
    cmd.add("--vmname", vmName);
    cmd.add("--vmid", vmId.toString());
    cmd.add("--vmip", nic.getIp());
    cmd.add("--vmmac", nic.getMac());
    cmd.add("--vif", vif);
    cmd.add("--brname", brname);
    if (dhcpServerIp != null) {
        cmd.add("--dhcpSvr", dhcpServerIp.getHostAddress());
    }
    cmd.add("--hostIp", hostIp);
    cmd.add("--hostMacAddr", hostMacAddr);
    final String result = cmd.execute();
    if (result != null) {
        return false;
    }
    return true;
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) Script(com.cloud.utils.script.Script)

Example 10 with InterfaceDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef in project cloudstack by apache.

the class LibvirtComputingResource method getInterfaces.

public List<InterfaceDef> getInterfaces(final Connect conn, final String vmName) {
    final LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
    Domain dm = null;
    try {
        dm = conn.domainLookupByName(vmName);
        parser.parseDomainXML(dm.getXMLDesc(0));
        return parser.getInterfaces();
    } catch (final LibvirtException e) {
        s_logger.debug("Failed to get dom xml: " + e.toString());
        return new ArrayList<InterfaceDef>();
    } finally {
        try {
            if (dm != null) {
                dm.free();
            }
        } catch (final LibvirtException e) {
            s_logger.trace("Ignoring libvirt error.", e);
        }
    }
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) Domain(org.libvirt.Domain)

Aggregations

InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)33 LibvirtException (org.libvirt.LibvirtException)23 Connect (org.libvirt.Connect)22 Domain (org.libvirt.Domain)15 Answer (com.cloud.agent.api.Answer)11 Test (org.junit.Test)11 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)10 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)10 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)10 ArrayList (java.util.ArrayList)10 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)10 NicTO (com.cloud.agent.api.to.NicTO)8 InternalErrorException (com.cloud.exception.InternalErrorException)8 DiskDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef)7 UnPlugNicCommand (com.cloud.agent.api.UnPlugNicCommand)5 ExecutionResult (com.cloud.utils.ExecutionResult)5 IpAddressTO (com.cloud.agent.api.to.IpAddressTO)4 VifDriver (com.cloud.hypervisor.kvm.resource.VifDriver)4 Script (com.cloud.utils.script.Script)4 PlugNicCommand (com.cloud.agent.api.PlugNicCommand)3