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 | |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 8 | #include <phosphor-logging/lg2.hpp> |
| 9 | |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 10 | #include <algorithm> |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 11 | #include <fstream> |
| 12 | #include <iostream> |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 13 | #include <map> |
| 14 | #include <string> |
| 15 | #include <string_view> |
| 16 | #include <vector> |
| 17 | |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 18 | using namespace sdbusplus::bus::match::rules; |
| 19 | |
| 20 | PHOSPHOR_LOG2_USING; |
| 21 | |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 22 | namespace pldm |
| 23 | { |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 24 | MctpDiscovery::MctpDiscovery( |
Patrick Williams | ba741d5 | 2024-05-08 02:32:10 -0500 | [diff] [blame] | 25 | sdbusplus::bus_t& bus, |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 26 | std::initializer_list<MctpDiscoveryHandlerIntf*> list) : |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 27 | bus(bus), mctpEndpointAddedSignal( |
| 28 | bus, interfacesAdded(MCTPPath), |
| 29 | std::bind_front(&MctpDiscovery::discoverEndpoints, this)), |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 30 | mctpEndpointRemovedSignal( |
| 31 | bus, interfacesRemoved(MCTPPath), |
| 32 | std::bind_front(&MctpDiscovery::removeEndpoints, this)), |
| 33 | handlers(list) |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 34 | { |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 35 | getMctpInfos(existingMctpInfos); |
| 36 | handleMctpEndpoints(existingMctpInfos); |
| 37 | } |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 38 | |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 39 | void MctpDiscovery::getMctpInfos(MctpInfos& mctpInfos) |
| 40 | { |
| 41 | // Find all implementations of the MCTP Endpoint interface |
| 42 | pldm::utils::GetSubTreeResponse mapperResponse; |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 43 | try |
| 44 | { |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 45 | mapperResponse = pldm::utils::DBusHandler().getSubtree( |
| 46 | MCTPPath, 0, std::vector<std::string>({MCTPInterface})); |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 47 | } |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 48 | catch (const sdbusplus::exception_t& e) |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 49 | { |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 50 | error( |
| 51 | "Failed to getSubtree call at path '{PATH}' and interface '{INTERFACE}', error - {ERROR} ", |
| 52 | "ERROR", e, "PATH", MCTPPath, "INTERFACE", MCTPInterface); |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 53 | return; |
| 54 | } |
| 55 | |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 56 | for (const auto& [path, services] : mapperResponse) |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 57 | { |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 58 | for (const auto& serviceIter : services) |
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 | const std::string& service = serviceIter.first; |
| 61 | try |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 62 | { |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 63 | auto properties = |
| 64 | pldm::utils::DBusHandler().getDbusPropertiesVariant( |
| 65 | service.c_str(), path.c_str(), MCTPInterface); |
| 66 | |
| 67 | if (properties.contains("NetworkId") && |
| 68 | properties.contains("EID") && |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 69 | properties.contains("SupportedMessageTypes")) |
| 70 | { |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 71 | auto networkId = |
| 72 | std::get<NetworkId>(properties.at("NetworkId")); |
Delphine CC Chiu | c00594e | 2023-04-19 11:13:17 +0800 | [diff] [blame] | 73 | auto eid = std::get<mctp_eid_t>(properties.at("EID")); |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 74 | auto types = std::get<std::vector<uint8_t>>( |
| 75 | properties.at("SupportedMessageTypes")); |
| 76 | if (std::find(types.begin(), types.end(), mctpTypePLDM) != |
| 77 | types.end()) |
| 78 | { |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 79 | info( |
| 80 | "Adding Endpoint networkId '{NETWORK}' and EID '{EID}'", |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 81 | "NETWORK", networkId, "EID", eid); |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 82 | mctpInfos.emplace_back( |
| 83 | MctpInfo(eid, emptyUUID, "", networkId)); |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | } |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 87 | catch (const sdbusplus::exception_t& e) |
| 88 | { |
| 89 | error( |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 90 | "Error reading MCTP Endpoint property at path '{PATH}' and service '{SERVICE}', error - {ERROR}", |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 91 | "ERROR", e, "SERVICE", service, "PATH", path); |
| 92 | return; |
| 93 | } |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 98 | void MctpDiscovery::getAddedMctpInfos(sdbusplus::message_t& msg, |
| 99 | MctpInfos& mctpInfos) |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 100 | { |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 101 | using ObjectPath = sdbusplus::message::object_path; |
| 102 | ObjectPath objPath; |
| 103 | using Property = std::string; |
| 104 | using PropertyMap = std::map<Property, dbus::Value>; |
| 105 | std::map<std::string, PropertyMap> interfaces; |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 106 | |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 107 | try |
| 108 | { |
| 109 | msg.read(objPath, interfaces); |
| 110 | } |
| 111 | catch (const sdbusplus::exception_t& e) |
| 112 | { |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 113 | error( |
| 114 | "Error reading MCTP Endpoint added interface message, error - {ERROR}", |
| 115 | "ERROR", e); |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 116 | return; |
| 117 | } |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 118 | |
| 119 | for (const auto& [intfName, properties] : interfaces) |
| 120 | { |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 121 | if (intfName == MCTPInterface) |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 122 | { |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 123 | if (properties.contains("NetworkId") && |
| 124 | properties.contains("EID") && |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 125 | properties.contains("SupportedMessageTypes")) |
| 126 | { |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 127 | auto networkId = |
| 128 | std::get<NetworkId>(properties.at("NetworkId")); |
Dung Cao | 66794d0 | 2023-10-25 04:06:23 +0000 | [diff] [blame] | 129 | auto eid = std::get<mctp_eid_t>(properties.at("EID")); |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 130 | auto types = std::get<std::vector<uint8_t>>( |
| 131 | properties.at("SupportedMessageTypes")); |
| 132 | if (std::find(types.begin(), types.end(), mctpTypePLDM) != |
| 133 | types.end()) |
| 134 | { |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 135 | info( |
| 136 | "Adding Endpoint networkId '{NETWORK}' and EID '{EID}'", |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 137 | "NETWORK", networkId, "EID", eid); |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 138 | mctpInfos.emplace_back( |
| 139 | MctpInfo(eid, emptyUUID, "", networkId)); |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 144 | } |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 145 | |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 146 | void MctpDiscovery::addToExistingMctpInfos(const MctpInfos& addedInfos) |
| 147 | { |
| 148 | for (const auto& mctpInfo : addedInfos) |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 149 | { |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 150 | if (std::find(existingMctpInfos.begin(), existingMctpInfos.end(), |
| 151 | mctpInfo) == existingMctpInfos.end()) |
| 152 | { |
| 153 | existingMctpInfos.emplace_back(mctpInfo); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void MctpDiscovery::removeFromExistingMctpInfos(MctpInfos& mctpInfos, |
| 159 | MctpInfos& removedInfos) |
| 160 | { |
| 161 | for (const auto& mctpInfo : existingMctpInfos) |
| 162 | { |
| 163 | if (std::find(mctpInfos.begin(), mctpInfos.end(), mctpInfo) == |
| 164 | mctpInfos.end()) |
| 165 | { |
| 166 | removedInfos.emplace_back(mctpInfo); |
| 167 | } |
| 168 | } |
| 169 | for (const auto& mctpInfo : removedInfos) |
| 170 | { |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 171 | info("Removing Endpoint networkId '{NETWORK}' and EID '{EID}'", |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 172 | "NETWORK", std::get<3>(mctpInfo), "EID", std::get<0>(mctpInfo)); |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 173 | existingMctpInfos.erase(std::remove(existingMctpInfos.begin(), |
| 174 | existingMctpInfos.end(), mctpInfo), |
| 175 | existingMctpInfos.end()); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void MctpDiscovery::discoverEndpoints(sdbusplus::message_t& msg) |
| 180 | { |
| 181 | MctpInfos addedInfos; |
| 182 | getAddedMctpInfos(msg, addedInfos); |
| 183 | addToExistingMctpInfos(addedInfos); |
| 184 | handleMctpEndpoints(addedInfos); |
| 185 | } |
| 186 | |
| 187 | void MctpDiscovery::removeEndpoints(sdbusplus::message_t&) |
| 188 | { |
| 189 | MctpInfos mctpInfos; |
| 190 | MctpInfos removedInfos; |
| 191 | getMctpInfos(mctpInfos); |
| 192 | removeFromExistingMctpInfos(mctpInfos, removedInfos); |
| 193 | handleRemovedMctpEndpoints(removedInfos); |
| 194 | } |
| 195 | |
| 196 | void MctpDiscovery::handleMctpEndpoints(const MctpInfos& mctpInfos) |
| 197 | { |
| 198 | for (const auto& handler : handlers) |
| 199 | { |
| 200 | if (handler) |
| 201 | { |
| 202 | handler->handleMctpEndpoints(mctpInfos); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | void MctpDiscovery::handleRemovedMctpEndpoints(const MctpInfos& mctpInfos) |
| 208 | { |
| 209 | for (const auto& handler : handlers) |
| 210 | { |
| 211 | if (handler) |
| 212 | { |
| 213 | handler->handleRemovedMctpEndpoints(mctpInfos); |
| 214 | } |
Tom Joseph | fb3bc06 | 2021-08-17 07:48:11 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Andrew Jeffery | 27a022c | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 218 | } // namespace pldm |