xref: /openbmc/phosphor-net-ipmid/sol/sol_manager.hpp (revision c936ecaa4f4480fd18455ca573471aa0db0caa7d)
1 #pragma once
2 
3 #include "console_buffer.hpp"
4 #include "main.hpp"
5 #include "session.hpp"
6 #include "sol_context.hpp"
7 
8 #include <boost/asio/io_context.hpp>
9 #include <boost/asio/local/stream_protocol.hpp>
10 #include <cstddef>
11 #include <map>
12 #include <memory>
13 #include <string>
14 
15 namespace sol
16 {
17 
18 constexpr size_t MAX_PAYLOAD_SIZE = 255;
19 constexpr uint8_t MAJOR_VERSION = 0x01;
20 constexpr uint8_t MINOR_VERSION = 0x00;
21 
22 constexpr char CONSOLE_SOCKET_PATH[] = "\0obmc-console";
23 constexpr size_t CONSOLE_SOCKET_PATH_LEN = sizeof(CONSOLE_SOCKET_PATH) - 1;
24 
25 constexpr uint8_t accIntervalFactor = 5;
26 constexpr uint8_t retryIntervalFactor = 10;
27 
28 using Instance = uint8_t;
29 
30 using namespace std::chrono_literals;
31 
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  */
38 class Manager
39 {
40   private:
41     struct Private
42     {
43     };
44 
45   public:
46     /** @brief SOL Payload Instance is the key for the map, the value is the
47      *         SOL context.
48      */
49     using SOLPayloadMap = std::map<Instance, std::shared_ptr<Context>>;
50 
51     Manager() = delete;
52     ~Manager() = default;
53     Manager(const Manager&) = delete;
54     Manager& operator=(const Manager&) = delete;
55     Manager(Manager&&) = default;
56     Manager& operator=(Manager&&) = default;
57 
58     Manager(std::shared_ptr<boost::asio::io_context>& io, const Private&) :
59         io(io)
60     {
61     }
62 
63     /**
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 
79     /** @brief io context to add events to */
80     std::shared_ptr<boost::asio::io_context> io;
81 
82     /** @brief Host Console Buffer. */
83     ConsoleData dataBuffer;
84 
85     /** @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;
94 
95     /** @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;
101 
102     /** @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;
112 
113     /** @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;
124 
125     /** @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;
131 
132     /** @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;
145 
146     /** @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;
159 
160     /** @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;
168 
169     /** @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;
179 
180     /** @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;
189 
190     /** @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 
196     /** @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);
207 
208     /** @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);
217 
218     /* @brief Stop all the active SOL payload instances */
219     void stopAllPayloadInstance();
220 
221     /** @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);
230 
231         if (iter != payloadMap.end())
232         {
233             return *(iter->second);
234         }
235 
236         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)
249         {
250             if (kv.second->sessionID == sessionID)
251             {
252                 return *kv.second;
253             }
254         }
255 
256         std::string msg = "Invalid SOL SessionID " + sessionID;
257         throw std::runtime_error(msg.c_str());
258     }
259 
260     /** @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     }
270 
271     /** @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;
278     void updateSOLParameter(uint8_t channelNum);
279 
280   private:
281     SOLPayloadMap payloadMap;
282 
283     /** @brief Local stream socket for the host console. */
284     std::unique_ptr<boost::asio::local::stream_protocol::socket> consoleSocket =
285         nullptr;
286 
287     /** @brief Initialize the host console file descriptor. */
288     void initConsoleSocket();
289 
290     /** @brief Handle incoming console data on the console socket */
291     void consoleInputHandler();
292 };
293 
294 /** @brief Callback method to close SOL sessions for SOL service change  */
295 void registerSOLServiceChangeCallback();
296 
297 /** @brief Callback register method to SOL conf parameters change */
298 void registerSolConfChangeCallbackHandler(std::string channel);
299 
300 } // namespace sol
301