xref: /openbmc/pldm/softoff/softoff.cpp (revision 3c8b3e353728899e1c32827c1ade9f4b427006a9)
1 #include "softoff.hpp"
2 
3 #include "common/instance_id.hpp"
4 #include "common/transport.hpp"
5 #include "common/utils.hpp"
6 
7 #include <libpldm/entity.h>
8 #include <libpldm/platform.h>
9 #include <libpldm/state_set.h>
10 
11 #include <phosphor-logging/lg2.hpp>
12 #include <sdbusplus/bus.hpp>
13 #include <sdeventplus/clock.hpp>
14 #include <sdeventplus/exception.hpp>
15 #include <sdeventplus/source/io.hpp>
16 #include <sdeventplus/source/time.hpp>
17 
18 #include <array>
19 #include <filesystem>
20 #include <fstream>
21 
22 PHOSPHOR_LOG2_USING;
23 
24 namespace pldm
25 {
26 using namespace sdeventplus;
27 using namespace sdeventplus::source;
28 namespace fs = std::filesystem;
29 constexpr auto clockId = sdeventplus::ClockId::RealTime;
30 using Clock = Clock<clockId>;
31 using Timer = Time<clockId>;
32 
33 using sdbusplus::exception::SdBusError;
34 
35 // Shutdown effecter terminus ID, set when we look up the effecter
36 pldm::pdr::TerminusID TID = 0;
37 
38 namespace sdbusRule = sdbusplus::bus::match::rules;
39 
SoftPowerOff(sdbusplus::bus_t & bus,sd_event * event,pldm::InstanceIdDb & instanceIdDb)40 SoftPowerOff::SoftPowerOff(sdbusplus::bus_t& bus, sd_event* event,
41                            pldm::InstanceIdDb& instanceIdDb) :
42     bus(bus), timer(event), instanceIdDb(instanceIdDb)
43 {
44     auto jsonData = parseConfig();
45 
46     if (jsonData.is_discarded())
47     {
48         error("Failed to parse softoff config JSON file");
49         return;
50     }
51 
52     getHostState();
53     if (hasError || completed)
54     {
55         return;
56     }
57     const std::vector<Json> emptyJsonList{};
58     auto entries = jsonData.value("entries", emptyJsonList);
59     for (const auto& entry : entries)
60     {
61         TID = entry.value("tid", 0);
62         pldm::pdr::EntityType entityType = entry.value("entityType", 0);
63         pldm::pdr::StateSetId stateSetId = entry.value("stateSetId", 0);
64 
65         bool effecterFound = getEffecterID(entityType, stateSetId);
66         if (effecterFound)
67         {
68             auto rc = getSensorInfo(entityType, stateSetId);
69             if (rc != PLDM_SUCCESS)
70             {
71                 error("Failed to get Sensor PDRs, response code '{RC}'", "RC",
72                       lg2::hex, rc);
73                 hasError = true;
74                 return;
75             }
76             break;
77         }
78         else
79         {
80             continue;
81         }
82     }
83 
84     // Matches on the pldm StateSensorEvent signal
85     pldmEventSignal = std::make_unique<sdbusplus::bus::match_t>(
86         bus,
87         sdbusRule::type::signal() + sdbusRule::member("StateSensorEvent") +
88             sdbusRule::path("/xyz/openbmc_project/pldm") +
89             sdbusRule::interface("xyz.openbmc_project.PLDM.Event"),
90         std::bind(std::mem_fn(&SoftPowerOff::hostSoftOffComplete), this,
91                   std::placeholders::_1));
92 }
93 
getHostState()94 int SoftPowerOff::getHostState()
95 {
96     try
97     {
98         pldm::utils::PropertyValue propertyValue =
99             pldm::utils::DBusHandler().getDbusPropertyVariant(
100                 "/xyz/openbmc_project/state/host0", "CurrentHostState",
101                 "xyz.openbmc_project.State.Host");
102 
103         if ((std::get<std::string>(propertyValue) !=
104              "xyz.openbmc_project.State.Host.HostState.Running") &&
105             (std::get<std::string>(propertyValue) !=
106              "xyz.openbmc_project.State.Host.HostState.TransitioningToOff"))
107         {
108             // Host state is not "Running", this app should return success
109             completed = true;
110             return PLDM_SUCCESS;
111         }
112     }
113     catch (const std::exception& e)
114     {
115         error(
116             "PLDM remote terminus soft off. Can't get current remote terminus state, error - {ERROR}",
117             "ERROR", e);
118         hasError = true;
119         return PLDM_ERROR;
120     }
121 
122     return PLDM_SUCCESS;
123 }
124 
hostSoftOffComplete(sdbusplus::message_t & msg)125 void SoftPowerOff::hostSoftOffComplete(sdbusplus::message_t& msg)
126 {
127     pldm::pdr::TerminusID msgTID;
128     pldm::pdr::SensorID msgSensorID;
129     pldm::pdr::SensorOffset msgSensorOffset;
130     pldm::pdr::EventState msgEventState;
131     pldm::pdr::EventState msgPreviousEventState;
132 
133     // Read the msg and populate each variable
134     msg.read(msgTID, msgSensorID, msgSensorOffset, msgEventState,
135              msgPreviousEventState);
136 
137     if (msgSensorID == sensorID && msgSensorOffset == sensorOffset &&
138         msgEventState == PLDM_SW_TERM_GRACEFUL_SHUTDOWN && msgTID == TID)
139     {
140         // Receive Graceful shutdown completion event message. Disable the timer
141         auto rc = timer.stop();
142         if (rc < 0)
143         {
144             error(
145                 "Failure to STOP the timer of PLDM soft off, response code '{RC}'",
146                 "RC", rc);
147         }
148 
149         // This marks the completion of pldm soft power off.
150         completed = true;
151     }
152 }
153 
parseConfig()154 Json SoftPowerOff::parseConfig()
155 {
156     fs::path softoffConfigJson(
157         fs::path(SOFTOFF_CONFIG_JSON) / "softoff_config.json");
158 
159     if (!fs::exists(softoffConfigJson) || fs::is_empty(softoffConfigJson))
160     {
161         error(
162             "Failed to parse softoff config JSON file '{PATH}', file does not exist",
163             "PATH", softoffConfigJson);
164         return PLDM_ERROR;
165     }
166 
167     std::ifstream jsonFile(softoffConfigJson);
168     return Json::parse(jsonFile);
169 }
170 
getEffecterID(pldm::pdr::EntityType & entityType,pldm::pdr::StateSetId & stateSetId)171 bool SoftPowerOff::getEffecterID(pldm::pdr::EntityType& entityType,
172                                  pldm::pdr::StateSetId& stateSetId)
173 {
174     auto& bus = pldm::utils::DBusHandler::getBus();
175     try
176     {
177         std::vector<std::vector<uint8_t>> response{};
178         auto method = bus.new_method_call(
179             "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm",
180             "xyz.openbmc_project.PLDM.PDR", "FindStateEffecterPDR");
181         method.append(TID, entityType, stateSetId);
182         auto responseMsg = bus.call(method, dbusTimeout);
183 
184         responseMsg.read(response);
185         if (response.size())
186         {
187             for (auto& rep : response)
188             {
189                 auto softoffPdr = new (rep.data()) pldm_state_effecter_pdr;
190                 effecterID = softoffPdr->effecter_id;
191             }
192         }
193         else
194         {
195             return false;
196         }
197     }
198     catch (const sdbusplus::exception_t& e)
199     {
200         error("Failed to get softPowerOff PDR, error - {ERROR}", "ERROR", e);
201         return false;
202     }
203     return true;
204 }
205 
getSensorInfo(pldm::pdr::EntityType & entityType,pldm::pdr::StateSetId & stateSetId)206 int SoftPowerOff::getSensorInfo(pldm::pdr::EntityType& entityType,
207                                 pldm::pdr::StateSetId& stateSetId)
208 {
209     try
210     {
211         auto& bus = pldm::utils::DBusHandler::getBus();
212         std::vector<std::vector<uint8_t>> Response{};
213         auto method = bus.new_method_call(
214             "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm",
215             "xyz.openbmc_project.PLDM.PDR", "FindStateSensorPDR");
216         method.append(TID, entityType, stateSetId);
217 
218         auto ResponseMsg = bus.call(method, dbusTimeout);
219 
220         ResponseMsg.read(Response);
221 
222         if (Response.size() == 0)
223         {
224             error("No sensor PDR has been found that matches the criteria");
225             return PLDM_ERROR;
226         }
227 
228         pldm_state_sensor_pdr* pdr = nullptr;
229         for (auto& rep : Response)
230         {
231             pdr = new (rep.data()) pldm_state_sensor_pdr;
232             if (!pdr)
233             {
234                 error("Failed to get state sensor PDR.");
235                 return PLDM_ERROR;
236             }
237         }
238 
239         sensorID = pdr->sensor_id;
240 
241         auto compositeSensorCount = pdr->composite_sensor_count;
242         auto possibleStatesStart = pdr->possible_states;
243 
244         for (auto offset = 0; offset < compositeSensorCount; offset++)
245         {
246             auto possibleStates = new (possibleStatesStart)
247                 state_sensor_possible_states;
248             auto setId = possibleStates->state_set_id;
249             auto possibleStateSize = possibleStates->possible_states_size;
250 
251             if (setId == PLDM_STATE_SET_SW_TERMINATION_STATUS)
252             {
253                 sensorOffset = offset;
254                 break;
255             }
256             possibleStatesStart +=
257                 possibleStateSize + sizeof(setId) + sizeof(possibleStateSize);
258         }
259     }
260     catch (const sdbusplus::exception_t& e)
261     {
262         error("Failed to get state sensor PDR during soft-off, error - {ERROR}",
263               "ERROR", e);
264         return PLDM_ERROR;
265     }
266 
267     return PLDM_SUCCESS;
268 }
269 
hostSoftOff(sdeventplus::Event & event)270 int SoftPowerOff::hostSoftOff(sdeventplus::Event& event)
271 {
272     constexpr uint8_t effecterCount = 1;
273     PldmTransport pldmTransport{};
274     uint8_t instanceID;
275     uint8_t mctpEID;
276 
277     mctpEID = pldm::utils::readHostEID();
278     // TODO: fix mapping to work around OpenBMC ecosystem deficiencies
279     pldm_tid_t pldmTID = static_cast<pldm_tid_t>(mctpEID);
280 
281     std::array<uint8_t,
282                sizeof(pldm_msg_hdr) + sizeof(effecterID) +
283                    sizeof(effecterCount) + sizeof(set_effecter_state_field)>
284         requestMsg{};
285     auto request = new (requestMsg.data()) pldm_msg;
286     set_effecter_state_field stateField{
287         PLDM_REQUEST_SET, PLDM_SW_TERM_GRACEFUL_SHUTDOWN_REQUESTED};
288     instanceID = instanceIdDb.next(pldmTID);
289     auto rc = encode_set_state_effecter_states_req(
290         instanceID, effecterID, effecterCount, &stateField, request);
291     if (rc != PLDM_SUCCESS)
292     {
293         instanceIdDb.free(pldmTID, instanceID);
294         error(
295             "Failed to encode set state effecter states request message, response code '{RC}'",
296             "RC", lg2::hex, rc);
297         return PLDM_ERROR;
298     }
299 
300     // Add a timer to the event loop, default 30s.
301     auto timerCallback = [=, this](Timer& /*source*/,
302                                    Timer::TimePoint /*time*/) mutable {
303         if (!responseReceived)
304         {
305             instanceIdDb.free(pldmTID, instanceID);
306             error(
307                 "PLDM soft off failed, can't get the response for the PLDM request msg. Time out! Exit the pldm-softpoweroff");
308             exit(-1);
309         }
310         return;
311     };
312     Timer time(event, (Clock(event).now() + std::chrono::seconds{30}),
313                std::chrono::seconds{1}, std::move(timerCallback));
314 
315     // Add a callback to handle EPOLLIN on fd
316     auto callback = [=, &pldmTransport,
317                      this](IO& io, int fd, uint32_t revents) mutable {
318         if (fd != pldmTransport.getEventSource())
319         {
320             return;
321         }
322 
323         if (!(revents & EPOLLIN))
324         {
325             return;
326         }
327 
328         void* responseMsg = nullptr;
329         size_t responseMsgSize{};
330         pldm_tid_t srcTID = pldmTID;
331 
332         auto rc = pldmTransport.recvMsg(pldmTID, responseMsg, responseMsgSize);
333         if (rc)
334         {
335             error(
336                 "Failed to receive pldm data during soft-off, response code '{RC}'",
337                 "RC", rc);
338             return;
339         }
340 
341         std::unique_ptr<void, decltype(std::free)*> responseMsgPtr{
342             responseMsg, std::free};
343 
344         // We've got the response meant for the PLDM request msg that was
345         // sent out
346         io.set_enabled(Enabled::Off);
347         auto response = new (responseMsgPtr.get()) pldm_msg;
348 
349         if (srcTID != pldmTID ||
350             !pldm_msg_hdr_correlate_response(&request->hdr, &response->hdr))
351         {
352             /* This isn't the response we were looking for */
353             return;
354         }
355 
356         /* We have the right response, release the instance ID and process */
357         io.set_enabled(Enabled::Off);
358         instanceIdDb.free(pldmTID, instanceID);
359 
360         if (response->payload[0] != PLDM_SUCCESS)
361         {
362             error("Getting the wrong response, response code '{RC}'", "RC",
363                   response->payload[0]);
364             exit(-1);
365         }
366 
367         responseReceived = true;
368 
369         // Start Timer
370         using namespace std::chrono;
371         auto timeMicroseconds =
372             duration_cast<microseconds>(seconds(SOFTOFF_TIMEOUT_SECONDS));
373 
374         auto ret = startTimer(timeMicroseconds);
375         if (ret < 0)
376         {
377             error(
378                 "Failure to start remote terminus soft off wait timer, Exit the pldm-softpoweroff with response code:{NUM}",
379                 "NUM", ret);
380             exit(-1);
381         }
382         else
383         {
384             error(
385                 "Timer started waiting for remote terminus soft off, timeout in sec '{TIMEOUT_SEC}'",
386                 "TIMEOUT_SEC", SOFTOFF_TIMEOUT_SECONDS);
387         }
388         return;
389     };
390     IO io(event, pldmTransport.getEventSource(), EPOLLIN, std::move(callback));
391 
392     // Asynchronously send the PLDM request
393     rc = pldmTransport.sendMsg(pldmTID, requestMsg.data(), requestMsg.size());
394     if (0 > rc)
395     {
396         instanceIdDb.free(pldmTID, instanceID);
397         error(
398             "Failed to send message/receive response, response code '{RC}' and error - {ERROR}",
399             "RC", rc, "ERROR", errno);
400         return PLDM_ERROR;
401     }
402 
403     // Time out or soft off complete
404     while (!isCompleted() && !isTimerExpired())
405     {
406         try
407         {
408             event.run(std::nullopt);
409         }
410         catch (const sdeventplus::SdEventError& e)
411         {
412             instanceIdDb.free(pldmTID, instanceID);
413             error(
414                 "Failed to process request while remote terminus soft off, error - {ERROR}",
415                 "ERROR", e);
416             return PLDM_ERROR;
417         }
418     }
419 
420     return PLDM_SUCCESS;
421 }
422 
startTimer(const std::chrono::microseconds & usec)423 int SoftPowerOff::startTimer(const std::chrono::microseconds& usec)
424 {
425     return timer.start(usec);
426 }
427 } // namespace pldm
428