blob: 1ad217581f69e76ceaef8ea03c584ab0da365cca [file] [log] [blame]
Tom Joseph07181f52016-08-08 08:17:08 -05001#include "command_table.hpp"
2
Richard Marian Thomaiyar472a37b2018-09-06 07:11:07 +05303#include "main.hpp"
Tom Joseph07181f52016-08-08 08:17:08 -05004#include "message_handler.hpp"
Tom Joseph1efcb492017-01-31 16:56:47 +05305#include "message_parsers.hpp"
Tom Joseph07181f52016-08-08 08:17:08 -05006#include "sessions_manager.hpp"
Nagaraju Goruganti1d9d4162018-03-22 01:27:37 -05007
Vernon Mauery9e801a22018-10-12 13:20:49 -07008#include <iomanip>
Vernon Mauery66501642018-07-30 09:07:10 -07009#include <main.hpp>
Vernon Mauery9e801a22018-10-12 13:20:49 -070010#include <phosphor-logging/elog-errors.hpp>
11#include <phosphor-logging/log.hpp>
Vernon Mauery66501642018-07-30 09:07:10 -070012#include <user_channel/user_layer.hpp>
Vernon Maueryf41a5542018-10-15 16:04:17 -070013#include <xyz/openbmc_project/Common/error.hpp>
Vernon Mauery9e801a22018-10-12 13:20:49 -070014
Vernon Mauery66501642018-07-30 09:07:10 -070015using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Nagaraju Goruganti1d9d4162018-03-22 01:27:37 -050016using namespace phosphor::logging;
Tom Joseph07181f52016-08-08 08:17:08 -050017
Vernon Mauery66501642018-07-30 09:07:10 -070018namespace ipmi
19{
20using Value = sdbusplus::message::variant<bool, uint8_t, int16_t, uint16_t,
21 int32_t, uint32_t, int64_t, uint64_t,
22 double, std::string>;
23
24} // namespace ipmi
25
Tom Joseph07181f52016-08-08 08:17:08 -050026namespace command
27{
28
29void Table::registerCommand(CommandID inCommand, std::unique_ptr<Entry>&& entry)
30{
Feist, Jamesbd45aae2017-10-26 15:06:13 -070031 auto& command = commandTable[inCommand.command];
32
33 if (command)
34 {
Vernon Mauery9e801a22018-10-12 13:20:49 -070035 log<level::DEBUG>(
36 "Already Registered",
Vernon Mauery66501642018-07-30 09:07:10 -070037 phosphor::logging::entry("SKIPPED_ENTRY=0x%x", inCommand.command));
Feist, Jamesbd45aae2017-10-26 15:06:13 -070038 return;
39 }
40
Feist, Jamesbd45aae2017-10-26 15:06:13 -070041 command = std::move(entry);
Tom Joseph07181f52016-08-08 08:17:08 -050042}
43
Vernon Mauery8d6f2002018-11-07 09:55:53 -080044void Table::executeCommand(uint32_t inCommand,
45 std::vector<uint8_t>& commandData,
46 std::shared_ptr<message::Handler> handler)
Tom Joseph07181f52016-08-08 08:17:08 -050047{
48 using namespace std::chrono_literals;
49
Tom Joseph07181f52016-08-08 08:17:08 -050050 auto iterator = commandTable.find(inCommand);
51
52 if (iterator == commandTable.end())
53 {
Vernon Mauery66501642018-07-30 09:07:10 -070054 CommandID command(inCommand);
55
56 auto bus = getSdBus();
57 // forward the request onto the main ipmi queue
Vernon Mauery8d6f2002018-11-07 09:55:53 -080058 using IpmiDbusRspType = std::tuple<uint8_t, uint8_t, uint8_t, uint8_t,
59 std::vector<uint8_t>>;
Vernon Mauery66501642018-07-30 09:07:10 -070060 uint8_t lun = command.lun();
61 uint8_t netFn = command.netFn();
62 uint8_t cmd = command.cmd();
63 std::shared_ptr<session::Session> session =
64 std::get<session::Manager&>(singletonPool)
Vernon Mauery8d6f2002018-11-07 09:55:53 -080065 .getSession(handler->sessionID);
Vernon Mauery66501642018-07-30 09:07:10 -070066 std::map<std::string, ipmi::Value> options = {
Vernon Maueryd92bc322019-03-15 15:24:30 -070067 {"userId", ipmi::Value(static_cast<int>(
68 ipmi::ipmiUserGetUserId(session->userName)))},
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +053069 {"privilege",
70 ipmi::Value(static_cast<int>(session->currentPrivilege()))},
Vernon Mauery66501642018-07-30 09:07:10 -070071 };
Vernon Mauery8d6f2002018-11-07 09:55:53 -080072 bus->async_method_call(
73 [handler, this](const boost::system::error_code& ec,
74 const IpmiDbusRspType& response) {
75 if (!ec)
76 {
77 const uint8_t& cc = std::get<3>(response);
78 const std::vector<uint8_t>& responseData =
79 std::get<4>(response);
80 std::vector<uint8_t> payload;
81 payload.reserve(1 + responseData.size());
82 payload.push_back(cc);
83 payload.insert(payload.end(), responseData.begin(),
84 responseData.end());
85 handler->outPayload = std::move(payload);
86 }
87 else
88 {
89 std::vector<uint8_t> payload;
90 payload.push_back(IPMI_CC_UNSPECIFIED_ERROR);
91 handler->outPayload = std::move(payload);
92 }
93 },
94 "xyz.openbmc_project.Ipmi.Host", "/xyz/openbmc_project/Ipmi",
95 "xyz.openbmc_project.Ipmi.Server", "execute", netFn, lun, cmd,
96 commandData, options);
Tom Joseph07181f52016-08-08 08:17:08 -050097 }
98 else
99 {
100 auto start = std::chrono::steady_clock::now();
101
Vernon Mauery8d6f2002018-11-07 09:55:53 -0800102 handler->outPayload =
103 iterator->second->executeCommand(commandData, handler);
Tom Joseph07181f52016-08-08 08:17:08 -0500104
105 auto end = std::chrono::steady_clock::now();
106
Vernon Mauery9e801a22018-10-12 13:20:49 -0700107 auto elapsedSeconds =
108 std::chrono::duration_cast<std::chrono::seconds>(end - start);
Tom Joseph07181f52016-08-08 08:17:08 -0500109
110 // If command time execution time exceeds 2 seconds, log a time
111 // exceeded message
112 if (elapsedSeconds > 2s)
113 {
Vernon Maueryfc37e592018-12-19 14:55:15 -0800114 log<level::ERR>("IPMI command timed out",
115 entry("DELAY=%d", elapsedSeconds.count()));
Tom Joseph07181f52016-08-08 08:17:08 -0500116 }
117 }
Tom Joseph07181f52016-08-08 08:17:08 -0500118}
119
Vernon Mauery9e801a22018-10-12 13:20:49 -0700120std::vector<uint8_t>
121 NetIpmidEntry::executeCommand(std::vector<uint8_t>& commandData,
Vernon Mauery8d6f2002018-11-07 09:55:53 -0800122 std::shared_ptr<message::Handler> handler)
Tom Joseph07181f52016-08-08 08:17:08 -0500123{
124 std::vector<uint8_t> errResponse;
125
126 // Check if the command qualifies to be run prior to establishing a session
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +0530127 if (!sessionless && (handler->sessionID == session::sessionZero))
Tom Joseph07181f52016-08-08 08:17:08 -0500128 {
129 errResponse.resize(1);
130 errResponse[0] = IPMI_CC_INSUFFICIENT_PRIVILEGE;
Vernon Maueryfc37e592018-12-19 14:55:15 -0800131 log<level::INFO>("Table: Insufficient privilege for command",
Vernon Mauery66501642018-07-30 09:07:10 -0700132 entry("LUN=%x", command.lun()),
133 entry("NETFN=%x", command.netFn()),
134 entry("CMD=%x", command.cmd()));
Tom Joseph07181f52016-08-08 08:17:08 -0500135 return errResponse;
136 }
137
Vernon Mauery8d6f2002018-11-07 09:55:53 -0800138 return functor(commandData, *handler);
Tom Joseph07181f52016-08-08 08:17:08 -0500139}
140
Tom Joseph07181f52016-08-08 08:17:08 -0500141} // namespace command