xref: /openbmc/pldm/pldmd/pldmd.cpp (revision a4a96162)
1 #include "libpldm/base.h"
2 #include "libpldm/bios.h"
3 #include "libpldm/pdr.h"
4 #include "libpldm/platform.h"
5 
6 #include "common/utils.hpp"
7 #include "dbus_impl_pdr.hpp"
8 #include "dbus_impl_requester.hpp"
9 #include "host-bmc/dbus_to_host_effecters.hpp"
10 #include "host-bmc/host_pdr_handler.hpp"
11 #include "invoker.hpp"
12 #include "libpldmresponder/base.hpp"
13 #include "libpldmresponder/bios.hpp"
14 #include "libpldmresponder/fru.hpp"
15 #include "libpldmresponder/platform.hpp"
16 #include "xyz/openbmc_project/PLDM/Event/server.hpp"
17 
18 #include <err.h>
19 #include <getopt.h>
20 #include <poll.h>
21 #include <stdlib.h>
22 #include <sys/socket.h>
23 #include <sys/types.h>
24 #include <sys/un.h>
25 #include <unistd.h>
26 
27 #include <sdeventplus/event.hpp>
28 #include <sdeventplus/source/io.hpp>
29 
30 #include <cstdio>
31 #include <cstring>
32 #include <fstream>
33 #include <iomanip>
34 #include <iostream>
35 #include <iterator>
36 #include <memory>
37 #include <sstream>
38 #include <stdexcept>
39 #include <string>
40 #include <vector>
41 
42 #ifdef OEM_IBM
43 #include "libpldmresponder/file_io.hpp"
44 #endif
45 
46 constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
47 
48 using namespace pldm::responder;
49 using namespace pldm;
50 using namespace sdeventplus;
51 using namespace sdeventplus::source;
52 
53 static Response processRxMsg(const std::vector<uint8_t>& requestMsg,
54                              Invoker& invoker, dbus_api::Requester& requester)
55 {
56 
57     Response response;
58     uint8_t eid = requestMsg[0];
59     uint8_t type = requestMsg[1];
60     pldm_header_info hdrFields{};
61     auto hdr = reinterpret_cast<const pldm_msg_hdr*>(
62         requestMsg.data() + sizeof(eid) + sizeof(type));
63     if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields))
64     {
65         std::cerr << "Empty PLDM request header \n";
66     }
67     else if (PLDM_RESPONSE != hdrFields.msg_type)
68     {
69         auto request = reinterpret_cast<const pldm_msg*>(hdr);
70         size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) -
71                             sizeof(eid) - sizeof(type);
72         try
73         {
74             response = invoker.handle(hdrFields.pldm_type, hdrFields.command,
75                                       request, requestLen);
76         }
77         catch (const std::out_of_range& e)
78         {
79             uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
80             response.resize(sizeof(pldm_msg_hdr));
81             auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data());
82             pldm_header_info header{};
83             header.msg_type = PLDM_RESPONSE;
84             header.instance = hdrFields.instance;
85             header.pldm_type = hdrFields.pldm_type;
86             header.command = hdrFields.command;
87             auto result = pack_pldm_header(&header, responseHdr);
88             if (PLDM_SUCCESS != result)
89             {
90                 std::cerr << "Failed adding response header \n";
91             }
92             response.insert(response.end(), completion_code);
93         }
94     }
95     else
96     {
97         requester.markFree(eid, hdr->instance_id);
98     }
99     return response;
100 }
101 
102 void printBuffer(const std::vector<uint8_t>& buffer)
103 {
104     std::ostringstream tempStream;
105     tempStream << "Buffer Data: ";
106     if (!buffer.empty())
107     {
108         for (int byte : buffer)
109         {
110             tempStream << std::setfill('0') << std::setw(2) << std::hex << byte
111                        << " ";
112         }
113     }
114     std::cout << tempStream.str().c_str() << std::endl;
115 }
116 
117 void optionUsage(void)
118 {
119     std::cerr << "Usage: pldmd [options]\n";
120     std::cerr << "Options:\n";
121     std::cerr
122         << "  --verbose=<0/1>  0 - Disable verbosity, 1 - Enable verbosity\n";
123     std::cerr << "Defaulted settings:  --verbose=0 \n";
124 }
125 
126 int main(int argc, char** argv)
127 {
128 
129     bool verbose = false;
130     static struct option long_options[] = {
131         {"verbose", required_argument, 0, 'v'}, {0, 0, 0, 0}};
132 
133     auto argflag = getopt_long(argc, argv, "v:", long_options, nullptr);
134     switch (argflag)
135     {
136         case 'v':
137             switch (std::stoi(optarg))
138             {
139                 case 0:
140                     verbose = false;
141                     break;
142                 case 1:
143                     verbose = true;
144                     break;
145                 default:
146                     optionUsage();
147                     break;
148             }
149             break;
150         default:
151             optionUsage();
152             break;
153     }
154 
155     /* Create local socket. */
156     int returnCode = 0;
157     int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
158     if (-1 == sockfd)
159     {
160         returnCode = -errno;
161         std::cerr << "Failed to create the socket, RC= " << returnCode << "\n";
162         exit(EXIT_FAILURE);
163     }
164 
165     auto event = Event::get_default();
166     std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo(
167         pldm_pdr_init(), pldm_pdr_destroy);
168     std::unique_ptr<pldm_entity_association_tree,
169                     decltype(&pldm_entity_association_tree_destroy)>
170         entityTree(pldm_entity_association_tree_init(),
171                    pldm_entity_association_tree_destroy);
172     auto& bus = pldm::utils::DBusHandler::getBus();
173     dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm");
174     std::unique_ptr<HostPDRHandler> hostPDRHandler;
175     std::unique_ptr<pldm::host_effecters::HostEffecterParser>
176         hostEffecterParser;
177     auto dbusHandler = std::make_unique<DBusHandler>();
178     auto hostEID = pldm::utils::readHostEID();
179     if (hostEID)
180     {
181         hostPDRHandler = std::make_unique<HostPDRHandler>(
182             sockfd, hostEID, event, pdrRepo.get(), entityTree.get(),
183             dbusImplReq);
184         hostEffecterParser =
185             std::make_unique<pldm::host_effecters::HostEffecterParser>(
186                 &dbusImplReq, sockfd, pdrRepo.get(), dbusHandler.get(),
187                 HOST_JSONS_DIR, verbose);
188     }
189 
190     Invoker invoker{};
191     invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>());
192     invoker.registerHandler(PLDM_BIOS, std::make_unique<bios::Handler>());
193     auto fruHandler = std::make_unique<fru::Handler>(
194         FRU_JSONS_DIR, pdrRepo.get(), entityTree.get());
195     // FRU table is built lazily when a FRU command or Get PDR command is
196     // handled. To enable building FRU table, the FRU handler is passed to the
197     // Platform handler.
198     invoker.registerHandler(PLDM_PLATFORM,
199                             std::make_unique<platform::Handler>(
200                                 dbusHandler.get(), PDR_JSONS_DIR,
201                                 EVENTS_JSONS_DIR, pdrRepo.get(),
202                                 hostPDRHandler.get(), fruHandler.get(), true));
203     invoker.registerHandler(PLDM_FRU, std::move(fruHandler));
204 
205 #ifdef OEM_IBM
206     invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>());
207 #endif
208 
209     pldm::utils::CustomFD socketFd(sockfd);
210 
211     struct sockaddr_un addr
212     {};
213     addr.sun_family = AF_UNIX;
214     const char path[] = "\0mctp-mux";
215     memcpy(addr.sun_path, path, sizeof(path) - 1);
216     int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr),
217                          sizeof(path) + sizeof(addr.sun_family) - 1);
218     if (-1 == result)
219     {
220         returnCode = -errno;
221         std::cerr << "Failed to connect to the socket, RC= " << returnCode
222                   << "\n";
223         exit(EXIT_FAILURE);
224     }
225 
226     result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM));
227     if (-1 == result)
228     {
229         returnCode = -errno;
230         std::cerr << "Failed to send message type as pldm to mctp, RC= "
231                   << returnCode << "\n";
232         exit(EXIT_FAILURE);
233     }
234 
235     dbus_api::Pdr dbusImplPdr(bus, "/xyz/openbmc_project/pldm", pdrRepo.get());
236     sdbusplus::xyz::openbmc_project::PLDM::server::Event dbusImplEvent(
237         bus, "/xyz/openbmc_project/pldm");
238     auto callback = [verbose, &invoker, &dbusImplReq](IO& /*io*/, int fd,
239                                                       uint32_t revents) {
240         if (!(revents & EPOLLIN))
241         {
242             return;
243         }
244 
245         // Outgoing message.
246         struct iovec iov[2]{};
247 
248         // This structure contains the parameter information for the response
249         // message.
250         struct msghdr msg
251         {};
252 
253         int returnCode = 0;
254         ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC);
255         if (0 == peekedLength)
256         {
257             std::cerr << "Socket has been closed \n";
258         }
259         else if (peekedLength <= -1)
260         {
261             returnCode = -errno;
262             std::cerr << "recv system call failed, RC= " << returnCode << "\n";
263         }
264         else
265         {
266             std::vector<uint8_t> requestMsg(peekedLength);
267             auto recvDataLength = recv(
268                 fd, static_cast<void*>(requestMsg.data()), peekedLength, 0);
269             if (recvDataLength == peekedLength)
270             {
271                 if (verbose)
272                 {
273                     std::cout << "Received Msg" << std::endl;
274                     printBuffer(requestMsg);
275                 }
276                 if (MCTP_MSG_TYPE_PLDM != requestMsg[1])
277                 {
278                     // Skip this message and continue.
279                     std::cerr << "Encountered Non-PLDM type message"
280                               << "\n";
281                 }
282                 else
283                 {
284                     // process message and send response
285                     auto response =
286                         processRxMsg(requestMsg, invoker, dbusImplReq);
287                     if (!response.empty())
288                     {
289                         if (verbose)
290                         {
291                             std::cout << "Sending Msg" << std::endl;
292                             printBuffer(response);
293                         }
294 
295                         iov[0].iov_base = &requestMsg[0];
296                         iov[0].iov_len =
297                             sizeof(requestMsg[0]) + sizeof(requestMsg[1]);
298                         iov[1].iov_base = response.data();
299                         iov[1].iov_len = response.size();
300 
301                         msg.msg_iov = iov;
302                         msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
303 
304                         int result = sendmsg(fd, &msg, 0);
305                         if (-1 == result)
306                         {
307                             returnCode = -errno;
308                             std::cerr << "sendto system call failed, RC= "
309                                       << returnCode << "\n";
310                         }
311                     }
312                 }
313             }
314             else
315             {
316                 std::cerr
317                     << "Failure to read peeked length packet. peekedLength= "
318                     << peekedLength << " recvDataLength=" << recvDataLength
319                     << "\n";
320             }
321         }
322     };
323 
324     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
325     bus.request_name("xyz.openbmc_project.PLDM");
326     IO io(event, socketFd(), EPOLLIN, std::move(callback));
327     event.loop();
328 
329     result = shutdown(sockfd, SHUT_RDWR);
330     if (-1 == result)
331     {
332         returnCode = -errno;
333         std::cerr << "Failed to shutdown the socket, RC=" << returnCode << "\n";
334         exit(EXIT_FAILURE);
335     }
336     exit(EXIT_FAILURE);
337 }
338