summaryrefslogtreecommitdiffstats
path: root/sol/sol_manager.hpp
blob: e853acfbcda542d86c6c653f9af9115f7f565795 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#pragma once

#include <map>
#include <memory>
#include "console_buffer.hpp"
#include "session.hpp"
#include "sol_context.hpp"

namespace sol
{

constexpr size_t MAX_PAYLOAD_INSTANCES = 16;
constexpr size_t MAX_PAYLOAD_SIZE = 255;
constexpr uint8_t MAJOR_VERSION = 0x01;
constexpr uint8_t MINOR_VERSION = 0x00;

constexpr char CONSOLE_SOCKET_PATH[] = "\0obmc-console";
constexpr size_t CONSOLE_SOCKET_PATH_LEN = sizeof(CONSOLE_SOCKET_PATH) - 1;

using Instance = uint8_t;

/** @struct CustomFD
 *
 *  RAII wrapper for file descriptor.
 */
struct CustomFD
{
    CustomFD(const CustomFD&) = delete;
    CustomFD& operator=(const CustomFD&) = delete;
    CustomFD(CustomFD&&) = delete;
    CustomFD& operator=(CustomFD&&) = delete;

    CustomFD(int fd) :
        fd(fd) {}

    ~CustomFD();

    int operator()() const
    {
        return fd;
    }

    private:
        int fd = -1;
};

using namespace std::chrono_literals;

/** @class Manager
 *
 *  Manager class acts a manager for the SOL payload instances and provides
 *  interfaces to start a payload instance, stop a payload instance and get
 *  reference to the context object.
 */
class Manager
{
    public:

        /** @brief SOL Payload Instance is the key for the map, the value is the
         *         SOL context.
         */
        using SOLPayloadMap = std::map<Instance, std::unique_ptr<Context>>;

        Manager() = default;
        ~Manager() = default;
        Manager(const Manager&) = delete;
        Manager& operator=(const Manager&) = delete;
        Manager(Manager&&) = default;
        Manager& operator=(Manager&&) = default;

        /** @brief Host Console Buffer. */
        ConsoleData dataBuffer;

        /** @brief Character Accumulate Interval
         *
         *  This sets the typical amount of time that the BMC will wait before
         *  transmitting a partial SOL character data packet. (Where a partial
         *  packet is defined as a packet that has fewer characters to transmit
         *  than the number of characters specified by the character send
         *  threshold. This parameter can be modified by the set SOL
         *  configuration parameters command. The SOL configuration parameter,
         *  Character Accumulate Interval is 5 ms increments, 1-based value. The
         *  parameter value is accumulateInterval/5. The accumulateInterval
         *  needs to be a multiple of 5.
         */
        std::chrono::milliseconds accumulateInterval = 100ms;

        /** @brief Character Send Threshold
         *
         *  The BMC will automatically send an SOL character data packet
         *  containing this number of characters as soon as this number of
         *  characters (or greater) has been accepted from the baseboard serial
         *  controller into the BMC. This provides a mechanism to tune the
         *  buffer to reduce latency to when the first characters are received
         *  after an idle interval. In the degenerate case, setting this value
         *  to a ‘1’ would cause the BMC to send a packet as soon as the first
         *  character was received. This parameter can be modified by the set
         *  SOL configuration parameters command.
         */
        uint8_t sendThreshold = 1;

        /** @brief Retry Count
         *
         *  1-based. 0 = no retries after packet is transmitted. Packet will be
         *  dropped if no ACK/NACK received by time retries expire. The maximum
         *  value for retry count is 7. This parameter can be modified by the
         *  set SOL configuration parameters command.
         */
        uint8_t retryCount = 7;

        /** @brief Retry Interval
         *
         *  Sets the time that the BMC will wait before the first retry and the
         *  time between retries when sending SOL packets to the remote console.
         *  This parameter can be modified by the set SOL configuration
         *  parameters command. The SOL configuration parameter Retry Interval
         *  is 10 ms increments, 1-based value. The parameter value is
         *  retryInterval/10. The retryInterval needs to be a multiple of 10.
         */
        std::chrono::milliseconds retryInterval = 100ms;

        /** @brief Start a SOL payload instance.
         *
         *  Starting a payload instance involves creating the context object,
         *  add the accumulate interval timer and retry interval timer to the
         *  event loop.
         *
         *  @param[in] payloadInstance - SOL payload instance.
         *  @param[in] sessionID - BMC session ID.
         */
        void startPayloadInstance(uint8_t payloadInstance,
                                  session::SessionID sessionID);

        /** @brief Stop SOL payload instance.
         *
         *  Stopping a payload instance involves stopping and removing the
         *  accumulate interval timer and retry interval timer from the event
         *  loop, delete the context object.
         *
         *  @param[in] payloadInstance - SOL payload instance
         */
        void stopPayloadInstance(uint8_t payloadInstance);

        /** @brief Get SOL Context by Payload Instance.
         *
         *  @param[in] payloadInstance - SOL payload instance.
         *
         *  @return reference to the SOL payload context.
         */
        Context& getContext(uint8_t payloadInstance)
        {
            auto iter = payloadMap.find(payloadInstance);

            if (iter != payloadMap.end())
            {
                return *(iter->second);
            }

            std::string msg = "Invalid SOL payload instance " + payloadInstance;
            throw std::runtime_error(msg.c_str());
         }

        /** @brief Get SOL Context by Session ID.
         *
         *  @param[in] sessionID - IPMI Session ID.
         *
         *  @return reference to the SOL payload context.
         */
        Context& getContext(session::SessionID sessionID)
        {
            for (const auto& kv : payloadMap)
            {
                if (kv.second->sessionID == sessionID)
                {
                    return *kv.second;
                }
            }

            std::string msg = "Invalid SOL SessionID " + sessionID;
            throw std::runtime_error(msg.c_str());
        }

        /** @brief Check if SOL payload is active.
         *
         *  @param[in] payloadInstance - SOL payload instance.
         *
         *  @return true if the instance is active and false it is not active.
         */
        auto isPayloadActive(uint8_t payloadInstance) const
        {
            return (0 != payloadMap.count(payloadInstance));
        }

        /** @brief Write data to the host console unix socket.
         *
         *  @param[in] input - Data from the remote console.
         *
         *  @return 0 on success and errno on failure.
         */
        int writeConsoleSocket(const Buffer& input) const;

    private:
        SOLPayloadMap payloadMap;

        /** @brief File descriptor for the host console. */
        std::unique_ptr<CustomFD> consoleFD = nullptr;

        /** @brief Initialize the host console file descriptor. */
        void initHostConsoleFd();
};

} //namespace sol
OpenPOWER on IntegriCloud