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> |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 10 | #include <phosphor-logging/lg2.hpp> |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 11 | #include <user_channel/user_layer.hpp> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 12 | |
George Liu | bc8958f | 2022-07-04 09:29:49 +0800 | [diff] [blame] | 13 | #include <iomanip> |
| 14 | |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 15 | namespace command |
| 16 | { |
| 17 | |
| 18 | void Table::registerCommand(CommandID inCommand, std::unique_ptr<Entry>&& entry) |
| 19 | { |
Feist, James | bd45aae | 2017-10-26 15:06:13 -0700 | [diff] [blame] | 20 | auto& command = commandTable[inCommand.command]; |
| 21 | |
| 22 | if (command) |
| 23 | { |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 24 | lg2::debug("Already Registered: {COMMAND}", "COMMAND", |
| 25 | inCommand.command); |
Feist, James | bd45aae | 2017-10-26 15:06:13 -0700 | [diff] [blame] | 26 | return; |
| 27 | } |
| 28 | |
Feist, James | bd45aae | 2017-10-26 15:06:13 -0700 | [diff] [blame] | 29 | command = std::move(entry); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 30 | } |
| 31 | |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 32 | void Table::executeCommand(uint32_t inCommand, |
| 33 | std::vector<uint8_t>& commandData, |
| 34 | std::shared_ptr<message::Handler> handler) |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 35 | { |
| 36 | using namespace std::chrono_literals; |
| 37 | |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 38 | auto iterator = commandTable.find(inCommand); |
| 39 | |
| 40 | if (iterator == commandTable.end()) |
| 41 | { |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 42 | CommandID command(inCommand); |
| 43 | |
Vernon Mauery | 779e7e1 | 2021-06-08 16:24:45 -0700 | [diff] [blame] | 44 | // Do not forward any session zero commands to ipmid |
| 45 | if (handler->sessionID == session::sessionZero) |
| 46 | { |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 47 | lg2::info( |
| 48 | "Table: refuse to forward session-zero command: lun: {LUN}, netFn: {NETFN}, command: {COMMAND}", |
| 49 | "LUN", command.lun(), "NETFN", command.netFn(), "COMMAND", |
| 50 | command.cmd()); |
Vernon Mauery | 779e7e1 | 2021-06-08 16:24:45 -0700 | [diff] [blame] | 51 | return; |
| 52 | } |
Vernon Mauery | cfb34ca | 2021-06-10 15:37:07 -0700 | [diff] [blame] | 53 | std::shared_ptr<session::Session> session = |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 54 | session::Manager::get().getSession(handler->sessionID); |
Vernon Mauery | cfb34ca | 2021-06-10 15:37:07 -0700 | [diff] [blame] | 55 | |
| 56 | // Ignore messages that are not part of an active session |
| 57 | auto state = static_cast<session::State>(session->state()); |
| 58 | if (state != session::State::active) |
| 59 | { |
| 60 | return; |
| 61 | } |
Vernon Mauery | 779e7e1 | 2021-06-08 16:24:45 -0700 | [diff] [blame] | 62 | |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 63 | auto bus = getSdBus(); |
| 64 | // forward the request onto the main ipmi queue |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 65 | using IpmiDbusRspType = std::tuple<uint8_t, uint8_t, uint8_t, uint8_t, |
| 66 | std::vector<uint8_t>>; |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 67 | uint8_t lun = command.lun(); |
| 68 | uint8_t netFn = command.netFn(); |
| 69 | uint8_t cmd = command.cmd(); |
Vernon Mauery | cfb34ca | 2021-06-10 15:37:07 -0700 | [diff] [blame] | 70 | |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 71 | std::map<std::string, ipmi::Value> options = { |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 72 | {"userId", ipmi::Value(static_cast<int>( |
| 73 | ipmi::ipmiUserGetUserId(session->userName)))}, |
Suryakanth Sekar | f8a34fc | 2019-06-12 20:59:18 +0530 | [diff] [blame] | 74 | {"privilege", |
| 75 | ipmi::Value(static_cast<int>(session->currentPrivilege()))}, |
Rajashekar Gade Reddy | 49a94b2 | 2019-11-13 16:46:32 +0530 | [diff] [blame] | 76 | {"currentSessionId", |
| 77 | ipmi::Value(static_cast<uint32_t>(session->getBMCSessionID()))}, |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 78 | }; |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 79 | bus->async_method_call( |
| 80 | [handler, this](const boost::system::error_code& ec, |
| 81 | const IpmiDbusRspType& response) { |
Patrick Williams | 099fb09 | 2023-05-10 07:50:31 -0500 | [diff] [blame] | 82 | if (!ec) |
| 83 | { |
| 84 | const uint8_t& cc = std::get<3>(response); |
| 85 | const std::vector<uint8_t>& responseData = |
| 86 | std::get<4>(response); |
| 87 | std::vector<uint8_t> payload; |
| 88 | payload.reserve(1 + responseData.size()); |
| 89 | payload.push_back(cc); |
| 90 | payload.insert(payload.end(), responseData.begin(), |
| 91 | responseData.end()); |
| 92 | handler->outPayload = std::move(payload); |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | std::vector<uint8_t> payload; |
| 97 | payload.push_back(IPMI_CC_UNSPECIFIED_ERROR); |
| 98 | handler->outPayload = std::move(payload); |
| 99 | } |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 100 | }, |
| 101 | "xyz.openbmc_project.Ipmi.Host", "/xyz/openbmc_project/Ipmi", |
| 102 | "xyz.openbmc_project.Ipmi.Server", "execute", netFn, lun, cmd, |
| 103 | commandData, options); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 104 | } |
| 105 | else |
| 106 | { |
| 107 | auto start = std::chrono::steady_clock::now(); |
| 108 | |
Vernon Mauery | cfb34ca | 2021-06-10 15:37:07 -0700 | [diff] [blame] | 109 | // Ignore messages that are not part of an active/pre-active session |
| 110 | if (handler->sessionID != session::sessionZero) |
| 111 | { |
| 112 | std::shared_ptr<session::Session> session = |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 113 | session::Manager::get().getSession(handler->sessionID); |
Vernon Mauery | cfb34ca | 2021-06-10 15:37:07 -0700 | [diff] [blame] | 114 | auto state = static_cast<session::State>(session->state()); |
| 115 | if ((state != session::State::setupInProgress) && |
| 116 | (state != session::State::active)) |
| 117 | { |
| 118 | return; |
| 119 | } |
| 120 | } |
| 121 | |
Patrick Williams | 099fb09 | 2023-05-10 07:50:31 -0500 | [diff] [blame] | 122 | handler->outPayload = iterator->second->executeCommand(commandData, |
| 123 | handler); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 124 | |
| 125 | auto end = std::chrono::steady_clock::now(); |
| 126 | |
Yong Li | 899cf5a | 2020-01-19 13:32:20 +0800 | [diff] [blame] | 127 | std::chrono::duration<size_t> elapsedSeconds = |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 128 | std::chrono::duration_cast<std::chrono::seconds>(end - start); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 129 | |
| 130 | // If command time execution time exceeds 2 seconds, log a time |
| 131 | // exceeded message |
| 132 | if (elapsedSeconds > 2s) |
| 133 | { |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 134 | lg2::error("IPMI command timed out: {DELAY}", "DELAY", |
| 135 | elapsedSeconds.count()); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 136 | } |
| 137 | } |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 138 | } |
| 139 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 140 | std::vector<uint8_t> |
| 141 | NetIpmidEntry::executeCommand(std::vector<uint8_t>& commandData, |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 142 | std::shared_ptr<message::Handler> handler) |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 143 | { |
| 144 | std::vector<uint8_t> errResponse; |
| 145 | |
| 146 | // 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] | 147 | if (!sessionless && (handler->sessionID == session::sessionZero)) |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 148 | { |
| 149 | errResponse.resize(1); |
| 150 | errResponse[0] = IPMI_CC_INSUFFICIENT_PRIVILEGE; |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 151 | lg2::info( |
| 152 | "Table: Insufficient privilege for command: lun: {LUN}, netFn: {NETFN}, command: {COMMAND}", |
| 153 | "LUN", command.lun(), "NETFN", command.netFn(), "COMMAND", |
| 154 | command.cmd()); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 155 | return errResponse; |
| 156 | } |
| 157 | |
Vernon Mauery | 41ff9b5 | 2021-06-11 11:37:40 -0700 | [diff] [blame] | 158 | return functor(commandData, handler); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 159 | } |
| 160 | |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 161 | } // namespace command |