| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 3 | #include "mctp_endpoint_discovery.hpp" |
| 4 | |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 5 | #include "common/types.hpp" |
| 6 | #include "common/utils.hpp" |
| 7 | |
| Thu Nguyen | 9027497 | 2024-07-17 07:02:16 +0000 | [diff] [blame] | 8 | #include <linux/mctp.h> |
| 9 | |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 10 | #include <phosphor-logging/lg2.hpp> |
| 11 | |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 12 | #include <algorithm> |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 13 | #include <fstream> |
| 14 | #include <iostream> |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 15 | #include <map> |
| 16 | #include <string> |
| 17 | #include <string_view> |
| 18 | #include <vector> |
| 19 | |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 20 | using namespace sdbusplus::bus::match::rules; |
| 21 | |
| 22 | PHOSPHOR_LOG2_USING; |
| 23 | |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 24 | namespace pldm |
| 25 | { |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 26 | MctpDiscovery::MctpDiscovery( |
| Patrick Williams | ba741d5 | 2024-05-08 02:32:10 -0500 | [diff] [blame] | 27 | sdbusplus::bus_t& bus, |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 28 | std::initializer_list<MctpDiscoveryHandlerIntf*> list) : |
| Eric Yang | 70eca96 | 2025-05-11 01:48:15 +0800 | [diff] [blame] | 29 | bus(bus), |
| 30 | mctpEndpointAddedSignal( |
| 31 | bus, interfacesAdded(MCTPPath), |
| 32 | [this](sdbusplus::message_t& msg) { this->discoverEndpoints(msg); }), |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 33 | mctpEndpointRemovedSignal( |
| 34 | bus, interfacesRemoved(MCTPPath), |
| Eric Yang | 70eca96 | 2025-05-11 01:48:15 +0800 | [diff] [blame] | 35 | [this](sdbusplus::message_t& msg) { this->removeEndpoints(msg); }), |
| Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 36 | mctpEndpointPropChangedSignal( |
| 37 | bus, propertiesChangedNamespace(MCTPPath, MCTPInterfaceCC), |
| Eric Yang | 70eca96 | 2025-05-11 01:48:15 +0800 | [diff] [blame] | 38 | [this](sdbusplus::message_t& msg) { this->propertiesChangedCb(msg); }), |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 39 | handlers(list) |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 40 | { |
| Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 41 | std::map<MctpInfo, Availability> currentMctpInfoMap; |
| 42 | getMctpInfos(currentMctpInfoMap); |
| 43 | for (const auto& mapIt : currentMctpInfoMap) |
| 44 | { |
| 45 | if (mapIt.second) |
| 46 | { |
| 47 | // Only add the available endpoints to the terminus |
| 48 | // Let the propertiesChanged signal tells us when it comes back |
| 49 | // to Available again |
| 50 | addToExistingMctpInfos(MctpInfos(1, mapIt.first)); |
| 51 | } |
| 52 | } |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 53 | handleMctpEndpoints(existingMctpInfos); |
| 54 | } |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 55 | |
| Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 56 | void MctpDiscovery::getMctpInfos(std::map<MctpInfo, Availability>& mctpInfoMap) |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 57 | { |
| 58 | // Find all implementations of the MCTP Endpoint interface |
| 59 | pldm::utils::GetSubTreeResponse mapperResponse; |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 60 | try |
| 61 | { |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 62 | mapperResponse = pldm::utils::DBusHandler().getSubtree( |
| Alexander Hansen | 1f2794d | 2025-11-11 15:58:13 +0100 | [diff] [blame^] | 63 | MCTPPath, 0, std::vector<std::string>({MCTPEndpoint::interface})); |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 64 | } |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 65 | catch (const sdbusplus::exception_t& e) |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 66 | { |
| Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 67 | error( |
| 68 | "Failed to getSubtree call at path '{PATH}' and interface '{INTERFACE}', error - {ERROR} ", |
| Alexander Hansen | 1f2794d | 2025-11-11 15:58:13 +0100 | [diff] [blame^] | 69 | "ERROR", e, "PATH", MCTPPath, "INTERFACE", MCTPEndpoint::interface); |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 70 | return; |
| 71 | } |
| 72 | |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 73 | for (const auto& [path, services] : mapperResponse) |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 74 | { |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 75 | for (const auto& serviceIter : services) |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 76 | { |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 77 | const std::string& service = serviceIter.first; |
| Thu Nguyen | 9027497 | 2024-07-17 07:02:16 +0000 | [diff] [blame] | 78 | const MctpEndpointProps& epProps = |
| 79 | getMctpEndpointProps(service, path); |
| 80 | const UUID& uuid = getEndpointUUIDProp(service, path); |
| Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 81 | const Availability& availability = |
| 82 | getEndpointConnectivityProp(path); |
| Thu Nguyen | 9027497 | 2024-07-17 07:02:16 +0000 | [diff] [blame] | 83 | auto types = std::get<MCTPMsgTypes>(epProps); |
| 84 | if (std::find(types.begin(), types.end(), mctpTypePLDM) != |
| 85 | types.end()) |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 86 | { |
| Unive Tien | c40d4a6 | 2025-03-12 11:36:07 +0800 | [diff] [blame] | 87 | auto mctpInfo = |
| 88 | MctpInfo(std::get<eid>(epProps), uuid, "", |
| 89 | std::get<NetworkId>(epProps), std::nullopt); |
| 90 | searchConfigurationFor(pldm::utils::DBusHandler(), mctpInfo); |
| 91 | mctpInfoMap[std::move(mctpInfo)] = availability; |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 92 | } |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| Thu Nguyen | 9027497 | 2024-07-17 07:02:16 +0000 | [diff] [blame] | 97 | MctpEndpointProps MctpDiscovery::getMctpEndpointProps( |
| 98 | const std::string& service, const std::string& path) |
| 99 | { |
| 100 | try |
| 101 | { |
| 102 | auto properties = pldm::utils::DBusHandler().getDbusPropertiesVariant( |
| Alexander Hansen | 1f2794d | 2025-11-11 15:58:13 +0100 | [diff] [blame^] | 103 | service.c_str(), path.c_str(), MCTPEndpoint::interface); |
| Thu Nguyen | 9027497 | 2024-07-17 07:02:16 +0000 | [diff] [blame] | 104 | |
| Alexander Hansen | 1f2794d | 2025-11-11 15:58:13 +0100 | [diff] [blame^] | 105 | if (properties.contains(MCTPEndpoint::property_names::network_id) && |
| 106 | properties.contains(MCTPEndpoint::property_names::eid) && |
| 107 | properties.contains( |
| 108 | MCTPEndpoint::property_names::supported_message_types)) |
| Thu Nguyen | 9027497 | 2024-07-17 07:02:16 +0000 | [diff] [blame] | 109 | { |
| Alexander Hansen | 1f2794d | 2025-11-11 15:58:13 +0100 | [diff] [blame^] | 110 | auto networkId = std::get<NetworkId>( |
| 111 | properties.at(MCTPEndpoint::property_names::network_id)); |
| 112 | auto eid = std::get<mctp_eid_t>( |
| 113 | properties.at(MCTPEndpoint::property_names::eid)); |
| 114 | auto types = std::get<std::vector<uint8_t>>(properties.at( |
| 115 | MCTPEndpoint::property_names::supported_message_types)); |
| Thu Nguyen | 9027497 | 2024-07-17 07:02:16 +0000 | [diff] [blame] | 116 | return MctpEndpointProps(networkId, eid, types); |
| 117 | } |
| 118 | } |
| 119 | catch (const sdbusplus::exception_t& e) |
| 120 | { |
| 121 | error( |
| 122 | "Error reading MCTP Endpoint property at path '{PATH}' and service '{SERVICE}', error - {ERROR}", |
| 123 | "SERVICE", service, "PATH", path, "ERROR", e); |
| 124 | return MctpEndpointProps(0, MCTP_ADDR_ANY, {}); |
| 125 | } |
| 126 | |
| 127 | return MctpEndpointProps(0, MCTP_ADDR_ANY, {}); |
| 128 | } |
| 129 | |
| 130 | UUID MctpDiscovery::getEndpointUUIDProp(const std::string& service, |
| 131 | const std::string& path) |
| 132 | { |
| 133 | try |
| 134 | { |
| 135 | auto properties = pldm::utils::DBusHandler().getDbusPropertiesVariant( |
| 136 | service.c_str(), path.c_str(), EndpointUUID); |
| 137 | |
| 138 | if (properties.contains("UUID")) |
| 139 | { |
| 140 | return std::get<UUID>(properties.at("UUID")); |
| 141 | } |
| 142 | } |
| 143 | catch (const sdbusplus::exception_t& e) |
| 144 | { |
| 145 | error( |
| 146 | "Error reading Endpoint UUID property at path '{PATH}' and service '{SERVICE}', error - {ERROR}", |
| 147 | "SERVICE", service, "PATH", path, "ERROR", e); |
| 148 | return static_cast<UUID>(emptyUUID); |
| 149 | } |
| 150 | |
| 151 | return static_cast<UUID>(emptyUUID); |
| 152 | } |
| 153 | |
| Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 154 | Availability MctpDiscovery::getEndpointConnectivityProp(const std::string& path) |
| 155 | { |
| 156 | Availability available = false; |
| 157 | try |
| 158 | { |
| 159 | pldm::utils::PropertyValue propertyValue = |
| 160 | pldm::utils::DBusHandler().getDbusPropertyVariant( |
| 161 | path.c_str(), MCTPConnectivityProp, MCTPInterfaceCC); |
| 162 | if (std::get<std::string>(propertyValue) == "Available") |
| 163 | { |
| 164 | available = true; |
| 165 | } |
| 166 | } |
| 167 | catch (const sdbusplus::exception_t& e) |
| 168 | { |
| 169 | error( |
| 170 | "Error reading Endpoint Connectivity property at path '{PATH}', error - {ERROR}", |
| 171 | "PATH", path, "ERROR", e); |
| 172 | } |
| 173 | |
| 174 | return available; |
| 175 | } |
| 176 | |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 177 | void MctpDiscovery::getAddedMctpInfos(sdbusplus::message_t& msg, |
| 178 | MctpInfos& mctpInfos) |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 179 | { |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 180 | using ObjectPath = sdbusplus::message::object_path; |
| 181 | ObjectPath objPath; |
| 182 | using Property = std::string; |
| 183 | using PropertyMap = std::map<Property, dbus::Value>; |
| 184 | std::map<std::string, PropertyMap> interfaces; |
| Thu Nguyen | 9027497 | 2024-07-17 07:02:16 +0000 | [diff] [blame] | 185 | std::string uuid = emptyUUID; |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 186 | |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 187 | try |
| 188 | { |
| 189 | msg.read(objPath, interfaces); |
| 190 | } |
| 191 | catch (const sdbusplus::exception_t& e) |
| 192 | { |
| Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 193 | error( |
| 194 | "Error reading MCTP Endpoint added interface message, error - {ERROR}", |
| 195 | "ERROR", e); |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 196 | return; |
| 197 | } |
| Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 198 | const Availability& availability = getEndpointConnectivityProp(objPath.str); |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 199 | |
| Thu Nguyen | 9027497 | 2024-07-17 07:02:16 +0000 | [diff] [blame] | 200 | /* Get UUID */ |
| 201 | try |
| 202 | { |
| 203 | auto service = pldm::utils::DBusHandler().getService( |
| 204 | objPath.str.c_str(), EndpointUUID); |
| 205 | uuid = getEndpointUUIDProp(service, objPath.str); |
| 206 | } |
| 207 | catch (const sdbusplus::exception_t& e) |
| 208 | { |
| 209 | error("Error getting Endpoint UUID D-Bus interface, error - {ERROR}", |
| 210 | "ERROR", e); |
| 211 | } |
| 212 | |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 213 | for (const auto& [intfName, properties] : interfaces) |
| 214 | { |
| Alexander Hansen | 1f2794d | 2025-11-11 15:58:13 +0100 | [diff] [blame^] | 215 | if (intfName == MCTPEndpoint::interface) |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 216 | { |
| Alexander Hansen | 1f2794d | 2025-11-11 15:58:13 +0100 | [diff] [blame^] | 217 | if (properties.contains(MCTPEndpoint::property_names::network_id) && |
| 218 | properties.contains(MCTPEndpoint::property_names::eid) && |
| 219 | properties.contains( |
| 220 | MCTPEndpoint::property_names::supported_message_types)) |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 221 | { |
| Alexander Hansen | 1f2794d | 2025-11-11 15:58:13 +0100 | [diff] [blame^] | 222 | auto networkId = std::get<NetworkId>( |
| 223 | properties.at(MCTPEndpoint::property_names::network_id)); |
| 224 | auto eid = std::get<mctp_eid_t>( |
| 225 | properties.at(MCTPEndpoint::property_names::eid)); |
| 226 | auto types = std::get<std::vector<uint8_t>>(properties.at( |
| 227 | MCTPEndpoint::property_names::supported_message_types)); |
| Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 228 | |
| 229 | if (!availability) |
| 230 | { |
| 231 | // Log an error message here, but still add it to the |
| 232 | // terminus |
| 233 | error( |
| 234 | "mctpd added a DEGRADED endpoint {EID} networkId {NET} to D-Bus", |
| 235 | "NET", networkId, "EID", static_cast<unsigned>(eid)); |
| 236 | } |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 237 | if (std::find(types.begin(), types.end(), mctpTypePLDM) != |
| 238 | types.end()) |
| 239 | { |
| Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 240 | info( |
| Thu Nguyen | 9027497 | 2024-07-17 07:02:16 +0000 | [diff] [blame] | 241 | "Adding Endpoint networkId '{NETWORK}' and EID '{EID}' UUID '{UUID}'", |
| 242 | "NETWORK", networkId, "EID", eid, "UUID", uuid); |
| Unive Tien | c40d4a6 | 2025-03-12 11:36:07 +0800 | [diff] [blame] | 243 | auto mctpInfo = |
| 244 | MctpInfo(eid, uuid, "", networkId, std::nullopt); |
| 245 | searchConfigurationFor(pldm::utils::DBusHandler(), |
| 246 | mctpInfo); |
| 247 | mctpInfos.emplace_back(std::move(mctpInfo)); |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | } |
| 251 | } |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 252 | } |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 253 | |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 254 | void MctpDiscovery::addToExistingMctpInfos(const MctpInfos& addedInfos) |
| 255 | { |
| 256 | for (const auto& mctpInfo : addedInfos) |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 257 | { |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 258 | if (std::find(existingMctpInfos.begin(), existingMctpInfos.end(), |
| 259 | mctpInfo) == existingMctpInfos.end()) |
| 260 | { |
| 261 | existingMctpInfos.emplace_back(mctpInfo); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | void MctpDiscovery::removeFromExistingMctpInfos(MctpInfos& mctpInfos, |
| 267 | MctpInfos& removedInfos) |
| 268 | { |
| 269 | for (const auto& mctpInfo : existingMctpInfos) |
| 270 | { |
| 271 | if (std::find(mctpInfos.begin(), mctpInfos.end(), mctpInfo) == |
| 272 | mctpInfos.end()) |
| 273 | { |
| 274 | removedInfos.emplace_back(mctpInfo); |
| 275 | } |
| 276 | } |
| 277 | for (const auto& mctpInfo : removedInfos) |
| 278 | { |
| Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 279 | info("Removing Endpoint networkId '{NETWORK}' and EID '{EID}'", |
| Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 280 | "NETWORK", std::get<3>(mctpInfo), "EID", std::get<0>(mctpInfo)); |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 281 | existingMctpInfos.erase(std::remove(existingMctpInfos.begin(), |
| 282 | existingMctpInfos.end(), mctpInfo), |
| 283 | existingMctpInfos.end()); |
| 284 | } |
| 285 | } |
| 286 | |
| Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 287 | void MctpDiscovery::propertiesChangedCb(sdbusplus::message_t& msg) |
| 288 | { |
| 289 | using Interface = std::string; |
| 290 | using Property = std::string; |
| 291 | using Value = std::string; |
| 292 | using Properties = std::map<Property, std::variant<Value>>; |
| 293 | |
| 294 | Interface interface; |
| 295 | Properties properties; |
| 296 | std::string objPath{}; |
| 297 | std::string service{}; |
| 298 | |
| 299 | try |
| 300 | { |
| 301 | msg.read(interface, properties); |
| 302 | objPath = msg.get_path(); |
| 303 | } |
| 304 | catch (const sdbusplus::exception_t& e) |
| 305 | { |
| 306 | error( |
| 307 | "Error handling Connectivity property changed message, error - {ERROR}", |
| 308 | "ERROR", e); |
| 309 | return; |
| 310 | } |
| 311 | |
| 312 | for (const auto& [key, valueVariant] : properties) |
| 313 | { |
| 314 | Value propVal = std::get<std::string>(valueVariant); |
| 315 | auto availability = (propVal == "Available") ? true : false; |
| 316 | |
| 317 | if (key == MCTPConnectivityProp) |
| 318 | { |
| Alexander Hansen | 1f2794d | 2025-11-11 15:58:13 +0100 | [diff] [blame^] | 319 | service = pldm::utils::DBusHandler().getService( |
| 320 | objPath.c_str(), MCTPEndpoint::interface); |
| Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 321 | const MctpEndpointProps& epProps = |
| 322 | getMctpEndpointProps(service, objPath); |
| 323 | |
| 324 | auto types = std::get<MCTPMsgTypes>(epProps); |
| 325 | if (!std::ranges::contains(types, mctpTypePLDM)) |
| 326 | { |
| 327 | return; |
| 328 | } |
| 329 | const UUID& uuid = getEndpointUUIDProp(service, objPath); |
| 330 | |
| 331 | MctpInfo mctpInfo(std::get<eid>(epProps), uuid, "", |
| Unive Tien | c40d4a6 | 2025-03-12 11:36:07 +0800 | [diff] [blame] | 332 | std::get<NetworkId>(epProps), std::nullopt); |
| 333 | searchConfigurationFor(pldm::utils::DBusHandler(), mctpInfo); |
| Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 334 | if (!std::ranges::contains(existingMctpInfos, mctpInfo)) |
| 335 | { |
| 336 | if (availability) |
| 337 | { |
| 338 | // The endpoint not in existingMctpInfos and is |
| 339 | // available Add it to existingMctpInfos |
| 340 | info( |
| 341 | "Adding Endpoint networkId {NETWORK} ID {EID} by propertiesChanged signal", |
| 342 | "NETWORK", std::get<3>(mctpInfo), "EID", |
| 343 | unsigned(std::get<0>(mctpInfo))); |
| 344 | addToExistingMctpInfos(MctpInfos(1, mctpInfo)); |
| 345 | handleMctpEndpoints(MctpInfos(1, mctpInfo)); |
| 346 | } |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | // The endpoint already in existingMctpInfos |
| 351 | updateMctpEndpointAvailability(mctpInfo, availability); |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 357 | void MctpDiscovery::discoverEndpoints(sdbusplus::message_t& msg) |
| 358 | { |
| 359 | MctpInfos addedInfos; |
| 360 | getAddedMctpInfos(msg, addedInfos); |
| 361 | addToExistingMctpInfos(addedInfos); |
| 362 | handleMctpEndpoints(addedInfos); |
| 363 | } |
| 364 | |
| 365 | void MctpDiscovery::removeEndpoints(sdbusplus::message_t&) |
| 366 | { |
| 367 | MctpInfos mctpInfos; |
| 368 | MctpInfos removedInfos; |
| Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 369 | std::map<MctpInfo, Availability> currentMctpInfoMap; |
| 370 | getMctpInfos(currentMctpInfoMap); |
| 371 | for (const auto& mapIt : currentMctpInfoMap) |
| 372 | { |
| 373 | mctpInfos.push_back(mapIt.first); |
| 374 | } |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 375 | removeFromExistingMctpInfos(mctpInfos, removedInfos); |
| 376 | handleRemovedMctpEndpoints(removedInfos); |
| Unive Tien | c40d4a6 | 2025-03-12 11:36:07 +0800 | [diff] [blame] | 377 | removeConfigs(removedInfos); |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | void MctpDiscovery::handleMctpEndpoints(const MctpInfos& mctpInfos) |
| 381 | { |
| 382 | for (const auto& handler : handlers) |
| 383 | { |
| 384 | if (handler) |
| 385 | { |
| Unive Tien | c40d4a6 | 2025-03-12 11:36:07 +0800 | [diff] [blame] | 386 | handler->handleConfigurations(configurations); |
| Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 387 | handler->handleMctpEndpoints(mctpInfos); |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | void MctpDiscovery::handleRemovedMctpEndpoints(const MctpInfos& mctpInfos) |
| 393 | { |
| 394 | for (const auto& handler : handlers) |
| 395 | { |
| 396 | if (handler) |
| 397 | { |
| 398 | handler->handleRemovedMctpEndpoints(mctpInfos); |
| 399 | } |
| Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
| Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 403 | void MctpDiscovery::updateMctpEndpointAvailability(const MctpInfo& mctpInfo, |
| 404 | Availability availability) |
| 405 | { |
| 406 | for (const auto& handler : handlers) |
| 407 | { |
| 408 | if (handler) |
| 409 | { |
| 410 | handler->updateMctpEndpointAvailability(mctpInfo, availability); |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| Unive Tien | c40d4a6 | 2025-03-12 11:36:07 +0800 | [diff] [blame] | 415 | std::string MctpDiscovery::getNameFromProperties( |
| 416 | const utils::PropertyMap& properties) |
| 417 | { |
| 418 | if (!properties.contains("Name")) |
| 419 | { |
| 420 | error("Missing name property"); |
| 421 | return ""; |
| 422 | } |
| 423 | return std::get<std::string>(properties.at("Name")); |
| 424 | } |
| 425 | |
| 426 | std::string MctpDiscovery::constructMctpReactorObjectPath( |
| 427 | const MctpInfo& mctpInfo) |
| 428 | { |
| 429 | const auto networkId = std::get<NetworkId>(mctpInfo); |
| 430 | const auto eid = std::get<pldm::eid>(mctpInfo); |
| 431 | return std::string{MCTPPath} + "/networks/" + std::to_string(networkId) + |
| 432 | "/endpoints/" + std::to_string(eid) + "/configured_by"; |
| 433 | } |
| 434 | |
| 435 | void MctpDiscovery::searchConfigurationFor( |
| 436 | const pldm::utils::DBusHandler& handler, MctpInfo& mctpInfo) |
| 437 | { |
| 438 | const auto mctpReactorObjectPath = constructMctpReactorObjectPath(mctpInfo); |
| 439 | try |
| 440 | { |
| 441 | std::string associatedObjPath; |
| 442 | std::string associatedService; |
| 443 | std::string associatedInterface; |
| 444 | sdbusplus::message::object_path inventorySubtreePath( |
| 445 | inventorySubtreePathStr); |
| 446 | |
| 447 | //"/{board or chassis type}/{board or chassis}/{device}" |
| 448 | auto constexpr subTreeDepth = 3; |
| 449 | auto response = handler.getAssociatedSubTree( |
| 450 | mctpReactorObjectPath, inventorySubtreePath, subTreeDepth, |
| 451 | interfaceFilter); |
| 452 | if (response.empty()) |
| 453 | { |
| 454 | warning("No associated subtree found for path {PATH}", "PATH", |
| 455 | mctpReactorObjectPath); |
| 456 | return; |
| 457 | } |
| 458 | // Assume the first entry is the one we want |
| 459 | auto subTree = response.begin(); |
| 460 | associatedObjPath = subTree->first; |
| 461 | auto associatedServiceProp = subTree->second; |
| 462 | if (associatedServiceProp.empty()) |
| 463 | { |
| 464 | warning("No associated service found for path {PATH}", "PATH", |
| 465 | mctpReactorObjectPath); |
| 466 | return; |
| 467 | } |
| 468 | // Assume the first entry is the one we want |
| 469 | auto entry = associatedServiceProp.begin(); |
| 470 | associatedService = entry->first; |
| 471 | auto dBusIntfList = entry->second; |
| 472 | auto associatedInterfaceItr = std::find_if( |
| 473 | dBusIntfList.begin(), dBusIntfList.end(), [](const auto& intf) { |
| 474 | return std::find(interfaceFilter.begin(), interfaceFilter.end(), |
| 475 | intf) != interfaceFilter.end(); |
| 476 | }); |
| 477 | if (associatedInterfaceItr == dBusIntfList.end()) |
| 478 | { |
| 479 | error("No associated interface found for path {PATH}", "PATH", |
| 480 | mctpReactorObjectPath); |
| 481 | return; |
| 482 | } |
| 483 | associatedInterface = *associatedInterfaceItr; |
| 484 | auto mctpTargetProperties = handler.getDbusPropertiesVariant( |
| 485 | associatedService.c_str(), associatedObjPath.c_str(), |
| 486 | associatedInterface.c_str()); |
| 487 | auto name = getNameFromProperties(mctpTargetProperties); |
| 488 | if (!name.empty()) |
| 489 | { |
| 490 | std::get<std::optional<std::string>>(mctpInfo) = name; |
| 491 | } |
| 492 | configurations.emplace(associatedObjPath, mctpInfo); |
| 493 | } |
| 494 | catch (const std::exception& e) |
| 495 | { |
| 496 | error( |
| 497 | "Error getting associated subtree for path {PATH}, error - {ERROR}", |
| 498 | "PATH", mctpReactorObjectPath, "ERROR", e); |
| 499 | return; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | void MctpDiscovery::removeConfigs(const MctpInfos& removedInfos) |
| 504 | { |
| 505 | for (const auto& mctpInfo : removedInfos) |
| 506 | { |
| Eric Yang | 6449a3b | 2025-10-08 10:53:04 +0000 | [diff] [blame] | 507 | const auto eidToRemove = std::get<eid>(mctpInfo); |
| 508 | const auto netToRemove = std::get<NetworkId>(mctpInfo); |
| 509 | |
| 510 | std::erase_if(configurations, [eidToRemove, |
| 511 | netToRemove](const auto& config) { |
| 512 | const auto& [__, mctpInfo] = config; |
| 513 | const auto eidValue = std::get<eid>(mctpInfo); |
| 514 | const auto netValue = std::get<NetworkId>(mctpInfo); |
| 515 | |
| 516 | return eidValue == eidToRemove && netValue == netToRemove; |
| Unive Tien | c40d4a6 | 2025-03-12 11:36:07 +0800 | [diff] [blame] | 517 | }); |
| 518 | } |
| 519 | } |
| 520 | |
| Andrew Jeffery | 27a022c | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 521 | } // namespace pldm |