blob: 76f0bd805a9cbf39dad194d76a0a98966a8882de [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);
Jian Zhangc936eca2022-03-22 11:25:29 +080029std::unique_ptr<sdbusplus::bus::match_t> solConfPropertiesSignal(nullptr);
srikanta mondalf6e72302020-06-08 12:46:28 +000030
Vernon Mauery7e4a6512018-11-09 08:43:36 -080031void Manager::initConsoleSocket()
Tom Josephb81f7612017-04-25 17:59:02 +053032{
Vernon Mauery7e4a6512018-11-09 08:43:36 -080033 // explicit length constructor for NUL-prefixed abstract path
34 std::string path(CONSOLE_SOCKET_PATH, CONSOLE_SOCKET_PATH_LEN);
35 boost::asio::local::stream_protocol::endpoint ep(path);
36 consoleSocket =
37 std::make_unique<boost::asio::local::stream_protocol::socket>(*io);
38 consoleSocket->connect(ep);
Tom Josephb81f7612017-04-25 17:59:02 +053039}
40
Vernon Mauery7e4a6512018-11-09 08:43:36 -080041void Manager::consoleInputHandler()
Tom Joseph22c5ad32017-03-14 18:04:22 +053042{
Vernon Mauery7e4a6512018-11-09 08:43:36 -080043 boost::system::error_code ec;
44 boost::asio::socket_base::bytes_readable cmd(true);
45 consoleSocket->io_control(cmd, ec);
46 size_t readSize;
47 if (!ec)
Tom Joseph22c5ad32017-03-14 18:04:22 +053048 {
Vernon Mauery7e4a6512018-11-09 08:43:36 -080049 readSize = cmd.get();
50 }
51 else
52 {
53 log<level::ERR>("Reading ready count from host console socket failed:",
54 entry("EXCEPTION=%s", ec.message().c_str()));
55 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 {
63 log<level::ERR>("Reading from host console socket failed:",
64 entry("EXCEPTION=%s", ec.message().c_str()));
65 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;
115 sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection());
116 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();
130 phosphor::logging::log<phosphor::logging::level::ERR>(
131 "Error: get SOL service failed");
132 return;
133 }
134 }
135 try
136 {
137 properties = ipmi::getAllDbusProperties(
138 dbus, solService, solPathWitheEthName, solInterface);
139 }
140 catch (const std::runtime_error&)
141 {
142 phosphor::logging::log<phosphor::logging::level::ERR>(
143 "Error setting sol parameter");
144 return;
145 }
146
147 progress = std::get<uint8_t>(properties["Progress"]);
148
149 enable = std::get<bool>(properties["Enable"]);
150
151 forceEncrypt = std::get<bool>(properties["ForceEncryption"]);
152
153 forceAuth = std::get<bool>(properties["ForceAuthentication"]);
154
155 solMinPrivilege = static_cast<session::Privilege>(
156 std::get<uint8_t>(properties["Privilege"]));
157
158 accumulateInterval =
159 std::get<uint8_t>((properties["AccumulateIntervalMS"])) *
160 sol::accIntervalFactor * 1ms;
161
162 sendThreshold = std::get<uint8_t>(properties["Threshold"]);
163
164 retryCount = std::get<uint8_t>(properties["RetryCount"]);
165
166 retryInterval = std::get<uint8_t>(properties["RetryIntervalMS"]) *
167 sol::retryIntervalFactor * 1ms;
168
169 return;
170}
171
Tom Joseph2e44e0e2017-03-14 18:09:11 +0530172void Manager::startPayloadInstance(uint8_t payloadInstance,
173 session::SessionID sessionID)
174{
175 if (payloadMap.empty())
176 {
Rashmi RV0f63e012019-11-21 15:38:50 +0530177 try
178 {
179 startHostConsole();
180 }
181 catch (const std::exception& e)
182 {
183 log<level::ERR>("Encountered exception when starting host console. "
184 "Hence stopping host console.",
185 entry("EXCEPTION=%s", e.what()));
186 stopHostConsole();
187 throw;
188 }
Tom Joseph2e44e0e2017-03-14 18:09:11 +0530189 }
190
191 // Create the SOL Context data for payload instance
Vernon Mauerya6ad5e12020-02-21 14:54:24 -0800192 std::shared_ptr<Context> context = Context::makeContext(
193 io, retryCount, sendThreshold, payloadInstance, sessionID);
Tom Joseph2e44e0e2017-03-14 18:09:11 +0530194
Tom Joseph2e44e0e2017-03-14 18:09:11 +0530195 payloadMap.emplace(payloadInstance, std::move(context));
196}
197
Tom Joseph4ff14b52017-03-14 18:10:52 +0530198void Manager::stopPayloadInstance(uint8_t payloadInstance)
199{
200 auto iter = payloadMap.find(payloadInstance);
201 if (iter == payloadMap.end())
202 {
203 throw std::runtime_error("SOL Payload instance not found ");
204 }
205
206 payloadMap.erase(iter);
207
Tom Joseph4ff14b52017-03-14 18:10:52 +0530208 if (payloadMap.empty())
209 {
Vernon Mauery7e4a6512018-11-09 08:43:36 -0800210 stopHostConsole();
Tom Josephde203ce2017-08-01 16:48:06 +0530211
212 dataBuffer.erase(dataBuffer.size());
Tom Joseph4ff14b52017-03-14 18:10:52 +0530213 }
214}
215
srikanta mondalf6e72302020-06-08 12:46:28 +0000216void Manager::stopAllPayloadInstance()
217{
218 // Erase all payload instance
219 payloadMap.erase(payloadMap.begin(), payloadMap.end());
220
221 stopHostConsole();
222
223 dataBuffer.erase(dataBuffer.size());
224}
225
226void registerSOLServiceChangeCallback()
227{
228 using namespace sdbusplus::bus::match::rules;
229 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
230 try
231 {
232 auto servicePath = ipmi::getDbusObject(
233 bus, "xyz.openbmc_project.Control.Service.Attributes",
234 "/xyz/openbmc_project/control/service", "obmc_2dconsole");
235
236 if (!std::empty(servicePath.first))
237 {
238 matchPtrSOL = std::make_unique<sdbusplus::bus::match_t>(
239 bus,
240 path_namespace(servicePath.first) +
241 "arg0namespace='xyz.openbmc_project.Control.Service."
242 "Attributes'"
243 ", " +
244 type::signal() + member("PropertiesChanged") +
245 interface("org.freedesktop.DBus.Properties"),
246 [](sdbusplus::message::message& msg) {
247 std::string intfName;
248 std::map<std::string, std::variant<bool>> properties;
249 msg.read(intfName, properties);
250
251 const auto it = properties.find("Enabled");
252 if (it != properties.end())
253 {
254 const bool* state = std::get_if<bool>(&it->second);
255
256 if (state != nullptr && *state == false)
257 {
258 // Stop all the payload session.
Vernon Mauery2085ae02021-06-10 11:51:00 -0700259 sol::Manager::get().stopAllPayloadInstance();
srikanta mondalf6e72302020-06-08 12:46:28 +0000260 }
261 }
262 });
263 }
264 }
Patrick Williams12d199b2021-10-06 12:36:48 -0500265 catch (const sdbusplus::exception_t& e)
srikanta mondalf6e72302020-06-08 12:46:28 +0000266 {
267 log<level::ERR>(
268 "Failed to get service path in registerSOLServiceChangeCallback");
269 }
270}
271
Jian Zhangc936eca2022-03-22 11:25:29 +0800272void procSolConfChange(sdbusplus::message::message& msg)
273{
274 using SolConfVariant = std::variant<bool, uint8_t>;
275 using SolConfProperties =
276 std::vector<std::pair<std::string, SolConfVariant>>;
277
278 std::string iface;
279 SolConfProperties properties;
280
281 try
282 {
283 msg.read(iface, properties);
284 }
285 catch (const std::exception& e)
286 {
287 phosphor::logging::log<phosphor::logging::level::ERR>(
288 "procSolConfChange get properties FAIL",
289 entry("ERROR=%s", e.what()));
290 return;
291 }
292
293 for (const auto& prop : properties)
294 {
295 if (prop.first == "Progress")
296 {
297 sol::Manager::get().progress = std::get<uint8_t>(prop.second);
298 }
299 else if (prop.first == "Enable")
300 {
301 sol::Manager::get().enable = std::get<bool>(prop.second);
302 }
303 else if (prop.first == "ForceEncryption")
304 {
305 sol::Manager::get().forceEncrypt = std::get<bool>(prop.second);
306 }
307 else if (prop.first == "ForceAuthentication")
308 {
309 sol::Manager::get().forceAuth = std::get<bool>(prop.second);
310 }
311 else if (prop.first == "Privilege")
312 {
313 sol::Manager::get().solMinPrivilege =
314 static_cast<session::Privilege>(std::get<uint8_t>(prop.second));
315 }
316 else if (prop.first == "AccumulateIntervalMS")
317 {
318 sol::Manager::get().accumulateInterval =
319 std::get<uint8_t>(prop.second) * sol::accIntervalFactor * 1ms;
320 }
321 else if (prop.first == "Threshold")
322 {
323 sol::Manager::get().sendThreshold = std::get<uint8_t>(prop.second);
324 }
325 else if (prop.first == "RetryCount")
326 {
327 sol::Manager::get().retryCount = std::get<uint8_t>(prop.second);
328 }
329 else if (prop.first == "RetryIntervalMS")
330 {
331 sol::Manager::get().retryInterval =
332 std::get<uint8_t>(prop.second) * sol::retryIntervalFactor * 1ms;
333 }
334 }
335}
336
337void registerSolConfChangeCallbackHandler(std::string channel)
338{
339 if (solConfPropertiesSignal == nullptr)
340 {
341 using namespace sdbusplus::bus::match::rules;
342 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
343 try
344 {
345 auto servicePath = solPath + channel;
346
347 solConfPropertiesSignal = std::make_unique<sdbusplus::bus::match_t>(
348 bus, propertiesChangedNamespace(servicePath, solInterface),
349 procSolConfChange);
350 }
351 catch (const sdbusplus::exception_t& e)
352 {
353 log<level::ERR>("Failed to get service path in "
354 "registerSolConfChangeCallbackHandler",
355 entry("CHANNEL=%s", channel.c_str()));
356 }
357 }
358 return;
359}
360
Tom Joseph22c5ad32017-03-14 18:04:22 +0530361} // namespace sol