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