1 #include "config.h"
2
3 #include "mctp_endpoint_discovery.hpp"
4
5 #include "common/types.hpp"
6 #include "common/utils.hpp"
7
8 #include <linux/mctp.h>
9
10 #include <phosphor-logging/lg2.hpp>
11
12 #include <algorithm>
13 #include <fstream>
14 #include <iostream>
15 #include <map>
16 #include <string>
17 #include <string_view>
18 #include <vector>
19
20 using namespace sdbusplus::bus::match::rules;
21
22 PHOSPHOR_LOG2_USING;
23
24 namespace pldm
25 {
MctpDiscovery(sdbusplus::bus_t & bus,std::initializer_list<MctpDiscoveryHandlerIntf * > list)26 MctpDiscovery::MctpDiscovery(
27 sdbusplus::bus_t& bus,
28 std::initializer_list<MctpDiscoveryHandlerIntf*> list) :
29 bus(bus),
30 mctpEndpointAddedSignal(
31 bus, interfacesAdded(MCTPPath),
32 [this](sdbusplus::message_t& msg) { this->discoverEndpoints(msg); }),
33 mctpEndpointRemovedSignal(
34 bus, interfacesRemoved(MCTPPath),
__anon124d2f790202(sdbusplus::message_t& msg) 35 [this](sdbusplus::message_t& msg) { this->removeEndpoints(msg); }),
36 mctpEndpointPropChangedSignal(
37 bus, propertiesChangedNamespace(MCTPPath, MCTPInterfaceCC),
__anon124d2f790302(sdbusplus::message_t& msg) 38 [this](sdbusplus::message_t& msg) { this->propertiesChangedCb(msg); }),
39 handlers(list)
40 {
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 }
53 handleMctpEndpoints(existingMctpInfos);
54 }
55
getMctpInfos(std::map<MctpInfo,Availability> & mctpInfoMap)56 void MctpDiscovery::getMctpInfos(std::map<MctpInfo, Availability>& mctpInfoMap)
57 {
58 // Find all implementations of the MCTP Endpoint interface
59 pldm::utils::GetSubTreeResponse mapperResponse;
60 try
61 {
62 mapperResponse = pldm::utils::DBusHandler().getSubtree(
63 MCTPPath, 0, std::vector<std::string>({MCTPEndpoint::interface}));
64 }
65 catch (const sdbusplus::exception_t& e)
66 {
67 error(
68 "Failed to getSubtree call at path '{PATH}' and interface '{INTERFACE}', error - {ERROR} ",
69 "ERROR", e, "PATH", MCTPPath, "INTERFACE", MCTPEndpoint::interface);
70 return;
71 }
72
73 for (const auto& [path, services] : mapperResponse)
74 {
75 for (const auto& serviceIter : services)
76 {
77 const std::string& service = serviceIter.first;
78 const MctpEndpointProps& epProps =
79 getMctpEndpointProps(service, path);
80 const UUID& uuid = getEndpointUUIDProp(service, path);
81 const Availability& availability =
82 getEndpointConnectivityProp(path);
83 auto types = std::get<MCTPMsgTypes>(epProps);
84 if (std::find(types.begin(), types.end(), mctpTypePLDM) !=
85 types.end())
86 {
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;
92 }
93 }
94 }
95 }
96
getMctpEndpointProps(const std::string & service,const std::string & path)97 MctpEndpointProps MctpDiscovery::getMctpEndpointProps(
98 const std::string& service, const std::string& path)
99 {
100 try
101 {
102 auto properties = pldm::utils::DBusHandler().getDbusPropertiesVariant(
103 service.c_str(), path.c_str(), MCTPEndpoint::interface);
104
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))
109 {
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));
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
getEndpointUUIDProp(const std::string & service,const std::string & path)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
getEndpointConnectivityProp(const std::string & path)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
getAddedMctpInfos(sdbusplus::message_t & msg,MctpInfos & mctpInfos)177 void MctpDiscovery::getAddedMctpInfos(sdbusplus::message_t& msg,
178 MctpInfos& mctpInfos)
179 {
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;
185 std::string uuid = emptyUUID;
186
187 try
188 {
189 msg.read(objPath, interfaces);
190 }
191 catch (const sdbusplus::exception_t& e)
192 {
193 error(
194 "Error reading MCTP Endpoint added interface message, error - {ERROR}",
195 "ERROR", e);
196 return;
197 }
198 const Availability& availability = getEndpointConnectivityProp(objPath.str);
199
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
213 for (const auto& [intfName, properties] : interfaces)
214 {
215 if (intfName == MCTPEndpoint::interface)
216 {
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))
221 {
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));
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 }
237 if (std::find(types.begin(), types.end(), mctpTypePLDM) !=
238 types.end())
239 {
240 info(
241 "Adding Endpoint networkId '{NETWORK}' and EID '{EID}' UUID '{UUID}'",
242 "NETWORK", networkId, "EID", eid, "UUID", uuid);
243 auto mctpInfo =
244 MctpInfo(eid, uuid, "", networkId, std::nullopt);
245 searchConfigurationFor(pldm::utils::DBusHandler(),
246 mctpInfo);
247 mctpInfos.emplace_back(std::move(mctpInfo));
248 }
249 }
250 }
251 }
252 }
253
addToExistingMctpInfos(const MctpInfos & addedInfos)254 void MctpDiscovery::addToExistingMctpInfos(const MctpInfos& addedInfos)
255 {
256 for (const auto& mctpInfo : addedInfos)
257 {
258 if (std::find(existingMctpInfos.begin(), existingMctpInfos.end(),
259 mctpInfo) == existingMctpInfos.end())
260 {
261 existingMctpInfos.emplace_back(mctpInfo);
262 }
263 }
264 }
265
removeFromExistingMctpInfos(MctpInfos & mctpInfos,MctpInfos & removedInfos)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 {
279 info("Removing Endpoint networkId '{NETWORK}' and EID '{EID}'",
280 "NETWORK", std::get<3>(mctpInfo), "EID", std::get<0>(mctpInfo));
281 existingMctpInfos.erase(std::remove(existingMctpInfos.begin(),
282 existingMctpInfos.end(), mctpInfo),
283 existingMctpInfos.end());
284 }
285 }
286
propertiesChangedCb(sdbusplus::message_t & msg)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 {
319 service = pldm::utils::DBusHandler().getService(
320 objPath.c_str(), MCTPEndpoint::interface);
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, "",
332 std::get<NetworkId>(epProps), std::nullopt);
333 searchConfigurationFor(pldm::utils::DBusHandler(), mctpInfo);
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
discoverEndpoints(sdbusplus::message_t & msg)357 void MctpDiscovery::discoverEndpoints(sdbusplus::message_t& msg)
358 {
359 MctpInfos addedInfos;
360 getAddedMctpInfos(msg, addedInfos);
361 addToExistingMctpInfos(addedInfos);
362 handleMctpEndpoints(addedInfos);
363 }
364
removeEndpoints(sdbusplus::message_t &)365 void MctpDiscovery::removeEndpoints(sdbusplus::message_t&)
366 {
367 MctpInfos mctpInfos;
368 MctpInfos removedInfos;
369 std::map<MctpInfo, Availability> currentMctpInfoMap;
370 getMctpInfos(currentMctpInfoMap);
371 for (const auto& mapIt : currentMctpInfoMap)
372 {
373 mctpInfos.push_back(mapIt.first);
374 }
375 removeFromExistingMctpInfos(mctpInfos, removedInfos);
376 handleRemovedMctpEndpoints(removedInfos);
377 removeConfigs(removedInfos);
378 }
379
handleMctpEndpoints(const MctpInfos & mctpInfos)380 void MctpDiscovery::handleMctpEndpoints(const MctpInfos& mctpInfos)
381 {
382 for (const auto& handler : handlers)
383 {
384 if (handler)
385 {
386 handler->handleConfigurations(configurations);
387 handler->handleMctpEndpoints(mctpInfos);
388 }
389 }
390 }
391
handleRemovedMctpEndpoints(const MctpInfos & mctpInfos)392 void MctpDiscovery::handleRemovedMctpEndpoints(const MctpInfos& mctpInfos)
393 {
394 for (const auto& handler : handlers)
395 {
396 if (handler)
397 {
398 handler->handleRemovedMctpEndpoints(mctpInfos);
399 }
400 }
401 }
402
updateMctpEndpointAvailability(const MctpInfo & mctpInfo,Availability availability)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
getNameFromProperties(const utils::PropertyMap & properties)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
constructMctpReactorObjectPath(const MctpInfo & mctpInfo)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
searchConfigurationFor(const pldm::utils::DBusHandler & handler,MctpInfo & mctpInfo)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
removeConfigs(const MctpInfos & removedInfos)503 void MctpDiscovery::removeConfigs(const MctpInfos& removedInfos)
504 {
505 for (const auto& mctpInfo : removedInfos)
506 {
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;
517 });
518 }
519 }
520
521 } // namespace pldm
522