blob: 7182c4befbcd3bd41344ccd11b72116fb0001da6 [file] [log] [blame]
Tom Joseph74f27c72021-05-16 07:58:53 -07001#pragma once
2
Manojkiran Edaef773052021-07-29 09:29:28 +05303#include "common/flight_recorder.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 Josephe5268cd2021-09-07 13:04:03 +05306#include "common/utils.hpp"
Tom Joseph74f27c72021-05-16 07:58:53 -07007
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
15#include <chrono>
16#include <functional>
17#include <iostream>
18
Riya Dixit49cfb132023-03-02 04:26:53 -060019PHOSPHOR_LOG2_USING;
20
Tom Joseph74f27c72021-05-16 07:58:53 -070021namespace pldm
22{
Tom Joseph74f27c72021-05-16 07:58:53 -070023namespace requester
24{
Tom Joseph74f27c72021-05-16 07:58:53 -070025/** @class RequestRetryTimer
26 *
27 * The abstract base class for implementing the PLDM request retry logic. This
28 * class handles number of times the PLDM request needs to be retried if the
29 * response is not received and the time to wait between each retry. It
30 * provides APIs to start and stop the request flow.
31 */
32class RequestRetryTimer
33{
34 public:
35 RequestRetryTimer() = delete;
36 RequestRetryTimer(const RequestRetryTimer&) = delete;
Pavithra Barithayaa7dbca52023-07-07 04:19:37 -050037 RequestRetryTimer(RequestRetryTimer&&) = delete;
Tom Joseph74f27c72021-05-16 07:58:53 -070038 RequestRetryTimer& operator=(const RequestRetryTimer&) = delete;
Pavithra Barithayaa7dbca52023-07-07 04:19:37 -050039 RequestRetryTimer& operator=(RequestRetryTimer&&) = delete;
Tom Joseph74f27c72021-05-16 07:58:53 -070040 virtual ~RequestRetryTimer() = default;
41
42 /** @brief Constructor
43 *
44 * @param[in] event - reference to PLDM daemon's main event loop
45 * @param[in] numRetries - number of request retries
46 * @param[in] timeout - time to wait between each retry in milliseconds
47 */
48 explicit RequestRetryTimer(sdeventplus::Event& event, uint8_t numRetries,
Brad Bishop5079ac42021-08-19 18:35:06 -040049 std::chrono::milliseconds timeout) :
Tom Joseph74f27c72021-05-16 07:58:53 -070050
Patrick Williams16c2a0a2024-08-16 15:20:59 -040051 event(event), numRetries(numRetries), timeout(timeout),
Tom Joseph74f27c72021-05-16 07:58:53 -070052 timer(event.get(), std::bind_front(&RequestRetryTimer::callback, this))
53 {}
54
55 /** @brief Starts the request flow and arms the timer for request retries
56 *
57 * @return return PLDM_SUCCESS on success and PLDM_ERROR otherwise
58 */
59 int start()
60 {
61 auto rc = send();
Tom Josepha5ed6582021-06-17 22:08:47 -070062 if (rc)
Tom Joseph74f27c72021-05-16 07:58:53 -070063 {
64 return rc;
65 }
66
67 try
68 {
69 if (numRetries)
70 {
Brad Bishop5079ac42021-08-19 18:35:06 -040071 timer.start(duration_cast<std::chrono::microseconds>(timeout),
72 true);
Tom Joseph74f27c72021-05-16 07:58:53 -070073 }
74 }
75 catch (const std::runtime_error& e)
76 {
Riya Dixit087a7512024-04-06 14:28:08 -050077 error("Failed to start the request timer, error - {ERROR}", "ERROR",
78 e);
Tom Joseph74f27c72021-05-16 07:58:53 -070079 return PLDM_ERROR;
80 }
81
82 return PLDM_SUCCESS;
83 }
84
85 /** @brief Stops the timer and no further request retries happen */
86 void stop()
87 {
88 auto rc = timer.stop();
89 if (rc)
90 {
Riya Dixit087a7512024-04-06 14:28:08 -050091 error("Failed to stop the request timer, response code '{RC}'",
Riya Dixit1e5c81e2024-05-03 07:54:00 -050092 "RC", rc);
Tom Joseph74f27c72021-05-16 07:58:53 -070093 }
94 }
95
96 protected:
97 sdeventplus::Event& event; //!< reference to PLDM daemon's main event loop
98 uint8_t numRetries; //!< number of request retries
Brad Bishop5079ac42021-08-19 18:35:06 -040099 std::chrono::milliseconds
Patrick Williams35535cf2023-12-05 12:45:02 -0600100 timeout; //!< time to wait between each retry in milliseconds
101 sdbusplus::Timer timer; //!< manages starting timers and handling timeouts
Tom Joseph74f27c72021-05-16 07:58:53 -0700102
103 /** @brief Sends the PLDM request message
104 *
105 * @return return PLDM_SUCCESS on success and PLDM_ERROR otherwise
106 */
107 virtual int send() const = 0;
108
109 /** @brief Callback function invoked when the timeout happens */
110 void callback()
111 {
112 if (numRetries--)
113 {
114 send();
115 }
116 else
117 {
118 stop();
119 }
120 }
121};
122
123/** @class Request
124 *
125 * The concrete implementation of RequestIntf. This class implements the send()
126 * to send the PLDM request message over MCTP socket.
127 * This class encapsulates the PLDM request message, the number of times the
128 * request needs to retried if the response is not received and the amount of
129 * time to wait between each retry. It provides APIs to start and stop the
130 * request flow.
131 */
132class Request final : public RequestRetryTimer
133{
134 public:
135 Request() = delete;
136 Request(const Request&) = delete;
Pavithra Barithayaa7dbca52023-07-07 04:19:37 -0500137 Request(Request&&) = delete;
Tom Joseph74f27c72021-05-16 07:58:53 -0700138 Request& operator=(const Request&) = delete;
Pavithra Barithayaa7dbca52023-07-07 04:19:37 -0500139 Request& operator=(Request&&) = delete;
Tom Joseph74f27c72021-05-16 07:58:53 -0700140 ~Request() = default;
141
142 /** @brief Constructor
143 *
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000144 * @param[in] pldm_transport - PLDM transport object
Tom Joseph74f27c72021-05-16 07:58:53 -0700145 * @param[in] eid - endpoint ID of the remote MCTP endpoint
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530146 * @param[in] currrentSendbuffSize - the current send buffer size
Tom Joseph74f27c72021-05-16 07:58:53 -0700147 * @param[in] event - reference to PLDM daemon's main event loop
148 * @param[in] requestMsg - PLDM request message
149 * @param[in] numRetries - number of request retries
150 * @param[in] timeout - time to wait between each retry in milliseconds
Tom Josephe5268cd2021-09-07 13:04:03 +0530151 * @param[in] verbose - verbose tracing flag
Tom Joseph74f27c72021-05-16 07:58:53 -0700152 */
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000153 explicit Request(PldmTransport* pldmTransport, mctp_eid_t eid,
154 sdeventplus::Event& event, pldm::Request&& requestMsg,
155 uint8_t numRetries, std::chrono::milliseconds timeout,
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530156 bool verbose) :
Tom Joseph74f27c72021-05-16 07:58:53 -0700157 RequestRetryTimer(event, numRetries, timeout),
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000158 pldmTransport(pldmTransport), eid(eid),
159 requestMsg(std::move(requestMsg)), verbose(verbose)
Tom Joseph74f27c72021-05-16 07:58:53 -0700160 {}
161
162 private:
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000163 PldmTransport* pldmTransport; //!< PLDM transport
164 mctp_eid_t eid; //!< endpoint ID of the remote MCTP endpoint
165 pldm::Request requestMsg; //!< PLDM request message
166 bool verbose; //!< verbose tracing flag
Tom Joseph74f27c72021-05-16 07:58:53 -0700167
168 /** @brief Sends the PLDM request message on the socket
169 *
170 * @return return PLDM_SUCCESS on success and PLDM_ERROR otherwise
171 */
172 int send() const
173 {
Tom Josephe5268cd2021-09-07 13:04:03 +0530174 if (verbose)
175 {
176 pldm::utils::printBuffer(pldm::utils::Tx, requestMsg);
177 }
Manojkiran Edaef773052021-07-29 09:29:28 +0530178 pldm::flightrecorder::FlightRecorder::GetInstance().saveRecord(
179 requestMsg, true);
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000180 const struct pldm_msg_hdr* hdr =
181 (struct pldm_msg_hdr*)(requestMsg.data());
182 if (!hdr->request)
183 {
184 return PLDM_REQUESTER_NOT_REQ_MSG;
185 }
186
187 if (pldmTransport == nullptr)
188 {
189 error("Invalid transport: Unable to send PLDM request");
190 return PLDM_ERROR;
191 }
192
193 auto rc = pldmTransport->sendMsg(static_cast<pldm_tid_t>(eid),
194 requestMsg.data(), requestMsg.size());
Tom Joseph74f27c72021-05-16 07:58:53 -0700195 if (rc < 0)
196 {
Riya Dixit087a7512024-04-06 14:28:08 -0500197 error(
198 "Failed to send pldmTransport message, response code '{RC}' and error - {ERROR}",
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500199 "RC", rc, "ERROR", errno);
Tom Joseph74f27c72021-05-16 07:58:53 -0700200 return PLDM_ERROR;
201 }
202 return PLDM_SUCCESS;
203 }
204};
205
206} // namespace requester
207
Sampa Misrac0c79482021-06-02 08:01:54 -0500208} // namespace pldm