blob: bfd3d716d09952385a0901b6eb280d31decbc011 [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);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500170 std::unique_ptr<HostPDRHandler> hostPDRHandler;
Sampa Misrac0c60542020-07-01 02:34:25 -0500171 std::unique_ptr<pldm::host_effecters::HostEffecterParser>
172 hostEffecterParser;
George Liucae18662020-05-15 09:32:57 +0800173 std::unique_ptr<DbusToPLDMEvent> dbusToPLDMEventHandler;
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500174 auto dbusHandler = std::make_unique<DBusHandler>();
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500175 auto hostEID = pldm::utils::readHostEID();
176 if (hostEID)
177 {
178 hostPDRHandler = std::make_unique<HostPDRHandler>(
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600179 sockfd, hostEID, event, pdrRepo.get(), EVENTS_JSONS_DIR,
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600180 entityTree.get(), dbusImplReq, verbose);
Sampa Misrac0c60542020-07-01 02:34:25 -0500181 hostEffecterParser =
182 std::make_unique<pldm::host_effecters::HostEffecterParser>(
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500183 &dbusImplReq, sockfd, pdrRepo.get(), dbusHandler.get(),
Sampa Misrac0c60542020-07-01 02:34:25 -0500184 HOST_JSONS_DIR, verbose);
George Liucae18662020-05-15 09:32:57 +0800185 dbusToPLDMEventHandler =
186 std::make_unique<DbusToPLDMEvent>(sockfd, hostEID, dbusImplReq);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500187 }
Sampa Misraaea5dde2020-08-31 08:33:47 -0500188 std::unique_ptr<oem_platform::Handler> oemPlatformHandler{};
189
190#ifdef OEM_IBM
191 std::unique_ptr<pldm::responder::CodeUpdate> codeUpdate =
192 std::make_unique<pldm::responder::CodeUpdate>(dbusHandler.get());
Varsha Kaverappa3ca29df2020-09-27 12:39:22 -0500193 codeUpdate->clearDirPath(LID_STAGING_DIR);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500194 oemPlatformHandler = std::make_unique<oem_ibm_platform::Handler>(
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500195 dbusHandler.get(), codeUpdate.get(), sockfd, hostEID, dbusImplReq,
196 event);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500197 codeUpdate->setOemPlatformHandler(oemPlatformHandler.get());
198 invoker.registerHandler(
Jayashankar Padathdb124362021-01-28 21:12:34 -0600199 PLDM_OEM, std::make_unique<oem_ibm::Handler>(
200 oemPlatformHandler.get(), sockfd, hostEID, &dbusImplReq));
Sampa Misraaea5dde2020-08-31 08:33:47 -0500201#endif
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500202 invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>());
Tom Joseph7f839f92020-09-21 10:20:44 +0530203 invoker.registerHandler(PLDM_BIOS, std::make_unique<bios::Handler>(
204 sockfd, hostEID, &dbusImplReq));
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530205 auto fruHandler = std::make_unique<fru::Handler>(
206 FRU_JSONS_DIR, pdrRepo.get(), entityTree.get());
207 // FRU table is built lazily when a FRU command or Get PDR command is
208 // handled. To enable building FRU table, the FRU handler is passed to the
209 // Platform handler.
Sampa Misraaea5dde2020-08-31 08:33:47 -0500210 auto platformHandler = std::make_unique<platform::Handler>(
211 dbusHandler.get(), PDR_JSONS_DIR, pdrRepo.get(), hostPDRHandler.get(),
212 dbusToPLDMEventHandler.get(), fruHandler.get(),
Sampa Misra5fb37d52021-03-06 07:26:00 -0600213 oemPlatformHandler.get(), event, true);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500214#ifdef OEM_IBM
Sampa Misraaea5dde2020-08-31 08:33:47 -0500215 pldm::responder::oem_ibm_platform::Handler* oemIbmPlatformHandler =
216 dynamic_cast<pldm::responder::oem_ibm_platform::Handler*>(
217 oemPlatformHandler.get());
218 oemIbmPlatformHandler->setPlatformHandler(platformHandler.get());
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500219#endif
220
Sampa Misraaea5dde2020-08-31 08:33:47 -0500221 invoker.registerHandler(PLDM_PLATFORM, std::move(platformHandler));
222 invoker.registerHandler(PLDM_FRU, std::move(fruHandler));
Tom Joseph02b4ee42021-05-02 22:44:36 -0700223 dbus_api::Pdr dbusImplPdr(bus, "/xyz/openbmc_project/pldm", pdrRepo.get());
224 sdbusplus::xyz::openbmc_project::PLDM::server::Event dbusImplEvent(
225 bus, "/xyz/openbmc_project/pldm");
226#endif
Sampa Misraaea5dde2020-08-31 08:33:47 -0500227
George Liu83409572019-12-24 18:42:54 +0800228 pldm::utils::CustomFD socketFd(sockfd);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500229
230 struct sockaddr_un addr
George Liu6492f522020-06-16 10:34:05 +0800231 {};
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500232 addr.sun_family = AF_UNIX;
233 const char path[] = "\0mctp-mux";
234 memcpy(addr.sun_path, path, sizeof(path) - 1);
235 int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr),
236 sizeof(path) + sizeof(addr.sun_family) - 1);
237 if (-1 == result)
238 {
239 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600240 std::cerr << "Failed to connect to the socket, RC= " << returnCode
241 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500242 exit(EXIT_FAILURE);
243 }
244
245 result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM));
246 if (-1 == result)
247 {
248 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600249 std::cerr << "Failed to send message type as pldm to mctp, RC= "
250 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500251 exit(EXIT_FAILURE);
252 }
253
Deepak Kodihalli23c52042020-09-01 03:04:32 -0500254 auto callback = [verbose, &invoker, &dbusImplReq](IO& io, int fd,
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600255 uint32_t revents) {
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600256 if (!(revents & EPOLLIN))
257 {
258 return;
259 }
260
261 // Outgoing message.
262 struct iovec iov[2]{};
263
264 // This structure contains the parameter information for the response
265 // message.
266 struct msghdr msg
George Liu6492f522020-06-16 10:34:05 +0800267 {};
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600268
269 int returnCode = 0;
270 ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500271 if (0 == peekedLength)
272 {
Deepak Kodihalli23c52042020-09-01 03:04:32 -0500273 // MCTP daemon has closed the socket this daemon is connected to.
274 // This may or may not be an error scenario, in either case the
275 // recovery mechanism for this daemon is to restart, and hence exit
276 // the event loop, that will cause this daemon to exit with a
277 // failure code.
278 io.get_event().exit(0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500279 }
280 else if (peekedLength <= -1)
281 {
282 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600283 std::cerr << "recv system call failed, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500284 }
285 else
286 {
287 std::vector<uint8_t> requestMsg(peekedLength);
288 auto recvDataLength = recv(
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600289 fd, static_cast<void*>(requestMsg.data()), peekedLength, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500290 if (recvDataLength == peekedLength)
291 {
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530292 if (verbose)
293 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600294 std::cout << "Received Msg" << std::endl;
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600295 printBuffer(requestMsg, verbose);
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530296 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500297 if (MCTP_MSG_TYPE_PLDM != requestMsg[1])
298 {
299 // Skip this message and continue.
Sampa Misraaa8ae722019-12-12 03:20:40 -0600300 std::cerr << "Encountered Non-PLDM type message"
301 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500302 }
303 else
304 {
305 // process message and send response
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600306 auto response =
307 processRxMsg(requestMsg, invoker, dbusImplReq);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500308 if (!response.empty())
309 {
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500310 if (verbose)
311 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600312 std::cout << "Sending Msg" << std::endl;
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600313 printBuffer(response, verbose);
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500314 }
315
Zahed Hossain09a96e02019-08-06 07:42:37 -0500316 iov[0].iov_base = &requestMsg[0];
317 iov[0].iov_len =
318 sizeof(requestMsg[0]) + sizeof(requestMsg[1]);
319 iov[1].iov_base = response.data();
320 iov[1].iov_len = response.size();
321
322 msg.msg_iov = iov;
323 msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
324
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600325 int result = sendmsg(fd, &msg, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500326 if (-1 == result)
327 {
328 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600329 std::cerr << "sendto system call failed, RC= "
330 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500331 }
332 }
333 }
334 }
335 else
336 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600337 std::cerr
338 << "Failure to read peeked length packet. peekedLength= "
339 << peekedLength << " recvDataLength=" << recvDataLength
340 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500341 }
342 }
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600343 };
344
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600345 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
346 bus.request_name("xyz.openbmc_project.PLDM");
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600347 IO io(event, socketFd(), EPOLLIN, std::move(callback));
348 event.loop();
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500349
350 result = shutdown(sockfd, SHUT_RDWR);
351 if (-1 == result)
352 {
353 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600354 std::cerr << "Failed to shutdown the socket, RC=" << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500355 exit(EXIT_FAILURE);
356 }
357 exit(EXIT_FAILURE);
358}