blob: 42296e398d5f6d157461bb5eb59d431c97a4617e [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>
srikanta mondalf6e72302020-06-08 12:46:28 +000013#include <ipmid/utils.hpp>
George Liu7b7f25f2022-07-04 17:07:32 +080014#include <phosphor-logging/lg2.hpp>
Cheng C Yang29086952020-03-09 15:29:18 +080015#include <sdbusplus/message/types.hpp>
16
George Liubc8958f2022-07-04 09:29:49 +080017#include <chrono>
18#include <cmath>
19
Cheng C Yang29086952020-03-09 15:29:18 +080020constexpr const char* solInterface = "xyz.openbmc_project.Ipmi.SOL";
21constexpr const char* solPath = "/xyz/openbmc_project/ipmi/sol/";
22constexpr const char* PROP_INTF = "org.freedesktop.DBus.Properties";
Tom Joseph22c5ad32017-03-14 18:04:22 +053023
24namespace sol
25{
26
srikanta mondalf6e72302020-06-08 12:46:28 +000027std::unique_ptr<sdbusplus::bus::match_t> matchPtrSOL(nullptr);
Jian Zhangc936eca2022-03-22 11:25:29 +080028std::unique_ptr<sdbusplus::bus::match_t> solConfPropertiesSignal(nullptr);
srikanta mondalf6e72302020-06-08 12:46:28 +000029
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 {
George Liu7b7f25f2022-07-04 17:07:32 +080052 lg2::error(
53 "Reading ready count from host console socket failed: {ERROR}",
54 "ERROR", ec.value());
Vernon Mauery7e4a6512018-11-09 08:43:36 -080055 return;
56 }
57 std::vector<uint8_t> buffer(readSize);
58 ec.clear();
59 size_t readDataLen =
60 consoleSocket->read_some(boost::asio::buffer(buffer), ec);
61 if (ec)
62 {
George Liu7b7f25f2022-07-04 17:07:32 +080063 lg2::error("Reading from host console socket failed: {ERROR}", "ERROR",
64 ec.value());
Vernon Mauery7e4a6512018-11-09 08:43:36 -080065 return;
Tom Joseph22c5ad32017-03-14 18:04:22 +053066 }
67
Vernon Mauery7e4a6512018-11-09 08:43:36 -080068 // Update the Console buffer with data read from the socket
69 buffer.resize(readDataLen);
70 dataBuffer.write(buffer);
Tom Joseph22c5ad32017-03-14 18:04:22 +053071}
72
Vernon Mauery70fd29c2017-11-30 13:11:43 -080073int Manager::writeConsoleSocket(const std::vector<uint8_t>& input) const
Tom Josephb51f6412017-03-14 18:08:14 +053074{
Vernon Mauery7e4a6512018-11-09 08:43:36 -080075 boost::system::error_code ec;
76 boost::asio::write(*consoleSocket, boost::asio::buffer(input), ec);
77 return ec.value();
78}
Tom Josephb51f6412017-03-14 18:08:14 +053079
Vernon Mauery7e4a6512018-11-09 08:43:36 -080080void Manager::startHostConsole()
81{
82 if (!consoleSocket)
Tom Josephb51f6412017-03-14 18:08:14 +053083 {
Vernon Mauery7e4a6512018-11-09 08:43:36 -080084 initConsoleSocket();
Tom Josephb51f6412017-03-14 18:08:14 +053085 }
srikanta mondalf6e72302020-06-08 12:46:28 +000086
87 // Register callback to close SOL session for disable SSH SOL
88 if (matchPtrSOL == nullptr)
89 {
90 registerSOLServiceChangeCallback();
91 }
92
Vernon Mauery7e4a6512018-11-09 08:43:36 -080093 consoleSocket->async_wait(boost::asio::socket_base::wait_read,
94 [this](const boost::system::error_code& ec) {
95 if (!ec)
96 {
97 consoleInputHandler();
98 startHostConsole();
99 }
100 });
Rashmi RV0f63e012019-11-21 15:38:50 +0530101} // namespace sol
Tom Josephb51f6412017-03-14 18:08:14 +0530102
Vernon Mauery7e4a6512018-11-09 08:43:36 -0800103void Manager::stopHostConsole()
104{
105 if (consoleSocket)
106 {
107 consoleSocket->cancel();
108 consoleSocket.reset();
109 }
Tom Josephb51f6412017-03-14 18:08:14 +0530110}
111
Cheng C Yang29086952020-03-09 15:29:18 +0800112void Manager::updateSOLParameter(uint8_t channelNum)
113{
114 std::variant<uint8_t, bool> value;
Patrick Williams0a590622022-07-22 19:26:53 -0500115 sdbusplus::bus_t dbus(ipmid_get_sd_bus_connection());
Cheng C Yang29086952020-03-09 15:29:18 +0800116 static std::string solService{};
117 ipmi::PropertyMap properties;
118 std::string ethdevice = ipmi::getChannelName(channelNum);
119 std::string solPathWitheEthName = solPath + ethdevice;
120 if (solService.empty())
121 {
122 try
123 {
124 solService =
125 ipmi::getService(dbus, solInterface, solPathWitheEthName);
126 }
127 catch (const std::runtime_error& e)
128 {
129 solService.clear();
George Liu7b7f25f2022-07-04 17:07:32 +0800130 lg2::error("Get SOL service failed: {ERROR}", "ERROR", e);
Cheng C Yang29086952020-03-09 15:29:18 +0800131 return;
132 }
133 }
134 try
135 {
136 properties = ipmi::getAllDbusProperties(
137 dbus, solService, solPathWitheEthName, solInterface);
138 }
George Liu7b7f25f2022-07-04 17:07:32 +0800139 catch (const std::runtime_error& e)
Cheng C Yang29086952020-03-09 15:29:18 +0800140 {
George Liu7b7f25f2022-07-04 17:07:32 +0800141 lg2::error("Setting sol parameter: {ERROR}", "ERROR", e);
Cheng C Yang29086952020-03-09 15:29:18 +0800142 return;
143 }
144
145 progress = std::get<uint8_t>(properties["Progress"]);
146
147 enable = std::get<bool>(properties["Enable"]);
148
149 forceEncrypt = std::get<bool>(properties["ForceEncryption"]);
150
151 forceAuth = std::get<bool>(properties["ForceAuthentication"]);
152
153 solMinPrivilege = static_cast<session::Privilege>(
154 std::get<uint8_t>(properties["Privilege"]));
155
156 accumulateInterval =
157 std::get<uint8_t>((properties["AccumulateIntervalMS"])) *
158 sol::accIntervalFactor * 1ms;
159
160 sendThreshold = std::get<uint8_t>(properties["Threshold"]);
161
162 retryCount = std::get<uint8_t>(properties["RetryCount"]);
163
164 retryInterval = std::get<uint8_t>(properties["RetryIntervalMS"]) *
165 sol::retryIntervalFactor * 1ms;
166
167 return;
168}
169
Tom Joseph2e44e0e2017-03-14 18:09:11 +0530170void Manager::startPayloadInstance(uint8_t payloadInstance,
171 session::SessionID sessionID)
172{
173 if (payloadMap.empty())
174 {
Rashmi RV0f63e012019-11-21 15:38:50 +0530175 try
176 {
177 startHostConsole();
178 }
179 catch (const std::exception& e)
180 {
George Liu7b7f25f2022-07-04 17:07:32 +0800181 lg2::error(
182 "Encountered exception when starting host console. Hence stopping host console: {ERROR}",
183 "ERROR", e);
Rashmi RV0f63e012019-11-21 15:38:50 +0530184 stopHostConsole();
185 throw;
186 }
Tom Joseph2e44e0e2017-03-14 18:09:11 +0530187 }
188
189 // Create the SOL Context data for payload instance
Vernon Mauerya6ad5e12020-02-21 14:54:24 -0800190 std::shared_ptr<Context> context = Context::makeContext(
191 io, retryCount, sendThreshold, payloadInstance, sessionID);
Tom Joseph2e44e0e2017-03-14 18:09:11 +0530192
Tom Joseph2e44e0e2017-03-14 18:09:11 +0530193 payloadMap.emplace(payloadInstance, std::move(context));
194}
195
Tom Joseph4ff14b52017-03-14 18:10:52 +0530196void Manager::stopPayloadInstance(uint8_t payloadInstance)
197{
198 auto iter = payloadMap.find(payloadInstance);
199 if (iter == payloadMap.end())
200 {
201 throw std::runtime_error("SOL Payload instance not found ");
202 }
203
204 payloadMap.erase(iter);
205
Tom Joseph4ff14b52017-03-14 18:10:52 +0530206 if (payloadMap.empty())
207 {
Vernon Mauery7e4a6512018-11-09 08:43:36 -0800208 stopHostConsole();
Tom Josephde203ce2017-08-01 16:48:06 +0530209
210 dataBuffer.erase(dataBuffer.size());
Tom Joseph4ff14b52017-03-14 18:10:52 +0530211 }
212}
213
srikanta mondalf6e72302020-06-08 12:46:28 +0000214void Manager::stopAllPayloadInstance()
215{
216 // Erase all payload instance
217 payloadMap.erase(payloadMap.begin(), payloadMap.end());
218
219 stopHostConsole();
220
221 dataBuffer.erase(dataBuffer.size());
222}
223
224void registerSOLServiceChangeCallback()
225{
226 using namespace sdbusplus::bus::match::rules;
Patrick Williams0a590622022-07-22 19:26:53 -0500227 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
srikanta mondalf6e72302020-06-08 12:46:28 +0000228 try
229 {
230 auto servicePath = ipmi::getDbusObject(
231 bus, "xyz.openbmc_project.Control.Service.Attributes",
232 "/xyz/openbmc_project/control/service", "obmc_2dconsole");
233
234 if (!std::empty(servicePath.first))
235 {
236 matchPtrSOL = std::make_unique<sdbusplus::bus::match_t>(
237 bus,
238 path_namespace(servicePath.first) +
239 "arg0namespace='xyz.openbmc_project.Control.Service."
240 "Attributes'"
241 ", " +
242 type::signal() + member("PropertiesChanged") +
243 interface("org.freedesktop.DBus.Properties"),
Patrick Williams0a590622022-07-22 19:26:53 -0500244 [](sdbusplus::message_t& msg) {
srikanta mondalf6e72302020-06-08 12:46:28 +0000245 std::string intfName;
246 std::map<std::string, std::variant<bool>> properties;
247 msg.read(intfName, properties);
248
249 const auto it = properties.find("Enabled");
250 if (it != properties.end())
251 {
252 const bool* state = std::get_if<bool>(&it->second);
253
254 if (state != nullptr && *state == false)
255 {
256 // Stop all the payload session.
Vernon Mauery2085ae02021-06-10 11:51:00 -0700257 sol::Manager::get().stopAllPayloadInstance();
srikanta mondalf6e72302020-06-08 12:46:28 +0000258 }
259 }
260 });
261 }
262 }
Patrick Williams12d199b2021-10-06 12:36:48 -0500263 catch (const sdbusplus::exception_t& e)
srikanta mondalf6e72302020-06-08 12:46:28 +0000264 {
George Liu7b7f25f2022-07-04 17:07:32 +0800265 lg2::error(
266 "Failed to get service path in registerSOLServiceChangeCallback: {ERROR}",
267 "ERROR", e);
srikanta mondalf6e72302020-06-08 12:46:28 +0000268 }
269}
270
Patrick Williams0a590622022-07-22 19:26:53 -0500271void procSolConfChange(sdbusplus::message_t& msg)
Jian Zhangc936eca2022-03-22 11:25:29 +0800272{
273 using SolConfVariant = std::variant<bool, uint8_t>;
274 using SolConfProperties =
275 std::vector<std::pair<std::string, SolConfVariant>>;
276
277 std::string iface;
278 SolConfProperties properties;
279
280 try
281 {
282 msg.read(iface, properties);
283 }
284 catch (const std::exception& e)
285 {
George Liu7b7f25f2022-07-04 17:07:32 +0800286 lg2::error("procSolConfChange get properties FAIL: {ERROR}", "ERROR",
287 e);
Jian Zhangc936eca2022-03-22 11:25:29 +0800288 return;
289 }
290
291 for (const auto& prop : properties)
292 {
293 if (prop.first == "Progress")
294 {
295 sol::Manager::get().progress = std::get<uint8_t>(prop.second);
296 }
297 else if (prop.first == "Enable")
298 {
299 sol::Manager::get().enable = std::get<bool>(prop.second);
300 }
301 else if (prop.first == "ForceEncryption")
302 {
303 sol::Manager::get().forceEncrypt = std::get<bool>(prop.second);
304 }
305 else if (prop.first == "ForceAuthentication")
306 {
307 sol::Manager::get().forceAuth = std::get<bool>(prop.second);
308 }
309 else if (prop.first == "Privilege")
310 {
311 sol::Manager::get().solMinPrivilege =
312 static_cast<session::Privilege>(std::get<uint8_t>(prop.second));
313 }
314 else if (prop.first == "AccumulateIntervalMS")
315 {
316 sol::Manager::get().accumulateInterval =
317 std::get<uint8_t>(prop.second) * sol::accIntervalFactor * 1ms;
318 }
319 else if (prop.first == "Threshold")
320 {
321 sol::Manager::get().sendThreshold = std::get<uint8_t>(prop.second);
322 }
323 else if (prop.first == "RetryCount")
324 {
325 sol::Manager::get().retryCount = std::get<uint8_t>(prop.second);
326 }
327 else if (prop.first == "RetryIntervalMS")
328 {
329 sol::Manager::get().retryInterval =
330 std::get<uint8_t>(prop.second) * sol::retryIntervalFactor * 1ms;
331 }
332 }
333}
334
335void registerSolConfChangeCallbackHandler(std::string channel)
336{
337 if (solConfPropertiesSignal == nullptr)
338 {
339 using namespace sdbusplus::bus::match::rules;
Patrick Williams0a590622022-07-22 19:26:53 -0500340 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Jian Zhangc936eca2022-03-22 11:25:29 +0800341 try
342 {
343 auto servicePath = solPath + channel;
344
345 solConfPropertiesSignal = std::make_unique<sdbusplus::bus::match_t>(
346 bus, propertiesChangedNamespace(servicePath, solInterface),
347 procSolConfChange);
348 }
349 catch (const sdbusplus::exception_t& e)
350 {
George Liu7b7f25f2022-07-04 17:07:32 +0800351 lg2::error(
352 "Failed to get service path in registerSolConfChangeCallbackHandler, channel: {CHANNEL}, error: {ERROR}",
353 "CHANNEL", channel, "ERROR", e);
Jian Zhangc936eca2022-03-22 11:25:29 +0800354 }
355 }
356 return;
357}
358
Tom Joseph22c5ad32017-03-14 18:04:22 +0530359} // namespace sol