blob: ffa683426e53d5c2daf35a46557376a8b74c0292 [file] [log] [blame]
Tom Josephfbcac2e2017-03-14 18:15:07 +05301#include <phosphor-logging/log.hpp>
2#include "main.hpp"
3#include "sd_event_loop.hpp"
4#include "sol_context.hpp"
5#include "sol_manager.hpp"
6
7namespace sol
8{
9
10using namespace phosphor::logging;
11
12void Context::processInboundPayload(uint8_t seqNum,
13 uint8_t ackSeqNum,
14 uint8_t count,
15 bool status,
Vernon Mauery70fd29c2017-11-30 13:11:43 -080016 const std::vector<uint8_t>& input)
Tom Josephfbcac2e2017-03-14 18:15:07 +053017{
18 uint8_t respAckSeqNum = 0;
19 uint8_t acceptedCount = 0;
20 auto ack = false;
21
22 /*
23 * Check if the Inbound sequence number is same as the expected one.
24 * If the Packet Sequence Number is 0, it is an ACK-Only packet. Multiple
25 * outstanding sequence numbers are not supported in this version of the SOL
26 * specification. Retried packets use the same sequence number as the first
27 * packet.
28 */
29 if(seqNum && (seqNum != seqNums.get(true)))
30 {
31 log<level::INFO>("Out of sequence SOL packet - packet is dropped");
32 return;
33 }
34
35 /*
36 * Check if the expected ACK/NACK sequence number is same as the
37 * ACK/NACK sequence number in the packet. If packet ACK/NACK sequence
38 * number is 0, then it is an informational packet. No request packet being
39 * ACK'd or NACK'd.
40 */
41 if (ackSeqNum && (ackSeqNum != seqNums.get(false)))
42 {
43 log<level::INFO>("Out of sequence ack number - SOL packet is dropped");
44 return;
45 }
46
47 /*
48 * Retry the SOL payload packet in the following conditions:
49 *
50 * a) NACK in Operation/Status
51 * b) Accepted Character Count does not match with the sent out SOL payload
52 * c) Non-zero Packet ACK/NACK Sequence Number
53 */
54 if (status || ((count != expectedCharCount) && ackSeqNum))
55 {
56 resendPayload(noClear);
57 std::get<eventloop::EventLoop&>(singletonPool).switchTimer
58 (payloadInstance, eventloop::Timers::RETRY, false);
59 std::get<eventloop::EventLoop&>(singletonPool).switchTimer
60 (payloadInstance, eventloop::Timers::RETRY, true);
61 return;
62 }
63 /*
64 * Clear the sent data once the acknowledgment sequence number matches
65 * and the expected character count matches.
66 */
67 else if ((count == expectedCharCount) && ackSeqNum)
68 {
69 // Clear the Host Console Buffer
70 std::get<sol::Manager&>(singletonPool).dataBuffer.erase(count);
71
72 // Once it is acknowledged stop the retry interval timer
73 std::get<eventloop::EventLoop&>(singletonPool).switchTimer(
74 payloadInstance, eventloop::Timers::RETRY, false);
75
76 retryCounter = maxRetryCount;
77 expectedCharCount = 0;
78 payloadCache.clear();
79 }
80
81 // Write character data to the Host Console
82 if (!input.empty() && seqNum)
83 {
84 auto rc = std::get<sol::Manager&>(singletonPool).writeConsoleSocket(
85 input);
86 if (rc)
87 {
88 log<level::ERR>("Writing to console socket descriptor failed");
89 ack = true;
90 }
91 else
92 {
93 respAckSeqNum = seqNum;
94 ack = false;
95 acceptedCount = input.size();
96 }
97 }
Tom Joseph694fc0c2017-08-21 11:47:25 +053098 /*
99 * SOL payload with no character data and valid sequence number can be used
100 * as method to keep the SOL session active.
101 */
102 else if (input.empty() && seqNum)
103 {
104 respAckSeqNum = seqNum;
105 }
Tom Josephfbcac2e2017-03-14 18:15:07 +0530106
107 if (seqNum != 0)
108 {
109 seqNums.incInboundSeqNum();
110 prepareResponse(respAckSeqNum, acceptedCount, ack);
111 }
112 else
113 {
114 std::get<eventloop::EventLoop&>(singletonPool).switchTimer
115 (payloadInstance, eventloop::Timers::ACCUMULATE, true);
116 }
117}
118
Tom Joseph75e15db2017-03-14 18:16:22 +0530119void Context::prepareResponse(uint8_t ackSeqNum, uint8_t count, bool ack)
120{
121 auto bufferSize = std::get<sol::Manager&>(singletonPool).dataBuffer.
122 size();
123
124 /* Sent a ACK only response */
125 if (payloadCache.size() != 0 || (bufferSize < sendThreshold))
126 {
127 std::get<eventloop::EventLoop&>(singletonPool).switchTimer
128 (payloadInstance, eventloop::Timers::ACCUMULATE, true);
129
Vernon Mauery70fd29c2017-11-30 13:11:43 -0800130 std::vector<uint8_t> outPayload(sizeof(Payload));
Tom Joseph75e15db2017-03-14 18:16:22 +0530131 auto response = reinterpret_cast<Payload*>(outPayload.data());
132 response->packetSeqNum = 0;
133 response->packetAckSeqNum = ackSeqNum;
134 response->acceptedCharCount = count;
135 response->outOperation.ack = ack;
136 sendPayload(outPayload);
137 return;
138 }
139
140 auto readSize = std::min(bufferSize, MAX_PAYLOAD_SIZE);
141 payloadCache.resize(sizeof(Payload) + readSize);
142 auto response = reinterpret_cast<Payload*>(payloadCache.data());
143 response->packetAckSeqNum = ackSeqNum;
144 response->acceptedCharCount = count;
145 response->outOperation.ack = ack;
146 response->packetSeqNum = seqNums.incOutboundSeqNum();
147
148
149 auto handle = std::get<sol::Manager&>(singletonPool).dataBuffer.read();
150 std::copy_n(handle, readSize, payloadCache.data() + sizeof(Payload));
151 expectedCharCount = readSize;
152
153 std::get<eventloop::EventLoop&>(singletonPool).switchTimer(
154 payloadInstance, eventloop::Timers::RETRY, true);
155 std::get<eventloop::EventLoop&>(singletonPool).switchTimer
156 (payloadInstance, eventloop::Timers::ACCUMULATE, false);
157
158 sendPayload(payloadCache);
159}
160
Tom Joseph73063142017-03-14 18:20:20 +0530161int Context::sendOutboundPayload()
162{
163 if (payloadCache.size() != 0)
164 {
165 std::get<eventloop::EventLoop&>(singletonPool).switchTimer(
166 payloadInstance, eventloop::Timers::ACCUMULATE, true);
167 return -1;
168 }
169
170 auto bufferSize = std::get<sol::Manager&>(singletonPool).dataBuffer.size();
171 auto readSize = std::min(bufferSize, MAX_PAYLOAD_SIZE);
172
173 payloadCache.resize(sizeof(Payload) + readSize);
174 auto response = reinterpret_cast<Payload*>(payloadCache.data());
175 response->packetAckSeqNum = 0;
176 response->acceptedCharCount = 0;
177 response->outOperation.ack = false;
178 response->packetSeqNum = seqNums.incOutboundSeqNum();
179
180 auto handle = std::get<sol::Manager&>(singletonPool).dataBuffer.read();
181 std::copy_n(handle, readSize, payloadCache.data() + sizeof(Payload));
182 expectedCharCount = readSize;
183
184 std::get<eventloop::EventLoop&>(singletonPool).switchTimer(
185 payloadInstance, eventloop::Timers::RETRY, true);
186 std::get<eventloop::EventLoop&>(singletonPool).switchTimer(
187 payloadInstance, eventloop::Timers::ACCUMULATE, false);
188
189 sendPayload(payloadCache);
190
191 return 0;
192}
193
Tom Joseph75e15db2017-03-14 18:16:22 +0530194void Context::resendPayload(bool clear)
Tom Josephfbcac2e2017-03-14 18:15:07 +0530195{
Tom Joseph2fd466f2017-03-30 08:16:47 +0530196 sendPayload(payloadCache);
Tom Josephfbcac2e2017-03-14 18:15:07 +0530197
Tom Joseph2fd466f2017-03-30 08:16:47 +0530198 if(clear)
199 {
200 payloadCache.clear();
201 expectedCharCount = 0;
202 std::get<sol::Manager&>(singletonPool).dataBuffer.erase(
203 expectedCharCount);
204 }
Tom Josephfbcac2e2017-03-14 18:15:07 +0530205}
206
Vernon Mauery70fd29c2017-11-30 13:11:43 -0800207void Context::sendPayload(const std::vector<uint8_t>& out) const
Tom Josephfbcac2e2017-03-14 18:15:07 +0530208{
Tom Joseph22596f22017-03-31 10:52:27 +0530209 auto session = (std::get<session::Manager&>(singletonPool).getSession(
210 sessionID)).lock();
Tom Josephfbcac2e2017-03-14 18:15:07 +0530211
Tom Joseph22596f22017-03-31 10:52:27 +0530212 message::Handler msgHandler(session->channelPtr, sessionID);
213
214 msgHandler.sendSOLPayload(out);
Tom Josephfbcac2e2017-03-14 18:15:07 +0530215}
216
217} // namespace sol