blob: dc0a2bb817ff440747b33a7f300bf82ad68925bc [file] [log] [blame]
George Liu6492f522020-06-16 10:34:05 +08001#include "libpldm/base.h"
2#include "libpldm/bios.h"
3#include "libpldm/pdr.h"
4#include "libpldm/platform.h"
5
Manojkiran Edaef773052021-07-29 09:29:28 +05306#include "common/flight_recorder.hpp"
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05007#include "common/utils.hpp"
Deepak Kodihalli4de4d002019-11-11 02:41:43 -06008#include "dbus_impl_requester.hpp"
Tom Josephfb3bc062021-08-17 07:48:11 -07009#include "fw-update/manager.hpp"
Deepak Kodihallibc669f12019-11-28 08:52:07 -060010#include "invoker.hpp"
Tom Joseph74f27c72021-05-16 07:58:53 -070011#include "requester/handler.hpp"
Tom Josephfb3bc062021-08-17 07:48:11 -070012#include "requester/mctp_endpoint_discovery.hpp"
Tom Joseph74f27c72021-05-16 07:58:53 -070013#include "requester/request.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050014
15#include <err.h>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053016#include <getopt.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
George Liu6492f522020-06-16 10:34:05 +080024#include <sdeventplus/event.hpp>
25#include <sdeventplus/source/io.hpp>
Manojkiran Edaef773052021-07-29 09:29:28 +053026#include <sdeventplus/source/signal.hpp>
27#include <stdplus/signal.hpp>
George Liu6492f522020-06-16 10:34:05 +080028
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050029#include <cstdio>
30#include <cstring>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050031#include <fstream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050032#include <iomanip>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053033#include <iostream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050034#include <iterator>
Deepak Kodihallic682fe22020-03-04 00:42:54 -060035#include <memory>
Tom Joseph74f27c72021-05-16 07:58:53 -070036#include <optional>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050037#include <sstream>
Deepak Kodihallibc669f12019-11-28 08:52:07 -060038#include <stdexcept>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053039#include <string>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050040#include <vector>
41
Tom Joseph02b4ee42021-05-02 22:44:36 -070042#ifdef LIBPLDMRESPONDER
43#include "dbus_impl_pdr.hpp"
44#include "host-bmc/dbus_to_event_handler.hpp"
45#include "host-bmc/dbus_to_host_effecters.hpp"
Tom Joseph20aa3e02021-08-17 04:44:19 -070046#include "host-bmc/host_condition.hpp"
Tom Joseph02b4ee42021-05-02 22:44:36 -070047#include "host-bmc/host_pdr_handler.hpp"
48#include "libpldmresponder/base.hpp"
49#include "libpldmresponder/bios.hpp"
50#include "libpldmresponder/fru.hpp"
51#include "libpldmresponder/oem_handler.hpp"
52#include "libpldmresponder/platform.hpp"
53#include "xyz/openbmc_project/PLDM/Event/server.hpp"
54#endif
55
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050056#ifdef OEM_IBM
57#include "libpldmresponder/file_io.hpp"
Sampa Misraaea5dde2020-08-31 08:33:47 -050058#include "libpldmresponder/oem_ibm_handler.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050059#endif
60
61constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
62
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050063using namespace pldm;
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060064using namespace sdeventplus;
65using namespace sdeventplus::source;
Tom Joseph02b4ee42021-05-02 22:44:36 -070066using namespace pldm::responder;
67using namespace pldm::utils;
Manojkiran Edaef773052021-07-29 09:29:28 +053068using sdeventplus::source::Signal;
69using namespace pldm::flightrecorder;
70
71void interruptFlightRecorderCallBack(Signal& /*signal*/,
72 const struct signalfd_siginfo*)
73{
74 std::cerr << "\nReceived SIGUR1(10) Signal interrupt\n";
75
76 // obtain the flight recorder instance and dump the recorder
77 FlightRecorder::GetInstance().playRecorder();
78}
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050079
Tom Joseph74f27c72021-05-16 07:58:53 -070080static std::optional<Response>
81 processRxMsg(const std::vector<uint8_t>& requestMsg, Invoker& invoker,
Tom Josephfb3bc062021-08-17 07:48:11 -070082 requester::Handler<requester::Request>& handler,
83 fw_update::Manager* fwManager)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050084{
Manojkiran Eda31a78442021-09-12 15:18:25 +053085 using type = uint8_t;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050086 uint8_t eid = requestMsg[0];
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050087 pldm_header_info hdrFields{};
88 auto hdr = reinterpret_cast<const pldm_msg_hdr*>(
89 requestMsg.data() + sizeof(eid) + sizeof(type));
90 if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields))
91 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060092 std::cerr << "Empty PLDM request header \n";
Tom Joseph74f27c72021-05-16 07:58:53 -070093 return std::nullopt;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050094 }
George Liub7095ff2021-06-14 16:01:57 +080095
96 if (PLDM_RESPONSE != hdrFields.msg_type)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050097 {
Tom Joseph74f27c72021-05-16 07:58:53 -070098 Response response;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050099 auto request = reinterpret_cast<const pldm_msg*>(hdr);
100 size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) -
101 sizeof(eid) - sizeof(type);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600102 try
103 {
Tom Josephfb3bc062021-08-17 07:48:11 -0700104 if (hdrFields.pldm_type != PLDM_FWUP)
105 {
106 response =
107 invoker.handle(hdrFields.pldm_type, hdrFields.command,
108 request, requestLen);
109 }
110 else
111 {
112 response = fwManager->handleRequest(eid, hdrFields.command,
113 request, requestLen);
114 }
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600115 }
116 catch (const std::out_of_range& e)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500117 {
118 uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
119 response.resize(sizeof(pldm_msg_hdr));
120 auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data());
121 pldm_header_info header{};
122 header.msg_type = PLDM_RESPONSE;
123 header.instance = hdrFields.instance;
124 header.pldm_type = hdrFields.pldm_type;
125 header.command = hdrFields.command;
George Liub7095ff2021-06-14 16:01:57 +0800126 if (PLDM_SUCCESS != pack_pldm_header(&header, responseHdr))
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500127 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600128 std::cerr << "Failed adding response header \n";
Tom Joseph74f27c72021-05-16 07:58:53 -0700129 return std::nullopt;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500130 }
131 response.insert(response.end(), completion_code);
132 }
Tom Joseph74f27c72021-05-16 07:58:53 -0700133 return response;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500134 }
Tom Joseph74f27c72021-05-16 07:58:53 -0700135 else if (PLDM_RESPONSE == hdrFields.msg_type)
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600136 {
Tom Joseph74f27c72021-05-16 07:58:53 -0700137 auto response = reinterpret_cast<const pldm_msg*>(hdr);
138 size_t responseLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) -
139 sizeof(eid) - sizeof(type);
140 handler.handleResponse(eid, hdrFields.instance, hdrFields.pldm_type,
141 hdrFields.command, response, responseLen);
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600142 }
Tom Joseph74f27c72021-05-16 07:58:53 -0700143 return std::nullopt;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500144}
145
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530146void optionUsage(void)
147{
148 std::cerr << "Usage: pldmd [options]\n";
149 std::cerr << "Options:\n";
Manojkiran Edaf25145f2022-09-10 01:43:39 -0500150 std::cerr << " [--verbose] - would enable verbosity\n";
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530151}
152
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500153int main(int argc, char** argv)
154{
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530155 bool verbose = false;
Manojkiran Edaf25145f2022-09-10 01:43:39 -0500156 static struct option long_options[] = {{"verbose", no_argument, 0, 'v'},
157 {0, 0, 0, 0}};
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530158
Manojkiran Edaf25145f2022-09-10 01:43:39 -0500159 auto argflag = getopt_long(argc, argv, "v", long_options, nullptr);
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530160 switch (argflag)
161 {
162 case 'v':
Manojkiran Edaf25145f2022-09-10 01:43:39 -0500163 verbose = true;
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530164 break;
Brad Bishopc80f3ef2021-10-07 16:34:13 -0400165 case -1:
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530166 break;
Brad Bishopc80f3ef2021-10-07 16:34:13 -0400167 default:
Manojkiran Edaf25145f2022-09-10 01:43:39 -0500168 optionUsage();
Brad Bishopc80f3ef2021-10-07 16:34:13 -0400169 exit(EXIT_FAILURE);
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530170 }
171
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500172 /* Create local socket. */
173 int returnCode = 0;
174 int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
175 if (-1 == sockfd)
176 {
177 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600178 std::cerr << "Failed to create the socket, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500179 exit(EXIT_FAILURE);
180 }
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530181 socklen_t optlen;
182 int currentSendbuffSize;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500183
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530184 // Get Current send buffer size
185 optlen = sizeof(currentSendbuffSize);
186
187 int res = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &currentSendbuffSize,
188 &optlen);
189 if (res == -1)
190 {
191 std::cerr << "Error in obtaining the default send buffer size, Error : "
192 << strerror(errno) << std::endl;
193 }
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500194 auto event = Event::get_default();
Tom Joseph02b4ee42021-05-02 22:44:36 -0700195 auto& bus = pldm::utils::DBusHandler::getBus();
Patrick Williams84b790c2022-07-22 19:26:56 -0500196 sdbusplus::server::manager_t objManager(bus,
197 "/xyz/openbmc_project/software");
Tom Joseph02b4ee42021-05-02 22:44:36 -0700198 dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm");
Pavithra Barithaya319ebb32021-05-06 06:09:11 -0500199
Tom Joseph02b4ee42021-05-02 22:44:36 -0700200 Invoker invoker{};
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530201 requester::Handler<requester::Request> reqHandler(
202 sockfd, event, dbusImplReq, currentSendbuffSize, verbose);
203
Tom Joseph02b4ee42021-05-02 22:44:36 -0700204#ifdef LIBPLDMRESPONDER
205 using namespace pldm::state_sensor;
Tom Joseph20aa3e02021-08-17 04:44:19 -0700206 dbus_api::Host dbusImplHost(bus, "/xyz/openbmc_project/pldm");
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500207 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo(
208 pldm_pdr_init(), pldm_pdr_destroy);
209 std::unique_ptr<pldm_entity_association_tree,
210 decltype(&pldm_entity_association_tree_destroy)>
211 entityTree(pldm_entity_association_tree_init(),
212 pldm_entity_association_tree_destroy);
Sampa Misrac073a202021-05-08 10:56:05 -0500213 std::unique_ptr<pldm_entity_association_tree,
214 decltype(&pldm_entity_association_tree_destroy)>
215 bmcEntityTree(pldm_entity_association_tree_init(),
216 pldm_entity_association_tree_destroy);
Pavithra Barithaya319ebb32021-05-06 06:09:11 -0500217 std::shared_ptr<HostPDRHandler> hostPDRHandler;
Sampa Misrac0c60542020-07-01 02:34:25 -0500218 std::unique_ptr<pldm::host_effecters::HostEffecterParser>
219 hostEffecterParser;
George Liucae18662020-05-15 09:32:57 +0800220 std::unique_ptr<DbusToPLDMEvent> dbusToPLDMEventHandler;
Brad Bishopfc0d14d2021-10-12 19:34:20 -0400221 DBusHandler dbusHandler;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500222 auto hostEID = pldm::utils::readHostEID();
223 if (hostEID)
224 {
Pavithra Barithaya319ebb32021-05-06 06:09:11 -0500225 hostPDRHandler = std::make_shared<HostPDRHandler>(
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600226 sockfd, hostEID, event, pdrRepo.get(), EVENTS_JSONS_DIR,
Tom Josephe5268cd2021-09-07 13:04:03 +0530227 entityTree.get(), bmcEntityTree.get(), dbusImplReq, &reqHandler);
Pavithra Barithaya319ebb32021-05-06 06:09:11 -0500228 // HostFirmware interface needs access to hostPDR to know if host
229 // is running
230 dbusImplHost.setHostPdrObj(hostPDRHandler);
231
Sampa Misrac0c60542020-07-01 02:34:25 -0500232 hostEffecterParser =
233 std::make_unique<pldm::host_effecters::HostEffecterParser>(
Brad Bishopfc0d14d2021-10-12 19:34:20 -0400234 &dbusImplReq, sockfd, pdrRepo.get(), &dbusHandler,
Tom Josephe5268cd2021-09-07 13:04:03 +0530235 HOST_JSONS_DIR, &reqHandler);
Sampa Misrac0c79482021-06-02 08:01:54 -0500236 dbusToPLDMEventHandler = std::make_unique<DbusToPLDMEvent>(
237 sockfd, hostEID, dbusImplReq, &reqHandler);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500238 }
Sampa Misraaea5dde2020-08-31 08:33:47 -0500239 std::unique_ptr<oem_platform::Handler> oemPlatformHandler{};
240
241#ifdef OEM_IBM
242 std::unique_ptr<pldm::responder::CodeUpdate> codeUpdate =
Brad Bishopfc0d14d2021-10-12 19:34:20 -0400243 std::make_unique<pldm::responder::CodeUpdate>(&dbusHandler);
Varsha Kaverappa3ca29df2020-09-27 12:39:22 -0500244 codeUpdate->clearDirPath(LID_STAGING_DIR);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500245 oemPlatformHandler = std::make_unique<oem_ibm_platform::Handler>(
Brad Bishopfc0d14d2021-10-12 19:34:20 -0400246 &dbusHandler, codeUpdate.get(), sockfd, hostEID, dbusImplReq, event,
247 &reqHandler);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500248 codeUpdate->setOemPlatformHandler(oemPlatformHandler.get());
Sampa Misrac0c79482021-06-02 08:01:54 -0500249 invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>(
250 oemPlatformHandler.get(), sockfd,
251 hostEID, &dbusImplReq, &reqHandler));
Sampa Misraaea5dde2020-08-31 08:33:47 -0500252#endif
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500253 invoker.registerHandler(
Sampa Misrac0c79482021-06-02 08:01:54 -0500254 PLDM_BIOS, std::make_unique<bios::Handler>(sockfd, hostEID,
255 &dbusImplReq, &reqHandler));
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530256 auto fruHandler = std::make_unique<fru::Handler>(
Manojkiran Eda03b01ca2021-06-29 08:55:09 +0530257 FRU_JSONS_DIR, FRU_MASTER_JSON, pdrRepo.get(), entityTree.get(),
258 bmcEntityTree.get());
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530259 // FRU table is built lazily when a FRU command or Get PDR command is
260 // handled. To enable building FRU table, the FRU handler is passed to the
261 // Platform handler.
Sampa Misraaea5dde2020-08-31 08:33:47 -0500262 auto platformHandler = std::make_unique<platform::Handler>(
Brad Bishopfc0d14d2021-10-12 19:34:20 -0400263 &dbusHandler, PDR_JSONS_DIR, pdrRepo.get(), hostPDRHandler.get(),
Sampa Misraaea5dde2020-08-31 08:33:47 -0500264 dbusToPLDMEventHandler.get(), fruHandler.get(),
Sampa Misra5fb37d52021-03-06 07:26:00 -0600265 oemPlatformHandler.get(), event, true);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500266#ifdef OEM_IBM
Sampa Misraaea5dde2020-08-31 08:33:47 -0500267 pldm::responder::oem_ibm_platform::Handler* oemIbmPlatformHandler =
268 dynamic_cast<pldm::responder::oem_ibm_platform::Handler*>(
269 oemPlatformHandler.get());
270 oemIbmPlatformHandler->setPlatformHandler(platformHandler.get());
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500271#endif
272
Sampa Misraaea5dde2020-08-31 08:33:47 -0500273 invoker.registerHandler(PLDM_PLATFORM, std::move(platformHandler));
Sagar Srinivas79669c92021-04-28 15:43:30 -0500274 invoker.registerHandler(
275 PLDM_BASE,
276 std::make_unique<base::Handler>(hostEID, dbusImplReq, event,
277 oemPlatformHandler.get(), &reqHandler));
Sampa Misraaea5dde2020-08-31 08:33:47 -0500278 invoker.registerHandler(PLDM_FRU, std::move(fruHandler));
Tom Joseph02b4ee42021-05-02 22:44:36 -0700279 dbus_api::Pdr dbusImplPdr(bus, "/xyz/openbmc_project/pldm", pdrRepo.get());
280 sdbusplus::xyz::openbmc_project::PLDM::server::Event dbusImplEvent(
281 bus, "/xyz/openbmc_project/pldm");
Tom Joseph20aa3e02021-08-17 04:44:19 -0700282
Tom Joseph02b4ee42021-05-02 22:44:36 -0700283#endif
Sampa Misraaea5dde2020-08-31 08:33:47 -0500284
George Liu83409572019-12-24 18:42:54 +0800285 pldm::utils::CustomFD socketFd(sockfd);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500286
287 struct sockaddr_un addr
George Liu6492f522020-06-16 10:34:05 +0800288 {};
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500289 addr.sun_family = AF_UNIX;
290 const char path[] = "\0mctp-mux";
291 memcpy(addr.sun_path, path, sizeof(path) - 1);
292 int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr),
293 sizeof(path) + sizeof(addr.sun_family) - 1);
294 if (-1 == result)
295 {
296 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600297 std::cerr << "Failed to connect to the socket, RC= " << returnCode
298 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500299 exit(EXIT_FAILURE);
300 }
301
302 result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM));
303 if (-1 == result)
304 {
305 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600306 std::cerr << "Failed to send message type as pldm to mctp, RC= "
307 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500308 exit(EXIT_FAILURE);
309 }
310
Tom Josephfb3bc062021-08-17 07:48:11 -0700311 std::unique_ptr<fw_update::Manager> fwManager =
312 std::make_unique<fw_update::Manager>(event, reqHandler, dbusImplReq);
313 std::unique_ptr<MctpDiscovery> mctpDiscoveryHandler =
314 std::make_unique<MctpDiscovery>(bus, fwManager.get());
315
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530316 auto callback = [verbose, &invoker, &reqHandler, currentSendbuffSize,
317 &fwManager](IO& io, int fd, uint32_t revents) mutable {
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600318 if (!(revents & EPOLLIN))
319 {
320 return;
321 }
322
323 // Outgoing message.
324 struct iovec iov[2]{};
325
326 // This structure contains the parameter information for the response
327 // message.
328 struct msghdr msg
George Liu6492f522020-06-16 10:34:05 +0800329 {};
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600330
331 int returnCode = 0;
332 ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500333 if (0 == peekedLength)
334 {
Deepak Kodihalli23c52042020-09-01 03:04:32 -0500335 // MCTP daemon has closed the socket this daemon is connected to.
336 // This may or may not be an error scenario, in either case the
337 // recovery mechanism for this daemon is to restart, and hence exit
338 // the event loop, that will cause this daemon to exit with a
339 // failure code.
340 io.get_event().exit(0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500341 }
342 else if (peekedLength <= -1)
343 {
344 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600345 std::cerr << "recv system call failed, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500346 }
347 else
348 {
349 std::vector<uint8_t> requestMsg(peekedLength);
350 auto recvDataLength = recv(
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600351 fd, static_cast<void*>(requestMsg.data()), peekedLength, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500352 if (recvDataLength == peekedLength)
353 {
Manojkiran Edaef773052021-07-29 09:29:28 +0530354 FlightRecorder::GetInstance().saveRecord(requestMsg, false);
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530355 if (verbose)
356 {
Tom Josephe5268cd2021-09-07 13:04:03 +0530357 printBuffer(Rx, requestMsg);
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530358 }
Tom Josephe5268cd2021-09-07 13:04:03 +0530359
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500360 if (MCTP_MSG_TYPE_PLDM != requestMsg[1])
361 {
362 // Skip this message and continue.
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500363 }
364 else
365 {
366 // process message and send response
Tom Josephfb3bc062021-08-17 07:48:11 -0700367 auto response = processRxMsg(requestMsg, invoker,
368 reqHandler, fwManager.get());
Tom Joseph74f27c72021-05-16 07:58:53 -0700369 if (response.has_value())
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500370 {
Manojkiran Edaef773052021-07-29 09:29:28 +0530371 FlightRecorder::GetInstance().saveRecord(*response,
372 true);
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500373 if (verbose)
374 {
Tom Josephe5268cd2021-09-07 13:04:03 +0530375 printBuffer(Tx, *response);
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500376 }
377
Zahed Hossain09a96e02019-08-06 07:42:37 -0500378 iov[0].iov_base = &requestMsg[0];
379 iov[0].iov_len =
380 sizeof(requestMsg[0]) + sizeof(requestMsg[1]);
Tom Joseph74f27c72021-05-16 07:58:53 -0700381 iov[1].iov_base = (*response).data();
382 iov[1].iov_len = (*response).size();
Zahed Hossain09a96e02019-08-06 07:42:37 -0500383
384 msg.msg_iov = iov;
385 msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530386 if (currentSendbuffSize >= 0 &&
387 (size_t)currentSendbuffSize < (*response).size())
388 {
389 int oldBuffSize = currentSendbuffSize;
390 currentSendbuffSize = (*response).size();
391 int res = setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
392 &currentSendbuffSize,
393 sizeof(currentSendbuffSize));
394 if (res == -1)
395 {
396 std::cerr
397 << "Responder : Failed to set the new send buffer size [bytes] : "
398 << currentSendbuffSize
399 << " from current size [bytes] : "
400 << oldBuffSize
401 << ", Error : " << strerror(errno)
402 << std::endl;
403 return;
404 }
405 }
Zahed Hossain09a96e02019-08-06 07:42:37 -0500406
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600407 int result = sendmsg(fd, &msg, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500408 if (-1 == result)
409 {
410 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600411 std::cerr << "sendto system call failed, RC= "
412 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500413 }
414 }
415 }
416 }
417 else
418 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600419 std::cerr
420 << "Failure to read peeked length packet. peekedLength= "
421 << peekedLength << " recvDataLength=" << recvDataLength
422 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500423 }
424 }
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600425 };
426
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600427 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
428 bus.request_name("xyz.openbmc_project.PLDM");
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600429 IO io(event, socketFd(), EPOLLIN, std::move(callback));
Manojkiran Eda88bc7182021-09-08 18:37:30 +0530430#ifdef LIBPLDMRESPONDER
431 if (hostPDRHandler)
432 {
433 hostPDRHandler->setHostFirmwareCondition();
434 }
435#endif
Manojkiran Edaef773052021-07-29 09:29:28 +0530436 stdplus::signal::block(SIGUSR1);
437 sdeventplus::source::Signal sigUsr1(
438 event, SIGUSR1, std::bind_front(&interruptFlightRecorderCallBack));
Brad Bishop3a22b972021-10-07 17:01:58 -0400439 returnCode = event.loop();
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500440
Brad Bishop3a22b972021-10-07 17:01:58 -0400441 if (shutdown(sockfd, SHUT_RDWR))
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500442 {
Brad Bishop3a22b972021-10-07 17:01:58 -0400443 std::perror("Failed to shutdown the socket");
444 }
445 if (returnCode)
446 {
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500447 exit(EXIT_FAILURE);
448 }
Brad Bishop3a22b972021-10-07 17:01:58 -0400449
450 exit(EXIT_SUCCESS);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500451}