Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 1 | #include "sol_context.hpp" |
| 2 | |
Tom Joseph | fbcac2e | 2017-03-14 18:15:07 +0530 | [diff] [blame] | 3 | #include "main.hpp" |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 4 | #include "message_handler.hpp" |
Tom Joseph | fbcac2e | 2017-03-14 18:15:07 +0530 | [diff] [blame] | 5 | #include "sd_event_loop.hpp" |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 6 | #include "sessions_manager.hpp" |
Tom Joseph | fbcac2e | 2017-03-14 18:15:07 +0530 | [diff] [blame] | 7 | #include "sol_manager.hpp" |
| 8 | |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 9 | #include <errno.h> |
| 10 | |
| 11 | #include <phosphor-logging/lg2.hpp> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 12 | |
Tom Joseph | fbcac2e | 2017-03-14 18:15:07 +0530 | [diff] [blame] | 13 | namespace sol |
| 14 | { |
Tom Joseph | fbcac2e | 2017-03-14 18:15:07 +0530 | [diff] [blame] | 15 | using namespace phosphor::logging; |
| 16 | |
Vernon Mauery | 6f353e8 | 2018-11-09 08:43:24 -0800 | [diff] [blame] | 17 | Context::Context(std::shared_ptr<boost::asio::io_context> io, |
| 18 | uint8_t maxRetryCount, uint8_t sendThreshold, uint8_t instance, |
| 19 | session::SessionID sessionID) : |
| 20 | accumulateTimer(*io), |
| 21 | retryTimer(*io), maxRetryCount(maxRetryCount), retryCounter(maxRetryCount), |
| 22 | sendThreshold(sendThreshold), payloadInstance(instance), |
| 23 | sessionID(sessionID) |
| 24 | { |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 25 | session = session::Manager::get().getSession(sessionID); |
Vernon Mauery | a6ad5e1 | 2020-02-21 14:54:24 -0800 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | std::shared_ptr<Context> |
| 29 | Context::makeContext(std::shared_ptr<boost::asio::io_context> io, |
| 30 | uint8_t maxRetryCount, uint8_t sendThreshold, |
| 31 | uint8_t instance, session::SessionID sessionID) |
| 32 | { |
| 33 | auto ctx = std::make_shared<Context>(io, maxRetryCount, sendThreshold, |
| 34 | instance, sessionID); |
| 35 | ctx->enableAccumulateTimer(true); |
| 36 | return ctx; |
Vernon Mauery | 6f353e8 | 2018-11-09 08:43:24 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void Context::enableAccumulateTimer(bool enable) |
| 40 | { |
| 41 | // fetch the timeout from the SOL manager |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 42 | std::chrono::microseconds interval = sol::Manager::get().accumulateInterval; |
Vernon Mauery | 6f353e8 | 2018-11-09 08:43:24 -0800 | [diff] [blame] | 43 | if (enable) |
| 44 | { |
| 45 | accumulateTimer.expires_after(interval); |
Vernon Mauery | a6ad5e1 | 2020-02-21 14:54:24 -0800 | [diff] [blame] | 46 | std::weak_ptr<Context> weakRef = weak_from_this(); |
| 47 | accumulateTimer.async_wait( |
| 48 | [weakRef](const boost::system::error_code& ec) { |
| 49 | std::shared_ptr<Context> self = weakRef.lock(); |
| 50 | if (!ec && self) |
| 51 | { |
| 52 | self->charAccTimerHandler(); |
| 53 | } |
| 54 | }); |
Vernon Mauery | 6f353e8 | 2018-11-09 08:43:24 -0800 | [diff] [blame] | 55 | } |
| 56 | else |
| 57 | { |
| 58 | accumulateTimer.cancel(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void Context::enableRetryTimer(bool enable) |
| 63 | { |
| 64 | if (enable) |
| 65 | { |
| 66 | // fetch the timeout from the SOL manager |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 67 | std::chrono::microseconds interval = sol::Manager::get().retryInterval; |
Vernon Mauery | 6f353e8 | 2018-11-09 08:43:24 -0800 | [diff] [blame] | 68 | retryTimer.expires_after(interval); |
Vernon Mauery | a6ad5e1 | 2020-02-21 14:54:24 -0800 | [diff] [blame] | 69 | std::weak_ptr<Context> weakRef = weak_from_this(); |
| 70 | retryTimer.async_wait([weakRef](const boost::system::error_code& ec) { |
| 71 | std::shared_ptr<Context> self = weakRef.lock(); |
| 72 | if (!ec && self) |
Vernon Mauery | 6f353e8 | 2018-11-09 08:43:24 -0800 | [diff] [blame] | 73 | { |
Vernon Mauery | a6ad5e1 | 2020-02-21 14:54:24 -0800 | [diff] [blame] | 74 | self->retryTimerHandler(); |
Vernon Mauery | 6f353e8 | 2018-11-09 08:43:24 -0800 | [diff] [blame] | 75 | } |
| 76 | }); |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | retryTimer.cancel(); |
| 81 | } |
| 82 | } |
| 83 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 84 | void Context::processInboundPayload(uint8_t seqNum, uint8_t ackSeqNum, |
| 85 | uint8_t count, bool status, |
Vernon Mauery | 70fd29c | 2017-11-30 13:11:43 -0800 | [diff] [blame] | 86 | const std::vector<uint8_t>& input) |
Tom Joseph | fbcac2e | 2017-03-14 18:15:07 +0530 | [diff] [blame] | 87 | { |
| 88 | uint8_t respAckSeqNum = 0; |
| 89 | uint8_t acceptedCount = 0; |
| 90 | auto ack = false; |
| 91 | |
| 92 | /* |
| 93 | * Check if the Inbound sequence number is same as the expected one. |
| 94 | * If the Packet Sequence Number is 0, it is an ACK-Only packet. Multiple |
| 95 | * outstanding sequence numbers are not supported in this version of the SOL |
| 96 | * specification. Retried packets use the same sequence number as the first |
| 97 | * packet. |
| 98 | */ |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 99 | if (seqNum && (seqNum != seqNums.get(true))) |
Tom Joseph | fbcac2e | 2017-03-14 18:15:07 +0530 | [diff] [blame] | 100 | { |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 101 | lg2::info("Out of sequence SOL packet - packet is dropped"); |
Tom Joseph | fbcac2e | 2017-03-14 18:15:07 +0530 | [diff] [blame] | 102 | return; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Check if the expected ACK/NACK sequence number is same as the |
| 107 | * ACK/NACK sequence number in the packet. If packet ACK/NACK sequence |
| 108 | * number is 0, then it is an informational packet. No request packet being |
| 109 | * ACK'd or NACK'd. |
| 110 | */ |
| 111 | if (ackSeqNum && (ackSeqNum != seqNums.get(false))) |
| 112 | { |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 113 | lg2::info("Out of sequence ack number - SOL packet is dropped"); |
Tom Joseph | fbcac2e | 2017-03-14 18:15:07 +0530 | [diff] [blame] | 114 | return; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Retry the SOL payload packet in the following conditions: |
| 119 | * |
| 120 | * a) NACK in Operation/Status |
| 121 | * b) Accepted Character Count does not match with the sent out SOL payload |
| 122 | * c) Non-zero Packet ACK/NACK Sequence Number |
| 123 | */ |
| 124 | if (status || ((count != expectedCharCount) && ackSeqNum)) |
| 125 | { |
| 126 | resendPayload(noClear); |
Vernon Mauery | 6f353e8 | 2018-11-09 08:43:24 -0800 | [diff] [blame] | 127 | enableRetryTimer(false); |
| 128 | enableRetryTimer(true); |
Tom Joseph | fbcac2e | 2017-03-14 18:15:07 +0530 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | /* |
| 132 | * Clear the sent data once the acknowledgment sequence number matches |
| 133 | * and the expected character count matches. |
| 134 | */ |
| 135 | else if ((count == expectedCharCount) && ackSeqNum) |
| 136 | { |
| 137 | // Clear the Host Console Buffer |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 138 | sol::Manager::get().dataBuffer.erase(count); |
Tom Joseph | fbcac2e | 2017-03-14 18:15:07 +0530 | [diff] [blame] | 139 | |
| 140 | // Once it is acknowledged stop the retry interval timer |
Vernon Mauery | 6f353e8 | 2018-11-09 08:43:24 -0800 | [diff] [blame] | 141 | enableRetryTimer(false); |
Tom Joseph | fbcac2e | 2017-03-14 18:15:07 +0530 | [diff] [blame] | 142 | |
| 143 | retryCounter = maxRetryCount; |
| 144 | expectedCharCount = 0; |
| 145 | payloadCache.clear(); |
| 146 | } |
| 147 | |
| 148 | // Write character data to the Host Console |
| 149 | if (!input.empty() && seqNum) |
| 150 | { |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 151 | auto rc = sol::Manager::get().writeConsoleSocket(input); |
Tom Joseph | fbcac2e | 2017-03-14 18:15:07 +0530 | [diff] [blame] | 152 | if (rc) |
| 153 | { |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 154 | lg2::error("Writing to console socket descriptor failed: {ERROR}", |
| 155 | "ERROR", strerror(errno)); |
Tom Joseph | fbcac2e | 2017-03-14 18:15:07 +0530 | [diff] [blame] | 156 | ack = true; |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | respAckSeqNum = seqNum; |
| 161 | ack = false; |
| 162 | acceptedCount = input.size(); |
| 163 | } |
| 164 | } |
Tom Joseph | 694fc0c | 2017-08-21 11:47:25 +0530 | [diff] [blame] | 165 | /* |
| 166 | * SOL payload with no character data and valid sequence number can be used |
| 167 | * as method to keep the SOL session active. |
| 168 | */ |
| 169 | else if (input.empty() && seqNum) |
| 170 | { |
| 171 | respAckSeqNum = seqNum; |
| 172 | } |
Tom Joseph | fbcac2e | 2017-03-14 18:15:07 +0530 | [diff] [blame] | 173 | |
| 174 | if (seqNum != 0) |
| 175 | { |
| 176 | seqNums.incInboundSeqNum(); |
| 177 | prepareResponse(respAckSeqNum, acceptedCount, ack); |
| 178 | } |
| 179 | else |
| 180 | { |
Vernon Mauery | 6f353e8 | 2018-11-09 08:43:24 -0800 | [diff] [blame] | 181 | enableAccumulateTimer(true); |
Tom Joseph | fbcac2e | 2017-03-14 18:15:07 +0530 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Tom Joseph | 75e15db | 2017-03-14 18:16:22 +0530 | [diff] [blame] | 185 | void Context::prepareResponse(uint8_t ackSeqNum, uint8_t count, bool ack) |
| 186 | { |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 187 | auto bufferSize = sol::Manager::get().dataBuffer.size(); |
Tom Joseph | 75e15db | 2017-03-14 18:16:22 +0530 | [diff] [blame] | 188 | |
| 189 | /* Sent a ACK only response */ |
| 190 | if (payloadCache.size() != 0 || (bufferSize < sendThreshold)) |
| 191 | { |
Vernon Mauery | 6f353e8 | 2018-11-09 08:43:24 -0800 | [diff] [blame] | 192 | enableAccumulateTimer(true); |
Tom Joseph | 75e15db | 2017-03-14 18:16:22 +0530 | [diff] [blame] | 193 | |
Vernon Mauery | 70fd29c | 2017-11-30 13:11:43 -0800 | [diff] [blame] | 194 | std::vector<uint8_t> outPayload(sizeof(Payload)); |
Tom Joseph | 75e15db | 2017-03-14 18:16:22 +0530 | [diff] [blame] | 195 | auto response = reinterpret_cast<Payload*>(outPayload.data()); |
| 196 | response->packetSeqNum = 0; |
| 197 | response->packetAckSeqNum = ackSeqNum; |
| 198 | response->acceptedCharCount = count; |
| 199 | response->outOperation.ack = ack; |
| 200 | sendPayload(outPayload); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | auto readSize = std::min(bufferSize, MAX_PAYLOAD_SIZE); |
| 205 | payloadCache.resize(sizeof(Payload) + readSize); |
| 206 | auto response = reinterpret_cast<Payload*>(payloadCache.data()); |
| 207 | response->packetAckSeqNum = ackSeqNum; |
| 208 | response->acceptedCharCount = count; |
| 209 | response->outOperation.ack = ack; |
| 210 | response->packetSeqNum = seqNums.incOutboundSeqNum(); |
| 211 | |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 212 | auto handle = sol::Manager::get().dataBuffer.read(); |
Tom Joseph | 75e15db | 2017-03-14 18:16:22 +0530 | [diff] [blame] | 213 | std::copy_n(handle, readSize, payloadCache.data() + sizeof(Payload)); |
| 214 | expectedCharCount = readSize; |
| 215 | |
Vernon Mauery | 6f353e8 | 2018-11-09 08:43:24 -0800 | [diff] [blame] | 216 | enableRetryTimer(true); |
| 217 | enableAccumulateTimer(false); |
Tom Joseph | 75e15db | 2017-03-14 18:16:22 +0530 | [diff] [blame] | 218 | |
| 219 | sendPayload(payloadCache); |
| 220 | } |
| 221 | |
Tom Joseph | 7306314 | 2017-03-14 18:20:20 +0530 | [diff] [blame] | 222 | int Context::sendOutboundPayload() |
| 223 | { |
| 224 | if (payloadCache.size() != 0) |
| 225 | { |
Vernon Mauery | 6f353e8 | 2018-11-09 08:43:24 -0800 | [diff] [blame] | 226 | enableAccumulateTimer(true); |
Tom Joseph | 7306314 | 2017-03-14 18:20:20 +0530 | [diff] [blame] | 227 | return -1; |
| 228 | } |
| 229 | |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 230 | auto bufferSize = sol::Manager::get().dataBuffer.size(); |
Tom Joseph | 7306314 | 2017-03-14 18:20:20 +0530 | [diff] [blame] | 231 | auto readSize = std::min(bufferSize, MAX_PAYLOAD_SIZE); |
| 232 | |
| 233 | payloadCache.resize(sizeof(Payload) + readSize); |
| 234 | auto response = reinterpret_cast<Payload*>(payloadCache.data()); |
| 235 | response->packetAckSeqNum = 0; |
| 236 | response->acceptedCharCount = 0; |
| 237 | response->outOperation.ack = false; |
| 238 | response->packetSeqNum = seqNums.incOutboundSeqNum(); |
| 239 | |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 240 | auto handle = sol::Manager::get().dataBuffer.read(); |
Tom Joseph | 7306314 | 2017-03-14 18:20:20 +0530 | [diff] [blame] | 241 | std::copy_n(handle, readSize, payloadCache.data() + sizeof(Payload)); |
| 242 | expectedCharCount = readSize; |
| 243 | |
Vernon Mauery | 6f353e8 | 2018-11-09 08:43:24 -0800 | [diff] [blame] | 244 | enableRetryTimer(true); |
| 245 | enableAccumulateTimer(false); |
Tom Joseph | 7306314 | 2017-03-14 18:20:20 +0530 | [diff] [blame] | 246 | |
| 247 | sendPayload(payloadCache); |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
Tom Joseph | 75e15db | 2017-03-14 18:16:22 +0530 | [diff] [blame] | 252 | void Context::resendPayload(bool clear) |
Tom Joseph | fbcac2e | 2017-03-14 18:15:07 +0530 | [diff] [blame] | 253 | { |
Tom Joseph | 2fd466f | 2017-03-30 08:16:47 +0530 | [diff] [blame] | 254 | sendPayload(payloadCache); |
Tom Joseph | fbcac2e | 2017-03-14 18:15:07 +0530 | [diff] [blame] | 255 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 256 | if (clear) |
Tom Joseph | 2fd466f | 2017-03-30 08:16:47 +0530 | [diff] [blame] | 257 | { |
| 258 | payloadCache.clear(); |
| 259 | expectedCharCount = 0; |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 260 | sol::Manager::get().dataBuffer.erase(expectedCharCount); |
Tom Joseph | 2fd466f | 2017-03-30 08:16:47 +0530 | [diff] [blame] | 261 | } |
Tom Joseph | fbcac2e | 2017-03-14 18:15:07 +0530 | [diff] [blame] | 262 | } |
| 263 | |
Vernon Mauery | 70fd29c | 2017-11-30 13:11:43 -0800 | [diff] [blame] | 264 | void Context::sendPayload(const std::vector<uint8_t>& out) const |
Tom Joseph | fbcac2e | 2017-03-14 18:15:07 +0530 | [diff] [blame] | 265 | { |
Tom Joseph | 22596f2 | 2017-03-31 10:52:27 +0530 | [diff] [blame] | 266 | message::Handler msgHandler(session->channelPtr, sessionID); |
| 267 | |
| 268 | msgHandler.sendSOLPayload(out); |
Tom Joseph | fbcac2e | 2017-03-14 18:15:07 +0530 | [diff] [blame] | 269 | } |
| 270 | |
Vernon Mauery | 6f353e8 | 2018-11-09 08:43:24 -0800 | [diff] [blame] | 271 | void Context::charAccTimerHandler() |
| 272 | { |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 273 | auto bufferSize = sol::Manager::get().dataBuffer.size(); |
Vernon Mauery | 6f353e8 | 2018-11-09 08:43:24 -0800 | [diff] [blame] | 274 | |
| 275 | try |
| 276 | { |
| 277 | if (bufferSize > 0) |
| 278 | { |
| 279 | int rc = sendOutboundPayload(); |
| 280 | if (rc == 0) |
| 281 | { |
| 282 | return; |
| 283 | } |
| 284 | } |
| 285 | enableAccumulateTimer(true); |
| 286 | } |
Patrick Williams | 12d199b | 2021-10-06 12:36:48 -0500 | [diff] [blame] | 287 | catch (const std::exception& e) |
Vernon Mauery | 6f353e8 | 2018-11-09 08:43:24 -0800 | [diff] [blame] | 288 | { |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 289 | lg2::error("Failed to call the sendOutboundPayload method: {ERROR}", |
| 290 | "ERROR", e); |
Vernon Mauery | 6f353e8 | 2018-11-09 08:43:24 -0800 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | void Context::retryTimerHandler() |
| 295 | { |
| 296 | try |
| 297 | { |
| 298 | if (retryCounter) |
| 299 | { |
| 300 | --retryCounter; |
| 301 | enableRetryTimer(true); |
| 302 | resendPayload(sol::Context::noClear); |
| 303 | } |
| 304 | else |
| 305 | { |
| 306 | retryCounter = maxRetryCount; |
| 307 | resendPayload(sol::Context::clear); |
| 308 | enableRetryTimer(false); |
| 309 | enableAccumulateTimer(true); |
| 310 | } |
| 311 | } |
Patrick Williams | 12d199b | 2021-10-06 12:36:48 -0500 | [diff] [blame] | 312 | catch (const std::exception& e) |
Vernon Mauery | 6f353e8 | 2018-11-09 08:43:24 -0800 | [diff] [blame] | 313 | { |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 314 | lg2::error("Failed to retry timer: {ERROR}", "ERROR", e); |
Vernon Mauery | 6f353e8 | 2018-11-09 08:43:24 -0800 | [diff] [blame] | 315 | } |
| 316 | } |
Tom Joseph | fbcac2e | 2017-03-14 18:15:07 +0530 | [diff] [blame] | 317 | } // namespace sol |