blob: d48844f039550ad298700d7eb2df3b3121e0ec64 [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
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05006#include "common/utils.hpp"
Deepak Kodihalli4de4d002019-11-11 02:41:43 -06007#include "dbus_impl_requester.hpp"
Tom Josephfb3bc062021-08-17 07:48:11 -07008#include "fw-update/manager.hpp"
Deepak Kodihallibc669f12019-11-28 08:52:07 -06009#include "invoker.hpp"
Tom Joseph74f27c72021-05-16 07:58:53 -070010#include "requester/handler.hpp"
Tom Josephfb3bc062021-08-17 07:48:11 -070011#include "requester/mctp_endpoint_discovery.hpp"
Tom Joseph74f27c72021-05-16 07:58:53 -070012#include "requester/request.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050013
14#include <err.h>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053015#include <getopt.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050016#include <poll.h>
17#include <stdlib.h>
18#include <sys/socket.h>
19#include <sys/types.h>
20#include <sys/un.h>
21#include <unistd.h>
22
George Liu6492f522020-06-16 10:34:05 +080023#include <sdeventplus/event.hpp>
24#include <sdeventplus/source/io.hpp>
25
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050026#include <cstdio>
27#include <cstring>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050028#include <fstream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050029#include <iomanip>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053030#include <iostream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050031#include <iterator>
Deepak Kodihallic682fe22020-03-04 00:42:54 -060032#include <memory>
Tom Joseph74f27c72021-05-16 07:58:53 -070033#include <optional>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050034#include <sstream>
Deepak Kodihallibc669f12019-11-28 08:52:07 -060035#include <stdexcept>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053036#include <string>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050037#include <vector>
38
Tom Joseph02b4ee42021-05-02 22:44:36 -070039#ifdef LIBPLDMRESPONDER
40#include "dbus_impl_pdr.hpp"
41#include "host-bmc/dbus_to_event_handler.hpp"
42#include "host-bmc/dbus_to_host_effecters.hpp"
Tom Joseph20aa3e02021-08-17 04:44:19 -070043#include "host-bmc/host_condition.hpp"
Tom Joseph02b4ee42021-05-02 22:44:36 -070044#include "host-bmc/host_pdr_handler.hpp"
45#include "libpldmresponder/base.hpp"
46#include "libpldmresponder/bios.hpp"
47#include "libpldmresponder/fru.hpp"
48#include "libpldmresponder/oem_handler.hpp"
49#include "libpldmresponder/platform.hpp"
50#include "xyz/openbmc_project/PLDM/Event/server.hpp"
51#endif
52
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050053#ifdef OEM_IBM
54#include "libpldmresponder/file_io.hpp"
Sampa Misraaea5dde2020-08-31 08:33:47 -050055#include "libpldmresponder/oem_ibm_handler.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050056#endif
57
58constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
59
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050060using namespace pldm;
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060061using namespace sdeventplus;
62using namespace sdeventplus::source;
Tom Joseph02b4ee42021-05-02 22:44:36 -070063using namespace pldm::responder;
64using namespace pldm::utils;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050065
Tom Joseph74f27c72021-05-16 07:58:53 -070066static std::optional<Response>
67 processRxMsg(const std::vector<uint8_t>& requestMsg, Invoker& invoker,
Tom Josephfb3bc062021-08-17 07:48:11 -070068 requester::Handler<requester::Request>& handler,
69 fw_update::Manager* fwManager)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050070{
Manojkiran Eda31a78442021-09-12 15:18:25 +053071 using type = uint8_t;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050072 uint8_t eid = requestMsg[0];
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050073 pldm_header_info hdrFields{};
74 auto hdr = reinterpret_cast<const pldm_msg_hdr*>(
75 requestMsg.data() + sizeof(eid) + sizeof(type));
76 if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields))
77 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060078 std::cerr << "Empty PLDM request header \n";
Tom Joseph74f27c72021-05-16 07:58:53 -070079 return std::nullopt;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050080 }
George Liub7095ff2021-06-14 16:01:57 +080081
82 if (PLDM_RESPONSE != hdrFields.msg_type)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050083 {
Tom Joseph74f27c72021-05-16 07:58:53 -070084 Response response;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050085 auto request = reinterpret_cast<const pldm_msg*>(hdr);
86 size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) -
87 sizeof(eid) - sizeof(type);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060088 try
89 {
Tom Josephfb3bc062021-08-17 07:48:11 -070090 if (hdrFields.pldm_type != PLDM_FWUP)
91 {
92 response =
93 invoker.handle(hdrFields.pldm_type, hdrFields.command,
94 request, requestLen);
95 }
96 else
97 {
98 response = fwManager->handleRequest(eid, hdrFields.command,
99 request, requestLen);
100 }
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600101 }
102 catch (const std::out_of_range& e)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500103 {
104 uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
105 response.resize(sizeof(pldm_msg_hdr));
106 auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data());
107 pldm_header_info header{};
108 header.msg_type = PLDM_RESPONSE;
109 header.instance = hdrFields.instance;
110 header.pldm_type = hdrFields.pldm_type;
111 header.command = hdrFields.command;
George Liub7095ff2021-06-14 16:01:57 +0800112 if (PLDM_SUCCESS != pack_pldm_header(&header, responseHdr))
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500113 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600114 std::cerr << "Failed adding response header \n";
Tom Joseph74f27c72021-05-16 07:58:53 -0700115 return std::nullopt;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500116 }
117 response.insert(response.end(), completion_code);
118 }
Tom Joseph74f27c72021-05-16 07:58:53 -0700119 return response;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500120 }
Tom Joseph74f27c72021-05-16 07:58:53 -0700121 else if (PLDM_RESPONSE == hdrFields.msg_type)
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600122 {
Tom Joseph74f27c72021-05-16 07:58:53 -0700123 auto response = reinterpret_cast<const pldm_msg*>(hdr);
124 size_t responseLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) -
125 sizeof(eid) - sizeof(type);
126 handler.handleResponse(eid, hdrFields.instance, hdrFields.pldm_type,
127 hdrFields.command, response, responseLen);
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600128 }
Tom Joseph74f27c72021-05-16 07:58:53 -0700129 return std::nullopt;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500130}
131
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530132void optionUsage(void)
133{
134 std::cerr << "Usage: pldmd [options]\n";
135 std::cerr << "Options:\n";
136 std::cerr
137 << " --verbose=<0/1> 0 - Disable verbosity, 1 - Enable verbosity\n";
138 std::cerr << "Defaulted settings: --verbose=0 \n";
139}
140
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500141int main(int argc, char** argv)
142{
143
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530144 bool verbose = false;
145 static struct option long_options[] = {
146 {"verbose", required_argument, 0, 'v'}, {0, 0, 0, 0}};
147
148 auto argflag = getopt_long(argc, argv, "v:", long_options, nullptr);
149 switch (argflag)
150 {
151 case 'v':
152 switch (std::stoi(optarg))
153 {
154 case 0:
155 verbose = false;
156 break;
157 case 1:
158 verbose = true;
159 break;
160 default:
161 optionUsage();
Brad Bishopc80f3ef2021-10-07 16:34:13 -0400162 exit(EXIT_FAILURE);
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530163 }
164 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:
168 exit(EXIT_FAILURE);
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530169 }
170
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500171 /* Create local socket. */
172 int returnCode = 0;
173 int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
174 if (-1 == sockfd)
175 {
176 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600177 std::cerr << "Failed to create the socket, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500178 exit(EXIT_FAILURE);
179 }
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530180 socklen_t optlen;
181 int currentSendbuffSize;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500182
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530183 // Get Current send buffer size
184 optlen = sizeof(currentSendbuffSize);
185
186 int res = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &currentSendbuffSize,
187 &optlen);
188 if (res == -1)
189 {
190 std::cerr << "Error in obtaining the default send buffer size, Error : "
191 << strerror(errno) << std::endl;
192 }
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500193 auto event = Event::get_default();
Tom Joseph02b4ee42021-05-02 22:44:36 -0700194 auto& bus = pldm::utils::DBusHandler::getBus();
Tom Josephfb3bc062021-08-17 07:48:11 -0700195 sdbusplus::server::manager::manager objManager(
196 bus, "/xyz/openbmc_project/software");
Tom Joseph02b4ee42021-05-02 22:44:36 -0700197 dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm");
Pavithra Barithaya319ebb32021-05-06 06:09:11 -0500198
Tom Joseph02b4ee42021-05-02 22:44:36 -0700199 Invoker invoker{};
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530200 requester::Handler<requester::Request> reqHandler(
201 sockfd, event, dbusImplReq, currentSendbuffSize, verbose);
202
Tom Joseph02b4ee42021-05-02 22:44:36 -0700203#ifdef LIBPLDMRESPONDER
204 using namespace pldm::state_sensor;
Tom Joseph20aa3e02021-08-17 04:44:19 -0700205 dbus_api::Host dbusImplHost(bus, "/xyz/openbmc_project/pldm");
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500206 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo(
207 pldm_pdr_init(), pldm_pdr_destroy);
208 std::unique_ptr<pldm_entity_association_tree,
209 decltype(&pldm_entity_association_tree_destroy)>
210 entityTree(pldm_entity_association_tree_init(),
211 pldm_entity_association_tree_destroy);
Sampa Misrac073a202021-05-08 10:56:05 -0500212 std::unique_ptr<pldm_entity_association_tree,
213 decltype(&pldm_entity_association_tree_destroy)>
214 bmcEntityTree(pldm_entity_association_tree_init(),
215 pldm_entity_association_tree_destroy);
Pavithra Barithaya319ebb32021-05-06 06:09:11 -0500216 std::shared_ptr<HostPDRHandler> hostPDRHandler;
Sampa Misrac0c60542020-07-01 02:34:25 -0500217 std::unique_ptr<pldm::host_effecters::HostEffecterParser>
218 hostEffecterParser;
George Liucae18662020-05-15 09:32:57 +0800219 std::unique_ptr<DbusToPLDMEvent> dbusToPLDMEventHandler;
Brad Bishopfc0d14d2021-10-12 19:34:20 -0400220 DBusHandler dbusHandler;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500221 auto hostEID = pldm::utils::readHostEID();
222 if (hostEID)
223 {
Pavithra Barithaya319ebb32021-05-06 06:09:11 -0500224 hostPDRHandler = std::make_shared<HostPDRHandler>(
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600225 sockfd, hostEID, event, pdrRepo.get(), EVENTS_JSONS_DIR,
Tom Josephe5268cd2021-09-07 13:04:03 +0530226 entityTree.get(), bmcEntityTree.get(), dbusImplReq, &reqHandler);
Pavithra Barithaya319ebb32021-05-06 06:09:11 -0500227 // HostFirmware interface needs access to hostPDR to know if host
228 // is running
229 dbusImplHost.setHostPdrObj(hostPDRHandler);
230
Sampa Misrac0c60542020-07-01 02:34:25 -0500231 hostEffecterParser =
232 std::make_unique<pldm::host_effecters::HostEffecterParser>(
Brad Bishopfc0d14d2021-10-12 19:34:20 -0400233 &dbusImplReq, sockfd, pdrRepo.get(), &dbusHandler,
Tom Josephe5268cd2021-09-07 13:04:03 +0530234 HOST_JSONS_DIR, &reqHandler);
Sampa Misrac0c79482021-06-02 08:01:54 -0500235 dbusToPLDMEventHandler = std::make_unique<DbusToPLDMEvent>(
236 sockfd, hostEID, dbusImplReq, &reqHandler);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500237 }
Sampa Misraaea5dde2020-08-31 08:33:47 -0500238 std::unique_ptr<oem_platform::Handler> oemPlatformHandler{};
239
240#ifdef OEM_IBM
241 std::unique_ptr<pldm::responder::CodeUpdate> codeUpdate =
Brad Bishopfc0d14d2021-10-12 19:34:20 -0400242 std::make_unique<pldm::responder::CodeUpdate>(&dbusHandler);
Varsha Kaverappa3ca29df2020-09-27 12:39:22 -0500243 codeUpdate->clearDirPath(LID_STAGING_DIR);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500244 oemPlatformHandler = std::make_unique<oem_ibm_platform::Handler>(
Brad Bishopfc0d14d2021-10-12 19:34:20 -0400245 &dbusHandler, codeUpdate.get(), sockfd, hostEID, dbusImplReq, event,
246 &reqHandler);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500247 codeUpdate->setOemPlatformHandler(oemPlatformHandler.get());
Sampa Misrac0c79482021-06-02 08:01:54 -0500248 invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>(
249 oemPlatformHandler.get(), sockfd,
250 hostEID, &dbusImplReq, &reqHandler));
Sampa Misraaea5dde2020-08-31 08:33:47 -0500251#endif
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500252 invoker.registerHandler(
Sampa Misrac0c79482021-06-02 08:01:54 -0500253 PLDM_BIOS, std::make_unique<bios::Handler>(sockfd, hostEID,
254 &dbusImplReq, &reqHandler));
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530255 auto fruHandler = std::make_unique<fru::Handler>(
Manojkiran Eda03b01ca2021-06-29 08:55:09 +0530256 FRU_JSONS_DIR, FRU_MASTER_JSON, pdrRepo.get(), entityTree.get(),
257 bmcEntityTree.get());
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530258 // FRU table is built lazily when a FRU command or Get PDR command is
259 // handled. To enable building FRU table, the FRU handler is passed to the
260 // Platform handler.
Sampa Misraaea5dde2020-08-31 08:33:47 -0500261 auto platformHandler = std::make_unique<platform::Handler>(
Brad Bishopfc0d14d2021-10-12 19:34:20 -0400262 &dbusHandler, PDR_JSONS_DIR, pdrRepo.get(), hostPDRHandler.get(),
Sampa Misraaea5dde2020-08-31 08:33:47 -0500263 dbusToPLDMEventHandler.get(), fruHandler.get(),
Sampa Misra5fb37d52021-03-06 07:26:00 -0600264 oemPlatformHandler.get(), event, true);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500265#ifdef OEM_IBM
Sampa Misraaea5dde2020-08-31 08:33:47 -0500266 pldm::responder::oem_ibm_platform::Handler* oemIbmPlatformHandler =
267 dynamic_cast<pldm::responder::oem_ibm_platform::Handler*>(
268 oemPlatformHandler.get());
269 oemIbmPlatformHandler->setPlatformHandler(platformHandler.get());
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500270#endif
271
Sampa Misraaea5dde2020-08-31 08:33:47 -0500272 invoker.registerHandler(PLDM_PLATFORM, std::move(platformHandler));
Sagar Srinivas79669c92021-04-28 15:43:30 -0500273 invoker.registerHandler(
274 PLDM_BASE,
275 std::make_unique<base::Handler>(hostEID, dbusImplReq, event,
276 oemPlatformHandler.get(), &reqHandler));
Sampa Misraaea5dde2020-08-31 08:33:47 -0500277 invoker.registerHandler(PLDM_FRU, std::move(fruHandler));
Tom Joseph02b4ee42021-05-02 22:44:36 -0700278 dbus_api::Pdr dbusImplPdr(bus, "/xyz/openbmc_project/pldm", pdrRepo.get());
279 sdbusplus::xyz::openbmc_project::PLDM::server::Event dbusImplEvent(
280 bus, "/xyz/openbmc_project/pldm");
Tom Joseph20aa3e02021-08-17 04:44:19 -0700281
Tom Joseph02b4ee42021-05-02 22:44:36 -0700282#endif
Sampa Misraaea5dde2020-08-31 08:33:47 -0500283
George Liu83409572019-12-24 18:42:54 +0800284 pldm::utils::CustomFD socketFd(sockfd);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500285
286 struct sockaddr_un addr
George Liu6492f522020-06-16 10:34:05 +0800287 {};
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500288 addr.sun_family = AF_UNIX;
289 const char path[] = "\0mctp-mux";
290 memcpy(addr.sun_path, path, sizeof(path) - 1);
291 int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr),
292 sizeof(path) + sizeof(addr.sun_family) - 1);
293 if (-1 == result)
294 {
295 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600296 std::cerr << "Failed to connect to the socket, RC= " << returnCode
297 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500298 exit(EXIT_FAILURE);
299 }
300
301 result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM));
302 if (-1 == result)
303 {
304 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600305 std::cerr << "Failed to send message type as pldm to mctp, RC= "
306 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500307 exit(EXIT_FAILURE);
308 }
309
Tom Josephfb3bc062021-08-17 07:48:11 -0700310 std::unique_ptr<fw_update::Manager> fwManager =
311 std::make_unique<fw_update::Manager>(event, reqHandler, dbusImplReq);
312 std::unique_ptr<MctpDiscovery> mctpDiscoveryHandler =
313 std::make_unique<MctpDiscovery>(bus, fwManager.get());
314
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530315 auto callback = [verbose, &invoker, &reqHandler, currentSendbuffSize,
316 &fwManager](IO& io, int fd, uint32_t revents) mutable {
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600317 if (!(revents & EPOLLIN))
318 {
319 return;
320 }
321
322 // Outgoing message.
323 struct iovec iov[2]{};
324
325 // This structure contains the parameter information for the response
326 // message.
327 struct msghdr msg
George Liu6492f522020-06-16 10:34:05 +0800328 {};
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600329
330 int returnCode = 0;
331 ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500332 if (0 == peekedLength)
333 {
Deepak Kodihalli23c52042020-09-01 03:04:32 -0500334 // MCTP daemon has closed the socket this daemon is connected to.
335 // This may or may not be an error scenario, in either case the
336 // recovery mechanism for this daemon is to restart, and hence exit
337 // the event loop, that will cause this daemon to exit with a
338 // failure code.
339 io.get_event().exit(0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500340 }
341 else if (peekedLength <= -1)
342 {
343 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600344 std::cerr << "recv system call failed, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500345 }
346 else
347 {
348 std::vector<uint8_t> requestMsg(peekedLength);
349 auto recvDataLength = recv(
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600350 fd, static_cast<void*>(requestMsg.data()), peekedLength, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500351 if (recvDataLength == peekedLength)
352 {
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530353 if (verbose)
354 {
Tom Josephe5268cd2021-09-07 13:04:03 +0530355 printBuffer(Rx, requestMsg);
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530356 }
Tom Josephe5268cd2021-09-07 13:04:03 +0530357
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500358 if (MCTP_MSG_TYPE_PLDM != requestMsg[1])
359 {
360 // Skip this message and continue.
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500361 }
362 else
363 {
364 // process message and send response
Tom Josephfb3bc062021-08-17 07:48:11 -0700365 auto response = processRxMsg(requestMsg, invoker,
366 reqHandler, fwManager.get());
Tom Joseph74f27c72021-05-16 07:58:53 -0700367 if (response.has_value())
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500368 {
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500369 if (verbose)
370 {
Tom Josephe5268cd2021-09-07 13:04:03 +0530371 printBuffer(Tx, *response);
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500372 }
373
Zahed Hossain09a96e02019-08-06 07:42:37 -0500374 iov[0].iov_base = &requestMsg[0];
375 iov[0].iov_len =
376 sizeof(requestMsg[0]) + sizeof(requestMsg[1]);
Tom Joseph74f27c72021-05-16 07:58:53 -0700377 iov[1].iov_base = (*response).data();
378 iov[1].iov_len = (*response).size();
Zahed Hossain09a96e02019-08-06 07:42:37 -0500379
380 msg.msg_iov = iov;
381 msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530382 if (currentSendbuffSize >= 0 &&
383 (size_t)currentSendbuffSize < (*response).size())
384 {
385 int oldBuffSize = currentSendbuffSize;
386 currentSendbuffSize = (*response).size();
387 int res = setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
388 &currentSendbuffSize,
389 sizeof(currentSendbuffSize));
390 if (res == -1)
391 {
392 std::cerr
393 << "Responder : Failed to set the new send buffer size [bytes] : "
394 << currentSendbuffSize
395 << " from current size [bytes] : "
396 << oldBuffSize
397 << ", Error : " << strerror(errno)
398 << std::endl;
399 return;
400 }
401 }
Zahed Hossain09a96e02019-08-06 07:42:37 -0500402
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600403 int result = sendmsg(fd, &msg, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500404 if (-1 == result)
405 {
406 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600407 std::cerr << "sendto system call failed, RC= "
408 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500409 }
410 }
411 }
412 }
413 else
414 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600415 std::cerr
416 << "Failure to read peeked length packet. peekedLength= "
417 << peekedLength << " recvDataLength=" << recvDataLength
418 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500419 }
420 }
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600421 };
422
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600423 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
424 bus.request_name("xyz.openbmc_project.PLDM");
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600425 IO io(event, socketFd(), EPOLLIN, std::move(callback));
Manojkiran Eda88bc7182021-09-08 18:37:30 +0530426#ifdef LIBPLDMRESPONDER
427 if (hostPDRHandler)
428 {
429 hostPDRHandler->setHostFirmwareCondition();
430 }
431#endif
Brad Bishop3a22b972021-10-07 17:01:58 -0400432 returnCode = event.loop();
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500433
Brad Bishop3a22b972021-10-07 17:01:58 -0400434 if (shutdown(sockfd, SHUT_RDWR))
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500435 {
Brad Bishop3a22b972021-10-07 17:01:58 -0400436 std::perror("Failed to shutdown the socket");
437 }
438 if (returnCode)
439 {
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500440 exit(EXIT_FAILURE);
441 }
Brad Bishop3a22b972021-10-07 17:01:58 -0400442
443 exit(EXIT_SUCCESS);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500444}