blob: e2b154ea5965963adc8fa3bfeac16bf1f3fc5631 [file] [log] [blame]
Tom Joseph5c846a82017-04-03 01:59:39 +05301#include "payload_cmds.hpp"
Vernon Mauery9e801a22018-10-12 13:20:49 -07002
Vernon Mauery2085ae02021-06-10 11:51:00 -07003#include "sessions_manager.hpp"
Tom Joseph5c846a82017-04-03 01:59:39 +05304#include "sol/sol_manager.hpp"
5#include "sol_cmds.hpp"
6
William A. Kennington III4f09eae2019-02-12 17:10:35 -08007#include <ipmid/api.h>
Vernon Mauery9e801a22018-10-12 13:20:49 -07008
Ayushi Smritib12d8752019-09-18 18:37:09 +05309#include <ipmid/api-types.hpp>
George Liu7b7f25f2022-07-04 17:07:32 +080010#include <phosphor-logging/lg2.hpp>
Vernon Mauery9e801a22018-10-12 13:20:49 -070011
Tom Joseph5c846a82017-04-03 01:59:39 +053012namespace sol
13{
14
15namespace command
16{
17
Tom Joseph18a45e92017-04-11 11:30:44 +053018std::vector<uint8_t> activatePayload(const std::vector<uint8_t>& inPayload,
Vernon Mauery41ff9b52021-06-11 11:37:40 -070019 std::shared_ptr<message::Handler>& handler)
Tom Joseph5c846a82017-04-03 01:59:39 +053020{
Vernon Mauery9e801a22018-10-12 13:20:49 -070021 auto request =
22 reinterpret_cast<const ActivatePayloadRequest*>(inPayload.data());
Zhikui Ren2b1edef2020-07-24 14:32:13 -070023 if (inPayload.size() != sizeof(*request))
24 {
25 std::vector<uint8_t> errorPayload{IPMI_CC_REQ_DATA_LEN_INVALID};
26 return errorPayload;
27 }
28
29 std::vector<uint8_t> outPayload(sizeof(ActivatePayloadResponse));
Vernon Mauery9e801a22018-10-12 13:20:49 -070030 auto response =
31 reinterpret_cast<ActivatePayloadResponse*>(outPayload.data());
Tom Joseph5c846a82017-04-03 01:59:39 +053032
33 response->completionCode = IPMI_CC_OK;
34
35 // SOL is the payload currently supported for activation.
36 if (static_cast<uint8_t>(message::PayloadType::SOL) != request->payloadType)
37 {
38 response->completionCode = IPMI_CC_INVALID_FIELD_REQUEST;
39 return outPayload;
40 }
41
Vernon Mauery2085ae02021-06-10 11:51:00 -070042 sol::Manager::get().updateSOLParameter(ipmi::convertCurrentChannelNum(
43 ipmi::currentChNum, getInterfaceIndex()));
44 if (!sol::Manager::get().enable)
Tom Joseph96b6d812017-04-28 01:27:17 +053045 {
46 response->completionCode = IPMI_CC_PAYLOAD_TYPE_DISABLED;
47 return outPayload;
48 }
49
Tom Joseph5c846a82017-04-03 01:59:39 +053050 // Only one instance of SOL is currently supported.
51 if (request->payloadInstance != 1)
52 {
53 response->completionCode = IPMI_CC_INVALID_FIELD_REQUEST;
54 return outPayload;
55 }
56
Vernon Mauery41ff9b52021-06-11 11:37:40 -070057 auto session = session::Manager::get().getSession(handler->sessionID);
Tom Joseph5c846a82017-04-03 01:59:39 +053058
59 if (!request->encryption && session->isCryptAlgoEnabled())
60 {
61 response->completionCode = IPMI_CC_PAYLOAD_WITHOUT_ENCRYPTION;
62 return outPayload;
63 }
64
Tang Yiweide4121c2022-05-17 18:21:03 +080065 if (session->currentPrivilege() <
66 static_cast<uint8_t>(sol::Manager::get().solMinPrivilege))
67 {
68 response->completionCode = IPMI_CC_INSUFFICIENT_PRIVILEGE;
69 return outPayload;
70 }
71
Saravanan Palanisamy0a269042019-07-05 02:21:21 +000072 // Is SOL Payload enabled for this user & channel.
73 auto userId = ipmi::ipmiUserGetUserId(session->userName);
74 ipmi::PayloadAccess payloadAccess = {};
75 if ((ipmi::ipmiUserGetUserPayloadAccess(session->channelNum(), userId,
76 payloadAccess) != IPMI_CC_OK) ||
77 !(payloadAccess.stdPayloadEnables1[static_cast<uint8_t>(
78 message::PayloadType::SOL)]))
79 {
80 response->completionCode = IPMI_CC_PAYLOAD_TYPE_DISABLED;
81 return outPayload;
82 }
83
Vernon Mauery2085ae02021-06-10 11:51:00 -070084 auto status = sol::Manager::get().isPayloadActive(request->payloadInstance);
Tom Joseph5c846a82017-04-03 01:59:39 +053085 if (status)
86 {
87 response->completionCode = IPMI_CC_PAYLOAD_ALREADY_ACTIVE;
88 return outPayload;
89 }
90
91 // Set the current command's socket channel to the session
Vernon Mauery41ff9b52021-06-11 11:37:40 -070092 handler->setChannelInSession();
Tom Joseph5c846a82017-04-03 01:59:39 +053093
94 // Start the SOL payload
95 try
96 {
Vernon Mauery2085ae02021-06-10 11:51:00 -070097 sol::Manager::get().startPayloadInstance(request->payloadInstance,
Vernon Mauery41ff9b52021-06-11 11:37:40 -070098 handler->sessionID);
Tom Joseph5c846a82017-04-03 01:59:39 +053099 }
Patrick Williams12d199b2021-10-06 12:36:48 -0500100 catch (const std::exception& e)
Tom Joseph5c846a82017-04-03 01:59:39 +0530101 {
George Liu7b7f25f2022-07-04 17:07:32 +0800102 lg2::error("Failed to start SOL payload: {ERROR}", "ERROR", e);
Tom Joseph5c846a82017-04-03 01:59:39 +0530103 response->completionCode = IPMI_CC_UNSPECIFIED_ERROR;
104 return outPayload;
105 }
106
107 response->inPayloadSize = endian::to_ipmi<uint16_t>(MAX_PAYLOAD_SIZE);
108 response->outPayloadSize = endian::to_ipmi<uint16_t>(MAX_PAYLOAD_SIZE);
109 response->portNum = endian::to_ipmi<uint16_t>(IPMI_STD_PORT);
110
111 // VLAN addressing is not used
112 response->vlanNum = 0xFFFF;
113
114 return outPayload;
115}
116
Vernon Mauery41ff9b52021-06-11 11:37:40 -0700117std::vector<uint8_t>
118 deactivatePayload(const std::vector<uint8_t>& inPayload,
Tang Yiweide4121c2022-05-17 18:21:03 +0800119 std::shared_ptr<message::Handler>& handler)
Tom Josephe2f0a8e2017-04-03 02:01:49 +0530120{
Vernon Mauery9e801a22018-10-12 13:20:49 -0700121 auto request =
122 reinterpret_cast<const DeactivatePayloadRequest*>(inPayload.data());
Zhikui Ren2b1edef2020-07-24 14:32:13 -0700123 if (inPayload.size() != sizeof(*request))
124 {
125 std::vector<uint8_t> errorPayload{IPMI_CC_REQ_DATA_LEN_INVALID};
126 return errorPayload;
127 }
128
129 std::vector<uint8_t> outPayload(sizeof(DeactivatePayloadResponse));
Vernon Mauery9e801a22018-10-12 13:20:49 -0700130 auto response =
131 reinterpret_cast<DeactivatePayloadResponse*>(outPayload.data());
Tom Josephe2f0a8e2017-04-03 02:01:49 +0530132 response->completionCode = IPMI_CC_OK;
133
134 // SOL is the payload currently supported for deactivation
135 if (static_cast<uint8_t>(message::PayloadType::SOL) != request->payloadType)
136 {
137 response->completionCode = IPMI_CC_INVALID_FIELD_REQUEST;
138 return outPayload;
139 }
140
141 // Only one instance of SOL is supported
142 if (request->payloadInstance != 1)
143 {
144 response->completionCode = IPMI_CC_INVALID_FIELD_REQUEST;
145 return outPayload;
146 }
147
Vernon Mauery2085ae02021-06-10 11:51:00 -0700148 auto status = sol::Manager::get().isPayloadActive(request->payloadInstance);
Tom Josephe2f0a8e2017-04-03 02:01:49 +0530149 if (!status)
150 {
151 response->completionCode = IPMI_CC_PAYLOAD_DEACTIVATED;
152 return outPayload;
153 }
154
Tang Yiweide4121c2022-05-17 18:21:03 +0800155 auto currentSession =
156 session::Manager::get().getSession(handler->sessionID);
157 auto solSessionID =
158 sol::Manager::get().getContext(request->payloadInstance).sessionID;
Jayaprakash Mutyala34a2c412023-09-08 10:30:28 +0000159 auto solActiveSession =
160 sol::Manager::get().getContext(request->payloadInstance).session;
Tang Yiweide4121c2022-05-17 18:21:03 +0800161 // The session owner or the ADMIN could deactivate the session
162 if (currentSession->userName != solActiveSession->userName &&
163 currentSession->currentPrivilege() !=
164 static_cast<uint8_t>(session::Privilege::ADMIN))
165 {
166 response->completionCode = IPMI_CC_INSUFFICIENT_PRIVILEGE;
167 return outPayload;
168 }
169
Tom Josephe2f0a8e2017-04-03 02:01:49 +0530170 try
171 {
Vernon Mauery2085ae02021-06-10 11:51:00 -0700172 sol::Manager::get().stopPayloadInstance(request->payloadInstance);
Tom Josephe2f0a8e2017-04-03 02:01:49 +0530173
Tom Joseph6516cef2017-07-31 18:48:34 +0530174 try
175 {
Tang Yiweide4121c2022-05-17 18:21:03 +0800176 activating(request->payloadInstance, solSessionID);
Tom Joseph6516cef2017-07-31 18:48:34 +0530177 }
Patrick Williams12d199b2021-10-06 12:36:48 -0500178 catch (const std::exception& e)
Tom Joseph6516cef2017-07-31 18:48:34 +0530179 {
George Liu7b7f25f2022-07-04 17:07:32 +0800180 lg2::info("Failed to call the activating method: {ERROR}", "ERROR",
181 e);
Tom Joseph6516cef2017-07-31 18:48:34 +0530182 /*
183 * In case session has been closed (like in the case of inactivity
184 * timeout), then activating function would throw an exception,
Jayaprakash Mutyala34a2c412023-09-08 10:30:28 +0000185 * since solSessionID is not found. As session is already closed,
186 * returning IPMI status code for Payload already deactivated
187 * as BMC automatically deactivates all active payloads when
188 * session is terminated.
Tom Joseph6516cef2017-07-31 18:48:34 +0530189 */
Jayaprakash Mutyala34a2c412023-09-08 10:30:28 +0000190 response->completionCode = IPMI_CC_PAYLOAD_DEACTIVATED;
Tom Joseph6516cef2017-07-31 18:48:34 +0530191 return outPayload;
192 }
Tom Josephe2f0a8e2017-04-03 02:01:49 +0530193 }
Patrick Williams12d199b2021-10-06 12:36:48 -0500194 catch (const std::exception& e)
Tom Josephe2f0a8e2017-04-03 02:01:49 +0530195 {
George Liu7b7f25f2022-07-04 17:07:32 +0800196 lg2::error("Failed to call the getContext method: {ERROR}", "ERROR", e);
Tom Josephe2f0a8e2017-04-03 02:01:49 +0530197 response->completionCode = IPMI_CC_UNSPECIFIED_ERROR;
198 return outPayload;
199 }
200
201 return outPayload;
202}
203
Vernon Mauery41ff9b52021-06-11 11:37:40 -0700204std::vector<uint8_t>
205 getPayloadStatus(const std::vector<uint8_t>& inPayload,
George Liube1470c2022-07-04 14:33:24 +0800206 std::shared_ptr<message::Handler>& /* handler */)
Tom Josephe1ae56c2017-04-03 02:03:41 +0530207{
Vernon Mauery9e801a22018-10-12 13:20:49 -0700208 auto request =
209 reinterpret_cast<const GetPayloadStatusRequest*>(inPayload.data());
Zhikui Ren2b1edef2020-07-24 14:32:13 -0700210 if (inPayload.size() != sizeof(*request))
211 {
212 std::vector<uint8_t> errorPayload{IPMI_CC_REQ_DATA_LEN_INVALID};
213 return errorPayload;
214 }
215
216 std::vector<uint8_t> outPayload(sizeof(GetPayloadStatusResponse));
Vernon Mauery9e801a22018-10-12 13:20:49 -0700217 auto response =
218 reinterpret_cast<GetPayloadStatusResponse*>(outPayload.data());
Tom Josephe1ae56c2017-04-03 02:03:41 +0530219
220 // SOL is the payload currently supported for payload status
221 if (static_cast<uint8_t>(message::PayloadType::SOL) != request->payloadType)
222 {
223 response->completionCode = IPMI_CC_UNSPECIFIED_ERROR;
224 return outPayload;
225 }
226
227 response->completionCode = IPMI_CC_OK;
Ayushi Smritiddba9d12019-09-13 12:03:31 +0530228
229 constexpr size_t maxSolPayloadInstances = 1;
230 response->capacity = maxSolPayloadInstances;
Tom Josephe1ae56c2017-04-03 02:03:41 +0530231
232 // Currently we support only one SOL session
Vernon Mauery2085ae02021-06-10 11:51:00 -0700233 response->instance1 = sol::Manager::get().isPayloadActive(1);
Tom Josephe1ae56c2017-04-03 02:03:41 +0530234
235 return outPayload;
236}
237
George Liube1470c2022-07-04 14:33:24 +0800238std::vector<uint8_t>
239 getPayloadInfo(const std::vector<uint8_t>& inPayload,
240 std::shared_ptr<message::Handler>& /* handler */)
Tom Joseph80938492018-03-22 10:05:20 +0530241{
Vernon Mauery9e801a22018-10-12 13:20:49 -0700242 auto request =
243 reinterpret_cast<const GetPayloadInfoRequest*>(inPayload.data());
Zhikui Ren2b1edef2020-07-24 14:32:13 -0700244
245 if (inPayload.size() != sizeof(*request))
246 {
247 std::vector<uint8_t> errorPayload{IPMI_CC_REQ_DATA_LEN_INVALID};
248 return errorPayload;
249 }
250
251 std::vector<uint8_t> outPayload(sizeof(GetPayloadInfoResponse));
Vernon Mauery9e801a22018-10-12 13:20:49 -0700252 auto response =
253 reinterpret_cast<GetPayloadInfoResponse*>(outPayload.data());
Tom Joseph80938492018-03-22 10:05:20 +0530254
255 // SOL is the payload currently supported for payload status & only one
256 // instance of SOL is supported.
Vernon Mauery9e801a22018-10-12 13:20:49 -0700257 if (static_cast<uint8_t>(message::PayloadType::SOL) !=
258 request->payloadType ||
259 request->payloadInstance != 1)
Tom Joseph80938492018-03-22 10:05:20 +0530260 {
261 response->completionCode = IPMI_CC_INVALID_FIELD_REQUEST;
262 return outPayload;
263 }
Vernon Mauery2085ae02021-06-10 11:51:00 -0700264 auto status = sol::Manager::get().isPayloadActive(request->payloadInstance);
Tom Joseph80938492018-03-22 10:05:20 +0530265
Richard Marian Thomaiyar5f1dd312019-01-22 15:55:41 +0530266 if (status)
Tom Joseph80938492018-03-22 10:05:20 +0530267 {
Vernon Mauery2085ae02021-06-10 11:51:00 -0700268 auto& context =
269 sol::Manager::get().getContext(request->payloadInstance);
Richard Marian Thomaiyar5f1dd312019-01-22 15:55:41 +0530270 response->sessionID = context.sessionID;
Tom Joseph80938492018-03-22 10:05:20 +0530271 }
Richard Marian Thomaiyar5f1dd312019-01-22 15:55:41 +0530272 else
273 {
274 // No active payload - return session id as 0
275 response->sessionID = 0;
276 }
277 response->completionCode = IPMI_CC_OK;
Tom Joseph80938492018-03-22 10:05:20 +0530278 return outPayload;
279}
280
Tom Joseph5c846a82017-04-03 01:59:39 +0530281} // namespace command
282
283} // namespace sol