John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 1 | |
| 2 | #include "estoraged.hpp" |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 3 | #include "getConfig.hpp" |
| 4 | #include "util.hpp" |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 5 | |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 6 | #include <boost/asio/deadline_timer.hpp> |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 7 | #include <boost/asio/io_context.hpp> |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 8 | #include <boost/asio/post.hpp> |
| 9 | #include <boost/container/flat_map.hpp> |
| 10 | #include <boost/container/throw_exception.hpp> |
John Edward Broadbent | 4e13b0a | 2021-11-15 15:21:59 -0800 | [diff] [blame] | 11 | #include <phosphor-logging/lg2.hpp> |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 12 | #include <sdbusplus/asio/connection.hpp> |
| 13 | #include <sdbusplus/asio/object_server.hpp> |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 14 | #include <sdbusplus/bus.hpp> |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 15 | #include <sdbusplus/bus/match.hpp> |
John Edward Broadbent | e35e736 | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 16 | #include <util.hpp> |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 17 | |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 18 | #include <cstdlib> |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 19 | #include <filesystem> |
| 20 | #include <iostream> |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 21 | #include <memory> |
Tom Tung | 043af59 | 2023-11-24 13:37:05 +0800 | [diff] [blame] | 22 | #include <optional> |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 25 | /* |
| 26 | * Get the configuration objects from Entity Manager and create new D-Bus |
| 27 | * objects for each one. This function can be called multiple times, in case |
| 28 | * new configuration objects show up later. |
| 29 | * |
| 30 | * Note: Currently, eStoraged can only support 1 eMMC device. |
| 31 | * Additional changes will be needed to support more than 1 eMMC, or to support |
| 32 | * more types of storage devices. |
| 33 | */ |
| 34 | void createStorageObjects( |
John Wedig | 61cf426 | 2023-03-17 14:32:04 -0700 | [diff] [blame] | 35 | sdbusplus::asio::object_server& objectServer, |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 36 | boost::container::flat_map< |
| 37 | std::string, std::unique_ptr<estoraged::EStoraged>>& storageObjects, |
| 38 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 39 | { |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 40 | auto getter = std::make_shared<estoraged::GetStorageConfiguration>( |
| 41 | dbusConnection, |
John Wedig | 61cf426 | 2023-03-17 14:32:04 -0700 | [diff] [blame] | 42 | [&objectServer, &storageObjects]( |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 43 | const estoraged::ManagedStorageType& storageConfigurations) { |
Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame^] | 44 | size_t numConfigObj = storageConfigurations.size(); |
| 45 | if (numConfigObj > 1) |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 46 | { |
Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame^] | 47 | lg2::error( |
| 48 | "eStoraged can only manage 1 eMMC device; found {NUM}", |
| 49 | "NUM", numConfigObj, "REDFISH_MESSAGE_ID", |
| 50 | std::string("OpenBMC.0.1.CreateStorageObjectsFail")); |
| 51 | return; |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame^] | 54 | for (const std::pair<sdbusplus::message::object_path, |
| 55 | estoraged::StorageData>& storage : |
| 56 | storageConfigurations) |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 57 | { |
Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame^] | 58 | const std::string& path = storage.first.str; |
| 59 | |
| 60 | if (storageObjects.find(path) != storageObjects.end()) |
| 61 | { |
| 62 | /* |
| 63 | * We've already created this object, or at least |
| 64 | * attempted to. |
| 65 | */ |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | /* Get the properties from the config object. */ |
| 70 | const estoraged::StorageData& data = storage.second; |
| 71 | |
| 72 | /* Look for the device file. */ |
| 73 | const std::filesystem::path blockDevDir{"/sys/block"}; |
| 74 | auto deviceInfo = |
| 75 | estoraged::util::findDevice(data, blockDevDir); |
| 76 | if (!deviceInfo) |
| 77 | { |
| 78 | lg2::error( |
| 79 | "Device not found for path {PATH}", "PATH", path, |
| 80 | "REDFISH_MESSAGE_ID", |
| 81 | std::string("OpenBMC.0.1.CreateStorageObjectsFail")); |
| 82 | /* |
| 83 | * Set a NULL pointer as a placeholder, so that we don't |
| 84 | * try and fail again later. |
| 85 | */ |
| 86 | storageObjects[path] = nullptr; |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | std::filesystem::path deviceFile = |
| 91 | std::move(deviceInfo->deviceFile); |
| 92 | std::filesystem::path sysfsDir = |
| 93 | std::move(deviceInfo->sysfsDir); |
| 94 | std::string luksName = std::move(deviceInfo->luksName); |
| 95 | std::string locationCode = std::move(deviceInfo->locationCode); |
| 96 | uint64_t eraseMaxGeometry = deviceInfo->eraseMaxGeometry; |
| 97 | uint64_t eraseMinGeometry = deviceInfo->eraseMinGeometry; |
| 98 | |
| 99 | uint64_t size = |
| 100 | estoraged::util::findSizeOfBlockDevice(deviceFile); |
| 101 | |
| 102 | uint8_t lifeleft = |
| 103 | estoraged::util::findPredictedMediaLifeLeftPercent( |
| 104 | sysfsDir); |
| 105 | std::string partNumber = |
| 106 | estoraged::util::getPartNumber(sysfsDir); |
| 107 | std::string serialNumber = |
| 108 | estoraged::util::getSerialNumber(sysfsDir); |
| 109 | const std::string& driveType = deviceInfo->driveType; |
| 110 | const std::string& driveProtocol = deviceInfo->driveProtocol; |
| 111 | /* Create the storage object. */ |
| 112 | storageObjects[path] = std::make_unique<estoraged::EStoraged>( |
| 113 | objectServer, path, deviceFile, luksName, size, lifeleft, |
| 114 | partNumber, serialNumber, locationCode, eraseMaxGeometry, |
| 115 | eraseMinGeometry, driveType, driveProtocol); |
| 116 | lg2::info("Created eStoraged object for path {PATH}", "PATH", |
| 117 | path, "REDFISH_MESSAGE_ID", |
| 118 | std::string("OpenBMC.0.1.CreateStorageObjects")); |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 119 | } |
Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame^] | 120 | }); |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 121 | getter->getConfiguration(); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 122 | } |
| 123 | |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 124 | int main(void) |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 125 | { |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 126 | try |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 127 | { |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 128 | // setup connection to dbus |
| 129 | boost::asio::io_context io; |
| 130 | auto conn = std::make_shared<sdbusplus::asio::connection>(io); |
| 131 | // request D-Bus server name. |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 132 | conn->request_name("xyz.openbmc_project.eStoraged"); |
| 133 | sdbusplus::asio::object_server server(conn); |
| 134 | boost::container::flat_map<std::string, |
| 135 | std::unique_ptr<estoraged::EStoraged>> |
| 136 | storageObjects; |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 137 | |
Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame^] | 138 | boost::asio::post(io, [&]() { |
| 139 | createStorageObjects(server, storageObjects, conn); |
| 140 | }); |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * Set up an event handler to process any new configuration objects |
| 144 | * that show up later. |
| 145 | */ |
| 146 | boost::asio::deadline_timer filterTimer(io); |
Patrick Williams | e3ef765 | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 147 | std::function<void(sdbusplus::message_t&)> eventHandler = |
| 148 | [&](sdbusplus::message_t& message) { |
Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame^] | 149 | if (message.is_method_error()) |
| 150 | { |
| 151 | lg2::error("eventHandler callback method error"); |
| 152 | return; |
| 153 | } |
| 154 | /* |
| 155 | * This implicitly cancels the timer, if it's already pending. |
| 156 | * If there's a burst of events within a short period, we want |
| 157 | * to handle them all at once. So, we will wait this long for no |
| 158 | * more events to occur, before processing them. |
| 159 | */ |
| 160 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); |
Patrick Williams | 04c28fa | 2023-05-10 07:51:24 -0500 | [diff] [blame] | 161 | |
Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame^] | 162 | filterTimer.async_wait( |
| 163 | [&](const boost::system::error_code& ec) { |
| 164 | if (ec == boost::asio::error::operation_aborted) |
| 165 | { |
| 166 | /* we were canceled */ |
| 167 | return; |
| 168 | } |
| 169 | if (ec) |
| 170 | { |
| 171 | lg2::error("timer error"); |
| 172 | return; |
| 173 | } |
| 174 | createStorageObjects(server, storageObjects, conn); |
| 175 | }); |
| 176 | }; |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 177 | |
Patrick Williams | e3ef765 | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 178 | auto match = std::make_unique<sdbusplus::bus::match_t>( |
| 179 | static_cast<sdbusplus::bus_t&>(*conn), |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 180 | "type='signal',member='PropertiesChanged',path_namespace='" + |
| 181 | std::string("/xyz/openbmc_project/inventory") + |
| 182 | "',arg0namespace='" + estoraged::emmcConfigInterface + "'", |
| 183 | eventHandler); |
| 184 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 185 | lg2::info("Storage management service is running", "REDFISH_MESSAGE_ID", |
| 186 | std::string("OpenBMC.1.0.ServiceStarted")); |
| 187 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 188 | io.run(); |
| 189 | return 0; |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 190 | } |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 191 | catch (const std::exception& e) |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 192 | { |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 193 | lg2::error(e.what(), "REDFISH_MESSAGE_ID", |
| 194 | std::string("OpenBMC.1.0.ServiceException")); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 195 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 196 | return 2; |
| 197 | } |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 198 | return 1; |
| 199 | } |