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) : |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 29 | bus(bus), mctpEndpointAddedSignal( |
| 30 | bus, interfacesAdded(MCTPPath), |
| 31 | std::bind_front(&MctpDiscovery::discoverEndpoints, this)), |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 32 | mctpEndpointRemovedSignal( |
| 33 | bus, interfacesRemoved(MCTPPath), |
| 34 | std::bind_front(&MctpDiscovery::removeEndpoints, this)), |
| 35 | handlers(list) |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 36 | { |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 37 | getMctpInfos(existingMctpInfos); |
| 38 | handleMctpEndpoints(existingMctpInfos); |
| 39 | } |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 40 | |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 41 | void MctpDiscovery::getMctpInfos(MctpInfos& mctpInfos) |
| 42 | { |
| 43 | // Find all implementations of the MCTP Endpoint interface |
| 44 | pldm::utils::GetSubTreeResponse mapperResponse; |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 45 | try |
| 46 | { |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 47 | mapperResponse = pldm::utils::DBusHandler().getSubtree( |
| 48 | MCTPPath, 0, std::vector<std::string>({MCTPInterface})); |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 49 | } |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 50 | catch (const sdbusplus::exception_t& e) |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 51 | { |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 52 | error( |
| 53 | "Failed to getSubtree call at path '{PATH}' and interface '{INTERFACE}', error - {ERROR} ", |
| 54 | "ERROR", e, "PATH", MCTPPath, "INTERFACE", MCTPInterface); |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 55 | return; |
| 56 | } |
| 57 | |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 58 | for (const auto& [path, services] : mapperResponse) |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 59 | { |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 60 | for (const auto& serviceIter : services) |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 61 | { |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 62 | const std::string& service = serviceIter.first; |
Thu Nguyen | 9027497 | 2024-07-17 07:02:16 +0000 | [diff] [blame] | 63 | const MctpEndpointProps& epProps = |
| 64 | getMctpEndpointProps(service, path); |
| 65 | const UUID& uuid = getEndpointUUIDProp(service, path); |
| 66 | auto types = std::get<MCTPMsgTypes>(epProps); |
| 67 | if (std::find(types.begin(), types.end(), mctpTypePLDM) != |
| 68 | types.end()) |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 69 | { |
Thu Nguyen | 9027497 | 2024-07-17 07:02:16 +0000 | [diff] [blame] | 70 | mctpInfos.emplace_back( |
| 71 | MctpInfo(std::get<eid>(epProps), uuid, "", |
| 72 | std::get<NetworkId>(epProps))); |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 73 | } |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Thu Nguyen | 9027497 | 2024-07-17 07:02:16 +0000 | [diff] [blame] | 78 | MctpEndpointProps MctpDiscovery::getMctpEndpointProps( |
| 79 | const std::string& service, const std::string& path) |
| 80 | { |
| 81 | try |
| 82 | { |
| 83 | auto properties = pldm::utils::DBusHandler().getDbusPropertiesVariant( |
| 84 | service.c_str(), path.c_str(), MCTPInterface); |
| 85 | |
| 86 | if (properties.contains("NetworkId") && properties.contains("EID") && |
| 87 | properties.contains("SupportedMessageTypes")) |
| 88 | { |
| 89 | auto networkId = std::get<NetworkId>(properties.at("NetworkId")); |
| 90 | auto eid = std::get<mctp_eid_t>(properties.at("EID")); |
| 91 | auto types = std::get<std::vector<uint8_t>>( |
| 92 | properties.at("SupportedMessageTypes")); |
| 93 | return MctpEndpointProps(networkId, eid, types); |
| 94 | } |
| 95 | } |
| 96 | catch (const sdbusplus::exception_t& e) |
| 97 | { |
| 98 | error( |
| 99 | "Error reading MCTP Endpoint property at path '{PATH}' and service '{SERVICE}', error - {ERROR}", |
| 100 | "SERVICE", service, "PATH", path, "ERROR", e); |
| 101 | return MctpEndpointProps(0, MCTP_ADDR_ANY, {}); |
| 102 | } |
| 103 | |
| 104 | return MctpEndpointProps(0, MCTP_ADDR_ANY, {}); |
| 105 | } |
| 106 | |
| 107 | UUID MctpDiscovery::getEndpointUUIDProp(const std::string& service, |
| 108 | const std::string& path) |
| 109 | { |
| 110 | try |
| 111 | { |
| 112 | auto properties = pldm::utils::DBusHandler().getDbusPropertiesVariant( |
| 113 | service.c_str(), path.c_str(), EndpointUUID); |
| 114 | |
| 115 | if (properties.contains("UUID")) |
| 116 | { |
| 117 | return std::get<UUID>(properties.at("UUID")); |
| 118 | } |
| 119 | } |
| 120 | catch (const sdbusplus::exception_t& e) |
| 121 | { |
| 122 | error( |
| 123 | "Error reading Endpoint UUID property at path '{PATH}' and service '{SERVICE}', error - {ERROR}", |
| 124 | "SERVICE", service, "PATH", path, "ERROR", e); |
| 125 | return static_cast<UUID>(emptyUUID); |
| 126 | } |
| 127 | |
| 128 | return static_cast<UUID>(emptyUUID); |
| 129 | } |
| 130 | |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 131 | void MctpDiscovery::getAddedMctpInfos(sdbusplus::message_t& msg, |
| 132 | MctpInfos& mctpInfos) |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 133 | { |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 134 | using ObjectPath = sdbusplus::message::object_path; |
| 135 | ObjectPath objPath; |
| 136 | using Property = std::string; |
| 137 | using PropertyMap = std::map<Property, dbus::Value>; |
| 138 | std::map<std::string, PropertyMap> interfaces; |
Thu Nguyen | 9027497 | 2024-07-17 07:02:16 +0000 | [diff] [blame] | 139 | std::string uuid = emptyUUID; |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 140 | |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 141 | try |
| 142 | { |
| 143 | msg.read(objPath, interfaces); |
| 144 | } |
| 145 | catch (const sdbusplus::exception_t& e) |
| 146 | { |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 147 | error( |
| 148 | "Error reading MCTP Endpoint added interface message, error - {ERROR}", |
| 149 | "ERROR", e); |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 150 | return; |
| 151 | } |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 152 | |
Thu Nguyen | 9027497 | 2024-07-17 07:02:16 +0000 | [diff] [blame] | 153 | /* Get UUID */ |
| 154 | try |
| 155 | { |
| 156 | auto service = pldm::utils::DBusHandler().getService( |
| 157 | objPath.str.c_str(), EndpointUUID); |
| 158 | uuid = getEndpointUUIDProp(service, objPath.str); |
| 159 | } |
| 160 | catch (const sdbusplus::exception_t& e) |
| 161 | { |
| 162 | error("Error getting Endpoint UUID D-Bus interface, error - {ERROR}", |
| 163 | "ERROR", e); |
| 164 | } |
| 165 | |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 166 | for (const auto& [intfName, properties] : interfaces) |
| 167 | { |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 168 | if (intfName == MCTPInterface) |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 169 | { |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 170 | if (properties.contains("NetworkId") && |
| 171 | properties.contains("EID") && |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 172 | properties.contains("SupportedMessageTypes")) |
| 173 | { |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 174 | auto networkId = |
| 175 | std::get<NetworkId>(properties.at("NetworkId")); |
Dung Cao | 66794d0 | 2023-10-25 04:06:23 +0000 | [diff] [blame] | 176 | auto eid = std::get<mctp_eid_t>(properties.at("EID")); |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 177 | auto types = std::get<std::vector<uint8_t>>( |
| 178 | properties.at("SupportedMessageTypes")); |
| 179 | if (std::find(types.begin(), types.end(), mctpTypePLDM) != |
| 180 | types.end()) |
| 181 | { |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 182 | info( |
Thu Nguyen | 9027497 | 2024-07-17 07:02:16 +0000 | [diff] [blame] | 183 | "Adding Endpoint networkId '{NETWORK}' and EID '{EID}' UUID '{UUID}'", |
| 184 | "NETWORK", networkId, "EID", eid, "UUID", uuid); |
| 185 | mctpInfos.emplace_back(MctpInfo(eid, uuid, "", networkId)); |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 190 | } |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 191 | |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 192 | void MctpDiscovery::addToExistingMctpInfos(const MctpInfos& addedInfos) |
| 193 | { |
| 194 | for (const auto& mctpInfo : addedInfos) |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 195 | { |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 196 | if (std::find(existingMctpInfos.begin(), existingMctpInfos.end(), |
| 197 | mctpInfo) == existingMctpInfos.end()) |
| 198 | { |
| 199 | existingMctpInfos.emplace_back(mctpInfo); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void MctpDiscovery::removeFromExistingMctpInfos(MctpInfos& mctpInfos, |
| 205 | MctpInfos& removedInfos) |
| 206 | { |
| 207 | for (const auto& mctpInfo : existingMctpInfos) |
| 208 | { |
| 209 | if (std::find(mctpInfos.begin(), mctpInfos.end(), mctpInfo) == |
| 210 | mctpInfos.end()) |
| 211 | { |
| 212 | removedInfos.emplace_back(mctpInfo); |
| 213 | } |
| 214 | } |
| 215 | for (const auto& mctpInfo : removedInfos) |
| 216 | { |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 217 | info("Removing Endpoint networkId '{NETWORK}' and EID '{EID}'", |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 218 | "NETWORK", std::get<3>(mctpInfo), "EID", std::get<0>(mctpInfo)); |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 219 | existingMctpInfos.erase(std::remove(existingMctpInfos.begin(), |
| 220 | existingMctpInfos.end(), mctpInfo), |
| 221 | existingMctpInfos.end()); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | void MctpDiscovery::discoverEndpoints(sdbusplus::message_t& msg) |
| 226 | { |
| 227 | MctpInfos addedInfos; |
| 228 | getAddedMctpInfos(msg, addedInfos); |
| 229 | addToExistingMctpInfos(addedInfos); |
| 230 | handleMctpEndpoints(addedInfos); |
| 231 | } |
| 232 | |
| 233 | void MctpDiscovery::removeEndpoints(sdbusplus::message_t&) |
| 234 | { |
| 235 | MctpInfos mctpInfos; |
| 236 | MctpInfos removedInfos; |
| 237 | getMctpInfos(mctpInfos); |
| 238 | removeFromExistingMctpInfos(mctpInfos, removedInfos); |
| 239 | handleRemovedMctpEndpoints(removedInfos); |
| 240 | } |
| 241 | |
| 242 | void MctpDiscovery::handleMctpEndpoints(const MctpInfos& mctpInfos) |
| 243 | { |
| 244 | for (const auto& handler : handlers) |
| 245 | { |
| 246 | if (handler) |
| 247 | { |
| 248 | handler->handleMctpEndpoints(mctpInfos); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | void MctpDiscovery::handleRemovedMctpEndpoints(const MctpInfos& mctpInfos) |
| 254 | { |
| 255 | for (const auto& handler : handlers) |
| 256 | { |
| 257 | if (handler) |
| 258 | { |
| 259 | handler->handleRemovedMctpEndpoints(mctpInfos); |
| 260 | } |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Andrew Jeffery | 27a022c | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 264 | } // namespace pldm |