blob: ca62bbddbd4cec9f7f60435b91f596b4d7afb931 [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"
Tom Joseph74f27c72021-05-16 07:58:53 -07009#include "requester/handler.hpp"
10#include "requester/request.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050011
12#include <err.h>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053013#include <getopt.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050014#include <poll.h>
15#include <stdlib.h>
16#include <sys/socket.h>
17#include <sys/types.h>
18#include <sys/un.h>
19#include <unistd.h>
20
George Liu6492f522020-06-16 10:34:05 +080021#include <sdeventplus/event.hpp>
22#include <sdeventplus/source/io.hpp>
23
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050024#include <cstdio>
25#include <cstring>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050026#include <fstream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050027#include <iomanip>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053028#include <iostream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050029#include <iterator>
Deepak Kodihallic682fe22020-03-04 00:42:54 -060030#include <memory>
Tom Joseph74f27c72021-05-16 07:58:53 -070031#include <optional>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050032#include <sstream>
Deepak Kodihallibc669f12019-11-28 08:52:07 -060033#include <stdexcept>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053034#include <string>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050035#include <vector>
36
Tom Joseph02b4ee42021-05-02 22:44:36 -070037#ifdef LIBPLDMRESPONDER
38#include "dbus_impl_pdr.hpp"
39#include "host-bmc/dbus_to_event_handler.hpp"
40#include "host-bmc/dbus_to_host_effecters.hpp"
41#include "host-bmc/host_pdr_handler.hpp"
42#include "libpldmresponder/base.hpp"
43#include "libpldmresponder/bios.hpp"
44#include "libpldmresponder/fru.hpp"
45#include "libpldmresponder/oem_handler.hpp"
46#include "libpldmresponder/platform.hpp"
47#include "xyz/openbmc_project/PLDM/Event/server.hpp"
48#endif
49
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050050#ifdef OEM_IBM
51#include "libpldmresponder/file_io.hpp"
Sampa Misraaea5dde2020-08-31 08:33:47 -050052#include "libpldmresponder/oem_ibm_handler.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050053#endif
54
55constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
56
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050057using namespace pldm;
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060058using namespace sdeventplus;
59using namespace sdeventplus::source;
Tom Joseph02b4ee42021-05-02 22:44:36 -070060using namespace pldm::responder;
61using namespace pldm::utils;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050062
Tom Joseph74f27c72021-05-16 07:58:53 -070063static std::optional<Response>
64 processRxMsg(const std::vector<uint8_t>& requestMsg, Invoker& invoker,
65 requester::Handler<requester::Request>& handler)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050066{
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050067 uint8_t eid = requestMsg[0];
68 uint8_t type = requestMsg[1];
69 pldm_header_info hdrFields{};
70 auto hdr = reinterpret_cast<const pldm_msg_hdr*>(
71 requestMsg.data() + sizeof(eid) + sizeof(type));
72 if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields))
73 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060074 std::cerr << "Empty PLDM request header \n";
Tom Joseph74f27c72021-05-16 07:58:53 -070075 return std::nullopt;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050076 }
George Liub7095ff2021-06-14 16:01:57 +080077
78 if (PLDM_RESPONSE != hdrFields.msg_type)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050079 {
Tom Joseph74f27c72021-05-16 07:58:53 -070080 Response response;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050081 auto request = reinterpret_cast<const pldm_msg*>(hdr);
82 size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) -
83 sizeof(eid) - sizeof(type);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060084 try
85 {
86 response = invoker.handle(hdrFields.pldm_type, hdrFields.command,
87 request, requestLen);
88 }
89 catch (const std::out_of_range& e)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050090 {
91 uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
92 response.resize(sizeof(pldm_msg_hdr));
93 auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data());
94 pldm_header_info header{};
95 header.msg_type = PLDM_RESPONSE;
96 header.instance = hdrFields.instance;
97 header.pldm_type = hdrFields.pldm_type;
98 header.command = hdrFields.command;
George Liub7095ff2021-06-14 16:01:57 +080099 if (PLDM_SUCCESS != pack_pldm_header(&header, responseHdr))
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500100 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600101 std::cerr << "Failed adding response header \n";
Tom Joseph74f27c72021-05-16 07:58:53 -0700102 return std::nullopt;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500103 }
104 response.insert(response.end(), completion_code);
105 }
Tom Joseph74f27c72021-05-16 07:58:53 -0700106 return response;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500107 }
Tom Joseph74f27c72021-05-16 07:58:53 -0700108 else if (PLDM_RESPONSE == hdrFields.msg_type)
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600109 {
Tom Joseph74f27c72021-05-16 07:58:53 -0700110 auto response = reinterpret_cast<const pldm_msg*>(hdr);
111 size_t responseLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) -
112 sizeof(eid) - sizeof(type);
113 handler.handleResponse(eid, hdrFields.instance, hdrFields.pldm_type,
114 hdrFields.command, response, responseLen);
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600115 }
Tom Joseph74f27c72021-05-16 07:58:53 -0700116 return std::nullopt;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500117}
118
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530119void optionUsage(void)
120{
121 std::cerr << "Usage: pldmd [options]\n";
122 std::cerr << "Options:\n";
123 std::cerr
124 << " --verbose=<0/1> 0 - Disable verbosity, 1 - Enable verbosity\n";
125 std::cerr << "Defaulted settings: --verbose=0 \n";
126}
127
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500128int main(int argc, char** argv)
129{
130
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530131 bool verbose = false;
132 static struct option long_options[] = {
133 {"verbose", required_argument, 0, 'v'}, {0, 0, 0, 0}};
134
135 auto argflag = getopt_long(argc, argv, "v:", long_options, nullptr);
136 switch (argflag)
137 {
138 case 'v':
139 switch (std::stoi(optarg))
140 {
141 case 0:
142 verbose = false;
143 break;
144 case 1:
145 verbose = true;
146 break;
147 default:
148 optionUsage();
149 break;
150 }
151 break;
152 default:
153 optionUsage();
154 break;
155 }
156
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500157 /* Create local socket. */
158 int returnCode = 0;
159 int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
160 if (-1 == sockfd)
161 {
162 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600163 std::cerr << "Failed to create the socket, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500164 exit(EXIT_FAILURE);
165 }
166
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500167 auto event = Event::get_default();
Tom Joseph02b4ee42021-05-02 22:44:36 -0700168 auto& bus = pldm::utils::DBusHandler::getBus();
169 dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm");
170 Invoker invoker{};
Tom Joseph74f27c72021-05-16 07:58:53 -0700171 requester::Handler<requester::Request> reqHandler(sockfd, event,
172 dbusImplReq);
Tom Joseph02b4ee42021-05-02 22:44:36 -0700173
174#ifdef LIBPLDMRESPONDER
175 using namespace pldm::state_sensor;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500176 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo(
177 pldm_pdr_init(), pldm_pdr_destroy);
178 std::unique_ptr<pldm_entity_association_tree,
179 decltype(&pldm_entity_association_tree_destroy)>
180 entityTree(pldm_entity_association_tree_init(),
181 pldm_entity_association_tree_destroy);
Sampa Misrac073a202021-05-08 10:56:05 -0500182 std::unique_ptr<pldm_entity_association_tree,
183 decltype(&pldm_entity_association_tree_destroy)>
184 bmcEntityTree(pldm_entity_association_tree_init(),
185 pldm_entity_association_tree_destroy);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500186 std::unique_ptr<HostPDRHandler> hostPDRHandler;
Sampa Misrac0c60542020-07-01 02:34:25 -0500187 std::unique_ptr<pldm::host_effecters::HostEffecterParser>
188 hostEffecterParser;
George Liucae18662020-05-15 09:32:57 +0800189 std::unique_ptr<DbusToPLDMEvent> dbusToPLDMEventHandler;
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500190 auto dbusHandler = std::make_unique<DBusHandler>();
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500191 auto hostEID = pldm::utils::readHostEID();
192 if (hostEID)
193 {
194 hostPDRHandler = std::make_unique<HostPDRHandler>(
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600195 sockfd, hostEID, event, pdrRepo.get(), EVENTS_JSONS_DIR,
Sampa Misrac0c79482021-06-02 08:01:54 -0500196 entityTree.get(), bmcEntityTree.get(), dbusImplReq, &reqHandler,
Tom Joseph74f27c72021-05-16 07:58:53 -0700197 verbose);
Sampa Misrac0c60542020-07-01 02:34:25 -0500198 hostEffecterParser =
199 std::make_unique<pldm::host_effecters::HostEffecterParser>(
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500200 &dbusImplReq, sockfd, pdrRepo.get(), dbusHandler.get(),
Sampa Misrac0c79482021-06-02 08:01:54 -0500201 HOST_JSONS_DIR, &reqHandler, verbose);
202 dbusToPLDMEventHandler = std::make_unique<DbusToPLDMEvent>(
203 sockfd, hostEID, dbusImplReq, &reqHandler);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500204 }
Sampa Misraaea5dde2020-08-31 08:33:47 -0500205 std::unique_ptr<oem_platform::Handler> oemPlatformHandler{};
206
207#ifdef OEM_IBM
208 std::unique_ptr<pldm::responder::CodeUpdate> codeUpdate =
209 std::make_unique<pldm::responder::CodeUpdate>(dbusHandler.get());
Varsha Kaverappa3ca29df2020-09-27 12:39:22 -0500210 codeUpdate->clearDirPath(LID_STAGING_DIR);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500211 oemPlatformHandler = std::make_unique<oem_ibm_platform::Handler>(
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500212 dbusHandler.get(), codeUpdate.get(), sockfd, hostEID, dbusImplReq,
Sampa Misrac0c79482021-06-02 08:01:54 -0500213 event, &reqHandler);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500214 codeUpdate->setOemPlatformHandler(oemPlatformHandler.get());
Sampa Misrac0c79482021-06-02 08:01:54 -0500215 invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>(
216 oemPlatformHandler.get(), sockfd,
217 hostEID, &dbusImplReq, &reqHandler));
Sampa Misraaea5dde2020-08-31 08:33:47 -0500218#endif
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500219 invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>());
Sampa Misrac0c79482021-06-02 08:01:54 -0500220 invoker.registerHandler(
221 PLDM_BIOS, std::make_unique<bios::Handler>(sockfd, hostEID,
222 &dbusImplReq, &reqHandler));
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530223 auto fruHandler = std::make_unique<fru::Handler>(
Sampa Misrac073a202021-05-08 10:56:05 -0500224 FRU_JSONS_DIR, pdrRepo.get(), entityTree.get(), bmcEntityTree.get());
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530225 // FRU table is built lazily when a FRU command or Get PDR command is
226 // handled. To enable building FRU table, the FRU handler is passed to the
227 // Platform handler.
Sampa Misraaea5dde2020-08-31 08:33:47 -0500228 auto platformHandler = std::make_unique<platform::Handler>(
229 dbusHandler.get(), PDR_JSONS_DIR, pdrRepo.get(), hostPDRHandler.get(),
230 dbusToPLDMEventHandler.get(), fruHandler.get(),
Sampa Misra5fb37d52021-03-06 07:26:00 -0600231 oemPlatformHandler.get(), event, true);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500232#ifdef OEM_IBM
Sampa Misraaea5dde2020-08-31 08:33:47 -0500233 pldm::responder::oem_ibm_platform::Handler* oemIbmPlatformHandler =
234 dynamic_cast<pldm::responder::oem_ibm_platform::Handler*>(
235 oemPlatformHandler.get());
236 oemIbmPlatformHandler->setPlatformHandler(platformHandler.get());
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500237#endif
238
Sampa Misraaea5dde2020-08-31 08:33:47 -0500239 invoker.registerHandler(PLDM_PLATFORM, std::move(platformHandler));
240 invoker.registerHandler(PLDM_FRU, std::move(fruHandler));
Tom Joseph02b4ee42021-05-02 22:44:36 -0700241 dbus_api::Pdr dbusImplPdr(bus, "/xyz/openbmc_project/pldm", pdrRepo.get());
242 sdbusplus::xyz::openbmc_project::PLDM::server::Event dbusImplEvent(
243 bus, "/xyz/openbmc_project/pldm");
244#endif
Sampa Misraaea5dde2020-08-31 08:33:47 -0500245
George Liu83409572019-12-24 18:42:54 +0800246 pldm::utils::CustomFD socketFd(sockfd);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500247
248 struct sockaddr_un addr
George Liu6492f522020-06-16 10:34:05 +0800249 {};
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500250 addr.sun_family = AF_UNIX;
251 const char path[] = "\0mctp-mux";
252 memcpy(addr.sun_path, path, sizeof(path) - 1);
253 int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr),
254 sizeof(path) + sizeof(addr.sun_family) - 1);
255 if (-1 == result)
256 {
257 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600258 std::cerr << "Failed to connect to the socket, RC= " << returnCode
259 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500260 exit(EXIT_FAILURE);
261 }
262
263 result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM));
264 if (-1 == result)
265 {
266 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600267 std::cerr << "Failed to send message type as pldm to mctp, RC= "
268 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500269 exit(EXIT_FAILURE);
270 }
271
Tom Joseph74f27c72021-05-16 07:58:53 -0700272 auto callback = [verbose, &invoker, &reqHandler](IO& io, int fd,
273 uint32_t revents) {
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600274 if (!(revents & EPOLLIN))
275 {
276 return;
277 }
278
279 // Outgoing message.
280 struct iovec iov[2]{};
281
282 // This structure contains the parameter information for the response
283 // message.
284 struct msghdr msg
George Liu6492f522020-06-16 10:34:05 +0800285 {};
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600286
287 int returnCode = 0;
288 ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500289 if (0 == peekedLength)
290 {
Deepak Kodihalli23c52042020-09-01 03:04:32 -0500291 // MCTP daemon has closed the socket this daemon is connected to.
292 // This may or may not be an error scenario, in either case the
293 // recovery mechanism for this daemon is to restart, and hence exit
294 // the event loop, that will cause this daemon to exit with a
295 // failure code.
296 io.get_event().exit(0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500297 }
298 else if (peekedLength <= -1)
299 {
300 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600301 std::cerr << "recv system call failed, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500302 }
303 else
304 {
305 std::vector<uint8_t> requestMsg(peekedLength);
306 auto recvDataLength = recv(
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600307 fd, static_cast<void*>(requestMsg.data()), peekedLength, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500308 if (recvDataLength == peekedLength)
309 {
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530310 if (verbose)
311 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600312 std::cout << "Received Msg" << std::endl;
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600313 printBuffer(requestMsg, verbose);
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530314 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500315 if (MCTP_MSG_TYPE_PLDM != requestMsg[1])
316 {
317 // Skip this message and continue.
Sampa Misraaa8ae722019-12-12 03:20:40 -0600318 std::cerr << "Encountered Non-PLDM type message"
319 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500320 }
321 else
322 {
323 // process message and send response
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600324 auto response =
Tom Joseph74f27c72021-05-16 07:58:53 -0700325 processRxMsg(requestMsg, invoker, reqHandler);
326 if (response.has_value())
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500327 {
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500328 if (verbose)
329 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600330 std::cout << "Sending Msg" << std::endl;
Tom Joseph74f27c72021-05-16 07:58:53 -0700331 printBuffer(*response, verbose);
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500332 }
333
Zahed Hossain09a96e02019-08-06 07:42:37 -0500334 iov[0].iov_base = &requestMsg[0];
335 iov[0].iov_len =
336 sizeof(requestMsg[0]) + sizeof(requestMsg[1]);
Tom Joseph74f27c72021-05-16 07:58:53 -0700337 iov[1].iov_base = (*response).data();
338 iov[1].iov_len = (*response).size();
Zahed Hossain09a96e02019-08-06 07:42:37 -0500339
340 msg.msg_iov = iov;
341 msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
342
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600343 int result = sendmsg(fd, &msg, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500344 if (-1 == result)
345 {
346 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600347 std::cerr << "sendto system call failed, RC= "
348 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500349 }
350 }
351 }
352 }
353 else
354 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600355 std::cerr
356 << "Failure to read peeked length packet. peekedLength= "
357 << peekedLength << " recvDataLength=" << recvDataLength
358 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500359 }
360 }
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600361 };
362
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600363 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
364 bus.request_name("xyz.openbmc_project.PLDM");
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600365 IO io(event, socketFd(), EPOLLIN, std::move(callback));
366 event.loop();
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500367
368 result = shutdown(sockfd, SHUT_RDWR);
369 if (-1 == result)
370 {
371 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600372 std::cerr << "Failed to shutdown the socket, RC=" << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500373 exit(EXIT_FAILURE);
374 }
375 exit(EXIT_FAILURE);
376}