blob: 8e57f20842e53a13aee0da46653bb30713b32955 [file] [log] [blame]
Manojkiran Edaef773052021-07-29 09:29:28 +05301#include "common/flight_recorder.hpp"
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05002#include "common/utils.hpp"
Deepak Kodihalli4de4d002019-11-11 02:41:43 -06003#include "dbus_impl_requester.hpp"
Tom Josephfb3bc062021-08-17 07:48:11 -07004#include "fw-update/manager.hpp"
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +09305#include "instance_id.hpp"
Deepak Kodihallibc669f12019-11-28 08:52:07 -06006#include "invoker.hpp"
Tom Joseph74f27c72021-05-16 07:58:53 -07007#include "requester/handler.hpp"
Tom Josephfb3bc062021-08-17 07:48:11 -07008#include "requester/mctp_endpoint_discovery.hpp"
Tom Joseph74f27c72021-05-16 07:58:53 -07009#include "requester/request.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050010
11#include <err.h>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053012#include <getopt.h>
George Liuc453e162022-12-21 17:16:23 +080013#include <libpldm/base.h>
14#include <libpldm/bios.h>
15#include <libpldm/pdr.h>
16#include <libpldm/platform.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050017#include <poll.h>
18#include <stdlib.h>
19#include <sys/socket.h>
20#include <sys/types.h>
21#include <sys/un.h>
22#include <unistd.h>
23
Riya Dixit49cfb132023-03-02 04:26:53 -060024#include <phosphor-logging/lg2.hpp>
George Liu6492f522020-06-16 10:34:05 +080025#include <sdeventplus/event.hpp>
26#include <sdeventplus/source/io.hpp>
Manojkiran Edaef773052021-07-29 09:29:28 +053027#include <sdeventplus/source/signal.hpp>
28#include <stdplus/signal.hpp>
George Liu6492f522020-06-16 10:34:05 +080029
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050030#include <cstdio>
31#include <cstring>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050032#include <fstream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050033#include <iomanip>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053034#include <iostream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050035#include <iterator>
Deepak Kodihallic682fe22020-03-04 00:42:54 -060036#include <memory>
Tom Joseph74f27c72021-05-16 07:58:53 -070037#include <optional>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050038#include <sstream>
Deepak Kodihallibc669f12019-11-28 08:52:07 -060039#include <stdexcept>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053040#include <string>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050041#include <vector>
42
Riya Dixit49cfb132023-03-02 04:26:53 -060043PHOSPHOR_LOG2_USING;
44
Tom Joseph02b4ee42021-05-02 22:44:36 -070045#ifdef LIBPLDMRESPONDER
46#include "dbus_impl_pdr.hpp"
47#include "host-bmc/dbus_to_event_handler.hpp"
48#include "host-bmc/dbus_to_host_effecters.hpp"
Tom Joseph20aa3e02021-08-17 04:44:19 -070049#include "host-bmc/host_condition.hpp"
Tom Joseph02b4ee42021-05-02 22:44:36 -070050#include "host-bmc/host_pdr_handler.hpp"
51#include "libpldmresponder/base.hpp"
52#include "libpldmresponder/bios.hpp"
53#include "libpldmresponder/fru.hpp"
54#include "libpldmresponder/oem_handler.hpp"
55#include "libpldmresponder/platform.hpp"
56#include "xyz/openbmc_project/PLDM/Event/server.hpp"
57#endif
58
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050059#ifdef OEM_IBM
Sagar Srinivas11ce8d22022-07-28 11:32:34 -050060#include "libpldmresponder/bios_oem_ibm.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050061#include "libpldmresponder/file_io.hpp"
Sampa Misraaea5dde2020-08-31 08:33:47 -050062#include "libpldmresponder/oem_ibm_handler.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050063#endif
64
65constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
66
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050067using namespace pldm;
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060068using namespace sdeventplus;
69using namespace sdeventplus::source;
Tom Joseph02b4ee42021-05-02 22:44:36 -070070using namespace pldm::responder;
71using namespace pldm::utils;
Manojkiran Edaef773052021-07-29 09:29:28 +053072using sdeventplus::source::Signal;
73using namespace pldm::flightrecorder;
74
75void interruptFlightRecorderCallBack(Signal& /*signal*/,
76 const struct signalfd_siginfo*)
77{
Riya Dixit49cfb132023-03-02 04:26:53 -060078 error("Received SIGUR1(10) Signal interrupt");
Manojkiran Edaef773052021-07-29 09:29:28 +053079 // obtain the flight recorder instance and dump the recorder
80 FlightRecorder::GetInstance().playRecorder();
81}
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050082
Tom Joseph74f27c72021-05-16 07:58:53 -070083static std::optional<Response>
84 processRxMsg(const std::vector<uint8_t>& requestMsg, Invoker& invoker,
Tom Josephfb3bc062021-08-17 07:48:11 -070085 requester::Handler<requester::Request>& handler,
86 fw_update::Manager* fwManager)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050087{
Manojkiran Eda31a78442021-09-12 15:18:25 +053088 using type = uint8_t;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050089 uint8_t eid = requestMsg[0];
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050090 pldm_header_info hdrFields{};
91 auto hdr = reinterpret_cast<const pldm_msg_hdr*>(
92 requestMsg.data() + sizeof(eid) + sizeof(type));
93 if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields))
94 {
Riya Dixit49cfb132023-03-02 04:26:53 -060095 error("Empty PLDM request header");
Tom Joseph74f27c72021-05-16 07:58:53 -070096 return std::nullopt;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050097 }
George Liub7095ff2021-06-14 16:01:57 +080098
99 if (PLDM_RESPONSE != hdrFields.msg_type)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500100 {
Tom Joseph74f27c72021-05-16 07:58:53 -0700101 Response response;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500102 auto request = reinterpret_cast<const pldm_msg*>(hdr);
103 size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) -
104 sizeof(eid) - sizeof(type);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600105 try
106 {
Tom Josephfb3bc062021-08-17 07:48:11 -0700107 if (hdrFields.pldm_type != PLDM_FWUP)
108 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500109 response = invoker.handle(hdrFields.pldm_type,
110 hdrFields.command, request,
111 requestLen);
Tom Josephfb3bc062021-08-17 07:48:11 -0700112 }
113 else
114 {
115 response = fwManager->handleRequest(eid, hdrFields.command,
116 request, requestLen);
117 }
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600118 }
119 catch (const std::out_of_range& e)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500120 {
121 uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
122 response.resize(sizeof(pldm_msg_hdr));
123 auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data());
124 pldm_header_info header{};
125 header.msg_type = PLDM_RESPONSE;
126 header.instance = hdrFields.instance;
127 header.pldm_type = hdrFields.pldm_type;
128 header.command = hdrFields.command;
George Liub7095ff2021-06-14 16:01:57 +0800129 if (PLDM_SUCCESS != pack_pldm_header(&header, responseHdr))
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500130 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600131 error("Failed adding response header");
Tom Joseph74f27c72021-05-16 07:58:53 -0700132 return std::nullopt;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500133 }
134 response.insert(response.end(), completion_code);
135 }
Tom Joseph74f27c72021-05-16 07:58:53 -0700136 return response;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500137 }
Tom Joseph74f27c72021-05-16 07:58:53 -0700138 else if (PLDM_RESPONSE == hdrFields.msg_type)
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600139 {
Tom Joseph74f27c72021-05-16 07:58:53 -0700140 auto response = reinterpret_cast<const pldm_msg*>(hdr);
141 size_t responseLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) -
142 sizeof(eid) - sizeof(type);
143 handler.handleResponse(eid, hdrFields.instance, hdrFields.pldm_type,
144 hdrFields.command, response, responseLen);
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600145 }
Tom Joseph74f27c72021-05-16 07:58:53 -0700146 return std::nullopt;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500147}
148
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530149void optionUsage(void)
150{
Riya Dixit797f3382023-08-22 22:27:51 -0500151 info("Usage: pldmd [options]");
152 info("Options:");
153 info(" [--verbose] - would enable verbosity");
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530154}
155
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500156int main(int argc, char** argv)
157{
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530158 bool verbose = false;
Manojkiran Edaf25145f2022-09-10 01:43:39 -0500159 static struct option long_options[] = {{"verbose", no_argument, 0, 'v'},
160 {0, 0, 0, 0}};
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530161
Manojkiran Edaf25145f2022-09-10 01:43:39 -0500162 auto argflag = getopt_long(argc, argv, "v", long_options, nullptr);
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530163 switch (argflag)
164 {
165 case 'v':
Manojkiran Edaf25145f2022-09-10 01:43:39 -0500166 verbose = true;
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530167 break;
Brad Bishopc80f3ef2021-10-07 16:34:13 -0400168 case -1:
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530169 break;
Brad Bishopc80f3ef2021-10-07 16:34:13 -0400170 default:
Manojkiran Edaf25145f2022-09-10 01:43:39 -0500171 optionUsage();
Brad Bishopc80f3ef2021-10-07 16:34:13 -0400172 exit(EXIT_FAILURE);
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530173 }
174
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500175 /* Create local socket. */
176 int returnCode = 0;
177 int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
178 if (-1 == sockfd)
179 {
180 returnCode = -errno;
Riya Dixit49cfb132023-03-02 04:26:53 -0600181 error("Failed to create the socket, RC= {RC}", "RC", returnCode);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500182 exit(EXIT_FAILURE);
183 }
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530184 socklen_t optlen;
185 int currentSendbuffSize;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500186
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530187 // Get Current send buffer size
188 optlen = sizeof(currentSendbuffSize);
189
190 int res = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &currentSendbuffSize,
191 &optlen);
192 if (res == -1)
193 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600194 error("Error in obtaining the default send buffer size, Error : {ERR}",
195 "ERR", strerror(errno));
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530196 }
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500197 auto event = Event::get_default();
Tom Joseph02b4ee42021-05-02 22:44:36 -0700198 auto& bus = pldm::utils::DBusHandler::getBus();
Patrick Williams84b790c2022-07-22 19:26:56 -0500199 sdbusplus::server::manager_t objManager(bus,
200 "/xyz/openbmc_project/software");
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +0930201
202 InstanceIdDb instanceIdDb;
203 dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm",
204 instanceIdDb);
George Liu682ee182020-12-25 15:24:33 +0800205 sdbusplus::server::manager::manager inventoryManager(
206 bus, "/xyz/openbmc_project/inventory");
Pavithra Barithaya319ebb32021-05-06 06:09:11 -0500207
Tom Joseph02b4ee42021-05-02 22:44:36 -0700208 Invoker invoker{};
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530209 requester::Handler<requester::Request> reqHandler(
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930210 sockfd, event, instanceIdDb, currentSendbuffSize, verbose);
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530211
Tom Joseph02b4ee42021-05-02 22:44:36 -0700212#ifdef LIBPLDMRESPONDER
213 using namespace pldm::state_sensor;
Tom Joseph20aa3e02021-08-17 04:44:19 -0700214 dbus_api::Host dbusImplHost(bus, "/xyz/openbmc_project/pldm");
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500215 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo(
216 pldm_pdr_init(), pldm_pdr_destroy);
Andrew Jefferyacb20292023-06-30 11:47:44 +0930217 if (!pdrRepo)
218 {
219 throw std::runtime_error("Failed to instantiate PDR repository");
220 }
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500221 std::unique_ptr<pldm_entity_association_tree,
222 decltype(&pldm_entity_association_tree_destroy)>
223 entityTree(pldm_entity_association_tree_init(),
224 pldm_entity_association_tree_destroy);
Andrew Jefferyabcd6ca2023-06-30 13:53:35 +0930225 if (!entityTree)
226 {
227 throw std::runtime_error(
228 "Failed to instantiate general PDR entity association tree");
229 }
Sampa Misrac073a202021-05-08 10:56:05 -0500230 std::unique_ptr<pldm_entity_association_tree,
231 decltype(&pldm_entity_association_tree_destroy)>
232 bmcEntityTree(pldm_entity_association_tree_init(),
233 pldm_entity_association_tree_destroy);
Andrew Jefferyabcd6ca2023-06-30 13:53:35 +0930234 if (!bmcEntityTree)
235 {
236 throw std::runtime_error(
237 "Failed to instantiate BMC PDR entity association tree");
238 }
Pavithra Barithaya319ebb32021-05-06 06:09:11 -0500239 std::shared_ptr<HostPDRHandler> hostPDRHandler;
Sampa Misrac0c60542020-07-01 02:34:25 -0500240 std::unique_ptr<pldm::host_effecters::HostEffecterParser>
241 hostEffecterParser;
George Liucae18662020-05-15 09:32:57 +0800242 std::unique_ptr<DbusToPLDMEvent> dbusToPLDMEventHandler;
Brad Bishopfc0d14d2021-10-12 19:34:20 -0400243 DBusHandler dbusHandler;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500244 auto hostEID = pldm::utils::readHostEID();
245 if (hostEID)
246 {
Pavithra Barithaya319ebb32021-05-06 06:09:11 -0500247 hostPDRHandler = std::make_shared<HostPDRHandler>(
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600248 sockfd, hostEID, event, pdrRepo.get(), EVENTS_JSONS_DIR,
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930249 entityTree.get(), bmcEntityTree.get(), instanceIdDb, &reqHandler);
Pavithra Barithaya319ebb32021-05-06 06:09:11 -0500250 // HostFirmware interface needs access to hostPDR to know if host
251 // is running
252 dbusImplHost.setHostPdrObj(hostPDRHandler);
253
Sampa Misrac0c60542020-07-01 02:34:25 -0500254 hostEffecterParser =
255 std::make_unique<pldm::host_effecters::HostEffecterParser>(
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930256 &instanceIdDb, sockfd, pdrRepo.get(), &dbusHandler,
Tom Josephe5268cd2021-09-07 13:04:03 +0530257 HOST_JSONS_DIR, &reqHandler);
Sampa Misrac0c79482021-06-02 08:01:54 -0500258 dbusToPLDMEventHandler = std::make_unique<DbusToPLDMEvent>(
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930259 sockfd, hostEID, instanceIdDb, &reqHandler);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500260 }
Sampa Misraaea5dde2020-08-31 08:33:47 -0500261 std::unique_ptr<oem_platform::Handler> oemPlatformHandler{};
Sagar Srinivas11ce8d22022-07-28 11:32:34 -0500262 std::unique_ptr<oem_bios::Handler> oemBiosHandler{};
Sampa Misraaea5dde2020-08-31 08:33:47 -0500263
264#ifdef OEM_IBM
265 std::unique_ptr<pldm::responder::CodeUpdate> codeUpdate =
Brad Bishopfc0d14d2021-10-12 19:34:20 -0400266 std::make_unique<pldm::responder::CodeUpdate>(&dbusHandler);
Varsha Kaverappa3ca29df2020-09-27 12:39:22 -0500267 codeUpdate->clearDirPath(LID_STAGING_DIR);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500268 oemPlatformHandler = std::make_unique<oem_ibm_platform::Handler>(
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930269 &dbusHandler, codeUpdate.get(), sockfd, hostEID, instanceIdDb, event,
Brad Bishopfc0d14d2021-10-12 19:34:20 -0400270 &reqHandler);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500271 codeUpdate->setOemPlatformHandler(oemPlatformHandler.get());
Sampa Misrac0c79482021-06-02 08:01:54 -0500272 invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>(
273 oemPlatformHandler.get(), sockfd,
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930274 hostEID, &instanceIdDb, &reqHandler));
Sagar Srinivas11ce8d22022-07-28 11:32:34 -0500275 oemBiosHandler = std::make_unique<oem::ibm::bios::Handler>(&dbusHandler);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500276#endif
Sagar Srinivas11ce8d22022-07-28 11:32:34 -0500277
278 auto biosHandler = std::make_unique<bios::Handler>(
279 sockfd, hostEID, &instanceIdDb, &reqHandler, oemBiosHandler.get());
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530280 auto fruHandler = std::make_unique<fru::Handler>(
Manojkiran Eda03b01ca2021-06-29 08:55:09 +0530281 FRU_JSONS_DIR, FRU_MASTER_JSON, pdrRepo.get(), entityTree.get(),
282 bmcEntityTree.get());
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530283 // FRU table is built lazily when a FRU command or Get PDR command is
284 // handled. To enable building FRU table, the FRU handler is passed to the
285 // Platform handler.
Sampa Misraaea5dde2020-08-31 08:33:47 -0500286 auto platformHandler = std::make_unique<platform::Handler>(
Brad Bishopfc0d14d2021-10-12 19:34:20 -0400287 &dbusHandler, PDR_JSONS_DIR, pdrRepo.get(), hostPDRHandler.get(),
Sampa Misraaea5dde2020-08-31 08:33:47 -0500288 dbusToPLDMEventHandler.get(), fruHandler.get(),
Sampa Misra5fb37d52021-03-06 07:26:00 -0600289 oemPlatformHandler.get(), event, true);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500290#ifdef OEM_IBM
Sampa Misraaea5dde2020-08-31 08:33:47 -0500291 pldm::responder::oem_ibm_platform::Handler* oemIbmPlatformHandler =
292 dynamic_cast<pldm::responder::oem_ibm_platform::Handler*>(
293 oemPlatformHandler.get());
294 oemIbmPlatformHandler->setPlatformHandler(platformHandler.get());
Sagar Srinivas11ce8d22022-07-28 11:32:34 -0500295
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500296#endif
297
Sagar Srinivas11ce8d22022-07-28 11:32:34 -0500298 invoker.registerHandler(PLDM_BIOS, std::move(biosHandler));
Sampa Misraaea5dde2020-08-31 08:33:47 -0500299 invoker.registerHandler(PLDM_PLATFORM, std::move(platformHandler));
Sagar Srinivas79669c92021-04-28 15:43:30 -0500300 invoker.registerHandler(
301 PLDM_BASE,
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930302 std::make_unique<base::Handler>(hostEID, instanceIdDb, event,
Sagar Srinivas79669c92021-04-28 15:43:30 -0500303 oemPlatformHandler.get(), &reqHandler));
Sampa Misraaea5dde2020-08-31 08:33:47 -0500304 invoker.registerHandler(PLDM_FRU, std::move(fruHandler));
Tom Joseph02b4ee42021-05-02 22:44:36 -0700305 dbus_api::Pdr dbusImplPdr(bus, "/xyz/openbmc_project/pldm", pdrRepo.get());
306 sdbusplus::xyz::openbmc_project::PLDM::server::Event dbusImplEvent(
307 bus, "/xyz/openbmc_project/pldm");
Tom Joseph20aa3e02021-08-17 04:44:19 -0700308
Tom Joseph02b4ee42021-05-02 22:44:36 -0700309#endif
Sampa Misraaea5dde2020-08-31 08:33:47 -0500310
George Liu83409572019-12-24 18:42:54 +0800311 pldm::utils::CustomFD socketFd(sockfd);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500312
313 struct sockaddr_un addr
George Liu6492f522020-06-16 10:34:05 +0800314 {};
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500315 addr.sun_family = AF_UNIX;
316 const char path[] = "\0mctp-mux";
317 memcpy(addr.sun_path, path, sizeof(path) - 1);
318 int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr),
319 sizeof(path) + sizeof(addr.sun_family) - 1);
320 if (-1 == result)
321 {
322 returnCode = -errno;
Riya Dixit49cfb132023-03-02 04:26:53 -0600323 error("Failed to connect to the socket, RC= {RC}", "RC", returnCode);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500324 exit(EXIT_FAILURE);
325 }
326
327 result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM));
328 if (-1 == result)
329 {
330 returnCode = -errno;
Riya Dixit49cfb132023-03-02 04:26:53 -0600331 error("Failed to send message type as pldm to mctp, RC= {RC}", "RC",
332 returnCode);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500333 exit(EXIT_FAILURE);
334 }
335
Tom Josephfb3bc062021-08-17 07:48:11 -0700336 std::unique_ptr<fw_update::Manager> fwManager =
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930337 std::make_unique<fw_update::Manager>(event, reqHandler, instanceIdDb);
Tom Josephfb3bc062021-08-17 07:48:11 -0700338 std::unique_ptr<MctpDiscovery> mctpDiscoveryHandler =
339 std::make_unique<MctpDiscovery>(bus, fwManager.get());
340
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530341 auto callback = [verbose, &invoker, &reqHandler, currentSendbuffSize,
342 &fwManager](IO& io, int fd, uint32_t revents) mutable {
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600343 if (!(revents & EPOLLIN))
344 {
345 return;
346 }
347
348 // Outgoing message.
349 struct iovec iov[2]{};
350
351 // This structure contains the parameter information for the response
352 // message.
353 struct msghdr msg
George Liu6492f522020-06-16 10:34:05 +0800354 {};
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600355
356 int returnCode = 0;
357 ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500358 if (0 == peekedLength)
359 {
Deepak Kodihalli23c52042020-09-01 03:04:32 -0500360 // MCTP daemon has closed the socket this daemon is connected to.
361 // This may or may not be an error scenario, in either case the
362 // recovery mechanism for this daemon is to restart, and hence exit
363 // the event loop, that will cause this daemon to exit with a
364 // failure code.
365 io.get_event().exit(0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500366 }
367 else if (peekedLength <= -1)
368 {
369 returnCode = -errno;
Riya Dixit49cfb132023-03-02 04:26:53 -0600370 error("recv system call failed, RC= {RC}", "RC", returnCode);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500371 }
372 else
373 {
374 std::vector<uint8_t> requestMsg(peekedLength);
375 auto recvDataLength = recv(
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600376 fd, static_cast<void*>(requestMsg.data()), peekedLength, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500377 if (recvDataLength == peekedLength)
378 {
Manojkiran Edaef773052021-07-29 09:29:28 +0530379 FlightRecorder::GetInstance().saveRecord(requestMsg, false);
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530380 if (verbose)
381 {
Tom Josephe5268cd2021-09-07 13:04:03 +0530382 printBuffer(Rx, requestMsg);
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530383 }
Tom Josephe5268cd2021-09-07 13:04:03 +0530384
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500385 if (MCTP_MSG_TYPE_PLDM != requestMsg[1])
386 {
387 // Skip this message and continue.
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500388 }
389 else
390 {
391 // process message and send response
Tom Josephfb3bc062021-08-17 07:48:11 -0700392 auto response = processRxMsg(requestMsg, invoker,
393 reqHandler, fwManager.get());
Tom Joseph74f27c72021-05-16 07:58:53 -0700394 if (response.has_value())
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500395 {
Manojkiran Edaef773052021-07-29 09:29:28 +0530396 FlightRecorder::GetInstance().saveRecord(*response,
397 true);
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500398 if (verbose)
399 {
Tom Josephe5268cd2021-09-07 13:04:03 +0530400 printBuffer(Tx, *response);
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500401 }
402
Zahed Hossain09a96e02019-08-06 07:42:37 -0500403 iov[0].iov_base = &requestMsg[0];
Patrick Williams6da4f912023-05-10 07:50:53 -0500404 iov[0].iov_len = sizeof(requestMsg[0]) +
405 sizeof(requestMsg[1]);
Tom Joseph74f27c72021-05-16 07:58:53 -0700406 iov[1].iov_base = (*response).data();
407 iov[1].iov_len = (*response).size();
Zahed Hossain09a96e02019-08-06 07:42:37 -0500408
409 msg.msg_iov = iov;
410 msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530411 if (currentSendbuffSize >= 0 &&
412 (size_t)currentSendbuffSize < (*response).size())
413 {
414 int oldBuffSize = currentSendbuffSize;
415 currentSendbuffSize = (*response).size();
416 int res = setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
417 &currentSendbuffSize,
418 sizeof(currentSendbuffSize));
419 if (res == -1)
420 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600421 error(
422 "Responder : Failed to set the new send buffer size [bytes] : {CURR_SND_BUF_SIZE}",
423 "CURR_SND_BUF_SIZE", currentSendbuffSize);
424 error(
425 "from current size [bytes] : {OLD_BUF_SIZE}, Error : {ERR}",
426 "OLD_BUF_SIZE", oldBuffSize, "ERR",
427 strerror(errno));
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530428 return;
429 }
430 }
Zahed Hossain09a96e02019-08-06 07:42:37 -0500431
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600432 int result = sendmsg(fd, &msg, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500433 if (-1 == result)
434 {
435 returnCode = -errno;
Riya Dixit49cfb132023-03-02 04:26:53 -0600436 error("sendto system call failed, RC= {RC}", "RC",
437 returnCode);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500438 }
439 }
440 }
441 }
442 else
443 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600444 error(
445 "Failure to read peeked length packet. peekedLength = {PEEK_LEN}, recvDataLength= {RECV_LEN}",
446 "PEEK_LEN", peekedLength, "RECV_LEN", recvDataLength);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500447 }
448 }
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600449 };
450
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600451 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
452 bus.request_name("xyz.openbmc_project.PLDM");
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600453 IO io(event, socketFd(), EPOLLIN, std::move(callback));
Manojkiran Eda88bc7182021-09-08 18:37:30 +0530454#ifdef LIBPLDMRESPONDER
455 if (hostPDRHandler)
456 {
457 hostPDRHandler->setHostFirmwareCondition();
458 }
459#endif
Manojkiran Edaef773052021-07-29 09:29:28 +0530460 stdplus::signal::block(SIGUSR1);
461 sdeventplus::source::Signal sigUsr1(
462 event, SIGUSR1, std::bind_front(&interruptFlightRecorderCallBack));
Brad Bishop3a22b972021-10-07 17:01:58 -0400463 returnCode = event.loop();
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500464
Brad Bishop3a22b972021-10-07 17:01:58 -0400465 if (shutdown(sockfd, SHUT_RDWR))
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500466 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600467 error("Failed to shutdown the socket");
Brad Bishop3a22b972021-10-07 17:01:58 -0400468 }
469 if (returnCode)
470 {
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500471 exit(EXIT_FAILURE);
472 }
Brad Bishop3a22b972021-10-07 17:01:58 -0400473
474 exit(EXIT_SUCCESS);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500475}