blob: b266519a86b63abe16651e703fbc9ec413c985c8 [file] [log] [blame]
Tom Joseph5a454a22017-02-15 14:51:42 +05301#pragma once
2
Tom Joseph5a454a22017-02-15 14:51:42 +05303#include "console_buffer.hpp"
Vernon Mauery2085ae02021-06-10 11:51:00 -07004#include "main.hpp"
Tom Joseph5a454a22017-02-15 14:51:42 +05305#include "session.hpp"
6#include "sol_context.hpp"
7
Vernon Mauery7e4a6512018-11-09 08:43:36 -08008#include <boost/asio/io_context.hpp>
9#include <boost/asio/local/stream_protocol.hpp>
Andrew Geissler9d9b7632020-05-17 09:18:05 -050010#include <cstddef>
Vernon Mauery9e801a22018-10-12 13:20:49 -070011#include <map>
12#include <memory>
Andrew Geissler7408e762020-05-17 08:56:05 -050013#include <string>
Vernon Mauery9e801a22018-10-12 13:20:49 -070014
Tom Joseph5a454a22017-02-15 14:51:42 +053015namespace sol
16{
17
Tom Joseph5a454a22017-02-15 14:51:42 +053018constexpr size_t MAX_PAYLOAD_SIZE = 255;
19constexpr uint8_t MAJOR_VERSION = 0x01;
20constexpr uint8_t MINOR_VERSION = 0x00;
21
22constexpr char CONSOLE_SOCKET_PATH[] = "\0obmc-console";
23constexpr size_t CONSOLE_SOCKET_PATH_LEN = sizeof(CONSOLE_SOCKET_PATH) - 1;
24
Tom Joseph1967cf52017-04-28 01:24:22 +053025constexpr uint8_t accIntervalFactor = 5;
26constexpr uint8_t retryIntervalFactor = 10;
27
Tom Joseph5a454a22017-02-15 14:51:42 +053028using Instance = uint8_t;
29
Tom Joseph704f3422017-04-26 10:13:03 +053030using namespace std::chrono_literals;
Tom Joseph5a454a22017-02-15 14:51:42 +053031
32/** @class Manager
33 *
34 * Manager class acts a manager for the SOL payload instances and provides
35 * interfaces to start a payload instance, stop a payload instance and get
36 * reference to the context object.
37 */
38class Manager
39{
Vernon Mauery2085ae02021-06-10 11:51:00 -070040 private:
41 struct Private
42 {
43 };
44
Vernon Mauery9e801a22018-10-12 13:20:49 -070045 public:
46 /** @brief SOL Payload Instance is the key for the map, the value is the
47 * SOL context.
48 */
Vernon Mauerya6ad5e12020-02-21 14:54:24 -080049 using SOLPayloadMap = std::map<Instance, std::shared_ptr<Context>>;
Tom Joseph5a454a22017-02-15 14:51:42 +053050
Vernon Mauery7e4a6512018-11-09 08:43:36 -080051 Manager() = delete;
Vernon Mauery9e801a22018-10-12 13:20:49 -070052 ~Manager() = default;
53 Manager(const Manager&) = delete;
54 Manager& operator=(const Manager&) = delete;
55 Manager(Manager&&) = default;
56 Manager& operator=(Manager&&) = default;
Tom Joseph5a454a22017-02-15 14:51:42 +053057
Vernon Mauery2085ae02021-06-10 11:51:00 -070058 Manager(std::shared_ptr<boost::asio::io_context>& io, const Private&) :
59 io(io)
Vernon Mauery7e4a6512018-11-09 08:43:36 -080060 {
61 }
62
Vernon Mauery2085ae02021-06-10 11:51:00 -070063 /**
64 * @brief Get a reference to the singleton Manager
65 *
66 * @return Manager reference
67 */
68 static Manager& get()
69 {
70 static std::shared_ptr<Manager> ptr = nullptr;
71 if (!ptr)
72 {
73 std::shared_ptr<boost::asio::io_context> io = getIo();
74 ptr = std::make_shared<Manager>(io, Private());
75 }
76 return *ptr;
77 }
78
Vernon Mauery7e4a6512018-11-09 08:43:36 -080079 /** @brief io context to add events to */
80 std::shared_ptr<boost::asio::io_context> io;
81
Vernon Mauery9e801a22018-10-12 13:20:49 -070082 /** @brief Host Console Buffer. */
83 ConsoleData dataBuffer;
Tom Joseph5a454a22017-02-15 14:51:42 +053084
Vernon Mauery9e801a22018-10-12 13:20:49 -070085 /** @brief Set in Progress.
86 *
87 * This parameter is used to indicate when any of the SOL parameters
88 * are being updated, and when the changes are completed. The bit is
89 * primarily provided to alert software than some other software or
90 * utility is in the process of making changes to the data. This field
91 * is initialized to set complete.
92 */
93 uint8_t progress = 0;
Tom Joseph5a454a22017-02-15 14:51:42 +053094
Vernon Mauery9e801a22018-10-12 13:20:49 -070095 /** @brief SOL enable
96 *
97 * This controls whether the SOL payload can be activated. By default
98 * the SOL is enabled.
99 */
100 bool enable = true;
Tom Joseph1967cf52017-04-28 01:24:22 +0530101
Vernon Mauery9e801a22018-10-12 13:20:49 -0700102 /** @brief SOL payload encryption.
103 *
104 * Force encryption: if the cipher suite for the session supports
105 * encryption, then this setting will force the use of encryption for
106 * all SOL payload data. Encryption controlled by remote console:
107 * Whether SOL packets are encrypted or not is selectable by the remote
108 * console at the time the payload is activated. The default is force
109 * encryption.
110 */
111 bool forceEncrypt = true;
Tom Joseph1967cf52017-04-28 01:24:22 +0530112
Vernon Mauery9e801a22018-10-12 13:20:49 -0700113 /** @brief SOL payload authentication.
114 *
115 * Force authentication: if the cipher suite for the session supports
116 * authentication, then this setting will force the use of for
117 * authentication for all SOL payload data. Authentication controlled
118 * by remote console: Note that for the standard Cipher Suites,
119 * if encryption is used authentication must also be used. Therefore,
120 * while encryption is being used software will not be able to select
121 * using unauthenticated payloads.
122 */
123 bool forceAuth = true;
Tom Joseph1967cf52017-04-28 01:24:22 +0530124
Vernon Mauery9e801a22018-10-12 13:20:49 -0700125 /** @brief SOL privilege level.
126 *
127 * Sets the minimum operating privilege level that is required to be
128 * able to activate SOL using the Activate Payload command.
129 */
130 session::Privilege solMinPrivilege = session::Privilege::USER;
Tom Joseph1967cf52017-04-28 01:24:22 +0530131
Vernon Mauery9e801a22018-10-12 13:20:49 -0700132 /** @brief Character Accumulate Interval
133 *
134 * This sets the typical amount of time that the BMC will wait before
135 * transmitting a partial SOL character data packet. (Where a partial
136 * packet is defined as a packet that has fewer characters to transmit
137 * than the number of characters specified by the character send
138 * threshold. This parameter can be modified by the set SOL
139 * configuration parameters command. The SOL configuration parameter,
140 * Character Accumulate Interval is 5 ms increments, 1-based value. The
141 * parameter value is accumulateInterval/5. The accumulateInterval
142 * needs to be a multiple of 5.
143 */
144 std::chrono::milliseconds accumulateInterval = 100ms;
Tom Joseph1967cf52017-04-28 01:24:22 +0530145
Vernon Mauery9e801a22018-10-12 13:20:49 -0700146 /** @brief Character Send Threshold
147 *
148 * The BMC will automatically send an SOL character data packet
149 * containing this number of characters as soon as this number of
150 * characters (or greater) has been accepted from the baseboard serial
151 * controller into the BMC. This provides a mechanism to tune the
152 * buffer to reduce latency to when the first characters are received
153 * after an idle interval. In the degenerate case, setting this value
154 * to a ‘1’ would cause the BMC to send a packet as soon as the first
155 * character was received. This parameter can be modified by the set
156 * SOL configuration parameters command.
157 */
158 uint8_t sendThreshold = 1;
Tom Joseph5a454a22017-02-15 14:51:42 +0530159
Vernon Mauery9e801a22018-10-12 13:20:49 -0700160 /** @brief Retry Count
161 *
162 * 1-based. 0 = no retries after packet is transmitted. Packet will be
163 * dropped if no ACK/NACK received by time retries expire. The maximum
164 * value for retry count is 7. This parameter can be modified by the
165 * set SOL configuration parameters command.
166 */
167 uint8_t retryCount = 7;
Tom Joseph5a454a22017-02-15 14:51:42 +0530168
Vernon Mauery9e801a22018-10-12 13:20:49 -0700169 /** @brief Retry Interval
170 *
171 * Sets the time that the BMC will wait before the first retry and the
172 * time between retries when sending SOL packets to the remote console.
173 * This parameter can be modified by the set SOL configuration
174 * parameters command. The SOL configuration parameter Retry Interval
175 * is 10 ms increments, 1-based value. The parameter value is
176 * retryInterval/10. The retryInterval needs to be a multiple of 10.
177 */
178 std::chrono::milliseconds retryInterval = 100ms;
Tom Joseph5a454a22017-02-15 14:51:42 +0530179
Vernon Mauery9e801a22018-10-12 13:20:49 -0700180 /** @brief Channel Number
181 *
182 * This parameter indicates which IPMI channel is being used for the
183 * communication parameters (e.g. IP address, MAC address) for the SOL
184 * Payload. Typically, these parameters will come from the same channel
185 * that the Activate Payload command for SOL was accepted over. The
186 * network channel number is defaulted to 1.
187 */
188 uint8_t channel = 1;
Tom Joseph5a454a22017-02-15 14:51:42 +0530189
Vernon Mauery7e4a6512018-11-09 08:43:36 -0800190 /** @brief Add host console I/O event source to the event loop. */
191 void startHostConsole();
192
193 /** @brief Remove host console I/O event source. */
194 void stopHostConsole();
195
Vernon Mauery9e801a22018-10-12 13:20:49 -0700196 /** @brief Start a SOL payload instance.
197 *
198 * Starting a payload instance involves creating the context object,
199 * add the accumulate interval timer and retry interval timer to the
200 * event loop.
201 *
202 * @param[in] payloadInstance - SOL payload instance.
203 * @param[in] sessionID - BMC session ID.
204 */
205 void startPayloadInstance(uint8_t payloadInstance,
206 session::SessionID sessionID);
Tom Joseph6f83cbc2018-03-27 03:01:58 +0530207
Vernon Mauery9e801a22018-10-12 13:20:49 -0700208 /** @brief Stop SOL payload instance.
209 *
210 * Stopping a payload instance involves stopping and removing the
211 * accumulate interval timer and retry interval timer from the event
212 * loop, delete the context object.
213 *
214 * @param[in] payloadInstance - SOL payload instance
215 */
216 void stopPayloadInstance(uint8_t payloadInstance);
Tom Joseph5a454a22017-02-15 14:51:42 +0530217
srikanta mondalf6e72302020-06-08 12:46:28 +0000218 /* @brief Stop all the active SOL payload instances */
219 void stopAllPayloadInstance();
220
Vernon Mauery9e801a22018-10-12 13:20:49 -0700221 /** @brief Get SOL Context by Payload Instance.
222 *
223 * @param[in] payloadInstance - SOL payload instance.
224 *
225 * @return reference to the SOL payload context.
226 */
227 Context& getContext(uint8_t payloadInstance)
228 {
229 auto iter = payloadMap.find(payloadInstance);
Tom Joseph5a454a22017-02-15 14:51:42 +0530230
Vernon Mauery9e801a22018-10-12 13:20:49 -0700231 if (iter != payloadMap.end())
Tom Joseph5a454a22017-02-15 14:51:42 +0530232 {
Vernon Mauery9e801a22018-10-12 13:20:49 -0700233 return *(iter->second);
Tom Joseph5a454a22017-02-15 14:51:42 +0530234 }
235
Vernon Mauery9e801a22018-10-12 13:20:49 -0700236 std::string msg = "Invalid SOL payload instance " + payloadInstance;
237 throw std::runtime_error(msg.c_str());
238 }
239
240 /** @brief Get SOL Context by Session ID.
241 *
242 * @param[in] sessionID - IPMI Session ID.
243 *
244 * @return reference to the SOL payload context.
245 */
246 Context& getContext(session::SessionID sessionID)
247 {
248 for (const auto& kv : payloadMap)
Tom Joseph5a454a22017-02-15 14:51:42 +0530249 {
Vernon Mauery9e801a22018-10-12 13:20:49 -0700250 if (kv.second->sessionID == sessionID)
251 {
252 return *kv.second;
253 }
Tom Joseph5a454a22017-02-15 14:51:42 +0530254 }
255
Vernon Mauery9e801a22018-10-12 13:20:49 -0700256 std::string msg = "Invalid SOL SessionID " + sessionID;
257 throw std::runtime_error(msg.c_str());
258 }
Tom Joseph5a454a22017-02-15 14:51:42 +0530259
Vernon Mauery9e801a22018-10-12 13:20:49 -0700260 /** @brief Check if SOL payload is active.
261 *
262 * @param[in] payloadInstance - SOL payload instance.
263 *
264 * @return true if the instance is active and false it is not active.
265 */
266 auto isPayloadActive(uint8_t payloadInstance) const
267 {
268 return (0 != payloadMap.count(payloadInstance));
269 }
Tom Joseph5a454a22017-02-15 14:51:42 +0530270
Vernon Mauery9e801a22018-10-12 13:20:49 -0700271 /** @brief Write data to the host console unix socket.
272 *
273 * @param[in] input - Data from the remote console.
274 *
275 * @return 0 on success and errno on failure.
276 */
277 int writeConsoleSocket(const std::vector<uint8_t>& input) const;
Cheng C Yang29086952020-03-09 15:29:18 +0800278 void updateSOLParameter(uint8_t channelNum);
Tom Joseph5a454a22017-02-15 14:51:42 +0530279
Vernon Mauery9e801a22018-10-12 13:20:49 -0700280 private:
281 SOLPayloadMap payloadMap;
282
Vernon Mauery7e4a6512018-11-09 08:43:36 -0800283 /** @brief Local stream socket for the host console. */
284 std::unique_ptr<boost::asio::local::stream_protocol::socket> consoleSocket =
285 nullptr;
Vernon Mauery9e801a22018-10-12 13:20:49 -0700286
287 /** @brief Initialize the host console file descriptor. */
Vernon Mauery7e4a6512018-11-09 08:43:36 -0800288 void initConsoleSocket();
289
290 /** @brief Handle incoming console data on the console socket */
291 void consoleInputHandler();
Tom Joseph5a454a22017-02-15 14:51:42 +0530292};
293
srikanta mondalf6e72302020-06-08 12:46:28 +0000294/** @brief Callback method to close SOL sessions for SOL service change */
295void registerSOLServiceChangeCallback();
296
Vernon Mauery9e801a22018-10-12 13:20:49 -0700297} // namespace sol