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