blob: 5c255878cba4735ea42fdc4b096369185a236b3c [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"
Deepak Kodihallibc669f12019-11-28 08:52:07 -06008#include "invoker.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -05009
10#include <err.h>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053011#include <getopt.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050012#include <poll.h>
13#include <stdlib.h>
14#include <sys/socket.h>
15#include <sys/types.h>
16#include <sys/un.h>
17#include <unistd.h>
18
George Liu6492f522020-06-16 10:34:05 +080019#include <sdeventplus/event.hpp>
20#include <sdeventplus/source/io.hpp>
21
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050022#include <cstdio>
23#include <cstring>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050024#include <fstream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050025#include <iomanip>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053026#include <iostream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050027#include <iterator>
Deepak Kodihallic682fe22020-03-04 00:42:54 -060028#include <memory>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050029#include <sstream>
Deepak Kodihallibc669f12019-11-28 08:52:07 -060030#include <stdexcept>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053031#include <string>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050032#include <vector>
33
Tom Joseph02b4ee42021-05-02 22:44:36 -070034#ifdef LIBPLDMRESPONDER
35#include "dbus_impl_pdr.hpp"
36#include "host-bmc/dbus_to_event_handler.hpp"
37#include "host-bmc/dbus_to_host_effecters.hpp"
38#include "host-bmc/host_pdr_handler.hpp"
39#include "libpldmresponder/base.hpp"
40#include "libpldmresponder/bios.hpp"
41#include "libpldmresponder/fru.hpp"
42#include "libpldmresponder/oem_handler.hpp"
43#include "libpldmresponder/platform.hpp"
44#include "xyz/openbmc_project/PLDM/Event/server.hpp"
45#endif
46
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050047#ifdef OEM_IBM
48#include "libpldmresponder/file_io.hpp"
Sampa Misraaea5dde2020-08-31 08:33:47 -050049#include "libpldmresponder/oem_ibm_handler.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050050#endif
51
52constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
53
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050054using namespace pldm;
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060055using namespace sdeventplus;
56using namespace sdeventplus::source;
Tom Joseph02b4ee42021-05-02 22:44:36 -070057using namespace pldm::responder;
58using namespace pldm::utils;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050059
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060060static Response processRxMsg(const std::vector<uint8_t>& requestMsg,
Deepak Kodihallibc669f12019-11-28 08:52:07 -060061 Invoker& invoker, dbus_api::Requester& requester)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050062{
63
64 Response response;
65 uint8_t eid = requestMsg[0];
66 uint8_t type = requestMsg[1];
67 pldm_header_info hdrFields{};
68 auto hdr = reinterpret_cast<const pldm_msg_hdr*>(
69 requestMsg.data() + sizeof(eid) + sizeof(type));
70 if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields))
71 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060072 std::cerr << "Empty PLDM request header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050073 }
74 else if (PLDM_RESPONSE != hdrFields.msg_type)
75 {
76 auto request = reinterpret_cast<const pldm_msg*>(hdr);
77 size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) -
78 sizeof(eid) - sizeof(type);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060079 try
80 {
81 response = invoker.handle(hdrFields.pldm_type, hdrFields.command,
82 request, requestLen);
83 }
84 catch (const std::out_of_range& e)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050085 {
86 uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
87 response.resize(sizeof(pldm_msg_hdr));
88 auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data());
89 pldm_header_info header{};
90 header.msg_type = PLDM_RESPONSE;
91 header.instance = hdrFields.instance;
92 header.pldm_type = hdrFields.pldm_type;
93 header.command = hdrFields.command;
94 auto result = pack_pldm_header(&header, responseHdr);
95 if (PLDM_SUCCESS != result)
96 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060097 std::cerr << "Failed adding response header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050098 }
99 response.insert(response.end(), completion_code);
100 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500101 }
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600102 else
103 {
104 requester.markFree(eid, hdr->instance_id);
105 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500106 return response;
107}
108
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530109void optionUsage(void)
110{
111 std::cerr << "Usage: pldmd [options]\n";
112 std::cerr << "Options:\n";
113 std::cerr
114 << " --verbose=<0/1> 0 - Disable verbosity, 1 - Enable verbosity\n";
115 std::cerr << "Defaulted settings: --verbose=0 \n";
116}
117
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500118int main(int argc, char** argv)
119{
120
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530121 bool verbose = false;
122 static struct option long_options[] = {
123 {"verbose", required_argument, 0, 'v'}, {0, 0, 0, 0}};
124
125 auto argflag = getopt_long(argc, argv, "v:", long_options, nullptr);
126 switch (argflag)
127 {
128 case 'v':
129 switch (std::stoi(optarg))
130 {
131 case 0:
132 verbose = false;
133 break;
134 case 1:
135 verbose = true;
136 break;
137 default:
138 optionUsage();
139 break;
140 }
141 break;
142 default:
143 optionUsage();
144 break;
145 }
146
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500147 /* Create local socket. */
148 int returnCode = 0;
149 int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
150 if (-1 == sockfd)
151 {
152 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600153 std::cerr << "Failed to create the socket, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500154 exit(EXIT_FAILURE);
155 }
156
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500157 auto event = Event::get_default();
Tom Joseph02b4ee42021-05-02 22:44:36 -0700158 auto& bus = pldm::utils::DBusHandler::getBus();
159 dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm");
160 Invoker invoker{};
161
162#ifdef LIBPLDMRESPONDER
163 using namespace pldm::state_sensor;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500164 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo(
165 pldm_pdr_init(), pldm_pdr_destroy);
166 std::unique_ptr<pldm_entity_association_tree,
167 decltype(&pldm_entity_association_tree_destroy)>
168 entityTree(pldm_entity_association_tree_init(),
169 pldm_entity_association_tree_destroy);
Sampa Misrac073a202021-05-08 10:56:05 -0500170 std::unique_ptr<pldm_entity_association_tree,
171 decltype(&pldm_entity_association_tree_destroy)>
172 bmcEntityTree(pldm_entity_association_tree_init(),
173 pldm_entity_association_tree_destroy);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500174 std::unique_ptr<HostPDRHandler> hostPDRHandler;
Sampa Misrac0c60542020-07-01 02:34:25 -0500175 std::unique_ptr<pldm::host_effecters::HostEffecterParser>
176 hostEffecterParser;
George Liucae18662020-05-15 09:32:57 +0800177 std::unique_ptr<DbusToPLDMEvent> dbusToPLDMEventHandler;
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500178 auto dbusHandler = std::make_unique<DBusHandler>();
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500179 auto hostEID = pldm::utils::readHostEID();
180 if (hostEID)
181 {
182 hostPDRHandler = std::make_unique<HostPDRHandler>(
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600183 sockfd, hostEID, event, pdrRepo.get(), EVENTS_JSONS_DIR,
Sampa Misrac073a202021-05-08 10:56:05 -0500184 entityTree.get(), bmcEntityTree.get(), dbusImplReq, verbose);
Sampa Misrac0c60542020-07-01 02:34:25 -0500185 hostEffecterParser =
186 std::make_unique<pldm::host_effecters::HostEffecterParser>(
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500187 &dbusImplReq, sockfd, pdrRepo.get(), dbusHandler.get(),
Sampa Misrac0c60542020-07-01 02:34:25 -0500188 HOST_JSONS_DIR, verbose);
George Liucae18662020-05-15 09:32:57 +0800189 dbusToPLDMEventHandler =
190 std::make_unique<DbusToPLDMEvent>(sockfd, hostEID, dbusImplReq);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500191 }
Sampa Misraaea5dde2020-08-31 08:33:47 -0500192 std::unique_ptr<oem_platform::Handler> oemPlatformHandler{};
193
194#ifdef OEM_IBM
195 std::unique_ptr<pldm::responder::CodeUpdate> codeUpdate =
196 std::make_unique<pldm::responder::CodeUpdate>(dbusHandler.get());
Varsha Kaverappa3ca29df2020-09-27 12:39:22 -0500197 codeUpdate->clearDirPath(LID_STAGING_DIR);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500198 oemPlatformHandler = std::make_unique<oem_ibm_platform::Handler>(
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500199 dbusHandler.get(), codeUpdate.get(), sockfd, hostEID, dbusImplReq,
200 event);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500201 codeUpdate->setOemPlatformHandler(oemPlatformHandler.get());
202 invoker.registerHandler(
Jayashankar Padathdb124362021-01-28 21:12:34 -0600203 PLDM_OEM, std::make_unique<oem_ibm::Handler>(
204 oemPlatformHandler.get(), sockfd, hostEID, &dbusImplReq));
Sampa Misraaea5dde2020-08-31 08:33:47 -0500205#endif
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500206 invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>());
Tom Joseph7f839f92020-09-21 10:20:44 +0530207 invoker.registerHandler(PLDM_BIOS, std::make_unique<bios::Handler>(
208 sockfd, hostEID, &dbusImplReq));
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530209 auto fruHandler = std::make_unique<fru::Handler>(
Sampa Misrac073a202021-05-08 10:56:05 -0500210 FRU_JSONS_DIR, pdrRepo.get(), entityTree.get(), bmcEntityTree.get());
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530211 // FRU table is built lazily when a FRU command or Get PDR command is
212 // handled. To enable building FRU table, the FRU handler is passed to the
213 // Platform handler.
Sampa Misraaea5dde2020-08-31 08:33:47 -0500214 auto platformHandler = std::make_unique<platform::Handler>(
215 dbusHandler.get(), PDR_JSONS_DIR, pdrRepo.get(), hostPDRHandler.get(),
216 dbusToPLDMEventHandler.get(), fruHandler.get(),
Sampa Misra5fb37d52021-03-06 07:26:00 -0600217 oemPlatformHandler.get(), event, true);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500218#ifdef OEM_IBM
Sampa Misraaea5dde2020-08-31 08:33:47 -0500219 pldm::responder::oem_ibm_platform::Handler* oemIbmPlatformHandler =
220 dynamic_cast<pldm::responder::oem_ibm_platform::Handler*>(
221 oemPlatformHandler.get());
222 oemIbmPlatformHandler->setPlatformHandler(platformHandler.get());
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500223#endif
224
Sampa Misraaea5dde2020-08-31 08:33:47 -0500225 invoker.registerHandler(PLDM_PLATFORM, std::move(platformHandler));
226 invoker.registerHandler(PLDM_FRU, std::move(fruHandler));
Tom Joseph02b4ee42021-05-02 22:44:36 -0700227 dbus_api::Pdr dbusImplPdr(bus, "/xyz/openbmc_project/pldm", pdrRepo.get());
228 sdbusplus::xyz::openbmc_project::PLDM::server::Event dbusImplEvent(
229 bus, "/xyz/openbmc_project/pldm");
230#endif
Sampa Misraaea5dde2020-08-31 08:33:47 -0500231
George Liu83409572019-12-24 18:42:54 +0800232 pldm::utils::CustomFD socketFd(sockfd);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500233
234 struct sockaddr_un addr
George Liu6492f522020-06-16 10:34:05 +0800235 {};
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500236 addr.sun_family = AF_UNIX;
237 const char path[] = "\0mctp-mux";
238 memcpy(addr.sun_path, path, sizeof(path) - 1);
239 int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr),
240 sizeof(path) + sizeof(addr.sun_family) - 1);
241 if (-1 == result)
242 {
243 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600244 std::cerr << "Failed to connect to the socket, RC= " << returnCode
245 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500246 exit(EXIT_FAILURE);
247 }
248
249 result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM));
250 if (-1 == result)
251 {
252 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600253 std::cerr << "Failed to send message type as pldm to mctp, RC= "
254 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500255 exit(EXIT_FAILURE);
256 }
257
Deepak Kodihalli23c52042020-09-01 03:04:32 -0500258 auto callback = [verbose, &invoker, &dbusImplReq](IO& io, int fd,
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600259 uint32_t revents) {
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600260 if (!(revents & EPOLLIN))
261 {
262 return;
263 }
264
265 // Outgoing message.
266 struct iovec iov[2]{};
267
268 // This structure contains the parameter information for the response
269 // message.
270 struct msghdr msg
George Liu6492f522020-06-16 10:34:05 +0800271 {};
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600272
273 int returnCode = 0;
274 ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500275 if (0 == peekedLength)
276 {
Deepak Kodihalli23c52042020-09-01 03:04:32 -0500277 // MCTP daemon has closed the socket this daemon is connected to.
278 // This may or may not be an error scenario, in either case the
279 // recovery mechanism for this daemon is to restart, and hence exit
280 // the event loop, that will cause this daemon to exit with a
281 // failure code.
282 io.get_event().exit(0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500283 }
284 else if (peekedLength <= -1)
285 {
286 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600287 std::cerr << "recv system call failed, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500288 }
289 else
290 {
291 std::vector<uint8_t> requestMsg(peekedLength);
292 auto recvDataLength = recv(
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600293 fd, static_cast<void*>(requestMsg.data()), peekedLength, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500294 if (recvDataLength == peekedLength)
295 {
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530296 if (verbose)
297 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600298 std::cout << "Received Msg" << std::endl;
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600299 printBuffer(requestMsg, verbose);
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530300 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500301 if (MCTP_MSG_TYPE_PLDM != requestMsg[1])
302 {
303 // Skip this message and continue.
Sampa Misraaa8ae722019-12-12 03:20:40 -0600304 std::cerr << "Encountered Non-PLDM type message"
305 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500306 }
307 else
308 {
309 // process message and send response
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600310 auto response =
311 processRxMsg(requestMsg, invoker, dbusImplReq);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500312 if (!response.empty())
313 {
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500314 if (verbose)
315 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600316 std::cout << "Sending Msg" << std::endl;
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600317 printBuffer(response, verbose);
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500318 }
319
Zahed Hossain09a96e02019-08-06 07:42:37 -0500320 iov[0].iov_base = &requestMsg[0];
321 iov[0].iov_len =
322 sizeof(requestMsg[0]) + sizeof(requestMsg[1]);
323 iov[1].iov_base = response.data();
324 iov[1].iov_len = response.size();
325
326 msg.msg_iov = iov;
327 msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
328
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600329 int result = sendmsg(fd, &msg, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500330 if (-1 == result)
331 {
332 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600333 std::cerr << "sendto system call failed, RC= "
334 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500335 }
336 }
337 }
338 }
339 else
340 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600341 std::cerr
342 << "Failure to read peeked length packet. peekedLength= "
343 << peekedLength << " recvDataLength=" << recvDataLength
344 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500345 }
346 }
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600347 };
348
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600349 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
350 bus.request_name("xyz.openbmc_project.PLDM");
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600351 IO io(event, socketFd(), EPOLLIN, std::move(callback));
352 event.loop();
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500353
354 result = shutdown(sockfd, SHUT_RDWR);
355 if (-1 == result)
356 {
357 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600358 std::cerr << "Failed to shutdown the socket, RC=" << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500359 exit(EXIT_FAILURE);
360 }
361 exit(EXIT_FAILURE);
362}