use of com.xensource.xenapi.PIF 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;
}
use of com.xensource.xenapi.PIF 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);
}
use of com.xensource.xenapi.PIF in project cloudstack by apache.
the class XenServer56Resource method disableVlanNetwork.
@Override
public void disableVlanNetwork(final Connection conn, final Network network, boolean deleteVlan) {
try {
final Network.Record networkr = network.getRecord(conn);
if (!networkr.nameLabel.startsWith("VLAN")) {
return;
}
if (deleteVlan) {
final String bridge = networkr.bridge.trim();
for (final PIF pif : networkr.PIFs) {
final PIF.Record pifr = pif.getRecord(conn);
if (!pifr.host.getUuid(conn).equalsIgnoreCase(_host.getUuid())) {
continue;
}
final VLAN vlan = pifr.VLANMasterOf;
if (vlan != null) {
final String vlannum = pifr.VLAN.toString();
final String device = pifr.device.trim();
if (vlannum.equals("-1")) {
return;
}
try {
vlan.destroy(conn);
final Host host = Host.getByUuid(conn, _host.getUuid());
host.forgetDataSourceArchives(conn, "pif_" + bridge + "_tx");
host.forgetDataSourceArchives(conn, "pif_" + bridge + "_rx");
host.forgetDataSourceArchives(conn, "pif_" + device + "." + vlannum + "_tx");
host.forgetDataSourceArchives(conn, "pif_" + device + "." + vlannum + "_rx");
} catch (final XenAPIException e) {
s_logger.trace("Catch " + e.getClass().getName() + ": failed to destroy VLAN " + device + " on host " + _host.getUuid() + " due to " + e.toString());
}
}
return;
}
}
} catch (final XenAPIException e) {
final String msg = "Unable to disable VLAN network due to " + e.toString();
s_logger.warn(msg, e);
} catch (final Exception e) {
final String msg = "Unable to disable VLAN network due to " + e.getMessage();
s_logger.warn(msg, e);
}
}
use of com.xensource.xenapi.PIF in project cloudstack by apache.
the class CitrixOvsFetchInterfaceCommandWrapper method execute.
@Override
public Answer execute(final OvsFetchInterfaceCommand command, final CitrixResourceBase citrixResourceBase) {
String label = command.getLabel();
// FIXME: this is a tricky to pass the network checking in XCP. I temporary get default label from Host.
if (citrixResourceBase.isXcp()) {
label = citrixResourceBase.getLabel();
}
s_logger.debug("Will look for network with name-label:" + label + " on host " + citrixResourceBase.getHost().getIp());
final Connection conn = citrixResourceBase.getConnection();
try {
final XsLocalNetwork nw = citrixResourceBase.getNetworkByName(conn, label);
if (nw == null) {
throw new CloudRuntimeException("Unable to locate the network with name-label: " + label + " on host: " + citrixResourceBase.getHost().getIp());
}
s_logger.debug("Network object:" + nw.getNetwork().getUuid(conn));
final PIF pif = nw.getPif(conn);
final PIF.Record pifRec = pif.getRecord(conn);
s_logger.debug("PIF object:" + pifRec.uuid + "(" + pifRec.device + ")");
return new OvsFetchInterfaceAnswer(command, true, "Interface " + pifRec.device + " retrieved successfully", pifRec.IP, pifRec.netmask, pifRec.MAC);
} catch (final BadServerResponse e) {
s_logger.error("An error occurred while fetching the interface for " + label + " on host " + citrixResourceBase.getHost().getIp(), e);
return new OvsFetchInterfaceAnswer(command, false, "EXCEPTION:" + e.getMessage());
} catch (final XenAPIException e) {
s_logger.error("An error occurred while fetching the interface for " + label + " on host " + citrixResourceBase.getHost().getIp(), e);
return new OvsFetchInterfaceAnswer(command, false, "EXCEPTION:" + e.getMessage());
} catch (final XmlRpcException e) {
s_logger.error("An error occurred while fetching the interface for " + label + " on host " + citrixResourceBase.getHost().getIp(), e);
return new OvsFetchInterfaceAnswer(command, false, "EXCEPTION:" + e.getMessage());
}
}
use of com.xensource.xenapi.PIF in project cloudstack by apache.
the class CitrixSetupCommandWrapper method execute.
@Override
public Answer execute(final SetupCommand command, final CitrixResourceBase citrixResourceBase) {
final Connection conn = citrixResourceBase.getConnection();
try {
final Map<Pool, Pool.Record> poolRecs = Pool.getAllRecords(conn);
if (poolRecs.size() != 1) {
throw new CloudRuntimeException("There are " + poolRecs.size() + " pool for host :" + citrixResourceBase.getHost().getUuid());
}
final Host master = poolRecs.values().iterator().next().master;
citrixResourceBase.setupServer(conn, master);
final Host host = Host.getByUuid(conn, citrixResourceBase.getHost().getUuid());
citrixResourceBase.setupServer(conn, host);
if (!citrixResourceBase.setIptables(conn)) {
s_logger.warn("set xenserver Iptable failed");
return null;
}
if (citrixResourceBase.isSecurityGroupEnabled()) {
final boolean canBridgeFirewall = citrixResourceBase.canBridgeFirewall(conn);
citrixResourceBase.setCanBridgeFirewall(canBridgeFirewall);
if (!canBridgeFirewall) {
final String msg = "Failed to configure brige firewall";
s_logger.warn(msg);
s_logger.warn("Check host " + citrixResourceBase.getHost().getIp() + " for CSP is installed or not and check network mode for bridge");
return new SetupAnswer(command, msg);
}
}
final boolean r = citrixResourceBase.launchHeartBeat(conn);
if (!r) {
return null;
}
citrixResourceBase.cleanupTemplateSR(conn);
try {
if (command.useMultipath()) {
// the config value is set to true
host.addToOtherConfig(conn, "multipathing", "true");
host.addToOtherConfig(conn, "multipathhandle", "dmp");
}
} catch (final Types.MapDuplicateKey e) {
s_logger.debug("multipath is already set");
}
if (command.needSetup()) {
final String result = citrixResourceBase.callHostPlugin(conn, "vmops", "setup_iscsi", "uuid", citrixResourceBase.getHost().getUuid());
if (!result.contains("> DONE <")) {
s_logger.warn("Unable to setup iscsi: " + result);
return new SetupAnswer(command, result);
}
Pair<PIF, PIF.Record> mgmtPif = null;
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(citrixResourceBase.getHost().getUuid()).append("; pif=").append(rec.uuid).append("; vlan=").append(rec.VLAN).toString();
s_logger.warn(msg);
return new SetupAnswer(command, msg);
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("Management network is on pif=" + rec.uuid);
}
mgmtPif = new Pair<PIF, PIF.Record>(pif, rec);
break;
}
}
if (mgmtPif == null) {
final String msg = "Unable to find management network for " + citrixResourceBase.getHost().getUuid();
s_logger.warn(msg);
return new SetupAnswer(command, msg);
}
final Map<Network, Network.Record> networks = Network.getAllRecords(conn);
if (networks == null) {
final String msg = "Unable to setup as there are no networks in the host: " + citrixResourceBase.getHost().getUuid();
s_logger.warn(msg);
return new SetupAnswer(command, msg);
}
for (final Network.Record network : networks.values()) {
if (network.nameLabel.equals("cloud-private")) {
for (final PIF pif : network.PIFs) {
final PIF.Record pr = pif.getRecord(conn);
if (citrixResourceBase.getHost().getUuid().equals(pr.host.getUuid(conn))) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Found a network called cloud-private. host=" + citrixResourceBase.getHost().getUuid() + "; Network=" + network.uuid + "; pif=" + pr.uuid);
}
if (pr.VLAN != null && pr.VLAN != -1) {
final String msg = new StringBuilder("Unsupported configuration. Network cloud-private is on a VLAN. Network=").append(network.uuid).append(" ; pif=").append(pr.uuid).toString();
s_logger.warn(msg);
return new SetupAnswer(command, msg);
}
if (!pr.management && pr.bondMasterOf != null && pr.bondMasterOf.size() > 0) {
if (pr.bondMasterOf.size() > 1) {
final String msg = new StringBuilder("Unsupported configuration. Network cloud-private has more than one bond. Network=").append(network.uuid).append("; pif=").append(pr.uuid).toString();
s_logger.warn(msg);
return new SetupAnswer(command, msg);
}
final Bond bond = pr.bondMasterOf.iterator().next();
final Set<PIF> slaves = bond.getSlaves(conn);
for (final PIF slave : slaves) {
final PIF.Record spr = slave.getRecord(conn);
if (spr.management) {
if (!citrixResourceBase.transferManagementNetwork(conn, host, slave, spr, pif)) {
final String msg = new StringBuilder("Unable to transfer management network. slave=" + spr.uuid + "; master=" + pr.uuid + "; host=" + citrixResourceBase.getHost().getUuid()).toString();
s_logger.warn(msg);
return new SetupAnswer(command, msg);
}
break;
}
}
}
}
}
}
}
}
return new SetupAnswer(command, false);
} catch (final XmlRpcException e) {
s_logger.warn("Unable to setup", e);
return new SetupAnswer(command, e.getMessage());
} catch (final XenAPIException e) {
s_logger.warn("Unable to setup", e);
return new SetupAnswer(command, e.getMessage());
} catch (final Exception e) {
s_logger.warn("Unable to setup", e);
return new SetupAnswer(command, e.getMessage());
}
}
Aggregations