/// <summary> /// Configure an IP address, management purpose, and set the disallow_unplug flag on the given existing_pif. /// </summary> /// <param name="new_pif">The source of the new IP details</param> /// <param name="existing_pif">The PIF to configure</param> internal static void BringUp(AsyncAction action, PIF new_pif, string new_ip, PIF existing_pif, int hi) { bool primary = string.IsNullOrEmpty(new_pif.GetManagementPurpose()); int lo = action.PercentComplete; int inc = (hi - lo) / (primary ? 2 : 3); log.DebugFormat("Bringing PIF {0} {1} up as {2}/{3}, {4}, {5}...", existing_pif.Name(), existing_pif.uuid, new_ip, new_pif.netmask, new_pif.gateway, new_pif.DNS); action.Description = string.Format(Messages.ACTION_CHANGE_NETWORKING_BRINGING_UP, existing_pif.Name()); PIF p = (PIF)existing_pif.Clone(); p.disallow_unplug = !primary; p.SetManagementPurpose(new_pif.GetManagementPurpose()); p.SaveChanges(action.Session); action.PercentComplete = lo + inc; ReconfigureIP(action, new_pif, existing_pif, new_ip, action.PercentComplete + inc); if (!primary) { Plug(action, existing_pif, hi); } action.Description = string.Format(Messages.ACTION_CHANGE_NETWORKING_BRINGING_UP_DONE, existing_pif.Name()); log.DebugFormat("Brought PIF {0} {1} up.", existing_pif.Name(), existing_pif.uuid); }
/// <summary> /// Copy the IP details from src to a clone of dest, and return the clone. /// </summary> public static PIF CopyIPConfig(PIF src, PIF dest) { PIF result = (PIF)dest.Clone(); result.SetManagementPurpose(src.GetManagementPurpose()); result.ip_configuration_mode = src.ip_configuration_mode; result.IP = src.IP; result.netmask = src.netmask; result.gateway = src.gateway; result.DNS = src.DNS; return(result); }
/// <summary> /// Clear the disallow_unplug and ManagementPurpose on the given NIC. /// </summary> private static void Depurpose(AsyncAction action, PIF pif, int hi) { log.DebugFormat("Depurposing PIF {0} {1}...", pif.Name(), pif.uuid); action.Description = string.Format(Messages.ACTION_CHANGE_NETWORKING_DEPURPOSING, pif.Name()); PIF p = (PIF)pif.Clone(); p.disallow_unplug = false; p.SetManagementPurpose(null); p.SaveChanges(action.Session); action.PercentComplete = hi; log.DebugFormat("Depurposed PIF {0} {1}.", pif.Name(), pif.uuid); action.Description = string.Format(Messages.ACTION_CHANGE_NETWORKING_DEPURPOSED, pif.Name()); }
/// <summary> /// Switch the host's management interface from its current setting over to the given PIF. /// </summary> private static void ReconfigureManagement_(AsyncAction action, PIF pif, int hi) { log.DebugFormat("Switching to PIF {0} {1} for management...", pif.Name(), pif.uuid); action.Description = string.Format(Messages.ACTION_CHANGE_NETWORKING_MANAGEMENT_RECONFIGURING, pif.Name()); int mid = (hi + action.PercentComplete) / 2; PIF p = (PIF)pif.Clone(); p.disallow_unplug = false; p.SetManagementPurpose(null); p.SaveChanges(action.Session); action.PercentComplete = mid; action.RelatedTask = XenAPI.Host.async_management_reconfigure(action.Session, pif.opaque_ref); action.PollToCompletion(mid, hi); log.DebugFormat("Switched to PIF {0} {1} for management.", pif.Name(), pif.uuid); action.Description = string.Format(Messages.ACTION_CHANGE_NETWORKING_MANAGEMENT_RECONFIGURED, pif.Name()); }
/// <summary> /// If the "from" PIF is a secondary management interface, /// move the management interface name to the "to" PIF instead. /// </summary> internal static void MoveManagementInterfaceName(AsyncAction action, PIF from, PIF to) { string managementPurpose = from.GetManagementPurpose(); if (string.IsNullOrEmpty(managementPurpose)) { return; } log.DebugFormat("Moving management interface name from {0} to {1}...", from.uuid, to.uuid); PIF to_clone = (PIF)to.Clone(); to_clone.SetManagementPurpose(from.GetManagementPurpose()); to_clone.SaveChanges(action.Session); PIF from_clone = (PIF)from.Clone(); from_clone.SetManagementPurpose(null); from_clone.SaveChanges(action.Session); log.DebugFormat("Moving management interface name from {0} to {1} done.", from.uuid, to.uuid); }
/// <summary> /// Will throw an exception if the network has gone away. /// </summary> /// <param name="page"></param> /// <param name="oldPIF"></param> /// <param name="newPIFs"></param> /// <param name="newNamePIFs"></param> private void CollateChanges(NetworkingPropertiesPage page, PIF oldPIF, List <PifTuple> newPIFs, List <PifTuple> newNamePIFs) { bool changed = false; bool changedName = false; if (oldPIF == null) { // This tab is newly created. XenAPI.Network network = (XenAPI.Network)page.NetworkComboBox.SelectedItem; oldPIF = FindPIFForThisHost(network.PIFs); if (oldPIF == null) { throw new Failure(Failure.INTERNAL_ERROR, "Network has gone away"); } changed = true; } else { // This tab was populated when this dialog was launched. XenAPI.Network network = connection.Resolve(oldPIF.network); if ((XenAPI.Network)page.NetworkComboBox.SelectedItem != network) { // The user has changed the network, so find the one we're using now. XenAPI.Network new_network = (XenAPI.Network)page.NetworkComboBox.SelectedItem; oldPIF = FindPIFForThisHost(new_network.PIFs); if (oldPIF == null) { throw new Failure(Failure.INTERNAL_ERROR, "Network has gone away"); } changed = true; } } PIF newPIF = (PIF)oldPIF.Clone(); newPIF.PropertyChanged += (sender, e) => { changed = true; }; PIF newNamePIF = (PIF)oldPIF.Clone(); newNamePIF.PropertyChanged += (sender, e) => { changedName = true; }; if (page.DHCPIPRadioButton.Checked) { newPIF.ip_configuration_mode = ip_configuration_mode.DHCP; } else { newPIF.ip_configuration_mode = ip_configuration_mode.Static; newPIF.netmask = page.SubnetTextBox.Text; newPIF.gateway = page.GatewayTextBox.Text; newPIF.IP = page.IPAddressTextBox.Text; List <string> dns = new List <string>(); if (page.PreferredDNSTextBox.Text.Length > 0) { dns.Add(page.PreferredDNSTextBox.Text); } if (page.AlternateDNS1TextBox.Text.Length > 0) { dns.Add(page.AlternateDNS1TextBox.Text); } if (page.AlternateDNS2TextBox.Text.Length > 0) { dns.Add(page.AlternateDNS2TextBox.Text); } var newDns = string.Join(",", dns.ToArray()); newPIF.DNS = newDns; } newPIF.management = page.type != NetworkingPropertiesPage.Type.SECONDARY; if (page.type == NetworkingPropertiesPage.Type.SECONDARY) { if (changed) { newPIF.SetManagementPurpose(page.PurposeTextBox.Text); } else { newNamePIF.SetManagementPurpose(page.PurposeTextBox.Text); } } newPIFs.Add(new PifTuple(newPIF, changed)); newNamePIFs.Add(new PifTuple(newNamePIF, changedName)); }