blob: eb882f409337b3196983684588f79dbe781b7597 [file] [log] [blame]
Deepak Kodihalli4de4d002019-11-11 02:41:43 -06001#include "dbus_impl_requester.hpp"
Pavithra Barithaya51efaf82020-04-02 02:42:27 -05002#include "host_pdr_handler.hpp"
Deepak Kodihallibc669f12019-11-28 08:52:07 -06003#include "invoker.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -05004#include "libpldmresponder/base.hpp"
5#include "libpldmresponder/bios.hpp"
Deepak Kodihallie60c5822019-10-23 03:26:15 -05006#include "libpldmresponder/fru.hpp"
Sampa Misraa2fa0702019-05-31 01:28:55 -05007#include "libpldmresponder/platform.hpp"
George Liu83409572019-12-24 18:42:54 +08008#include "utils.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
19#include <cstdio>
20#include <cstring>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050021#include <fstream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050022#include <iomanip>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053023#include <iostream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050024#include <iterator>
Deepak Kodihallic682fe22020-03-04 00:42:54 -060025#include <memory>
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060026#include <sdeventplus/event.hpp>
27#include <sdeventplus/source/io.hpp>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050028#include <sstream>
Deepak Kodihallibc669f12019-11-28 08:52:07 -060029#include <stdexcept>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053030#include <string>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050031#include <vector>
32
33#include "libpldm/base.h"
34#include "libpldm/bios.h"
Deepak Kodihallic682fe22020-03-04 00:42:54 -060035#include "libpldm/pdr.h"
Sampa Misraa2fa0702019-05-31 01:28:55 -050036#include "libpldm/platform.h"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050037
38#ifdef OEM_IBM
39#include "libpldmresponder/file_io.hpp"
40#endif
41
42constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
43
Deepak Kodihallibc669f12019-11-28 08:52:07 -060044using namespace pldm::responder;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050045using namespace pldm;
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060046using namespace sdeventplus;
47using namespace sdeventplus::source;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050048
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060049static Response processRxMsg(const std::vector<uint8_t>& requestMsg,
Deepak Kodihallibc669f12019-11-28 08:52:07 -060050 Invoker& invoker, dbus_api::Requester& requester)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050051{
52
53 Response response;
54 uint8_t eid = requestMsg[0];
55 uint8_t type = requestMsg[1];
56 pldm_header_info hdrFields{};
57 auto hdr = reinterpret_cast<const pldm_msg_hdr*>(
58 requestMsg.data() + sizeof(eid) + sizeof(type));
59 if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields))
60 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060061 std::cerr << "Empty PLDM request header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050062 }
63 else if (PLDM_RESPONSE != hdrFields.msg_type)
64 {
65 auto request = reinterpret_cast<const pldm_msg*>(hdr);
66 size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) -
67 sizeof(eid) - sizeof(type);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060068 try
69 {
70 response = invoker.handle(hdrFields.pldm_type, hdrFields.command,
71 request, requestLen);
72 }
73 catch (const std::out_of_range& e)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050074 {
75 uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
76 response.resize(sizeof(pldm_msg_hdr));
77 auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data());
78 pldm_header_info header{};
79 header.msg_type = PLDM_RESPONSE;
80 header.instance = hdrFields.instance;
81 header.pldm_type = hdrFields.pldm_type;
82 header.command = hdrFields.command;
83 auto result = pack_pldm_header(&header, responseHdr);
84 if (PLDM_SUCCESS != result)
85 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060086 std::cerr << "Failed adding response header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050087 }
88 response.insert(response.end(), completion_code);
89 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050090 }
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060091 else
92 {
93 requester.markFree(eid, hdr->instance_id);
94 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050095 return response;
96}
97
98void printBuffer(const std::vector<uint8_t>& buffer)
99{
100 std::ostringstream tempStream;
101 tempStream << "Buffer Data: ";
102 if (!buffer.empty())
103 {
104 for (int byte : buffer)
105 {
106 tempStream << std::setfill('0') << std::setw(2) << std::hex << byte
107 << " ";
108 }
109 }
Sampa Misraaa8ae722019-12-12 03:20:40 -0600110 std::cout << tempStream.str().c_str() << std::endl;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500111}
112
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530113void optionUsage(void)
114{
115 std::cerr << "Usage: pldmd [options]\n";
116 std::cerr << "Options:\n";
117 std::cerr
118 << " --verbose=<0/1> 0 - Disable verbosity, 1 - Enable verbosity\n";
119 std::cerr << "Defaulted settings: --verbose=0 \n";
120}
121
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500122int main(int argc, char** argv)
123{
124
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530125 bool verbose = false;
126 static struct option long_options[] = {
127 {"verbose", required_argument, 0, 'v'}, {0, 0, 0, 0}};
128
129 auto argflag = getopt_long(argc, argv, "v:", long_options, nullptr);
130 switch (argflag)
131 {
132 case 'v':
133 switch (std::stoi(optarg))
134 {
135 case 0:
136 verbose = false;
137 break;
138 case 1:
139 verbose = true;
140 break;
141 default:
142 optionUsage();
143 break;
144 }
145 break;
146 default:
147 optionUsage();
148 break;
149 }
150
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500151 /* Create local socket. */
152 int returnCode = 0;
153 int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
154 if (-1 == sockfd)
155 {
156 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600157 std::cerr << "Failed to create the socket, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500158 exit(EXIT_FAILURE);
159 }
160
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500161 auto event = Event::get_default();
162 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo(
163 pldm_pdr_init(), pldm_pdr_destroy);
164 std::unique_ptr<pldm_entity_association_tree,
165 decltype(&pldm_entity_association_tree_destroy)>
166 entityTree(pldm_entity_association_tree_init(),
167 pldm_entity_association_tree_destroy);
168 auto& bus = pldm::utils::DBusHandler::getBus();
169 dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm");
170 std::unique_ptr<HostPDRHandler> hostPDRHandler;
171 auto hostEID = pldm::utils::readHostEID();
172 if (hostEID)
173 {
174 hostPDRHandler = std::make_unique<HostPDRHandler>(
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500175 sockfd, hostEID, event, pdrRepo.get(), entityTree.get(),
176 dbusImplReq);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500177 }
178
179 Invoker invoker{};
180 invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>());
181 invoker.registerHandler(PLDM_BIOS, std::make_unique<bios::Handler>());
182 invoker.registerHandler(
183 PLDM_PLATFORM, std::make_unique<platform::Handler>(
184 PDR_JSONS_DIR, pdrRepo.get(), hostPDRHandler.get()));
185 invoker.registerHandler(
186 PLDM_FRU, std::make_unique<fru::Handler>(FRU_JSONS_DIR, pdrRepo.get(),
187 entityTree.get()));
188
189#ifdef OEM_IBM
190 invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>());
191#endif
192
George Liu83409572019-12-24 18:42:54 +0800193 pldm::utils::CustomFD socketFd(sockfd);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500194
195 struct sockaddr_un addr
196 {
197 };
198 addr.sun_family = AF_UNIX;
199 const char path[] = "\0mctp-mux";
200 memcpy(addr.sun_path, path, sizeof(path) - 1);
201 int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr),
202 sizeof(path) + sizeof(addr.sun_family) - 1);
203 if (-1 == result)
204 {
205 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600206 std::cerr << "Failed to connect to the socket, RC= " << returnCode
207 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500208 exit(EXIT_FAILURE);
209 }
210
211 result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM));
212 if (-1 == result)
213 {
214 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600215 std::cerr << "Failed to send message type as pldm to mctp, RC= "
216 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500217 exit(EXIT_FAILURE);
218 }
219
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600220 auto callback = [verbose, &invoker, &dbusImplReq](IO& /*io*/, int fd,
221 uint32_t revents) {
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600222 if (!(revents & EPOLLIN))
223 {
224 return;
225 }
226
227 // Outgoing message.
228 struct iovec iov[2]{};
229
230 // This structure contains the parameter information for the response
231 // message.
232 struct msghdr msg
233 {
234 };
235
236 int returnCode = 0;
237 ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500238 if (0 == peekedLength)
239 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600240 std::cerr << "Socket has been closed \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500241 }
242 else if (peekedLength <= -1)
243 {
244 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600245 std::cerr << "recv system call failed, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500246 }
247 else
248 {
249 std::vector<uint8_t> requestMsg(peekedLength);
250 auto recvDataLength = recv(
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600251 fd, static_cast<void*>(requestMsg.data()), peekedLength, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500252 if (recvDataLength == peekedLength)
253 {
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530254 if (verbose)
255 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600256 std::cout << "Received Msg" << std::endl;
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530257 printBuffer(requestMsg);
258 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500259 if (MCTP_MSG_TYPE_PLDM != requestMsg[1])
260 {
261 // Skip this message and continue.
Sampa Misraaa8ae722019-12-12 03:20:40 -0600262 std::cerr << "Encountered Non-PLDM type message"
263 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500264 }
265 else
266 {
267 // process message and send response
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600268 auto response =
269 processRxMsg(requestMsg, invoker, dbusImplReq);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500270 if (!response.empty())
271 {
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500272 if (verbose)
273 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600274 std::cout << "Sending Msg" << std::endl;
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500275 printBuffer(response);
276 }
277
Zahed Hossain09a96e02019-08-06 07:42:37 -0500278 iov[0].iov_base = &requestMsg[0];
279 iov[0].iov_len =
280 sizeof(requestMsg[0]) + sizeof(requestMsg[1]);
281 iov[1].iov_base = response.data();
282 iov[1].iov_len = response.size();
283
284 msg.msg_iov = iov;
285 msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
286
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600287 int result = sendmsg(fd, &msg, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500288 if (-1 == result)
289 {
290 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600291 std::cerr << "sendto system call failed, RC= "
292 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500293 }
294 }
295 }
296 }
297 else
298 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600299 std::cerr
300 << "Failure to read peeked length packet. peekedLength= "
301 << peekedLength << " recvDataLength=" << recvDataLength
302 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500303 }
304 }
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600305 };
306
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600307 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
308 bus.request_name("xyz.openbmc_project.PLDM");
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600309 IO io(event, socketFd(), EPOLLIN, std::move(callback));
310 event.loop();
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500311
312 result = shutdown(sockfd, SHUT_RDWR);
313 if (-1 == result)
314 {
315 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600316 std::cerr << "Failed to shutdown the socket, RC=" << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500317 exit(EXIT_FAILURE);
318 }
319 exit(EXIT_FAILURE);
320}