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