Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 3 | #include "console_buffer.hpp" |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 4 | #include "main.hpp" |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 5 | #include "session.hpp" |
| 6 | #include "sol_context.hpp" |
| 7 | |
Vernon Mauery | 7e4a651 | 2018-11-09 08:43:36 -0800 | [diff] [blame] | 8 | #include <boost/asio/io_context.hpp> |
| 9 | #include <boost/asio/local/stream_protocol.hpp> |
George Liu | bc8958f | 2022-07-04 09:29:49 +0800 | [diff] [blame] | 10 | |
Andrew Geissler | 9d9b763 | 2020-05-17 09:18:05 -0500 | [diff] [blame] | 11 | #include <cstddef> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 12 | #include <map> |
| 13 | #include <memory> |
Andrew Geissler | 7408e76 | 2020-05-17 08:56:05 -0500 | [diff] [blame] | 14 | #include <string> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 15 | |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 16 | namespace sol |
| 17 | { |
| 18 | |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 19 | constexpr size_t MAX_PAYLOAD_SIZE = 255; |
| 20 | constexpr uint8_t MAJOR_VERSION = 0x01; |
| 21 | constexpr uint8_t MINOR_VERSION = 0x00; |
| 22 | |
| 23 | constexpr char CONSOLE_SOCKET_PATH[] = "\0obmc-console"; |
| 24 | constexpr size_t CONSOLE_SOCKET_PATH_LEN = sizeof(CONSOLE_SOCKET_PATH) - 1; |
| 25 | |
Tom Joseph | 1967cf5 | 2017-04-28 01:24:22 +0530 | [diff] [blame] | 26 | constexpr uint8_t accIntervalFactor = 5; |
| 27 | constexpr uint8_t retryIntervalFactor = 10; |
| 28 | |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 29 | using Instance = uint8_t; |
| 30 | |
Tom Joseph | 704f342 | 2017-04-26 10:13:03 +0530 | [diff] [blame] | 31 | using namespace std::chrono_literals; |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 32 | |
| 33 | /** @class Manager |
| 34 | * |
| 35 | * Manager class acts a manager for the SOL payload instances and provides |
| 36 | * interfaces to start a payload instance, stop a payload instance and get |
| 37 | * reference to the context object. |
| 38 | */ |
| 39 | class Manager |
| 40 | { |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 41 | private: |
| 42 | struct Private |
George Liu | bc8958f | 2022-07-04 09:29:49 +0800 | [diff] [blame] | 43 | {}; |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 44 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 45 | public: |
| 46 | /** @brief SOL Payload Instance is the key for the map, the value is the |
| 47 | * SOL context. |
| 48 | */ |
Vernon Mauery | a6ad5e1 | 2020-02-21 14:54:24 -0800 | [diff] [blame] | 49 | using SOLPayloadMap = std::map<Instance, std::shared_ptr<Context>>; |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 50 | |
Vernon Mauery | 7e4a651 | 2018-11-09 08:43:36 -0800 | [diff] [blame] | 51 | Manager() = delete; |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 52 | ~Manager() = default; |
| 53 | Manager(const Manager&) = delete; |
| 54 | Manager& operator=(const Manager&) = delete; |
| 55 | Manager(Manager&&) = default; |
| 56 | Manager& operator=(Manager&&) = default; |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 57 | |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 58 | Manager(std::shared_ptr<boost::asio::io_context>& io, const Private&) : |
| 59 | io(io) |
George Liu | bc8958f | 2022-07-04 09:29:49 +0800 | [diff] [blame] | 60 | {} |
Vernon Mauery | 7e4a651 | 2018-11-09 08:43:36 -0800 | [diff] [blame] | 61 | |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 62 | /** |
| 63 | * @brief Get a reference to the singleton Manager |
| 64 | * |
| 65 | * @return Manager reference |
| 66 | */ |
| 67 | static Manager& get() |
| 68 | { |
| 69 | static std::shared_ptr<Manager> ptr = nullptr; |
| 70 | if (!ptr) |
| 71 | { |
| 72 | std::shared_ptr<boost::asio::io_context> io = getIo(); |
| 73 | ptr = std::make_shared<Manager>(io, Private()); |
| 74 | } |
| 75 | return *ptr; |
| 76 | } |
| 77 | |
Vernon Mauery | 7e4a651 | 2018-11-09 08:43:36 -0800 | [diff] [blame] | 78 | /** @brief io context to add events to */ |
| 79 | std::shared_ptr<boost::asio::io_context> io; |
| 80 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 81 | /** @brief Host Console Buffer. */ |
| 82 | ConsoleData dataBuffer; |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 83 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 84 | /** @brief Set in Progress. |
| 85 | * |
| 86 | * This parameter is used to indicate when any of the SOL parameters |
| 87 | * are being updated, and when the changes are completed. The bit is |
| 88 | * primarily provided to alert software than some other software or |
| 89 | * utility is in the process of making changes to the data. This field |
| 90 | * is initialized to set complete. |
| 91 | */ |
| 92 | uint8_t progress = 0; |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 93 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 94 | /** @brief SOL enable |
| 95 | * |
| 96 | * This controls whether the SOL payload can be activated. By default |
| 97 | * the SOL is enabled. |
| 98 | */ |
| 99 | bool enable = true; |
Tom Joseph | 1967cf5 | 2017-04-28 01:24:22 +0530 | [diff] [blame] | 100 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 101 | /** @brief SOL payload encryption. |
| 102 | * |
| 103 | * Force encryption: if the cipher suite for the session supports |
| 104 | * encryption, then this setting will force the use of encryption for |
| 105 | * all SOL payload data. Encryption controlled by remote console: |
| 106 | * Whether SOL packets are encrypted or not is selectable by the remote |
| 107 | * console at the time the payload is activated. The default is force |
| 108 | * encryption. |
| 109 | */ |
| 110 | bool forceEncrypt = true; |
Tom Joseph | 1967cf5 | 2017-04-28 01:24:22 +0530 | [diff] [blame] | 111 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 112 | /** @brief SOL payload authentication. |
| 113 | * |
| 114 | * Force authentication: if the cipher suite for the session supports |
| 115 | * authentication, then this setting will force the use of for |
| 116 | * authentication for all SOL payload data. Authentication controlled |
| 117 | * by remote console: Note that for the standard Cipher Suites, |
| 118 | * if encryption is used authentication must also be used. Therefore, |
| 119 | * while encryption is being used software will not be able to select |
| 120 | * using unauthenticated payloads. |
| 121 | */ |
| 122 | bool forceAuth = true; |
Tom Joseph | 1967cf5 | 2017-04-28 01:24:22 +0530 | [diff] [blame] | 123 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 124 | /** @brief SOL privilege level. |
| 125 | * |
| 126 | * Sets the minimum operating privilege level that is required to be |
| 127 | * able to activate SOL using the Activate Payload command. |
| 128 | */ |
| 129 | session::Privilege solMinPrivilege = session::Privilege::USER; |
Tom Joseph | 1967cf5 | 2017-04-28 01:24:22 +0530 | [diff] [blame] | 130 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 131 | /** @brief Character Accumulate Interval |
| 132 | * |
| 133 | * This sets the typical amount of time that the BMC will wait before |
| 134 | * transmitting a partial SOL character data packet. (Where a partial |
| 135 | * packet is defined as a packet that has fewer characters to transmit |
| 136 | * than the number of characters specified by the character send |
| 137 | * threshold. This parameter can be modified by the set SOL |
| 138 | * configuration parameters command. The SOL configuration parameter, |
| 139 | * Character Accumulate Interval is 5 ms increments, 1-based value. The |
| 140 | * parameter value is accumulateInterval/5. The accumulateInterval |
| 141 | * needs to be a multiple of 5. |
| 142 | */ |
| 143 | std::chrono::milliseconds accumulateInterval = 100ms; |
Tom Joseph | 1967cf5 | 2017-04-28 01:24:22 +0530 | [diff] [blame] | 144 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 145 | /** @brief Character Send Threshold |
| 146 | * |
| 147 | * The BMC will automatically send an SOL character data packet |
| 148 | * containing this number of characters as soon as this number of |
| 149 | * characters (or greater) has been accepted from the baseboard serial |
| 150 | * controller into the BMC. This provides a mechanism to tune the |
| 151 | * buffer to reduce latency to when the first characters are received |
| 152 | * after an idle interval. In the degenerate case, setting this value |
| 153 | * to a ‘1’ would cause the BMC to send a packet as soon as the first |
| 154 | * character was received. This parameter can be modified by the set |
| 155 | * SOL configuration parameters command. |
| 156 | */ |
| 157 | uint8_t sendThreshold = 1; |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 158 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 159 | /** @brief Retry Count |
| 160 | * |
| 161 | * 1-based. 0 = no retries after packet is transmitted. Packet will be |
| 162 | * dropped if no ACK/NACK received by time retries expire. The maximum |
| 163 | * value for retry count is 7. This parameter can be modified by the |
| 164 | * set SOL configuration parameters command. |
| 165 | */ |
| 166 | uint8_t retryCount = 7; |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 167 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 168 | /** @brief Retry Interval |
| 169 | * |
| 170 | * Sets the time that the BMC will wait before the first retry and the |
| 171 | * time between retries when sending SOL packets to the remote console. |
| 172 | * This parameter can be modified by the set SOL configuration |
| 173 | * parameters command. The SOL configuration parameter Retry Interval |
| 174 | * is 10 ms increments, 1-based value. The parameter value is |
| 175 | * retryInterval/10. The retryInterval needs to be a multiple of 10. |
| 176 | */ |
| 177 | std::chrono::milliseconds retryInterval = 100ms; |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 178 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 179 | /** @brief Channel Number |
| 180 | * |
| 181 | * This parameter indicates which IPMI channel is being used for the |
| 182 | * communication parameters (e.g. IP address, MAC address) for the SOL |
| 183 | * Payload. Typically, these parameters will come from the same channel |
| 184 | * that the Activate Payload command for SOL was accepted over. The |
| 185 | * network channel number is defaulted to 1. |
| 186 | */ |
| 187 | uint8_t channel = 1; |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 188 | |
Vernon Mauery | 7e4a651 | 2018-11-09 08:43:36 -0800 | [diff] [blame] | 189 | /** @brief Add host console I/O event source to the event loop. */ |
| 190 | void startHostConsole(); |
| 191 | |
| 192 | /** @brief Remove host console I/O event source. */ |
| 193 | void stopHostConsole(); |
| 194 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 195 | /** @brief Start a SOL payload instance. |
| 196 | * |
| 197 | * Starting a payload instance involves creating the context object, |
| 198 | * add the accumulate interval timer and retry interval timer to the |
| 199 | * event loop. |
| 200 | * |
| 201 | * @param[in] payloadInstance - SOL payload instance. |
| 202 | * @param[in] sessionID - BMC session ID. |
| 203 | */ |
| 204 | void startPayloadInstance(uint8_t payloadInstance, |
| 205 | session::SessionID sessionID); |
Tom Joseph | 6f83cbc | 2018-03-27 03:01:58 +0530 | [diff] [blame] | 206 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 207 | /** @brief Stop SOL payload instance. |
| 208 | * |
| 209 | * Stopping a payload instance involves stopping and removing the |
| 210 | * accumulate interval timer and retry interval timer from the event |
| 211 | * loop, delete the context object. |
| 212 | * |
| 213 | * @param[in] payloadInstance - SOL payload instance |
| 214 | */ |
| 215 | void stopPayloadInstance(uint8_t payloadInstance); |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 216 | |
srikanta mondal | f6e7230 | 2020-06-08 12:46:28 +0000 | [diff] [blame] | 217 | /* @brief Stop all the active SOL payload instances */ |
| 218 | void stopAllPayloadInstance(); |
| 219 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 220 | /** @brief Get SOL Context by Payload Instance. |
| 221 | * |
| 222 | * @param[in] payloadInstance - SOL payload instance. |
| 223 | * |
| 224 | * @return reference to the SOL payload context. |
| 225 | */ |
| 226 | Context& getContext(uint8_t payloadInstance) |
| 227 | { |
| 228 | auto iter = payloadMap.find(payloadInstance); |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 229 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 230 | if (iter != payloadMap.end()) |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 231 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 232 | return *(iter->second); |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 233 | } |
| 234 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 235 | std::string msg = "Invalid SOL payload instance " + payloadInstance; |
| 236 | throw std::runtime_error(msg.c_str()); |
| 237 | } |
| 238 | |
| 239 | /** @brief Get SOL Context by Session ID. |
| 240 | * |
| 241 | * @param[in] sessionID - IPMI Session ID. |
| 242 | * |
| 243 | * @return reference to the SOL payload context. |
| 244 | */ |
| 245 | Context& getContext(session::SessionID sessionID) |
| 246 | { |
| 247 | for (const auto& kv : payloadMap) |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 248 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 249 | if (kv.second->sessionID == sessionID) |
| 250 | { |
| 251 | return *kv.second; |
| 252 | } |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 253 | } |
| 254 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 255 | std::string msg = "Invalid SOL SessionID " + sessionID; |
| 256 | throw std::runtime_error(msg.c_str()); |
| 257 | } |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 258 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 259 | /** @brief Check if SOL payload is active. |
| 260 | * |
| 261 | * @param[in] payloadInstance - SOL payload instance. |
| 262 | * |
| 263 | * @return true if the instance is active and false it is not active. |
| 264 | */ |
| 265 | auto isPayloadActive(uint8_t payloadInstance) const |
| 266 | { |
| 267 | return (0 != payloadMap.count(payloadInstance)); |
| 268 | } |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 269 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 270 | /** @brief Write data to the host console unix socket. |
| 271 | * |
| 272 | * @param[in] input - Data from the remote console. |
| 273 | * |
| 274 | * @return 0 on success and errno on failure. |
| 275 | */ |
Tingting Chen | ec43741 | 2022-09-27 18:35:22 +0800 | [diff] [blame] | 276 | int writeConsoleSocket(const std::vector<uint8_t>& input, |
| 277 | bool breakFlag) const; |
Cheng C Yang | 2908695 | 2020-03-09 15:29:18 +0800 | [diff] [blame] | 278 | void updateSOLParameter(uint8_t channelNum); |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 279 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 280 | private: |
| 281 | SOLPayloadMap payloadMap; |
| 282 | |
Vernon Mauery | 7e4a651 | 2018-11-09 08:43:36 -0800 | [diff] [blame] | 283 | /** @brief Local stream socket for the host console. */ |
| 284 | std::unique_ptr<boost::asio::local::stream_protocol::socket> consoleSocket = |
| 285 | nullptr; |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 286 | |
| 287 | /** @brief Initialize the host console file descriptor. */ |
Vernon Mauery | 7e4a651 | 2018-11-09 08:43:36 -0800 | [diff] [blame] | 288 | void initConsoleSocket(); |
| 289 | |
| 290 | /** @brief Handle incoming console data on the console socket */ |
| 291 | void consoleInputHandler(); |
Tom Joseph | 5a454a2 | 2017-02-15 14:51:42 +0530 | [diff] [blame] | 292 | }; |
| 293 | |
srikanta mondal | f6e7230 | 2020-06-08 12:46:28 +0000 | [diff] [blame] | 294 | /** @brief Callback method to close SOL sessions for SOL service change */ |
| 295 | void registerSOLServiceChangeCallback(); |
| 296 | |
Jian Zhang | c936eca | 2022-03-22 11:25:29 +0800 | [diff] [blame] | 297 | /** @brief Callback register method to SOL conf parameters change */ |
| 298 | void registerSolConfChangeCallbackHandler(std::string channel); |
| 299 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 300 | } // namespace sol |