blob: 02c5d0214f8867084d3c61b533d56b60fd0d4800 [file] [log] [blame]
Tom Joseph74f27c72021-05-16 07:58:53 -07001#pragma once
2
Andrew Jeffery2abbce72023-10-18 10:17:35 +10303#include "common/instance_id.hpp"
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +10004#include "common/transport.hpp"
Tom Joseph74f27c72021-05-16 07:58:53 -07005#include "common/types.hpp"
Tom Joseph74f27c72021-05-16 07:58:53 -07006#include "request.hpp"
7
George Liuc453e162022-12-21 17:16:23 +08008#include <libpldm/base.h>
Manojkiran Eda9fffea22021-10-27 16:03:27 +05309#include <sys/socket.h>
10
Riya Dixit49cfb132023-03-02 04:26:53 -060011#include <phosphor-logging/lg2.hpp>
Tom Joseph74f27c72021-05-16 07:58:53 -070012#include <sdbusplus/timer.hpp>
13#include <sdeventplus/event.hpp>
14#include <sdeventplus/source/event.hpp>
15
16#include <cassert>
17#include <chrono>
Thu Nguyen4ddee3a2023-08-03 08:20:03 +070018#include <deque>
Patrick Williamscdbb9e22023-07-13 18:05:40 -050019#include <functional>
Tom Joseph74f27c72021-05-16 07:58:53 -070020#include <memory>
Thu Nguyen4ddee3a2023-08-03 08:20:03 +070021#include <mutex>
22#include <queue>
Tom Joseph74f27c72021-05-16 07:58:53 -070023#include <tuple>
24#include <unordered_map>
25
Riya Dixit49cfb132023-03-02 04:26:53 -060026PHOSPHOR_LOG2_USING;
27
Tom Joseph74f27c72021-05-16 07:58:53 -070028namespace pldm
29{
Tom Joseph74f27c72021-05-16 07:58:53 -070030namespace requester
31{
Tom Joseph74f27c72021-05-16 07:58:53 -070032/** @struct RequestKey
33 *
34 * RequestKey uniquely identifies the PLDM request message to match it with the
35 * response and a combination of MCTP endpoint ID, PLDM instance ID, PLDM type
36 * and PLDM command is the key.
37 */
38struct RequestKey
39{
40 mctp_eid_t eid; //!< MCTP endpoint ID
41 uint8_t instanceId; //!< PLDM instance ID
42 uint8_t type; //!< PLDM type
43 uint8_t command; //!< PLDM command
44
45 bool operator==(const RequestKey& e) const
46 {
47 return ((eid == e.eid) && (instanceId == e.instanceId) &&
48 (type == e.type) && (command == e.command));
49 }
50};
51
52/** @struct RequestKeyHasher
53 *
54 * This is a simple hash function, since the instance ID generator API
55 * generates unique instance IDs for MCTP endpoint ID.
56 */
57struct RequestKeyHasher
58{
59 std::size_t operator()(const RequestKey& key) const
60 {
61 return (key.eid << 24 | key.instanceId << 16 | key.type << 8 |
62 key.command);
63 }
64};
65
Thu Nguyen4ddee3a2023-08-03 08:20:03 +070066using ResponseHandler = std::function<void(
Tom Joseph74f27c72021-05-16 07:58:53 -070067 mctp_eid_t eid, const pldm_msg* response, size_t respMsgLen)>;
Tom Joseph74f27c72021-05-16 07:58:53 -070068
Thu Nguyen4ddee3a2023-08-03 08:20:03 +070069/** @struct RegisteredRequest
70 *
71 * This struct is used to store the registered request to one endpoint.
72 */
73struct RegisteredRequest
74{
75 RequestKey key; //!< Responder MCTP endpoint ID
76 std::vector<uint8_t> reqMsg; //!< Request messages queue
77 ResponseHandler responseHandler; //!< Waiting for response flag
78};
79
80/** @struct EndpointMessageQueue
81 *
82 * This struct is used to save the list of request messages of one endpoint and
83 * the existing of the request message to the endpoint with its' EID.
84 */
85struct EndpointMessageQueue
86{
87 mctp_eid_t eid; //!< Responder MCTP endpoint ID
88 std::deque<std::shared_ptr<RegisteredRequest>> requestQueue; //!< Queue
89 bool activeRequest; //!< Waiting for response flag
90
91 bool operator==(const mctp_eid_t& mctpEid) const
92 {
93 return (eid == mctpEid);
94 }
95};
96
Tom Joseph74f27c72021-05-16 07:58:53 -070097/** @class Handler
98 *
99 * This class handles the lifecycle of the PLDM request message based on the
100 * instance ID expiration interval, number of request retries and the timeout
101 * waiting for a response. The registered response handlers are invoked with
102 * response once the PLDM responder sends the response. If no response is
103 * received within the instance ID expiration interval or any other failure the
104 * response handler is invoked with the empty response.
105 *
106 * @tparam RequestInterface - Request class type
107 */
108template <class RequestInterface>
109class Handler
110{
Tom Joseph74f27c72021-05-16 07:58:53 -0700111 public:
112 Handler() = delete;
113 Handler(const Handler&) = delete;
114 Handler(Handler&&) = delete;
115 Handler& operator=(const Handler&) = delete;
116 Handler& operator=(Handler&&) = delete;
117 ~Handler() = default;
118
119 /** @brief Constructor
120 *
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000121 * @param[in] pldm_transport - PLDM requester
Tom Joseph74f27c72021-05-16 07:58:53 -0700122 * @param[in] event - reference to PLDM daemon's main event loop
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930123 * @param[in] instanceIdDb - reference to an InstanceIdDb
Tom Josephe5268cd2021-09-07 13:04:03 +0530124 * @param[in] verbose - verbose tracing flag
Tom Joseph74f27c72021-05-16 07:58:53 -0700125 * @param[in] instanceIdExpiryInterval - instance ID expiration interval
126 * @param[in] numRetries - number of request retries
127 * @param[in] responseTimeOut - time to wait between each retry
128 */
129 explicit Handler(
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000130 PldmTransport* pldmTransport, sdeventplus::Event& event,
131 pldm::InstanceIdDb& instanceIdDb, bool verbose,
Brad Bishop5079ac42021-08-19 18:35:06 -0400132 std::chrono::seconds instanceIdExpiryInterval =
133 std::chrono::seconds(INSTANCE_ID_EXPIRATION_INTERVAL),
Tom Joseph74f27c72021-05-16 07:58:53 -0700134 uint8_t numRetries = static_cast<uint8_t>(NUMBER_OF_REQUEST_RETRIES),
Brad Bishop5079ac42021-08-19 18:35:06 -0400135 std::chrono::milliseconds responseTimeOut =
136 std::chrono::milliseconds(RESPONSE_TIME_OUT)) :
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000137 pldmTransport(pldmTransport),
138 event(event), instanceIdDb(instanceIdDb), verbose(verbose),
Tom Joseph74f27c72021-05-16 07:58:53 -0700139 instanceIdExpiryInterval(instanceIdExpiryInterval),
140 numRetries(numRetries), responseTimeOut(responseTimeOut)
141 {}
142
Thu Nguyen4ddee3a2023-08-03 08:20:03 +0700143 void instanceIdExpiryCallBack(RequestKey key)
144 {
145 auto eid = key.eid;
146 if (this->handlers.contains(key))
147 {
148 error("The eid:InstanceID {EID}:{IID} is using.", "EID",
149 (unsigned)key.eid, "IID", (unsigned)key.instanceId);
150 auto& [request, responseHandler,
151 timerInstance] = this->handlers[key];
152 request->stop();
153 auto rc = timerInstance->stop();
154 if (rc)
155 {
156 error("Failed to stop the instance ID expiry timer. RC = {RC}",
157 "RC", static_cast<int>(rc));
158 }
159 // Call response handler with an empty response to indicate no
160 // response
161 responseHandler(eid, nullptr, 0);
162 this->removeRequestContainer.emplace(
163 key,
164 std::make_unique<sdeventplus::source::Defer>(
165 event, std::bind(&Handler::removeRequestEntry, this, key)));
166 endpointMessageQueues[eid]->activeRequest = false;
167
168 /* try to send new request if the endpoint is free */
169 pollEndpointQueue(eid);
170 }
171 else
172 {
173 // This condition is not possible, if a response is received
174 // before the instance ID expiry, then the response handler
175 // is executed and the entry will be removed.
176 assert(false);
177 }
178 }
179
180 /** @brief Send the remaining PLDM request messages in endpoint queue
181 *
182 * @param[in] eid - endpoint ID of the remote MCTP endpoint
183 */
184 int pollEndpointQueue(mctp_eid_t eid)
185 {
186 if (endpointMessageQueues[eid]->activeRequest ||
187 endpointMessageQueues[eid]->requestQueue.empty())
188 {
189 return PLDM_SUCCESS;
190 }
191
192 endpointMessageQueues[eid]->activeRequest = true;
193 auto requestMsg = endpointMessageQueues[eid]->requestQueue.front();
194 endpointMessageQueues[eid]->requestQueue.pop_front();
195
196 auto request = std::make_unique<RequestInterface>(
197 pldmTransport, requestMsg->key.eid, event,
198 std::move(requestMsg->reqMsg), numRetries, responseTimeOut,
199 verbose);
200 auto timer = std::make_unique<phosphor::Timer>(
201 event.get(), std::bind(&Handler::instanceIdExpiryCallBack, this,
202 requestMsg->key));
203
204 auto rc = request->start();
205 if (rc)
206 {
207 instanceIdDb.free(requestMsg->key.eid, requestMsg->key.instanceId);
208 error("Failure to send the PLDM request message");
209 endpointMessageQueues[eid]->activeRequest = false;
210 return rc;
211 }
212
213 try
214 {
215 timer->start(duration_cast<std::chrono::microseconds>(
216 instanceIdExpiryInterval));
217 }
218 catch (const std::runtime_error& e)
219 {
220 instanceIdDb.free(requestMsg->key.eid, requestMsg->key.instanceId);
221 error(
222 "Failed to start the instance ID expiry timer. RC = {ERR_EXCEP}",
223 "ERR_EXCEP", e.what());
224 endpointMessageQueues[eid]->activeRequest = false;
225 return PLDM_ERROR;
226 }
227
228 handlers.emplace(requestMsg->key,
229 std::make_tuple(std::move(request),
230 std::move(requestMsg->responseHandler),
231 std::move(timer)));
232 return PLDM_SUCCESS;
233 }
234
Tom Joseph74f27c72021-05-16 07:58:53 -0700235 /** @brief Register a PLDM request message
236 *
237 * @param[in] eid - endpoint ID of the remote MCTP endpoint
238 * @param[in] instanceId - instance ID to match request and response
239 * @param[in] type - PLDM type
240 * @param[in] command - PLDM command
241 * @param[in] requestMsg - PLDM request message
242 * @param[in] responseHandler - Response handler for this request
243 *
244 * @return return PLDM_SUCCESS on success and PLDM_ERROR otherwise
245 */
246 int registerRequest(mctp_eid_t eid, uint8_t instanceId, uint8_t type,
247 uint8_t command, pldm::Request&& requestMsg,
248 ResponseHandler&& responseHandler)
249 {
250 RequestKey key{eid, instanceId, type, command};
251
Thu Nguyenb4e8cfd2023-03-23 15:06:44 +0700252 if (handlers.contains(key))
253 {
254 error("The eid:InstanceID {EID}:{IID} is using.", "EID",
255 (unsigned)eid, "IID", (unsigned)instanceId);
256 return PLDM_ERROR;
257 }
258
Thu Nguyen4ddee3a2023-08-03 08:20:03 +0700259 auto inputRequest = std::make_shared<RegisteredRequest>(
260 key, std::move(requestMsg), std::move(responseHandler));
261 if (endpointMessageQueues.contains(eid))
Tom Joseph74f27c72021-05-16 07:58:53 -0700262 {
Thu Nguyen4ddee3a2023-08-03 08:20:03 +0700263 endpointMessageQueues[eid]->requestQueue.push_back(inputRequest);
264 }
265 else
266 {
267 std::deque<std::shared_ptr<RegisteredRequest>> reqQueue;
268 reqQueue.push_back(inputRequest);
269 endpointMessageQueues[eid] =
270 std::make_shared<EndpointMessageQueue>(eid, reqQueue, false);
Tom Joseph74f27c72021-05-16 07:58:53 -0700271 }
272
Thu Nguyen4ddee3a2023-08-03 08:20:03 +0700273 /* try to send new request if the endpoint is free */
274 pollEndpointQueue(eid);
Tom Joseph74f27c72021-05-16 07:58:53 -0700275
Thu Nguyen4ddee3a2023-08-03 08:20:03 +0700276 return PLDM_SUCCESS;
Tom Joseph74f27c72021-05-16 07:58:53 -0700277 }
278
279 /** @brief Handle PLDM response message
280 *
281 * @param[in] eid - endpoint ID of the remote MCTP endpoint
282 * @param[in] instanceId - instance ID to match request and response
283 * @param[in] type - PLDM type
284 * @param[in] command - PLDM command
285 * @param[in] response - PLDM response message
286 * @param[in] respMsgLen - length of the response message
287 */
288 void handleResponse(mctp_eid_t eid, uint8_t instanceId, uint8_t type,
289 uint8_t command, const pldm_msg* response,
290 size_t respMsgLen)
291 {
292 RequestKey key{eid, instanceId, type, command};
293 if (handlers.contains(key))
294 {
295 auto& [request, responseHandler, timerInstance] = handlers[key];
296 request->stop();
297 auto rc = timerInstance->stop();
298 if (rc)
299 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600300 error("Failed to stop the instance ID expiry timer. RC = {RC}",
301 "RC", static_cast<int>(rc));
Tom Joseph74f27c72021-05-16 07:58:53 -0700302 }
303 responseHandler(eid, response, respMsgLen);
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930304 instanceIdDb.free(key.eid, key.instanceId);
Tom Joseph74f27c72021-05-16 07:58:53 -0700305 handlers.erase(key);
Thu Nguyen4ddee3a2023-08-03 08:20:03 +0700306
307 endpointMessageQueues[eid]->activeRequest = false;
308 /* try to send new request if the endpoint is free */
309 pollEndpointQueue(eid);
Tom Joseph74f27c72021-05-16 07:58:53 -0700310 }
311 else
312 {
313 // Got a response for a PLDM request message not registered with the
314 // request handler, so freeing up the instance ID, this can be other
315 // OpenBMC applications relying on PLDM D-Bus apis like
316 // openpower-occ-control and softoff
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930317 instanceIdDb.free(key.eid, key.instanceId);
Tom Joseph74f27c72021-05-16 07:58:53 -0700318 }
319 }
320
321 private:
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000322 PldmTransport* pldmTransport; //!< PLDM transport object
Brad Bishop5079ac42021-08-19 18:35:06 -0400323 sdeventplus::Event& event; //!< reference to PLDM daemon's main event loop
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930324 pldm::InstanceIdDb& instanceIdDb; //!< reference to an InstanceIdDb
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930325 bool verbose; //!< verbose tracing flag
Brad Bishop5079ac42021-08-19 18:35:06 -0400326 std::chrono::seconds
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930327 instanceIdExpiryInterval; //!< Instance ID expiration interval
328 uint8_t numRetries; //!< number of request retries
Brad Bishop5079ac42021-08-19 18:35:06 -0400329 std::chrono::milliseconds
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930330 responseTimeOut; //!< time to wait between each retry
Tom Joseph74f27c72021-05-16 07:58:53 -0700331
Tom Josephe5268cd2021-09-07 13:04:03 +0530332 /** @brief Container for storing the details of the PLDM request
333 * message, handler for the corresponding PLDM response and the
334 * timer object for the Instance ID expiration
Tom Joseph74f27c72021-05-16 07:58:53 -0700335 */
Brad Bishop5079ac42021-08-19 18:35:06 -0400336 using RequestValue =
337 std::tuple<std::unique_ptr<RequestInterface>, ResponseHandler,
338 std::unique_ptr<phosphor::Timer>>;
Tom Joseph74f27c72021-05-16 07:58:53 -0700339
Thu Nguyen4ddee3a2023-08-03 08:20:03 +0700340 // Manage the requests of responders base on MCTP EID
341 std::map<mctp_eid_t, std::shared_ptr<EndpointMessageQueue>>
342 endpointMessageQueues;
343
Tom Joseph74f27c72021-05-16 07:58:53 -0700344 /** @brief Container for storing the PLDM request entries */
345 std::unordered_map<RequestKey, RequestValue, RequestKeyHasher> handlers;
346
347 /** @brief Container to store information about the request entries to be
348 * removed after the instance ID timer expires
349 */
Brad Bishop5079ac42021-08-19 18:35:06 -0400350 std::unordered_map<RequestKey, std::unique_ptr<sdeventplus::source::Defer>,
351 RequestKeyHasher>
Tom Joseph74f27c72021-05-16 07:58:53 -0700352 removeRequestContainer;
353
354 /** @brief Remove request entry for which the instance ID expired
355 *
356 * @param[in] key - key for the Request
357 */
358 void removeRequestEntry(RequestKey key)
359 {
360 if (removeRequestContainer.contains(key))
361 {
362 removeRequestContainer[key].reset();
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930363 instanceIdDb.free(key.eid, key.instanceId);
Tom Joseph74f27c72021-05-16 07:58:53 -0700364 handlers.erase(key);
365 removeRequestContainer.erase(key);
366 }
367 }
368};
369
370} // namespace requester
371
372} // namespace pldm