blob: 00acc65ab4cc5160031e7a000dad27d09052bd8a [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
Patrick Williams75a8d8b2021-09-02 11:30:44 -05008#include <ipmid/types.hpp>
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
George Liubc8958f2022-07-04 09:29:49 +080015#include <iomanip>
16
Vernon Mauery66501642018-07-30 09:07:10 -070017using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Nagaraju Goruganti1d9d4162018-03-22 01:27:37 -050018using namespace phosphor::logging;
Tom Joseph07181f52016-08-08 08:17:08 -050019
20namespace command
21{
22
23void Table::registerCommand(CommandID inCommand, std::unique_ptr<Entry>&& entry)
24{
Feist, Jamesbd45aae2017-10-26 15:06:13 -070025 auto& command = commandTable[inCommand.command];
26
27 if (command)
28 {
Vernon Mauery9e801a22018-10-12 13:20:49 -070029 log<level::DEBUG>(
30 "Already Registered",
Vernon Mauery66501642018-07-30 09:07:10 -070031 phosphor::logging::entry("SKIPPED_ENTRY=0x%x", inCommand.command));
Feist, Jamesbd45aae2017-10-26 15:06:13 -070032 return;
33 }
34
Feist, Jamesbd45aae2017-10-26 15:06:13 -070035 command = std::move(entry);
Tom Joseph07181f52016-08-08 08:17:08 -050036}
37
Vernon Mauery8d6f2002018-11-07 09:55:53 -080038void Table::executeCommand(uint32_t inCommand,
39 std::vector<uint8_t>& commandData,
40 std::shared_ptr<message::Handler> handler)
Tom Joseph07181f52016-08-08 08:17:08 -050041{
42 using namespace std::chrono_literals;
43
Tom Joseph07181f52016-08-08 08:17:08 -050044 auto iterator = commandTable.find(inCommand);
45
46 if (iterator == commandTable.end())
47 {
Vernon Mauery66501642018-07-30 09:07:10 -070048 CommandID command(inCommand);
49
Vernon Mauery779e7e12021-06-08 16:24:45 -070050 // Do not forward any session zero commands to ipmid
51 if (handler->sessionID == session::sessionZero)
52 {
53 log<level::INFO>("Table: refuse to forward session-zero command",
54 entry("LUN=%x", command.lun()),
55 entry("NETFN=%x", command.netFn()),
56 entry("CMD=%x", command.cmd()));
57 return;
58 }
Vernon Mauerycfb34ca2021-06-10 15:37:07 -070059 std::shared_ptr<session::Session> session =
Vernon Mauery2085ae02021-06-10 11:51:00 -070060 session::Manager::get().getSession(handler->sessionID);
Vernon Mauerycfb34ca2021-06-10 15:37:07 -070061
62 // Ignore messages that are not part of an active session
63 auto state = static_cast<session::State>(session->state());
64 if (state != session::State::active)
65 {
66 return;
67 }
Vernon Mauery779e7e12021-06-08 16:24:45 -070068
Vernon Mauery66501642018-07-30 09:07:10 -070069 auto bus = getSdBus();
70 // forward the request onto the main ipmi queue
Vernon Mauery8d6f2002018-11-07 09:55:53 -080071 using IpmiDbusRspType = std::tuple<uint8_t, uint8_t, uint8_t, uint8_t,
72 std::vector<uint8_t>>;
Vernon Mauery66501642018-07-30 09:07:10 -070073 uint8_t lun = command.lun();
74 uint8_t netFn = command.netFn();
75 uint8_t cmd = command.cmd();
Vernon Mauerycfb34ca2021-06-10 15:37:07 -070076
Vernon Mauery66501642018-07-30 09:07:10 -070077 std::map<std::string, ipmi::Value> options = {
Vernon Maueryd92bc322019-03-15 15:24:30 -070078 {"userId", ipmi::Value(static_cast<int>(
79 ipmi::ipmiUserGetUserId(session->userName)))},
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +053080 {"privilege",
81 ipmi::Value(static_cast<int>(session->currentPrivilege()))},
Rajashekar Gade Reddy49a94b22019-11-13 16:46:32 +053082 {"currentSessionId",
83 ipmi::Value(static_cast<uint32_t>(session->getBMCSessionID()))},
Vernon Mauery66501642018-07-30 09:07:10 -070084 };
Vernon Mauery8d6f2002018-11-07 09:55:53 -080085 bus->async_method_call(
86 [handler, this](const boost::system::error_code& ec,
87 const IpmiDbusRspType& response) {
88 if (!ec)
89 {
90 const uint8_t& cc = std::get<3>(response);
91 const std::vector<uint8_t>& responseData =
92 std::get<4>(response);
93 std::vector<uint8_t> payload;
94 payload.reserve(1 + responseData.size());
95 payload.push_back(cc);
96 payload.insert(payload.end(), responseData.begin(),
97 responseData.end());
98 handler->outPayload = std::move(payload);
99 }
100 else
101 {
102 std::vector<uint8_t> payload;
103 payload.push_back(IPMI_CC_UNSPECIFIED_ERROR);
104 handler->outPayload = std::move(payload);
105 }
106 },
107 "xyz.openbmc_project.Ipmi.Host", "/xyz/openbmc_project/Ipmi",
108 "xyz.openbmc_project.Ipmi.Server", "execute", netFn, lun, cmd,
109 commandData, options);
Tom Joseph07181f52016-08-08 08:17:08 -0500110 }
111 else
112 {
113 auto start = std::chrono::steady_clock::now();
114
Vernon Mauerycfb34ca2021-06-10 15:37:07 -0700115 // Ignore messages that are not part of an active/pre-active session
116 if (handler->sessionID != session::sessionZero)
117 {
118 std::shared_ptr<session::Session> session =
Vernon Mauery2085ae02021-06-10 11:51:00 -0700119 session::Manager::get().getSession(handler->sessionID);
Vernon Mauerycfb34ca2021-06-10 15:37:07 -0700120 auto state = static_cast<session::State>(session->state());
121 if ((state != session::State::setupInProgress) &&
122 (state != session::State::active))
123 {
124 return;
125 }
126 }
127
Vernon Mauery8d6f2002018-11-07 09:55:53 -0800128 handler->outPayload =
129 iterator->second->executeCommand(commandData, handler);
Tom Joseph07181f52016-08-08 08:17:08 -0500130
131 auto end = std::chrono::steady_clock::now();
132
Yong Li899cf5a2020-01-19 13:32:20 +0800133 std::chrono::duration<size_t> elapsedSeconds =
Vernon Mauery9e801a22018-10-12 13:20:49 -0700134 std::chrono::duration_cast<std::chrono::seconds>(end - start);
Tom Joseph07181f52016-08-08 08:17:08 -0500135
136 // If command time execution time exceeds 2 seconds, log a time
137 // exceeded message
138 if (elapsedSeconds > 2s)
139 {
Vernon Maueryfc37e592018-12-19 14:55:15 -0800140 log<level::ERR>("IPMI command timed out",
Yong Li899cf5a2020-01-19 13:32:20 +0800141 entry("DELAY=%zu", elapsedSeconds.count()));
Tom Joseph07181f52016-08-08 08:17:08 -0500142 }
143 }
Tom Joseph07181f52016-08-08 08:17:08 -0500144}
145
Vernon Mauery9e801a22018-10-12 13:20:49 -0700146std::vector<uint8_t>
147 NetIpmidEntry::executeCommand(std::vector<uint8_t>& commandData,
Vernon Mauery8d6f2002018-11-07 09:55:53 -0800148 std::shared_ptr<message::Handler> handler)
Tom Joseph07181f52016-08-08 08:17:08 -0500149{
150 std::vector<uint8_t> errResponse;
151
152 // Check if the command qualifies to be run prior to establishing a session
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +0530153 if (!sessionless && (handler->sessionID == session::sessionZero))
Tom Joseph07181f52016-08-08 08:17:08 -0500154 {
155 errResponse.resize(1);
156 errResponse[0] = IPMI_CC_INSUFFICIENT_PRIVILEGE;
Vernon Maueryfc37e592018-12-19 14:55:15 -0800157 log<level::INFO>("Table: Insufficient privilege for command",
Vernon Mauery66501642018-07-30 09:07:10 -0700158 entry("LUN=%x", command.lun()),
159 entry("NETFN=%x", command.netFn()),
160 entry("CMD=%x", command.cmd()));
Tom Joseph07181f52016-08-08 08:17:08 -0500161 return errResponse;
162 }
163
Vernon Mauery41ff9b52021-06-11 11:37:40 -0700164 return functor(commandData, handler);
Tom Joseph07181f52016-08-08 08:17:08 -0500165}
166
Tom Joseph07181f52016-08-08 08:17:08 -0500167} // namespace command