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 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 8 | #include <iomanip> |
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 | |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 15 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Nagaraju Goruganti | 1d9d416 | 2018-03-22 01:27:37 -0500 | [diff] [blame] | 16 | using namespace phosphor::logging; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 17 | |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 18 | namespace ipmi |
| 19 | { |
| 20 | using 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 Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 26 | namespace command |
| 27 | { |
| 28 | |
| 29 | void Table::registerCommand(CommandID inCommand, std::unique_ptr<Entry>&& entry) |
| 30 | { |
Feist, James | bd45aae | 2017-10-26 15:06:13 -0700 | [diff] [blame] | 31 | auto& command = commandTable[inCommand.command]; |
| 32 | |
| 33 | if (command) |
| 34 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 35 | log<level::DEBUG>( |
| 36 | "Already Registered", |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 37 | phosphor::logging::entry("SKIPPED_ENTRY=0x%x", inCommand.command)); |
Feist, James | bd45aae | 2017-10-26 15:06:13 -0700 | [diff] [blame] | 38 | return; |
| 39 | } |
| 40 | |
Feist, James | bd45aae | 2017-10-26 15:06:13 -0700 | [diff] [blame] | 41 | command = std::move(entry); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 42 | } |
| 43 | |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 44 | void Table::executeCommand(uint32_t inCommand, |
| 45 | std::vector<uint8_t>& commandData, |
| 46 | std::shared_ptr<message::Handler> handler) |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 47 | { |
| 48 | using namespace std::chrono_literals; |
| 49 | |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 50 | auto iterator = commandTable.find(inCommand); |
| 51 | |
| 52 | if (iterator == commandTable.end()) |
| 53 | { |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 54 | CommandID command(inCommand); |
| 55 | |
| 56 | auto bus = getSdBus(); |
| 57 | // forward the request onto the main ipmi queue |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 58 | using IpmiDbusRspType = std::tuple<uint8_t, uint8_t, uint8_t, uint8_t, |
| 59 | std::vector<uint8_t>>; |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 60 | 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 Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 65 | .getSession(handler->sessionID); |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 66 | std::map<std::string, ipmi::Value> options = { |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 67 | {"userId", ipmi::Value(static_cast<int>( |
| 68 | ipmi::ipmiUserGetUserId(session->userName)))}, |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 69 | {"privilege", |
| 70 | ipmi::Value(static_cast<int>(session->currentPrivilege()))}, |
Rajashekar Gade Reddy | 49a94b2 | 2019-11-13 16:46:32 +0530 | [diff] [blame] | 71 | {"currentSessionId", |
| 72 | ipmi::Value(static_cast<uint32_t>(session->getBMCSessionID()))}, |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 73 | }; |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 74 | bus->async_method_call( |
| 75 | [handler, this](const boost::system::error_code& ec, |
| 76 | const IpmiDbusRspType& response) { |
| 77 | if (!ec) |
| 78 | { |
| 79 | const uint8_t& cc = std::get<3>(response); |
| 80 | const std::vector<uint8_t>& responseData = |
| 81 | std::get<4>(response); |
| 82 | std::vector<uint8_t> payload; |
| 83 | payload.reserve(1 + responseData.size()); |
| 84 | payload.push_back(cc); |
| 85 | payload.insert(payload.end(), responseData.begin(), |
| 86 | responseData.end()); |
| 87 | handler->outPayload = std::move(payload); |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | std::vector<uint8_t> payload; |
| 92 | payload.push_back(IPMI_CC_UNSPECIFIED_ERROR); |
| 93 | handler->outPayload = std::move(payload); |
| 94 | } |
| 95 | }, |
| 96 | "xyz.openbmc_project.Ipmi.Host", "/xyz/openbmc_project/Ipmi", |
| 97 | "xyz.openbmc_project.Ipmi.Server", "execute", netFn, lun, cmd, |
| 98 | commandData, options); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 99 | } |
| 100 | else |
| 101 | { |
| 102 | auto start = std::chrono::steady_clock::now(); |
| 103 | |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 104 | handler->outPayload = |
| 105 | iterator->second->executeCommand(commandData, handler); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 106 | |
| 107 | auto end = std::chrono::steady_clock::now(); |
| 108 | |
Yong Li | 899cf5a | 2020-01-19 13:32:20 +0800 | [diff] [blame^] | 109 | std::chrono::duration<size_t> elapsedSeconds = |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 110 | std::chrono::duration_cast<std::chrono::seconds>(end - start); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 111 | |
| 112 | // If command time execution time exceeds 2 seconds, log a time |
| 113 | // exceeded message |
| 114 | if (elapsedSeconds > 2s) |
| 115 | { |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 116 | log<level::ERR>("IPMI command timed out", |
Yong Li | 899cf5a | 2020-01-19 13:32:20 +0800 | [diff] [blame^] | 117 | entry("DELAY=%zu", elapsedSeconds.count())); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 118 | } |
| 119 | } |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 120 | } |
| 121 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 122 | std::vector<uint8_t> |
| 123 | NetIpmidEntry::executeCommand(std::vector<uint8_t>& commandData, |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 124 | std::shared_ptr<message::Handler> handler) |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 125 | { |
| 126 | std::vector<uint8_t> errResponse; |
| 127 | |
| 128 | // 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] | 129 | if (!sessionless && (handler->sessionID == session::sessionZero)) |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 130 | { |
| 131 | errResponse.resize(1); |
| 132 | errResponse[0] = IPMI_CC_INSUFFICIENT_PRIVILEGE; |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 133 | log<level::INFO>("Table: Insufficient privilege for command", |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 134 | entry("LUN=%x", command.lun()), |
| 135 | entry("NETFN=%x", command.netFn()), |
| 136 | entry("CMD=%x", command.cmd())); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 137 | return errResponse; |
| 138 | } |
| 139 | |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 140 | return functor(commandData, *handler); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 141 | } |
| 142 | |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 143 | } // namespace command |