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