Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "config.h" |
| 4 | |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 5 | #include "common/types.hpp" |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame^] | 6 | #include "pldmd/instance_id.hpp" |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 7 | #include "request.hpp" |
| 8 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 9 | #include <libpldm/base.h> |
| 10 | #include <libpldm/pldm.h> |
Manojkiran Eda | 9fffea2 | 2021-10-27 16:03:27 +0530 | [diff] [blame] | 11 | #include <sys/socket.h> |
| 12 | |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 13 | #include <function2/function2.hpp> |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 14 | #include <phosphor-logging/lg2.hpp> |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 15 | #include <sdbusplus/timer.hpp> |
| 16 | #include <sdeventplus/event.hpp> |
| 17 | #include <sdeventplus/source/event.hpp> |
| 18 | |
| 19 | #include <cassert> |
| 20 | #include <chrono> |
| 21 | #include <memory> |
| 22 | #include <tuple> |
| 23 | #include <unordered_map> |
| 24 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 25 | PHOSPHOR_LOG2_USING; |
| 26 | |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 27 | namespace pldm |
| 28 | { |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 29 | namespace requester |
| 30 | { |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 31 | /** @struct RequestKey |
| 32 | * |
| 33 | * RequestKey uniquely identifies the PLDM request message to match it with the |
| 34 | * response and a combination of MCTP endpoint ID, PLDM instance ID, PLDM type |
| 35 | * and PLDM command is the key. |
| 36 | */ |
| 37 | struct RequestKey |
| 38 | { |
| 39 | mctp_eid_t eid; //!< MCTP endpoint ID |
| 40 | uint8_t instanceId; //!< PLDM instance ID |
| 41 | uint8_t type; //!< PLDM type |
| 42 | uint8_t command; //!< PLDM command |
| 43 | |
| 44 | bool operator==(const RequestKey& e) const |
| 45 | { |
| 46 | return ((eid == e.eid) && (instanceId == e.instanceId) && |
| 47 | (type == e.type) && (command == e.command)); |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | /** @struct RequestKeyHasher |
| 52 | * |
| 53 | * This is a simple hash function, since the instance ID generator API |
| 54 | * generates unique instance IDs for MCTP endpoint ID. |
| 55 | */ |
| 56 | struct RequestKeyHasher |
| 57 | { |
| 58 | std::size_t operator()(const RequestKey& key) const |
| 59 | { |
| 60 | return (key.eid << 24 | key.instanceId << 16 | key.type << 8 | |
| 61 | key.command); |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | using ResponseHandler = fu2::unique_function<void( |
| 66 | mctp_eid_t eid, const pldm_msg* response, size_t respMsgLen)>; |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 67 | |
| 68 | /** @class Handler |
| 69 | * |
| 70 | * This class handles the lifecycle of the PLDM request message based on the |
| 71 | * instance ID expiration interval, number of request retries and the timeout |
| 72 | * waiting for a response. The registered response handlers are invoked with |
| 73 | * response once the PLDM responder sends the response. If no response is |
| 74 | * received within the instance ID expiration interval or any other failure the |
| 75 | * response handler is invoked with the empty response. |
| 76 | * |
| 77 | * @tparam RequestInterface - Request class type |
| 78 | */ |
| 79 | template <class RequestInterface> |
| 80 | class Handler |
| 81 | { |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 82 | public: |
| 83 | Handler() = delete; |
| 84 | Handler(const Handler&) = delete; |
| 85 | Handler(Handler&&) = delete; |
| 86 | Handler& operator=(const Handler&) = delete; |
| 87 | Handler& operator=(Handler&&) = delete; |
| 88 | ~Handler() = default; |
| 89 | |
| 90 | /** @brief Constructor |
| 91 | * |
| 92 | * @param[in] fd - fd of MCTP communications socket |
| 93 | * @param[in] event - reference to PLDM daemon's main event loop |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame^] | 94 | * @param[in] instanceIdDb - reference to an InstanceIdDb |
Manojkiran Eda | 9fffea2 | 2021-10-27 16:03:27 +0530 | [diff] [blame] | 95 | * @param[in] currentSendbuffSize - current send buffer size |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 96 | * @param[in] verbose - verbose tracing flag |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 97 | * @param[in] instanceIdExpiryInterval - instance ID expiration interval |
| 98 | * @param[in] numRetries - number of request retries |
| 99 | * @param[in] responseTimeOut - time to wait between each retry |
| 100 | */ |
| 101 | explicit Handler( |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame^] | 102 | int fd, sdeventplus::Event& event, pldm::InstanceIdDb& instanceIdDb, |
Manojkiran Eda | 9fffea2 | 2021-10-27 16:03:27 +0530 | [diff] [blame] | 103 | int currentSendbuffSize, bool verbose, |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 104 | std::chrono::seconds instanceIdExpiryInterval = |
| 105 | std::chrono::seconds(INSTANCE_ID_EXPIRATION_INTERVAL), |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 106 | uint8_t numRetries = static_cast<uint8_t>(NUMBER_OF_REQUEST_RETRIES), |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 107 | std::chrono::milliseconds responseTimeOut = |
| 108 | std::chrono::milliseconds(RESPONSE_TIME_OUT)) : |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 109 | fd(fd), |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame^] | 110 | event(event), instanceIdDb(instanceIdDb), |
Manojkiran Eda | 9fffea2 | 2021-10-27 16:03:27 +0530 | [diff] [blame] | 111 | currentSendbuffSize(currentSendbuffSize), verbose(verbose), |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 112 | instanceIdExpiryInterval(instanceIdExpiryInterval), |
| 113 | numRetries(numRetries), responseTimeOut(responseTimeOut) |
| 114 | {} |
| 115 | |
| 116 | /** @brief Register a PLDM request message |
| 117 | * |
| 118 | * @param[in] eid - endpoint ID of the remote MCTP endpoint |
| 119 | * @param[in] instanceId - instance ID to match request and response |
| 120 | * @param[in] type - PLDM type |
| 121 | * @param[in] command - PLDM command |
| 122 | * @param[in] requestMsg - PLDM request message |
| 123 | * @param[in] responseHandler - Response handler for this request |
| 124 | * |
| 125 | * @return return PLDM_SUCCESS on success and PLDM_ERROR otherwise |
| 126 | */ |
| 127 | int registerRequest(mctp_eid_t eid, uint8_t instanceId, uint8_t type, |
| 128 | uint8_t command, pldm::Request&& requestMsg, |
| 129 | ResponseHandler&& responseHandler) |
| 130 | { |
| 131 | RequestKey key{eid, instanceId, type, command}; |
| 132 | |
| 133 | auto instanceIdExpiryCallBack = [key, this](void) { |
| 134 | if (this->handlers.contains(key)) |
| 135 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 136 | error( |
| 137 | "Response not received for the request, instance ID expired. EID = {EID} INSTANCE_ID = {INST_ID} TYPE = {REQ_KEY_TYPE} COMMAND = {REQ_KEY_CMD}", |
| 138 | "EID", (unsigned)key.eid, "INST_ID", |
| 139 | (unsigned)key.instanceId, "REQ_KEY_TYPE", |
| 140 | (unsigned)key.type, "REQ_KEY_CMD", (unsigned)key.command); |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 141 | auto& [request, responseHandler, |
| 142 | timerInstance] = this->handlers[key]; |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 143 | request->stop(); |
| 144 | auto rc = timerInstance->stop(); |
| 145 | if (rc) |
| 146 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 147 | error( |
| 148 | "Failed to stop the instance ID expiry timer. RC = {RC}", |
| 149 | "RC", static_cast<int>(rc)); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 150 | } |
| 151 | // Call response handler with an empty response to indicate no |
| 152 | // response |
| 153 | responseHandler(key.eid, nullptr, 0); |
| 154 | this->removeRequestContainer.emplace( |
| 155 | key, std::make_unique<sdeventplus::source::Defer>( |
| 156 | event, std::bind(&Handler::removeRequestEntry, |
| 157 | this, key))); |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | // This condition is not possible, if a response is received |
| 162 | // before the instance ID expiry, then the response handler |
| 163 | // is executed and the entry will be removed. |
| 164 | assert(false); |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | auto request = std::make_unique<RequestInterface>( |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 169 | fd, eid, event, std::move(requestMsg), numRetries, responseTimeOut, |
Manojkiran Eda | 9fffea2 | 2021-10-27 16:03:27 +0530 | [diff] [blame] | 170 | currentSendbuffSize, verbose); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 171 | auto timer = std::make_unique<phosphor::Timer>( |
| 172 | event.get(), instanceIdExpiryCallBack); |
| 173 | |
| 174 | auto rc = request->start(); |
Tom Joseph | a5ed658 | 2021-06-17 22:08:47 -0700 | [diff] [blame] | 175 | if (rc) |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 176 | { |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame^] | 177 | instanceIdDb.free(eid, instanceId); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 178 | error("Failure to send the PLDM request message"); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 179 | return rc; |
| 180 | } |
| 181 | |
| 182 | try |
| 183 | { |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 184 | timer->start(duration_cast<std::chrono::microseconds>( |
| 185 | instanceIdExpiryInterval)); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 186 | } |
| 187 | catch (const std::runtime_error& e) |
| 188 | { |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame^] | 189 | instanceIdDb.free(eid, instanceId); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 190 | error( |
| 191 | "Failed to start the instance ID expiry timer. RC = {ERR_EXCEP}", |
| 192 | "ERR_EXCEP", e.what()); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 193 | return PLDM_ERROR; |
| 194 | } |
| 195 | |
| 196 | handlers.emplace(key, std::make_tuple(std::move(request), |
| 197 | std::move(responseHandler), |
| 198 | std::move(timer))); |
| 199 | return rc; |
| 200 | } |
| 201 | |
| 202 | /** @brief Handle PLDM response message |
| 203 | * |
| 204 | * @param[in] eid - endpoint ID of the remote MCTP endpoint |
| 205 | * @param[in] instanceId - instance ID to match request and response |
| 206 | * @param[in] type - PLDM type |
| 207 | * @param[in] command - PLDM command |
| 208 | * @param[in] response - PLDM response message |
| 209 | * @param[in] respMsgLen - length of the response message |
| 210 | */ |
| 211 | void handleResponse(mctp_eid_t eid, uint8_t instanceId, uint8_t type, |
| 212 | uint8_t command, const pldm_msg* response, |
| 213 | size_t respMsgLen) |
| 214 | { |
| 215 | RequestKey key{eid, instanceId, type, command}; |
| 216 | if (handlers.contains(key)) |
| 217 | { |
| 218 | auto& [request, responseHandler, timerInstance] = handlers[key]; |
| 219 | request->stop(); |
| 220 | auto rc = timerInstance->stop(); |
| 221 | if (rc) |
| 222 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 223 | error("Failed to stop the instance ID expiry timer. RC = {RC}", |
| 224 | "RC", static_cast<int>(rc)); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 225 | } |
| 226 | responseHandler(eid, response, respMsgLen); |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame^] | 227 | instanceIdDb.free(key.eid, key.instanceId); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 228 | handlers.erase(key); |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | // Got a response for a PLDM request message not registered with the |
| 233 | // request handler, so freeing up the instance ID, this can be other |
| 234 | // OpenBMC applications relying on PLDM D-Bus apis like |
| 235 | // openpower-occ-control and softoff |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame^] | 236 | instanceIdDb.free(key.eid, key.instanceId); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
| 240 | private: |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 241 | int fd; //!< file descriptor of MCTP communications socket |
| 242 | sdeventplus::Event& event; //!< reference to PLDM daemon's main event loop |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame^] | 243 | pldm::InstanceIdDb& instanceIdDb; //!< reference to an InstanceIdDb |
| 244 | int currentSendbuffSize; //!< current Send Buffer size |
| 245 | bool verbose; //!< verbose tracing flag |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 246 | std::chrono::seconds |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame^] | 247 | instanceIdExpiryInterval; //!< Instance ID expiration interval |
| 248 | uint8_t numRetries; //!< number of request retries |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 249 | std::chrono::milliseconds |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame^] | 250 | responseTimeOut; //!< time to wait between each retry |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 251 | |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 252 | /** @brief Container for storing the details of the PLDM request |
| 253 | * message, handler for the corresponding PLDM response and the |
| 254 | * timer object for the Instance ID expiration |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 255 | */ |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 256 | using RequestValue = |
| 257 | std::tuple<std::unique_ptr<RequestInterface>, ResponseHandler, |
| 258 | std::unique_ptr<phosphor::Timer>>; |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 259 | |
| 260 | /** @brief Container for storing the PLDM request entries */ |
| 261 | std::unordered_map<RequestKey, RequestValue, RequestKeyHasher> handlers; |
| 262 | |
| 263 | /** @brief Container to store information about the request entries to be |
| 264 | * removed after the instance ID timer expires |
| 265 | */ |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 266 | std::unordered_map<RequestKey, std::unique_ptr<sdeventplus::source::Defer>, |
| 267 | RequestKeyHasher> |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 268 | removeRequestContainer; |
| 269 | |
| 270 | /** @brief Remove request entry for which the instance ID expired |
| 271 | * |
| 272 | * @param[in] key - key for the Request |
| 273 | */ |
| 274 | void removeRequestEntry(RequestKey key) |
| 275 | { |
| 276 | if (removeRequestContainer.contains(key)) |
| 277 | { |
| 278 | removeRequestContainer[key].reset(); |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame^] | 279 | instanceIdDb.free(key.eid, key.instanceId); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 280 | handlers.erase(key); |
| 281 | removeRequestContainer.erase(key); |
| 282 | } |
| 283 | } |
| 284 | }; |
| 285 | |
| 286 | } // namespace requester |
| 287 | |
| 288 | } // namespace pldm |