use of com.xensource.xenapi.VIF 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);
}
}
use of com.xensource.xenapi.VIF 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;
}
use of com.xensource.xenapi.VIF in project cloudstack by apache.
the class CitrixResourceBase method prepareNetworkElementCommand.
protected ExecutionResult prepareNetworkElementCommand(final SetSourceNatCommand cmd) {
final Connection conn = getConnection();
final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
final IpAddressTO pubIp = cmd.getIpAddress();
try {
final VM router = getVM(conn, routerName);
final VIF correctVif = getCorrectVif(conn, router, pubIp);
pubIp.setNicDevId(Integer.valueOf(correctVif.getDevice(conn)));
} catch (final Exception e) {
final String msg = "Ip SNAT failure due to " + e.toString();
s_logger.error(msg, e);
return new ExecutionResult(false, msg);
}
return new ExecutionResult(true, null);
}
use of com.xensource.xenapi.VIF in project cloudstack by apache.
the class CitrixResourceBase method cleanUpTmpDomVif.
public void cleanUpTmpDomVif(final Connection conn, final Network nw) throws XenAPIException, XmlRpcException {
final Pair<VM, VM.Record> vm = getControlDomain(conn);
final VM dom0 = vm.first();
final Set<VIF> dom0Vifs = dom0.getVIFs(conn);
for (final VIF v : dom0Vifs) {
String vifName = "unknown";
try {
final VIF.Record vifr = v.getRecord(conn);
if (v.getNetwork(conn).getUuid(conn).equals(nw.getUuid(conn))) {
if (vifr != null) {
final Map<String, String> config = vifr.otherConfig;
vifName = config.get("nameLabel");
}
s_logger.debug("A VIF in dom0 for the network is found - so destroy the vif");
v.destroy(conn);
s_logger.debug("Destroy temp dom0 vif" + vifName + " success");
}
} catch (final Exception e) {
s_logger.warn("Destroy temp dom0 vif " + vifName + "failed", e);
}
}
}
use of com.xensource.xenapi.VIF in project cloudstack by apache.
the class CitrixResourceBase method prepareNetworkElementCommand.
/**
* @param cmd
* @return
*/
private ExecutionResult prepareNetworkElementCommand(final SetupGuestNetworkCommand cmd) {
final Connection conn = getConnection();
final NicTO nic = cmd.getNic();
final String domrName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
try {
final Set<VM> vms = VM.getByNameLabel(conn, domrName);
if (vms == null || vms.isEmpty()) {
return new ExecutionResult(false, "Can not find VM " + domrName);
}
final VM vm = vms.iterator().next();
final String mac = nic.getMac();
VIF domrVif = null;
for (final VIF vif : vm.getVIFs(conn)) {
final String lmac = vif.getMAC(conn);
if (lmac.equals(mac)) {
domrVif = vif;
// Do not break it! We have 2 routers.
// break;
}
}
if (domrVif == null) {
return new ExecutionResult(false, "Can not find vif with mac " + mac + " for VM " + domrName);
}
nic.setDeviceId(Integer.parseInt(domrVif.getDevice(conn)));
} catch (final Exception e) {
final String msg = "Creating guest network failed due to " + e.toString();
s_logger.warn(msg, e);
return new ExecutionResult(false, msg);
}
return new ExecutionResult(true, null);
}
Aggregations