blob: 8407292c77b48086d696c294556489f03a8ce768 [file] [log] [blame]
Vernon Mauery9e801a22018-10-12 13:20:49 -07001#include "sol_manager.hpp"
2
3#include "main.hpp"
4#include "sol_context.hpp"
5
Tom Joseph22c5ad32017-03-14 18:04:22 +05306#include <sys/socket.h>
7#include <sys/un.h>
Vernon Mauery9e801a22018-10-12 13:20:49 -07008
Vernon Mauery7e4a6512018-11-09 08:43:36 -08009#include <boost/asio/basic_stream_socket.hpp>
10#include <boost/asio/io_context.hpp>
11#include <boost/asio/local/stream_protocol.hpp>
12#include <boost/asio/write.hpp>
Tom Joseph2e44e0e2017-03-14 18:09:11 +053013#include <chrono>
Tom Joseph22c5ad32017-03-14 18:04:22 +053014#include <cmath>
srikanta mondalf6e72302020-06-08 12:46:28 +000015#include <ipmid/utils.hpp>
Tom Joseph22c5ad32017-03-14 18:04:22 +053016#include <phosphor-logging/log.hpp>
Cheng C Yang29086952020-03-09 15:29:18 +080017#include <sdbusplus/message/types.hpp>
18
19constexpr const char* solInterface = "xyz.openbmc_project.Ipmi.SOL";
20constexpr const char* solPath = "/xyz/openbmc_project/ipmi/sol/";
21constexpr const char* PROP_INTF = "org.freedesktop.DBus.Properties";
Tom Joseph22c5ad32017-03-14 18:04:22 +053022
23namespace sol
24{
25
26using namespace phosphor::logging;
27
srikanta mondalf6e72302020-06-08 12:46:28 +000028std::unique_ptr<sdbusplus::bus::match_t> matchPtrSOL(nullptr);
29
Vernon Mauery7e4a6512018-11-09 08:43:36 -080030void Manager::initConsoleSocket()
Tom Josephb81f7612017-04-25 17:59:02 +053031{
Vernon Mauery7e4a6512018-11-09 08:43:36 -080032 // explicit length constructor for NUL-prefixed abstract path
33 std::string path(CONSOLE_SOCKET_PATH, CONSOLE_SOCKET_PATH_LEN);
34 boost::asio::local::stream_protocol::endpoint ep(path);
35 consoleSocket =
36 std::make_unique<boost::asio::local::stream_protocol::socket>(*io);
37 consoleSocket->connect(ep);
Tom Josephb81f7612017-04-25 17:59:02 +053038}
39
Vernon Mauery7e4a6512018-11-09 08:43:36 -080040void Manager::consoleInputHandler()
Tom Joseph22c5ad32017-03-14 18:04:22 +053041{
Vernon Mauery7e4a6512018-11-09 08:43:36 -080042 boost::system::error_code ec;
43 boost::asio::socket_base::bytes_readable cmd(true);
44 consoleSocket->io_control(cmd, ec);
45 size_t readSize;
46 if (!ec)
Tom Joseph22c5ad32017-03-14 18:04:22 +053047 {
Vernon Mauery7e4a6512018-11-09 08:43:36 -080048 readSize = cmd.get();
49 }
50 else
51 {
52 log<level::ERR>("Reading ready count from host console socket failed:",
53 entry("EXCEPTION=%s", ec.message().c_str()));
54 return;
55 }
56 std::vector<uint8_t> buffer(readSize);
57 ec.clear();
58 size_t readDataLen =
59 consoleSocket->read_some(boost::asio::buffer(buffer), ec);
60 if (ec)
61 {
62 log<level::ERR>("Reading from host console socket failed:",
63 entry("EXCEPTION=%s", ec.message().c_str()));
64 return;
Tom Joseph22c5ad32017-03-14 18:04:22 +053065 }
66
Vernon Mauery7e4a6512018-11-09 08:43:36 -080067 // Update the Console buffer with data read from the socket
68 buffer.resize(readDataLen);
69 dataBuffer.write(buffer);
Tom Joseph22c5ad32017-03-14 18:04:22 +053070}
71
Vernon Mauery70fd29c2017-11-30 13:11:43 -080072int Manager::writeConsoleSocket(const std::vector<uint8_t>& input) const
Tom Josephb51f6412017-03-14 18:08:14 +053073{
Vernon Mauery7e4a6512018-11-09 08:43:36 -080074 boost::system::error_code ec;
75 boost::asio::write(*consoleSocket, boost::asio::buffer(input), ec);
76 return ec.value();
77}
Tom Josephb51f6412017-03-14 18:08:14 +053078
Vernon Mauery7e4a6512018-11-09 08:43:36 -080079void Manager::startHostConsole()
80{
81 if (!consoleSocket)
Tom Josephb51f6412017-03-14 18:08:14 +053082 {
Vernon Mauery7e4a6512018-11-09 08:43:36 -080083 initConsoleSocket();
Tom Josephb51f6412017-03-14 18:08:14 +053084 }
srikanta mondalf6e72302020-06-08 12:46:28 +000085
86 // Register callback to close SOL session for disable SSH SOL
87 if (matchPtrSOL == nullptr)
88 {
89 registerSOLServiceChangeCallback();
90 }
91
Vernon Mauery7e4a6512018-11-09 08:43:36 -080092 consoleSocket->async_wait(boost::asio::socket_base::wait_read,
93 [this](const boost::system::error_code& ec) {
94 if (!ec)
95 {
96 consoleInputHandler();
97 startHostConsole();
98 }
99 });
Rashmi RV0f63e012019-11-21 15:38:50 +0530100} // namespace sol
Tom Josephb51f6412017-03-14 18:08:14 +0530101
Vernon Mauery7e4a6512018-11-09 08:43:36 -0800102void Manager::stopHostConsole()
103{
104 if (consoleSocket)
105 {
106 consoleSocket->cancel();
107 consoleSocket.reset();
108 }
Tom Josephb51f6412017-03-14 18:08:14 +0530109}
110
Cheng C Yang29086952020-03-09 15:29:18 +0800111void Manager::updateSOLParameter(uint8_t channelNum)
112{
113 std::variant<uint8_t, bool> value;
114 sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection());
115 static std::string solService{};
116 ipmi::PropertyMap properties;
117 std::string ethdevice = ipmi::getChannelName(channelNum);
118 std::string solPathWitheEthName = solPath + ethdevice;
119 if (solService.empty())
120 {
121 try
122 {
123 solService =
124 ipmi::getService(dbus, solInterface, solPathWitheEthName);
125 }
126 catch (const std::runtime_error& e)
127 {
128 solService.clear();
129 phosphor::logging::log<phosphor::logging::level::ERR>(
130 "Error: get SOL service failed");
131 return;
132 }
133 }
134 try
135 {
136 properties = ipmi::getAllDbusProperties(
137 dbus, solService, solPathWitheEthName, solInterface);
138 }
139 catch (const std::runtime_error&)
140 {
141 phosphor::logging::log<phosphor::logging::level::ERR>(
142 "Error setting sol parameter");
143 return;
144 }
145
146 progress = std::get<uint8_t>(properties["Progress"]);
147
148 enable = std::get<bool>(properties["Enable"]);
149
150 forceEncrypt = std::get<bool>(properties["ForceEncryption"]);
151
152 forceAuth = std::get<bool>(properties["ForceAuthentication"]);
153
154 solMinPrivilege = static_cast<session::Privilege>(
155 std::get<uint8_t>(properties["Privilege"]));
156
157 accumulateInterval =
158 std::get<uint8_t>((properties["AccumulateIntervalMS"])) *
159 sol::accIntervalFactor * 1ms;
160
161 sendThreshold = std::get<uint8_t>(properties["Threshold"]);
162
163 retryCount = std::get<uint8_t>(properties["RetryCount"]);
164
165 retryInterval = std::get<uint8_t>(properties["RetryIntervalMS"]) *
166 sol::retryIntervalFactor * 1ms;
167
168 return;
169}
170
Tom Joseph2e44e0e2017-03-14 18:09:11 +0530171void Manager::startPayloadInstance(uint8_t payloadInstance,
172 session::SessionID sessionID)
173{
174 if (payloadMap.empty())
175 {
Rashmi RV0f63e012019-11-21 15:38:50 +0530176 try
177 {
178 startHostConsole();
179 }
180 catch (const std::exception& e)
181 {
182 log<level::ERR>("Encountered exception when starting host console. "
183 "Hence stopping host console.",
184 entry("EXCEPTION=%s", e.what()));
185 stopHostConsole();
186 throw;
187 }
Tom Joseph2e44e0e2017-03-14 18:09:11 +0530188 }
189
190 // Create the SOL Context data for payload instance
Vernon Mauerya6ad5e12020-02-21 14:54:24 -0800191 std::shared_ptr<Context> context = Context::makeContext(
192 io, retryCount, sendThreshold, payloadInstance, sessionID);
Tom Joseph2e44e0e2017-03-14 18:09:11 +0530193
Tom Joseph2e44e0e2017-03-14 18:09:11 +0530194 payloadMap.emplace(payloadInstance, std::move(context));
195}
196
Tom Joseph4ff14b52017-03-14 18:10:52 +0530197void Manager::stopPayloadInstance(uint8_t payloadInstance)
198{
199 auto iter = payloadMap.find(payloadInstance);
200 if (iter == payloadMap.end())
201 {
202 throw std::runtime_error("SOL Payload instance not found ");
203 }
204
205 payloadMap.erase(iter);
206
Tom Joseph4ff14b52017-03-14 18:10:52 +0530207 if (payloadMap.empty())
208 {
Vernon Mauery7e4a6512018-11-09 08:43:36 -0800209 stopHostConsole();
Tom Josephde203ce2017-08-01 16:48:06 +0530210
211 dataBuffer.erase(dataBuffer.size());
Tom Joseph4ff14b52017-03-14 18:10:52 +0530212 }
213}
214
srikanta mondalf6e72302020-06-08 12:46:28 +0000215void Manager::stopAllPayloadInstance()
216{
217 // Erase all payload instance
218 payloadMap.erase(payloadMap.begin(), payloadMap.end());
219
220 stopHostConsole();
221
222 dataBuffer.erase(dataBuffer.size());
223}
224
225void registerSOLServiceChangeCallback()
226{
227 using namespace sdbusplus::bus::match::rules;
228 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
229 try
230 {
231 auto servicePath = ipmi::getDbusObject(
232 bus, "xyz.openbmc_project.Control.Service.Attributes",
233 "/xyz/openbmc_project/control/service", "obmc_2dconsole");
234
235 if (!std::empty(servicePath.first))
236 {
237 matchPtrSOL = std::make_unique<sdbusplus::bus::match_t>(
238 bus,
239 path_namespace(servicePath.first) +
240 "arg0namespace='xyz.openbmc_project.Control.Service."
241 "Attributes'"
242 ", " +
243 type::signal() + member("PropertiesChanged") +
244 interface("org.freedesktop.DBus.Properties"),
245 [](sdbusplus::message::message& msg) {
246 std::string intfName;
247 std::map<std::string, std::variant<bool>> properties;
248 msg.read(intfName, properties);
249
250 const auto it = properties.find("Enabled");
251 if (it != properties.end())
252 {
253 const bool* state = std::get_if<bool>(&it->second);
254
255 if (state != nullptr && *state == false)
256 {
257 // Stop all the payload session.
258 std::get<sol::Manager&>(singletonPool)
259 .stopAllPayloadInstance();
260 }
261 }
262 });
263 }
264 }
265 catch (sdbusplus::exception_t& e)
266 {
267 log<level::ERR>(
268 "Failed to get service path in registerSOLServiceChangeCallback");
269 }
270}
271
Tom Joseph22c5ad32017-03-14 18:04:22 +0530272} // namespace sol