blob: c99ab182edc5cb4c19d1f8c3bbc47c3a87a1c297 [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"
Pavithra Barithaya0f74c982020-04-27 02:17:10 -05007#include "dbus_impl_pdr.hpp"
Deepak Kodihalli4de4d002019-11-11 02:41:43 -06008#include "dbus_impl_requester.hpp"
George Liucae18662020-05-15 09:32:57 +08009#include "host-bmc/dbus_to_event_handler.hpp"
Deepak Kodihalliac19bd62020-06-16 08:25:23 -050010#include "host-bmc/dbus_to_host_effecters.hpp"
11#include "host-bmc/host_pdr_handler.hpp"
Deepak Kodihallibc669f12019-11-28 08:52:07 -060012#include "invoker.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050013#include "libpldmresponder/base.hpp"
14#include "libpldmresponder/bios.hpp"
Deepak Kodihallie60c5822019-10-23 03:26:15 -050015#include "libpldmresponder/fru.hpp"
Sampa Misraa2fa0702019-05-31 01:28:55 -050016#include "libpldmresponder/platform.hpp"
Chicago Duan174665f2020-07-08 17:41:05 +080017#include "xyz/openbmc_project/PLDM/Event/server.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050018
19#include <err.h>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053020#include <getopt.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050021#include <poll.h>
22#include <stdlib.h>
23#include <sys/socket.h>
24#include <sys/types.h>
25#include <sys/un.h>
26#include <unistd.h>
27
George Liu6492f522020-06-16 10:34:05 +080028#include <sdeventplus/event.hpp>
29#include <sdeventplus/source/io.hpp>
30
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050031#include <cstdio>
32#include <cstring>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050033#include <fstream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050034#include <iomanip>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053035#include <iostream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050036#include <iterator>
Deepak Kodihallic682fe22020-03-04 00:42:54 -060037#include <memory>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050038#include <sstream>
Deepak Kodihallibc669f12019-11-28 08:52:07 -060039#include <stdexcept>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053040#include <string>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050041#include <vector>
42
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050043#ifdef OEM_IBM
44#include "libpldmresponder/file_io.hpp"
45#endif
46
47constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
48
Deepak Kodihallibc669f12019-11-28 08:52:07 -060049using namespace pldm::responder;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050050using namespace pldm;
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060051using namespace sdeventplus;
52using namespace sdeventplus::source;
George Liucae18662020-05-15 09:32:57 +080053using namespace pldm::state_sensor;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050054
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060055static Response processRxMsg(const std::vector<uint8_t>& requestMsg,
Deepak Kodihallibc669f12019-11-28 08:52:07 -060056 Invoker& invoker, dbus_api::Requester& requester)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050057{
58
59 Response response;
60 uint8_t eid = requestMsg[0];
61 uint8_t type = requestMsg[1];
62 pldm_header_info hdrFields{};
63 auto hdr = reinterpret_cast<const pldm_msg_hdr*>(
64 requestMsg.data() + sizeof(eid) + sizeof(type));
65 if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields))
66 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060067 std::cerr << "Empty PLDM request header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050068 }
69 else if (PLDM_RESPONSE != hdrFields.msg_type)
70 {
71 auto request = reinterpret_cast<const pldm_msg*>(hdr);
72 size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) -
73 sizeof(eid) - sizeof(type);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060074 try
75 {
76 response = invoker.handle(hdrFields.pldm_type, hdrFields.command,
77 request, requestLen);
78 }
79 catch (const std::out_of_range& e)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050080 {
81 uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
82 response.resize(sizeof(pldm_msg_hdr));
83 auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data());
84 pldm_header_info header{};
85 header.msg_type = PLDM_RESPONSE;
86 header.instance = hdrFields.instance;
87 header.pldm_type = hdrFields.pldm_type;
88 header.command = hdrFields.command;
89 auto result = pack_pldm_header(&header, responseHdr);
90 if (PLDM_SUCCESS != result)
91 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060092 std::cerr << "Failed adding response header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050093 }
94 response.insert(response.end(), completion_code);
95 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050096 }
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060097 else
98 {
99 requester.markFree(eid, hdr->instance_id);
100 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500101 return response;
102}
103
104void printBuffer(const std::vector<uint8_t>& buffer)
105{
106 std::ostringstream tempStream;
107 tempStream << "Buffer Data: ";
108 if (!buffer.empty())
109 {
110 for (int byte : buffer)
111 {
112 tempStream << std::setfill('0') << std::setw(2) << std::hex << byte
113 << " ";
114 }
115 }
Sampa Misraaa8ae722019-12-12 03:20:40 -0600116 std::cout << tempStream.str().c_str() << std::endl;
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();
168 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo(
169 pldm_pdr_init(), pldm_pdr_destroy);
170 std::unique_ptr<pldm_entity_association_tree,
171 decltype(&pldm_entity_association_tree_destroy)>
172 entityTree(pldm_entity_association_tree_init(),
173 pldm_entity_association_tree_destroy);
174 auto& bus = pldm::utils::DBusHandler::getBus();
175 dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm");
176 std::unique_ptr<HostPDRHandler> hostPDRHandler;
Sampa Misrac0c60542020-07-01 02:34:25 -0500177 std::unique_ptr<pldm::host_effecters::HostEffecterParser>
178 hostEffecterParser;
George Liucae18662020-05-15 09:32:57 +0800179 std::unique_ptr<DbusToPLDMEvent> dbusToPLDMEventHandler;
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500180 auto dbusHandler = std::make_unique<DBusHandler>();
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500181 auto hostEID = pldm::utils::readHostEID();
182 if (hostEID)
183 {
184 hostPDRHandler = std::make_unique<HostPDRHandler>(
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600185 sockfd, hostEID, event, pdrRepo.get(), EVENTS_JSONS_DIR,
186 entityTree.get(), dbusImplReq);
Sampa Misrac0c60542020-07-01 02:34:25 -0500187 hostEffecterParser =
188 std::make_unique<pldm::host_effecters::HostEffecterParser>(
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500189 &dbusImplReq, sockfd, pdrRepo.get(), dbusHandler.get(),
Sampa Misrac0c60542020-07-01 02:34:25 -0500190 HOST_JSONS_DIR, verbose);
George Liucae18662020-05-15 09:32:57 +0800191 dbusToPLDMEventHandler =
192 std::make_unique<DbusToPLDMEvent>(sockfd, hostEID, dbusImplReq);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500193 }
194
195 Invoker invoker{};
196 invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>());
Tom Joseph7f839f92020-09-21 10:20:44 +0530197 invoker.registerHandler(PLDM_BIOS, std::make_unique<bios::Handler>(
198 sockfd, hostEID, &dbusImplReq));
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530199 auto fruHandler = std::make_unique<fru::Handler>(
200 FRU_JSONS_DIR, pdrRepo.get(), entityTree.get());
201 // FRU table is built lazily when a FRU command or Get PDR command is
202 // handled. To enable building FRU table, the FRU handler is passed to the
203 // Platform handler.
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600204 invoker.registerHandler(
205 PLDM_PLATFORM, std::make_unique<platform::Handler>(
206 dbusHandler.get(), PDR_JSONS_DIR, pdrRepo.get(),
207 hostPDRHandler.get(), dbusToPLDMEventHandler.get(),
208 fruHandler.get(), true));
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530209 invoker.registerHandler(PLDM_FRU, std::move(fruHandler));
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500210
211#ifdef OEM_IBM
212 invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>());
213#endif
214
George Liu83409572019-12-24 18:42:54 +0800215 pldm::utils::CustomFD socketFd(sockfd);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500216
217 struct sockaddr_un addr
George Liu6492f522020-06-16 10:34:05 +0800218 {};
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500219 addr.sun_family = AF_UNIX;
220 const char path[] = "\0mctp-mux";
221 memcpy(addr.sun_path, path, sizeof(path) - 1);
222 int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr),
223 sizeof(path) + sizeof(addr.sun_family) - 1);
224 if (-1 == result)
225 {
226 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600227 std::cerr << "Failed to connect to the socket, RC= " << returnCode
228 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500229 exit(EXIT_FAILURE);
230 }
231
232 result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM));
233 if (-1 == result)
234 {
235 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600236 std::cerr << "Failed to send message type as pldm to mctp, RC= "
237 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500238 exit(EXIT_FAILURE);
239 }
240
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500241 dbus_api::Pdr dbusImplPdr(bus, "/xyz/openbmc_project/pldm", pdrRepo.get());
Chicago Duan174665f2020-07-08 17:41:05 +0800242 sdbusplus::xyz::openbmc_project::PLDM::server::Event dbusImplEvent(
243 bus, "/xyz/openbmc_project/pldm");
Deepak Kodihalli23c52042020-09-01 03:04:32 -0500244 auto callback = [verbose, &invoker, &dbusImplReq](IO& io, int fd,
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600245 uint32_t revents) {
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600246 if (!(revents & EPOLLIN))
247 {
248 return;
249 }
250
251 // Outgoing message.
252 struct iovec iov[2]{};
253
254 // This structure contains the parameter information for the response
255 // message.
256 struct msghdr msg
George Liu6492f522020-06-16 10:34:05 +0800257 {};
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600258
259 int returnCode = 0;
260 ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500261 if (0 == peekedLength)
262 {
Deepak Kodihalli23c52042020-09-01 03:04:32 -0500263 // MCTP daemon has closed the socket this daemon is connected to.
264 // This may or may not be an error scenario, in either case the
265 // recovery mechanism for this daemon is to restart, and hence exit
266 // the event loop, that will cause this daemon to exit with a
267 // failure code.
268 io.get_event().exit(0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500269 }
270 else if (peekedLength <= -1)
271 {
272 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600273 std::cerr << "recv system call failed, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500274 }
275 else
276 {
277 std::vector<uint8_t> requestMsg(peekedLength);
278 auto recvDataLength = recv(
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600279 fd, static_cast<void*>(requestMsg.data()), peekedLength, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500280 if (recvDataLength == peekedLength)
281 {
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530282 if (verbose)
283 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600284 std::cout << "Received Msg" << std::endl;
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530285 printBuffer(requestMsg);
286 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500287 if (MCTP_MSG_TYPE_PLDM != requestMsg[1])
288 {
289 // Skip this message and continue.
Sampa Misraaa8ae722019-12-12 03:20:40 -0600290 std::cerr << "Encountered Non-PLDM type message"
291 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500292 }
293 else
294 {
295 // process message and send response
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600296 auto response =
297 processRxMsg(requestMsg, invoker, dbusImplReq);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500298 if (!response.empty())
299 {
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500300 if (verbose)
301 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600302 std::cout << "Sending Msg" << std::endl;
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500303 printBuffer(response);
304 }
305
Zahed Hossain09a96e02019-08-06 07:42:37 -0500306 iov[0].iov_base = &requestMsg[0];
307 iov[0].iov_len =
308 sizeof(requestMsg[0]) + sizeof(requestMsg[1]);
309 iov[1].iov_base = response.data();
310 iov[1].iov_len = response.size();
311
312 msg.msg_iov = iov;
313 msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
314
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600315 int result = sendmsg(fd, &msg, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500316 if (-1 == result)
317 {
318 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600319 std::cerr << "sendto system call failed, RC= "
320 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500321 }
322 }
323 }
324 }
325 else
326 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600327 std::cerr
328 << "Failure to read peeked length packet. peekedLength= "
329 << peekedLength << " recvDataLength=" << recvDataLength
330 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500331 }
332 }
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600333 };
334
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600335 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
336 bus.request_name("xyz.openbmc_project.PLDM");
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600337 IO io(event, socketFd(), EPOLLIN, std::move(callback));
338 event.loop();
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500339
340 result = shutdown(sockfd, SHUT_RDWR);
341 if (-1 == result)
342 {
343 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600344 std::cerr << "Failed to shutdown the socket, RC=" << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500345 exit(EXIT_FAILURE);
346 }
347 exit(EXIT_FAILURE);
348}