Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Manojkiran Eda | ef77305 | 2021-07-29 09:29:28 +0530 | [diff] [blame] | 3 | #include "common/flight_recorder.hpp" |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 4 | #include "common/transport.hpp" |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 5 | #include "common/types.hpp" |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 6 | #include "common/utils.hpp" |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 7 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 8 | #include <libpldm/base.h> |
Manojkiran Eda | 9fffea2 | 2021-10-27 16:03:27 +0530 | [diff] [blame] | 9 | #include <sys/socket.h> |
| 10 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 11 | #include <phosphor-logging/lg2.hpp> |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 12 | #include <sdbusplus/timer.hpp> |
| 13 | #include <sdeventplus/event.hpp> |
| 14 | |
| 15 | #include <chrono> |
| 16 | #include <functional> |
| 17 | #include <iostream> |
| 18 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 19 | PHOSPHOR_LOG2_USING; |
| 20 | |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 21 | namespace pldm |
| 22 | { |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 23 | namespace requester |
| 24 | { |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 25 | /** @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 | */ |
| 32 | class RequestRetryTimer |
| 33 | { |
| 34 | public: |
| 35 | RequestRetryTimer() = delete; |
| 36 | RequestRetryTimer(const RequestRetryTimer&) = delete; |
Pavithra Barithaya | a7dbca5 | 2023-07-07 04:19:37 -0500 | [diff] [blame] | 37 | RequestRetryTimer(RequestRetryTimer&&) = delete; |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 38 | RequestRetryTimer& operator=(const RequestRetryTimer&) = delete; |
Pavithra Barithaya | a7dbca5 | 2023-07-07 04:19:37 -0500 | [diff] [blame] | 39 | RequestRetryTimer& operator=(RequestRetryTimer&&) = delete; |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 40 | 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 Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 49 | std::chrono::milliseconds timeout) : |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 50 | |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 51 | event(event), numRetries(numRetries), timeout(timeout), |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 52 | 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 Joseph | a5ed658 | 2021-06-17 22:08:47 -0700 | [diff] [blame] | 62 | if (rc) |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 63 | { |
| 64 | return rc; |
| 65 | } |
| 66 | |
| 67 | try |
| 68 | { |
| 69 | if (numRetries) |
| 70 | { |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 71 | timer.start(duration_cast<std::chrono::microseconds>(timeout), |
| 72 | true); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | catch (const std::runtime_error& e) |
| 76 | { |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 77 | error("Failed to start the request timer, error - {ERROR}", "ERROR", |
| 78 | e); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 79 | 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 Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 91 | error("Failed to stop the request timer, response code '{RC}'", |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 92 | "RC", rc); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 93 | } |
| 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 Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 99 | std::chrono::milliseconds |
Patrick Williams | 35535cf | 2023-12-05 12:45:02 -0600 | [diff] [blame] | 100 | timeout; //!< time to wait between each retry in milliseconds |
| 101 | sdbusplus::Timer timer; //!< manages starting timers and handling timeouts |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 102 | |
| 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 | */ |
| 132 | class Request final : public RequestRetryTimer |
| 133 | { |
| 134 | public: |
| 135 | Request() = delete; |
| 136 | Request(const Request&) = delete; |
Pavithra Barithaya | a7dbca5 | 2023-07-07 04:19:37 -0500 | [diff] [blame] | 137 | Request(Request&&) = delete; |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 138 | Request& operator=(const Request&) = delete; |
Pavithra Barithaya | a7dbca5 | 2023-07-07 04:19:37 -0500 | [diff] [blame] | 139 | Request& operator=(Request&&) = delete; |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 140 | ~Request() = default; |
| 141 | |
| 142 | /** @brief Constructor |
| 143 | * |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 144 | * @param[in] pldm_transport - PLDM transport object |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 145 | * @param[in] eid - endpoint ID of the remote MCTP endpoint |
Manojkiran Eda | 9fffea2 | 2021-10-27 16:03:27 +0530 | [diff] [blame] | 146 | * @param[in] currrentSendbuffSize - the current send buffer size |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 147 | * @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 Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 151 | * @param[in] verbose - verbose tracing flag |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 152 | */ |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 153 | explicit Request(PldmTransport* pldmTransport, mctp_eid_t eid, |
| 154 | sdeventplus::Event& event, pldm::Request&& requestMsg, |
| 155 | uint8_t numRetries, std::chrono::milliseconds timeout, |
Manojkiran Eda | 9fffea2 | 2021-10-27 16:03:27 +0530 | [diff] [blame] | 156 | bool verbose) : |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 157 | RequestRetryTimer(event, numRetries, timeout), |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 158 | pldmTransport(pldmTransport), eid(eid), |
| 159 | requestMsg(std::move(requestMsg)), verbose(verbose) |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 160 | {} |
| 161 | |
| 162 | private: |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 163 | 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 Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 167 | |
| 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 Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 174 | if (verbose) |
| 175 | { |
| 176 | pldm::utils::printBuffer(pldm::utils::Tx, requestMsg); |
| 177 | } |
Manojkiran Eda | ef77305 | 2021-07-29 09:29:28 +0530 | [diff] [blame] | 178 | pldm::flightrecorder::FlightRecorder::GetInstance().saveRecord( |
| 179 | requestMsg, true); |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 180 | 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 Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 195 | if (rc < 0) |
| 196 | { |
Riya Dixit | 087a751 | 2024-04-06 14:28:08 -0500 | [diff] [blame] | 197 | error( |
| 198 | "Failed to send pldmTransport message, response code '{RC}' and error - {ERROR}", |
Riya Dixit | 1e5c81e | 2024-05-03 07:54:00 -0500 | [diff] [blame] | 199 | "RC", rc, "ERROR", errno); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 200 | return PLDM_ERROR; |
| 201 | } |
| 202 | return PLDM_SUCCESS; |
| 203 | } |
| 204 | }; |
| 205 | |
| 206 | } // namespace requester |
| 207 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 208 | } // namespace pldm |