blob: a10799471a59577a6fd822868cb3c0120bad410e [file] [log] [blame]
Tom Joseph5a454a22017-02-15 14:51:42 +05301#pragma once
2
3#include <map>
4#include <memory>
5#include "console_buffer.hpp"
6#include "session.hpp"
7#include "sol_context.hpp"
8
9namespace sol
10{
11
12constexpr size_t MAX_PAYLOAD_INSTANCES = 16;
13constexpr size_t MAX_PAYLOAD_SIZE = 255;
14constexpr uint8_t MAJOR_VERSION = 0x01;
15constexpr uint8_t MINOR_VERSION = 0x00;
16
17constexpr char CONSOLE_SOCKET_PATH[] = "\0obmc-console";
18constexpr size_t CONSOLE_SOCKET_PATH_LEN = sizeof(CONSOLE_SOCKET_PATH) - 1;
19
Tom Joseph1967cf52017-04-28 01:24:22 +053020constexpr uint8_t accIntervalFactor = 5;
21constexpr uint8_t retryIntervalFactor = 10;
22
Tom Joseph5a454a22017-02-15 14:51:42 +053023using Instance = uint8_t;
24
25/** @struct CustomFD
26 *
27 * RAII wrapper for file descriptor.
28 */
29struct CustomFD
30{
31 CustomFD(const CustomFD&) = delete;
32 CustomFD& operator=(const CustomFD&) = delete;
33 CustomFD(CustomFD&&) = delete;
34 CustomFD& operator=(CustomFD&&) = delete;
35
36 CustomFD(int fd) :
37 fd(fd) {}
38
Tom Josephb81f7612017-04-25 17:59:02 +053039 ~CustomFD();
Tom Joseph5a454a22017-02-15 14:51:42 +053040
41 int operator()() const
42 {
43 return fd;
44 }
45
46 private:
47 int fd = -1;
48};
49
Tom Joseph704f3422017-04-26 10:13:03 +053050using namespace std::chrono_literals;
Tom Joseph5a454a22017-02-15 14:51:42 +053051
52/** @class Manager
53 *
54 * Manager class acts a manager for the SOL payload instances and provides
55 * interfaces to start a payload instance, stop a payload instance and get
56 * reference to the context object.
57 */
58class Manager
59{
60 public:
61
62 /** @brief SOL Payload Instance is the key for the map, the value is the
63 * SOL context.
64 */
65 using SOLPayloadMap = std::map<Instance, std::unique_ptr<Context>>;
66
67 Manager() = default;
68 ~Manager() = default;
69 Manager(const Manager&) = delete;
70 Manager& operator=(const Manager&) = delete;
71 Manager(Manager&&) = default;
72 Manager& operator=(Manager&&) = default;
73
74 /** @brief Host Console Buffer. */
75 ConsoleData dataBuffer;
76
Tom Joseph1967cf52017-04-28 01:24:22 +053077 /** @brief Set in Progress.
78 *
79 * This parameter is used to indicate when any of the SOL parameters
80 * are being updated, and when the changes are completed. The bit is
81 * primarily provided to alert software than some other software or
82 * utility is in the process of making changes to the data. This field
83 * is initialized to set complete.
84 */
85 uint8_t progress = 0;
86
87 /** @brief SOL enable
88 *
89 * This controls whether the SOL payload can be activated. By default
90 * the SOL is enabled.
91 */
92 bool enable = true;
93
94 /** @brief SOL payload encryption.
95 *
96 * Force encryption: if the cipher suite for the session supports
97 * encryption, then this setting will force the use of encryption for
98 * all SOL payload data. Encryption controlled by remote console:
99 * Whether SOL packets are encrypted or not is selectable by the remote
100 * console at the time the payload is activated. The default is force
101 * encryption.
102 */
103 bool forceEncrypt = true;
104
105 /** @brief SOL payload authentication.
106 *
107 * Force authentication: if the cipher suite for the session supports
108 * authentication, then this setting will force the use of for
109 * authentication for all SOL payload data. Authentication controlled
110 * by remote console: Note that for the standard Cipher Suites,
111 * if encryption is used authentication must also be used. Therefore,
112 * while encryption is being used software will not be able to select
113 * using unauthenticated payloads.
114 */
115 bool forceAuth = true;
116
117 /** @brief SOL privilege level.
118 *
119 * Sets the minimum operating privilege level that is required to be
120 * able to activate SOL using the Activate Payload command.
121 */
122 session::Privilege solMinPrivilege = session::Privilege::USER;
123
Tom Joseph5a454a22017-02-15 14:51:42 +0530124 /** @brief Character Accumulate Interval
125 *
Tom Joseph704f3422017-04-26 10:13:03 +0530126 * This sets the typical amount of time that the BMC will wait before
Tom Joseph5a454a22017-02-15 14:51:42 +0530127 * transmitting a partial SOL character data packet. (Where a partial
128 * packet is defined as a packet that has fewer characters to transmit
129 * than the number of characters specified by the character send
130 * threshold. This parameter can be modified by the set SOL
Tom Joseph704f3422017-04-26 10:13:03 +0530131 * configuration parameters command. The SOL configuration parameter,
132 * Character Accumulate Interval is 5 ms increments, 1-based value. The
133 * parameter value is accumulateInterval/5. The accumulateInterval
134 * needs to be a multiple of 5.
Tom Joseph5a454a22017-02-15 14:51:42 +0530135 */
Tom Joseph704f3422017-04-26 10:13:03 +0530136 std::chrono::milliseconds accumulateInterval = 100ms;
Tom Joseph5a454a22017-02-15 14:51:42 +0530137
138 /** @brief Character Send Threshold
139 *
140 * The BMC will automatically send an SOL character data packet
141 * containing this number of characters as soon as this number of
142 * characters (or greater) has been accepted from the baseboard serial
143 * controller into the BMC. This provides a mechanism to tune the
144 * buffer to reduce latency to when the first characters are received
145 * after an idle interval. In the degenerate case, setting this value
146 * to a ‘1’ would cause the BMC to send a packet as soon as the first
147 * character was received. This parameter can be modified by the set
148 * SOL configuration parameters command.
149 */
150 uint8_t sendThreshold = 1;
151
152 /** @brief Retry Count
153 *
154 * 1-based. 0 = no retries after packet is transmitted. Packet will be
155 * dropped if no ACK/NACK received by time retries expire. The maximum
156 * value for retry count is 7. This parameter can be modified by the
157 * set SOL configuration parameters command.
158 */
159 uint8_t retryCount = 7;
160
161 /** @brief Retry Interval
162 *
Tom Joseph704f3422017-04-26 10:13:03 +0530163 * Sets the time that the BMC will wait before the first retry and the
164 * time between retries when sending SOL packets to the remote console.
165 * This parameter can be modified by the set SOL configuration
166 * parameters command. The SOL configuration parameter Retry Interval
167 * is 10 ms increments, 1-based value. The parameter value is
168 * retryInterval/10. The retryInterval needs to be a multiple of 10.
Tom Joseph5a454a22017-02-15 14:51:42 +0530169 */
Tom Joseph704f3422017-04-26 10:13:03 +0530170 std::chrono::milliseconds retryInterval = 100ms;
Tom Joseph5a454a22017-02-15 14:51:42 +0530171
172 /** @brief Start a SOL payload instance.
173 *
174 * Starting a payload instance involves creating the context object,
175 * add the accumulate interval timer and retry interval timer to the
176 * event loop.
177 *
178 * @param[in] payloadInstance - SOL payload instance.
179 * @param[in] sessionID - BMC session ID.
180 */
181 void startPayloadInstance(uint8_t payloadInstance,
182 session::SessionID sessionID);
183
184 /** @brief Stop SOL payload instance.
185 *
186 * Stopping a payload instance involves stopping and removing the
187 * accumulate interval timer and retry interval timer from the event
188 * loop, delete the context object.
189 *
190 * @param[in] payloadInstance - SOL payload instance
191 */
192 void stopPayloadInstance(uint8_t payloadInstance);
193
194 /** @brief Get SOL Context by Payload Instance.
195 *
196 * @param[in] payloadInstance - SOL payload instance.
197 *
198 * @return reference to the SOL payload context.
199 */
200 Context& getContext(uint8_t payloadInstance)
201 {
202 auto iter = payloadMap.find(payloadInstance);
203
204 if (iter != payloadMap.end())
205 {
206 return *(iter->second);
207 }
208
209 std::string msg = "Invalid SOL payload instance " + payloadInstance;
210 throw std::runtime_error(msg.c_str());
211 }
212
213 /** @brief Get SOL Context by Session ID.
214 *
215 * @param[in] sessionID - IPMI Session ID.
216 *
217 * @return reference to the SOL payload context.
218 */
219 Context& getContext(session::SessionID sessionID)
220 {
221 for (const auto& kv : payloadMap)
222 {
223 if (kv.second->sessionID == sessionID)
224 {
225 return *kv.second;
226 }
227 }
228
229 std::string msg = "Invalid SOL SessionID " + sessionID;
230 throw std::runtime_error(msg.c_str());
231 }
232
233 /** @brief Check if SOL payload is active.
234 *
235 * @param[in] payloadInstance - SOL payload instance.
236 *
237 * @return true if the instance is active and false it is not active.
238 */
239 auto isPayloadActive(uint8_t payloadInstance) const
240 {
241 return (0 != payloadMap.count(payloadInstance));
242 }
243
244 /** @brief Write data to the host console unix socket.
245 *
246 * @param[in] input - Data from the remote console.
247 *
248 * @return 0 on success and errno on failure.
249 */
250 int writeConsoleSocket(const Buffer& input) const;
251
252 private:
253 SOLPayloadMap payloadMap;
254
255 /** @brief File descriptor for the host console. */
256 std::unique_ptr<CustomFD> consoleFD = nullptr;
257
258 /** @brief Initialize the host console file descriptor. */
259 void initHostConsoleFd();
260};
261
262} //namespace sol