|
| 1 | +import binascii |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +import netaddr |
| 5 | +import openstack |
| 6 | +from construct import core |
| 7 | +from ironic import objects |
| 8 | +from ironic.common import exception |
| 9 | +from ironic.drivers.modules.inspector import lldp_tlvs |
| 10 | +from ironic.drivers.modules.inspector.hooks import base |
| 11 | +from oslo_log import log as logging |
| 12 | + |
| 13 | +import ironic_understack.vlan_group_name_convention |
| 14 | + |
| 15 | +LOG = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +class UpdateBaremetalPortsHook(base.InspectionHook): |
| 19 | + """Hook to update ports according to LLDP data.""" |
| 20 | + |
| 21 | + dependencies = ["validate-interfaces"] |
| 22 | + |
| 23 | + def __call__(self, task, inventory, plugin_data): |
| 24 | + """Update Ports' local_link_info and physnet based on LLDP data. |
| 25 | +
|
| 26 | + Process the LLDP packet fields for each NIC in the inventory. |
| 27 | +
|
| 28 | + Updates attributes of the baremetal port: |
| 29 | +
|
| 30 | + - local_link_info.port_id (e.g. "Ethernet1/1") |
| 31 | + - local_link_info.switch_id (e.g. "aa:bb:cc:dd:ee:ff") |
| 32 | + - local_link_info.switch_info (e.g. "a1-1-1.ord1") |
| 33 | + - physical_network (e.g. "a1-1-network") |
| 34 | +
|
| 35 | + Also adds or removes node "traits" based on the inventory data. We |
| 36 | + control the trait "CUSTOM_STORAGE_SWITCH". |
| 37 | + """ |
| 38 | + lldp_raw = plugin_data.get("lldp_raw") or {} |
| 39 | + node_uuid = task.node.id |
| 40 | + interfaces = inventory["interfaces"] |
| 41 | + # The all_interfaces field in plugin_data is provided by the |
| 42 | + # validate-interfaces hook, so it is a dependency for this hook |
| 43 | + all_interfaces = plugin_data["all_interfaces"] |
| 44 | + context = task.context |
| 45 | + vlan_groups = set() |
| 46 | + |
| 47 | + for iface in interfaces: |
| 48 | + if iface["name"] not in all_interfaces: |
| 49 | + # This interface was not "validated" so don't bother with it |
| 50 | + continue |
| 51 | + |
| 52 | + mac_address = iface["mac_address"] |
| 53 | + port = objects.port.Port.get_by_address(context, mac_address) |
| 54 | + if not port: |
| 55 | + LOG.debug( |
| 56 | + "Skipping LLDP processing for interface %s of node " |
| 57 | + "%s: matching port not found in Ironic.", |
| 58 | + mac_address, |
| 59 | + node_uuid, |
| 60 | + ) |
| 61 | + continue |
| 62 | + |
| 63 | + lldp_data = lldp_raw.get(iface["name"]) or iface.get("lldp") |
| 64 | + if lldp_data is None: |
| 65 | + LOG.warning( |
| 66 | + "No LLDP data found for interface %s of node %s", |
| 67 | + mac_address, |
| 68 | + node_uuid, |
| 69 | + ) |
| 70 | + continue |
| 71 | + |
| 72 | + local_link_connection = _parse_lldp(lldp_data, node_uuid) |
| 73 | + vlan_group = vlan_group_name(local_link_connection) |
| 74 | + |
| 75 | + _set_local_link_connection(port, node_uuid, local_link_connection) |
| 76 | + _update_port_physical_network(port, vlan_group) |
| 77 | + vlan_groups.add(vlan_group) |
| 78 | + _update_node_traits(task, vlan_groups) |
| 79 | + |
| 80 | + |
| 81 | +def _set_local_link_connection(port: Any, node_uuid: str, local_link_connection: dict): |
| 82 | + try: |
| 83 | + LOG.debug("Updating port %s for node %s", port.address, node_uuid) |
| 84 | + port.local_link_connection = local_link_connection |
| 85 | + port.save() |
| 86 | + except exception.IronicException as e: |
| 87 | + LOG.warning( |
| 88 | + "Failed to update port %(uuid)s for node %(node)s. Error: %(error)s", |
| 89 | + {"uuid": port.id, "node": node_uuid, "error": e}, |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +def _parse_lldp(lldp_data: list, node_id: str) -> dict[str, str]: |
| 94 | + """Convert Ironic's "lldp_raw" format to local_link dict.""" |
| 95 | + try: |
| 96 | + decoded = {} |
| 97 | + for tlv_type, tlv_value in lldp_data: |
| 98 | + if tlv_type not in decoded: |
| 99 | + decoded[tlv_type] = [] |
| 100 | + decoded[tlv_type].append(bytearray(binascii.unhexlify(tlv_value))) |
| 101 | + |
| 102 | + return { |
| 103 | + "port_id": _extract_port_id(decoded), |
| 104 | + "switch_id": _extract_switch_id(decoded), |
| 105 | + "switch_info": _extract_hostname(decoded), |
| 106 | + } |
| 107 | + except (binascii.Error, core.MappingError, netaddr.AddrFormatError) as e: |
| 108 | + LOG.warning("Failed to parse lldp_raw data for Node %s: %s", node_id, e) |
| 109 | + return {} |
| 110 | + |
| 111 | + |
| 112 | +def _extract_port_id(data: dict) -> str | None: |
| 113 | + for value in data.get(lldp_tlvs.LLDP_TLV_PORT_ID, []): |
| 114 | + parsed = lldp_tlvs.PortId.parse(value) |
| 115 | + if parsed.value: # pyright: ignore reportAttributeAccessIssue |
| 116 | + return parsed.value.value # pyright: ignore reportAttributeAccessIssue |
| 117 | + |
| 118 | + |
| 119 | +def _extract_switch_id(data: dict) -> str | None: |
| 120 | + for value in data.get(lldp_tlvs.LLDP_TLV_CHASSIS_ID, []): |
| 121 | + parsed = lldp_tlvs.ChassisId.parse(value) |
| 122 | + if "mac_address" in parsed.subtype: # pyright: ignore reportAttributeAccessIssue |
| 123 | + return str(parsed.value.value) # pyright: ignore reportAttributeAccessIssue |
| 124 | + |
| 125 | + |
| 126 | +def _extract_hostname(data: dict) -> str | None: |
| 127 | + for value in data.get(lldp_tlvs.LLDP_TLV_SYS_NAME, []): |
| 128 | + parsed = lldp_tlvs.SysName.parse(value) |
| 129 | + if parsed.value: # pyright: ignore reportAttributeAccessIssue |
| 130 | + return parsed.value # pyright: ignore reportAttributeAccessIssue |
| 131 | + |
| 132 | + |
| 133 | +def vlan_group_name(local_link_connection) -> str | None: |
| 134 | + switch_name = local_link_connection.get("switch_info") |
| 135 | + if not switch_name: |
| 136 | + return |
| 137 | + |
| 138 | + return ironic_understack.vlan_group_name_convention.vlan_group_name(switch_name) |
| 139 | + |
| 140 | + |
| 141 | +def _update_port_physical_network(port, new_physical_network: str|None): |
| 142 | + old_physical_network = port.physical_network |
| 143 | + |
| 144 | + if new_physical_network == old_physical_network: |
| 145 | + return |
| 146 | + |
| 147 | + LOG.debug( |
| 148 | + "Updating port %s physical_network from %s to %s", |
| 149 | + port.id, |
| 150 | + old_physical_network, |
| 151 | + new_physical_network, |
| 152 | + ) |
| 153 | + port.physical_network = new_physical_network |
| 154 | + port.save() |
| 155 | + |
| 156 | + |
| 157 | +def _update_node_traits(task, vlan_groups: set[str]): |
| 158 | + """Add or remove traits to the node. |
| 159 | +
|
| 160 | + We manage one trait: "CUSTOM_STORAGE_SWITCH" which is added if the node has |
| 161 | + any ports connected to a storage fabric, othwise it is removed from the |
| 162 | + node. |
| 163 | + """ |
| 164 | + TRAIT_STORAGE_SWITCH = "CUSTOM_STORAGE_SWITCH" |
| 165 | + |
| 166 | + storage_vlan_groups = { |
| 167 | + x for x in vlan_groups if x.endswith("-storage") |
| 168 | + } |
| 169 | + |
| 170 | + if storage_vlan_groups: |
| 171 | + task.node.add_trait(TRAIT_STORAGE_SWITCH) |
| 172 | + else: |
| 173 | + try: |
| 174 | + task.node.remove_trait(TRAIT_STORAGE_SWITCH) |
| 175 | + except openstack.exceptions.NotFoundException: |
| 176 | + pass |
| 177 | + |
| 178 | + task.node.save() |
0 commit comments