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