Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 3 | #include "message_handler.hpp" |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 4 | |
William A. Kennington III | 4f09eae | 2019-02-12 17:10:35 -0800 | [diff] [blame] | 5 | #include <ipmid/api.h> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 6 | |
Andrew Geissler | 9d9b763 | 2020-05-17 09:18:05 -0500 | [diff] [blame] | 7 | #include <cstddef> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 8 | #include <functional> |
| 9 | #include <map> |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 10 | |
| 11 | namespace command |
| 12 | { |
| 13 | |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 14 | struct CommandID |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 15 | { |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 16 | static constexpr size_t lunBits = 2; |
| 17 | CommandID(uint32_t command) : command(command) |
George Liu | bc8958f | 2022-07-04 09:29:49 +0800 | [diff] [blame] | 18 | {} |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 19 | |
Vernon Mauery | 6650164 | 2018-07-30 09:07:10 -0700 | [diff] [blame] | 20 | uint8_t netFnLun() const |
| 21 | { |
| 22 | return static_cast<uint8_t>(command >> CHAR_BIT); |
| 23 | } |
| 24 | uint8_t netFn() const |
| 25 | { |
| 26 | return netFnLun() >> lunBits; |
| 27 | } |
| 28 | uint8_t lun() const |
| 29 | { |
| 30 | return netFnLun() & ((1 << (lunBits + 1)) - 1); |
| 31 | } |
| 32 | uint8_t cmd() const |
| 33 | { |
| 34 | return static_cast<uint8_t>(command); |
| 35 | } |
| 36 | uint32_t command; |
| 37 | }; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 38 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 39 | /** |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 40 | * CommandFunctor is the functor register for commands defined in |
| 41 | * phosphor-net-ipmid. This would take the request part of the command as a |
| 42 | * vector and a reference to the message handler. The response part of the |
| 43 | * command is returned as a vector. |
| 44 | */ |
| 45 | using CommandFunctor = std::function<std::vector<uint8_t>( |
Vernon Mauery | 41ff9b5 | 2021-06-11 11:37:40 -0700 | [diff] [blame] | 46 | const std::vector<uint8_t>&, std::shared_ptr<message::Handler>&)>; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 47 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 48 | /** |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 49 | * @struct CmdDetails |
| 50 | * |
| 51 | * Command details is used to register commands supported in phosphor-net-ipmid. |
| 52 | */ |
| 53 | struct CmdDetails |
| 54 | { |
| 55 | CommandID command; |
| 56 | CommandFunctor functor; |
| 57 | session::Privilege privilege; |
| 58 | bool sessionless; |
| 59 | }; |
| 60 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 61 | /** |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 62 | * @enum NetFns |
| 63 | * |
| 64 | * A field that identifies the functional class of the message. The Network |
| 65 | * Function clusters IPMI commands into different sets. |
| 66 | */ |
| 67 | enum class NetFns |
| 68 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 69 | CHASSIS = (0x00 << 10), |
| 70 | CHASSIS_RESP = (0x01 << 10), |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 71 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 72 | BRIDGE = (0x02 << 10), |
| 73 | BRIDGE_RESP = (0x03 << 10), |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 74 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 75 | SENSOR = (0x04 << 10), |
| 76 | SENSOR_RESP = (0x05 << 10), |
| 77 | EVENT = (0x04 << 10), |
| 78 | EVENT_RESP = (0x05 << 10), |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 79 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 80 | APP = (0x06 << 10), |
| 81 | APP_RESP = (0x07 << 10), |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 82 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 83 | FIRMWARE = (0x08 << 10), |
| 84 | FIRMWARE_RESP = (0x09 << 10), |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 85 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 86 | STORAGE = (0x0A << 10), |
| 87 | STORAGE_RESP = (0x0B << 10), |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 88 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 89 | TRANSPORT = (0x0C << 10), |
| 90 | TRANSPORT_RESP = (0x0D << 10), |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 91 | |
| 92 | //>> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 93 | RESERVED_START = (0x0E << 10), |
| 94 | RESERVED_END = (0x2B << 10), |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 95 | //<< |
| 96 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 97 | GROUP_EXTN = (0x2C << 10), |
| 98 | GROUP_EXTN_RESP = (0x2D << 10), |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 99 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 100 | OEM = (0x2E << 10), |
| 101 | OEM_RESP = (0x2F << 10), |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 102 | }; |
| 103 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 104 | /** |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 105 | * @class Entry |
| 106 | * |
| 107 | * This is the base class for registering IPMI commands. There are two ways of |
| 108 | * registering commands to phosphor-net-ipmid, the session related commands and |
| 109 | * provider commands |
| 110 | * |
| 111 | * Every commands has a privilege level which mentions the minimum session |
| 112 | * privilege level needed to execute the command |
| 113 | */ |
| 114 | |
| 115 | class Entry |
| 116 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 117 | public: |
| 118 | Entry(CommandID command, session::Privilege privilege) : |
| 119 | command(command), privilege(privilege) |
George Liu | bc8958f | 2022-07-04 09:29:49 +0800 | [diff] [blame] | 120 | {} |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 121 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 122 | /** |
| 123 | * @brief Execute the command |
| 124 | * |
| 125 | * Execute the command |
| 126 | * |
| 127 | * @param[in] commandData - Request Data for the command |
| 128 | * @param[in] handler - Reference to the Message Handler |
| 129 | * |
| 130 | * @return Response data for the command |
| 131 | */ |
| 132 | virtual std::vector<uint8_t> |
| 133 | executeCommand(std::vector<uint8_t>& commandData, |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 134 | std::shared_ptr<message::Handler> handler) = 0; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 135 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 136 | auto getCommand() const |
| 137 | { |
| 138 | return command; |
| 139 | } |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 140 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 141 | auto getPrivilege() const |
| 142 | { |
| 143 | return privilege; |
| 144 | } |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 145 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 146 | virtual ~Entry() = default; |
| 147 | Entry(const Entry&) = default; |
| 148 | Entry& operator=(const Entry&) = default; |
| 149 | Entry(Entry&&) = default; |
| 150 | Entry& operator=(Entry&&) = default; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 151 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 152 | protected: |
| 153 | CommandID command; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 154 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 155 | // Specifies the minimum privilege level required to execute this command |
| 156 | session::Privilege privilege; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 157 | }; |
| 158 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 159 | /** |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 160 | * @class NetIpmidEntry |
| 161 | * |
| 162 | * NetIpmidEntry is used to register commands that are consumed only in |
| 163 | * phosphor-net-ipmid. The RAKP commands, session commands and user management |
| 164 | * commands are examples of this. |
| 165 | * |
| 166 | * There are certain IPMI commands that can be executed before session can be |
| 167 | * established like Get System GUID, Get Channel Authentication Capabilities |
| 168 | * and RAKP commands. |
| 169 | */ |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 170 | class NetIpmidEntry final : public Entry |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 171 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 172 | public: |
| 173 | NetIpmidEntry(CommandID command, CommandFunctor functor, |
| 174 | session::Privilege privilege, bool sessionless) : |
| 175 | Entry(command, privilege), |
| 176 | functor(functor), sessionless(sessionless) |
George Liu | bc8958f | 2022-07-04 09:29:49 +0800 | [diff] [blame] | 177 | {} |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 178 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 179 | /** |
| 180 | * @brief Execute the command |
| 181 | * |
| 182 | * Execute the command |
| 183 | * |
| 184 | * @param[in] commandData - Request Data for the command |
| 185 | * @param[in] handler - Reference to the Message Handler |
| 186 | * |
| 187 | * @return Response data for the command |
| 188 | */ |
| 189 | std::vector<uint8_t> |
| 190 | executeCommand(std::vector<uint8_t>& commandData, |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 191 | std::shared_ptr<message::Handler> handler) override; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 192 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 193 | virtual ~NetIpmidEntry() = default; |
| 194 | NetIpmidEntry(const NetIpmidEntry&) = default; |
| 195 | NetIpmidEntry& operator=(const NetIpmidEntry&) = default; |
| 196 | NetIpmidEntry(NetIpmidEntry&&) = default; |
| 197 | NetIpmidEntry& operator=(NetIpmidEntry&&) = default; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 198 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 199 | private: |
| 200 | CommandFunctor functor; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 201 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 202 | bool sessionless; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 203 | }; |
| 204 | |
Tom Joseph | 3563f8f | 2017-05-08 15:42:54 +0530 | [diff] [blame] | 205 | /** |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 206 | * @class Table |
| 207 | * |
| 208 | * Table keeps the IPMI command entries as a sorted associative container with |
| 209 | * Command ID as the unique key. It has interfaces for registering commands |
| 210 | * and executing a command. |
| 211 | */ |
| 212 | class Table |
| 213 | { |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 214 | private: |
| 215 | struct Private |
George Liu | bc8958f | 2022-07-04 09:29:49 +0800 | [diff] [blame] | 216 | {}; |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 217 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 218 | public: |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 219 | explicit Table(const Private&) |
George Liu | bc8958f | 2022-07-04 09:29:49 +0800 | [diff] [blame] | 220 | {} |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 221 | Table() = delete; |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 222 | ~Table() = default; |
| 223 | // Command Table is a singleton so copy, copy-assignment, move and |
| 224 | // move assignment is deleted |
| 225 | Table(const Table&) = delete; |
| 226 | Table& operator=(const Table&) = delete; |
| 227 | Table(Table&&) = default; |
| 228 | Table& operator=(Table&&) = default; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 229 | |
Vernon Mauery | 2085ae0 | 2021-06-10 11:51:00 -0700 | [diff] [blame] | 230 | /** |
| 231 | * @brief Get a reference to the singleton Table |
| 232 | * |
| 233 | * @return Table reference |
| 234 | */ |
| 235 | static Table& get() |
| 236 | { |
| 237 | static std::shared_ptr<Table> ptr = nullptr; |
| 238 | if (!ptr) |
| 239 | { |
| 240 | ptr = std::make_shared<Table>(Private()); |
| 241 | } |
| 242 | return *ptr; |
| 243 | } |
| 244 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 245 | using CommandTable = std::map<uint32_t, std::unique_ptr<Entry>>; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 246 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 247 | /** |
| 248 | * @brief Register a command |
| 249 | * |
| 250 | * Register a command with the command table |
| 251 | * |
| 252 | * @param[in] inCommand - Command ID |
| 253 | * @param[in] entry - Command Entry |
| 254 | * |
| 255 | * @return: None |
| 256 | * |
| 257 | * @note: Duplicate registrations will be rejected. |
| 258 | * |
| 259 | */ |
| 260 | void registerCommand(CommandID inCommand, std::unique_ptr<Entry>&& entry); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 261 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 262 | /** |
| 263 | * @brief Execute the command |
| 264 | * |
| 265 | * Execute the command for the corresponding CommandID |
| 266 | * |
| 267 | * @param[in] inCommand - Command ID to execute. |
| 268 | * @param[in] commandData - Request Data for the command |
| 269 | * @param[in] handler - Reference to the Message Handler |
| 270 | * |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 271 | */ |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame] | 272 | void executeCommand(uint32_t inCommand, std::vector<uint8_t>& commandData, |
| 273 | std::shared_ptr<message::Handler> handler); |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 274 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 275 | private: |
| 276 | CommandTable commandTable; |
Tom Joseph | 07181f5 | 2016-08-08 08:17:08 -0500 | [diff] [blame] | 277 | }; |
| 278 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 279 | } // namespace command |