Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 1 | #include "command_table.hpp" |
| 2 | |
Richard Marian Thomaiyar | 472a37b | 2018-09-06 07:11:07 +0530 | [diff] [blame] | 3 | #include "main.hpp" |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 4 | #include "message_handler.hpp" |
Tom Joseph | 1efcb49 | 2017-01-31 16:56:47 +0530 | [diff] [blame] | 5 | #include "message_parsers.hpp" |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 6 | #include "sessions_manager.hpp" |
Nagaraju Goruganti | 1d9d416 | 2018-03-22 01:27:37 -0500 | [diff] [blame] | 7 | |
Patrick Williams | 75a8d8b | 2021-09-02 11:30:44 -0500 | [diff] [blame] | 8 | #include <ipmid/types.hpp> |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 9 | #include <main.hpp> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 10 | #include <phosphor-logging/elog-errors.hpp> |
| 11 | #include <phosphor-logging/log.hpp> |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 12 | #include <user_channel/user_layer.hpp> |
Vernon Mauery | f41a554 | 2018-10-15 16:04:17 -0700 | [diff] [blame] | 13 | #include <xyz/openbmc_project/Common/error.hpp> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 14 | |
George Liu | bc8958f | 2022-07-04 09:29:49 +0800 | [diff] [blame] | 15 | #include <iomanip> |
| 16 | |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 17 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Nagaraju Goruganti | 1d9d416 | 2018-03-22 01:27:37 -0500 | [diff] [blame] | 18 | using namespace phosphor::logging; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 19 | |
| 20 | namespace command |
| 21 | { |
| 22 | |
| 23 | void Table::registerCommand(CommandID inCommand, std::unique_ptr<Entry>&& entry) |
| 24 | { |
Feist, James | bd45aae | 2017-10-26 15:06:13 -0700 | [diff] [blame] | 25 | auto& command = commandTable[inCommand.command]; |
| 26 | |
| 27 | if (command) |
| 28 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 29 | log<level::DEBUG>( |
| 30 | "Already Registered", |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 31 | phosphor::logging::entry("SKIPPED_ENTRY=0x%x", inCommand.command)); |
Feist, James | bd45aae | 2017-10-26 15:06:13 -0700 | [diff] [blame] | 32 | return; |
| 33 | } |
| 34 | |
Feist, James | bd45aae | 2017-10-26 15:06:13 -0700 | [diff] [blame] | 35 | command = std::move(entry); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 36 | } |
| 37 | |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 38 | void Table::executeCommand(uint32_t inCommand, |
| 39 | std::vector<uint8_t>& commandData, |
| 40 | std::shared_ptr<message::Handler> handler) |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 41 | { |
| 42 | using namespace std::chrono_literals; |
| 43 | |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 44 | auto iterator = commandTable.find(inCommand); |
| 45 | |
| 46 | if (iterator == commandTable.end()) |
| 47 | { |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 48 | CommandID command(inCommand); |
| 49 | |
Vernon Mauery | 779e7e1 | 2021-06-08 16:24:45 -0700 | [diff] [blame] | 50 | // 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 Mauery | cfb34ca | 2021-06-10 15:37:07 -0700 | [diff] [blame] | 59 | std::shared_ptr<session::Session> session = |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 60 | session::Manager::get().getSession(handler->sessionID); |
Vernon Mauery | cfb34ca | 2021-06-10 15:37:07 -0700 | [diff] [blame] | 61 | |
| 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 Mauery | 779e7e1 | 2021-06-08 16:24:45 -0700 | [diff] [blame] | 68 | |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 69 | auto bus = getSdBus(); |
| 70 | // forward the request onto the main ipmi queue |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 71 | using IpmiDbusRspType = std::tuple<uint8_t, uint8_t, uint8_t, uint8_t, |
| 72 | std::vector<uint8_t>>; |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 73 | uint8_t lun = command.lun(); |
| 74 | uint8_t netFn = command.netFn(); |
| 75 | uint8_t cmd = command.cmd(); |
Vernon Mauery | cfb34ca | 2021-06-10 15:37:07 -0700 | [diff] [blame] | 76 | |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 77 | std::map<std::string, ipmi::Value> options = { |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 78 | {"userId", ipmi::Value(static_cast<int>( |
| 79 | ipmi::ipmiUserGetUserId(session->userName)))}, |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 80 | {"privilege", |
| 81 | ipmi::Value(static_cast<int>(session->currentPrivilege()))}, |
Rajashekar Gade Reddy | 49a94b2 | 2019-11-13 16:46:32 +0530 | [diff] [blame] | 82 | {"currentSessionId", |
| 83 | ipmi::Value(static_cast<uint32_t>(session->getBMCSessionID()))}, |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 84 | }; |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 85 | 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 Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 110 | } |
| 111 | else |
| 112 | { |
| 113 | auto start = std::chrono::steady_clock::now(); |
| 114 | |
Vernon Mauery | cfb34ca | 2021-06-10 15:37:07 -0700 | [diff] [blame] | 115 | // 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 Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 119 | session::Manager::get().getSession(handler->sessionID); |
Vernon Mauery | cfb34ca | 2021-06-10 15:37:07 -0700 | [diff] [blame] | 120 | 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 Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 128 | handler->outPayload = |
| 129 | iterator->second->executeCommand(commandData, handler); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 130 | |
| 131 | auto end = std::chrono::steady_clock::now(); |
| 132 | |
Yong Li | 899cf5a | 2020-01-19 13:32:20 +0800 | [diff] [blame] | 133 | std::chrono::duration<size_t> elapsedSeconds = |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 134 | std::chrono::duration_cast<std::chrono::seconds>(end - start); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 135 | |
| 136 | // If command time execution time exceeds 2 seconds, log a time |
| 137 | // exceeded message |
| 138 | if (elapsedSeconds > 2s) |
| 139 | { |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 140 | log<level::ERR>("IPMI command timed out", |
Yong Li | 899cf5a | 2020-01-19 13:32:20 +0800 | [diff] [blame] | 141 | entry("DELAY=%zu", elapsedSeconds.count())); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 142 | } |
| 143 | } |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 144 | } |
| 145 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 146 | std::vector<uint8_t> |
| 147 | NetIpmidEntry::executeCommand(std::vector<uint8_t>& commandData, |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 148 | std::shared_ptr<message::Handler> handler) |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 149 | { |
| 150 | std::vector<uint8_t> errResponse; |
| 151 | |
| 152 | // Check if the command qualifies to be run prior to establishing a session |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 153 | if (!sessionless && (handler->sessionID == session::sessionZero)) |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 154 | { |
| 155 | errResponse.resize(1); |
| 156 | errResponse[0] = IPMI_CC_INSUFFICIENT_PRIVILEGE; |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 157 | log<level::INFO>("Table: Insufficient privilege for command", |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 158 | entry("LUN=%x", command.lun()), |
| 159 | entry("NETFN=%x", command.netFn()), |
| 160 | entry("CMD=%x", command.cmd())); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 161 | return errResponse; |
| 162 | } |
| 163 | |
Vernon Mauery | 41ff9b5 | 2021-06-11 11:37:40 -0700 | [diff] [blame] | 164 | return functor(commandData, handler); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 165 | } |
| 166 | |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 167 | } // namespace command |