blob: a13383dee3f98977e34461a5a09e5b2af909f7c3 [file] [log] [blame]
Deepak Kodihalli4de4d002019-11-11 02:41:43 -06001#include "dbus_impl_requester.hpp"
Deepak Kodihallibc669f12019-11-28 08:52:07 -06002#include "invoker.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -05003#include "libpldmresponder/base.hpp"
4#include "libpldmresponder/bios.hpp"
Deepak Kodihallie60c5822019-10-23 03:26:15 -05005#include "libpldmresponder/fru.hpp"
Sampa Misraa2fa0702019-05-31 01:28:55 -05006#include "libpldmresponder/platform.hpp"
George Liu83409572019-12-24 18:42:54 +08007#include "utils.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -05008
9#include <err.h>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053010#include <getopt.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050011#include <poll.h>
12#include <stdlib.h>
13#include <sys/socket.h>
14#include <sys/types.h>
15#include <sys/un.h>
16#include <unistd.h>
17
18#include <cstdio>
19#include <cstring>
20#include <iomanip>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053021#include <iostream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050022#include <iterator>
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060023#include <sdeventplus/event.hpp>
24#include <sdeventplus/source/io.hpp>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050025#include <sstream>
Deepak Kodihallibc669f12019-11-28 08:52:07 -060026#include <stdexcept>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053027#include <string>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050028#include <vector>
29
30#include "libpldm/base.h"
31#include "libpldm/bios.h"
Sampa Misraa2fa0702019-05-31 01:28:55 -050032#include "libpldm/platform.h"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050033
34#ifdef OEM_IBM
35#include "libpldmresponder/file_io.hpp"
36#endif
37
38constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
39
Deepak Kodihallibc669f12019-11-28 08:52:07 -060040using namespace pldm::responder;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050041using namespace pldm;
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060042using namespace sdeventplus;
43using namespace sdeventplus::source;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050044
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060045static Response processRxMsg(const std::vector<uint8_t>& requestMsg,
Deepak Kodihallibc669f12019-11-28 08:52:07 -060046 Invoker& invoker, dbus_api::Requester& requester)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050047{
48
49 Response response;
50 uint8_t eid = requestMsg[0];
51 uint8_t type = requestMsg[1];
52 pldm_header_info hdrFields{};
53 auto hdr = reinterpret_cast<const pldm_msg_hdr*>(
54 requestMsg.data() + sizeof(eid) + sizeof(type));
55 if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields))
56 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060057 std::cerr << "Empty PLDM request header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050058 }
59 else if (PLDM_RESPONSE != hdrFields.msg_type)
60 {
61 auto request = reinterpret_cast<const pldm_msg*>(hdr);
62 size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) -
63 sizeof(eid) - sizeof(type);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060064 try
65 {
66 response = invoker.handle(hdrFields.pldm_type, hdrFields.command,
67 request, requestLen);
68 }
69 catch (const std::out_of_range& e)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050070 {
71 uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
72 response.resize(sizeof(pldm_msg_hdr));
73 auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data());
74 pldm_header_info header{};
75 header.msg_type = PLDM_RESPONSE;
76 header.instance = hdrFields.instance;
77 header.pldm_type = hdrFields.pldm_type;
78 header.command = hdrFields.command;
79 auto result = pack_pldm_header(&header, responseHdr);
80 if (PLDM_SUCCESS != result)
81 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060082 std::cerr << "Failed adding response header \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050083 }
84 response.insert(response.end(), completion_code);
85 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050086 }
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060087 else
88 {
89 requester.markFree(eid, hdr->instance_id);
90 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050091 return response;
92}
93
94void printBuffer(const std::vector<uint8_t>& buffer)
95{
96 std::ostringstream tempStream;
97 tempStream << "Buffer Data: ";
98 if (!buffer.empty())
99 {
100 for (int byte : buffer)
101 {
102 tempStream << std::setfill('0') << std::setw(2) << std::hex << byte
103 << " ";
104 }
105 }
Sampa Misraaa8ae722019-12-12 03:20:40 -0600106 std::cout << tempStream.str().c_str() << std::endl;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500107}
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
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600147 Invoker invoker{};
148 invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>());
149 invoker.registerHandler(PLDM_BIOS, std::make_unique<bios::Handler>());
150 invoker.registerHandler(PLDM_PLATFORM,
151 std::make_unique<platform::Handler>());
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500152 invoker.registerHandler(PLDM_FRU,
153 std::make_unique<fru::Handler>(FRU_JSONS_DIR));
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500154
155#ifdef OEM_IBM
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600156 invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>());
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500157#endif
158
159 /* Create local socket. */
160 int returnCode = 0;
161 int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
162 if (-1 == sockfd)
163 {
164 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600165 std::cerr << "Failed to create the socket, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500166 exit(EXIT_FAILURE);
167 }
168
George Liu83409572019-12-24 18:42:54 +0800169 pldm::utils::CustomFD socketFd(sockfd);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500170
171 struct sockaddr_un addr
172 {
173 };
174 addr.sun_family = AF_UNIX;
175 const char path[] = "\0mctp-mux";
176 memcpy(addr.sun_path, path, sizeof(path) - 1);
177 int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr),
178 sizeof(path) + sizeof(addr.sun_family) - 1);
179 if (-1 == result)
180 {
181 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600182 std::cerr << "Failed to connect to the socket, RC= " << returnCode
183 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500184 exit(EXIT_FAILURE);
185 }
186
187 result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM));
188 if (-1 == result)
189 {
190 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600191 std::cerr << "Failed to send message type as pldm to mctp, RC= "
192 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500193 exit(EXIT_FAILURE);
194 }
195
George Liu0e02c322020-01-01 09:41:51 +0800196 auto& bus = pldm::utils::DBusHandler::getBus();
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600197 dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm");
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600198 auto callback = [verbose, &invoker, &dbusImplReq](IO& /*io*/, int fd,
199 uint32_t revents) {
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600200 if (!(revents & EPOLLIN))
201 {
202 return;
203 }
204
205 // Outgoing message.
206 struct iovec iov[2]{};
207
208 // This structure contains the parameter information for the response
209 // message.
210 struct msghdr msg
211 {
212 };
213
214 int returnCode = 0;
215 ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500216 if (0 == peekedLength)
217 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600218 std::cerr << "Socket has been closed \n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500219 }
220 else if (peekedLength <= -1)
221 {
222 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600223 std::cerr << "recv system call failed, RC= " << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500224 }
225 else
226 {
227 std::vector<uint8_t> requestMsg(peekedLength);
228 auto recvDataLength = recv(
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600229 fd, static_cast<void*>(requestMsg.data()), peekedLength, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500230 if (recvDataLength == peekedLength)
231 {
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530232 if (verbose)
233 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600234 std::cout << "Received Msg" << std::endl;
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530235 printBuffer(requestMsg);
236 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500237 if (MCTP_MSG_TYPE_PLDM != requestMsg[1])
238 {
239 // Skip this message and continue.
Sampa Misraaa8ae722019-12-12 03:20:40 -0600240 std::cerr << "Encountered Non-PLDM type message"
241 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500242 }
243 else
244 {
245 // process message and send response
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600246 auto response =
247 processRxMsg(requestMsg, invoker, dbusImplReq);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500248 if (!response.empty())
249 {
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500250 if (verbose)
251 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600252 std::cout << "Sending Msg" << std::endl;
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500253 printBuffer(response);
254 }
255
Zahed Hossain09a96e02019-08-06 07:42:37 -0500256 iov[0].iov_base = &requestMsg[0];
257 iov[0].iov_len =
258 sizeof(requestMsg[0]) + sizeof(requestMsg[1]);
259 iov[1].iov_base = response.data();
260 iov[1].iov_len = response.size();
261
262 msg.msg_iov = iov;
263 msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
264
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600265 int result = sendmsg(fd, &msg, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500266 if (-1 == result)
267 {
268 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600269 std::cerr << "sendto system call failed, RC= "
270 << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500271 }
272 }
273 }
274 }
275 else
276 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600277 std::cerr
278 << "Failure to read peeked length packet. peekedLength= "
279 << peekedLength << " recvDataLength=" << recvDataLength
280 << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500281 }
282 }
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600283 };
284
285 auto event = Event::get_default();
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600286 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
287 bus.request_name("xyz.openbmc_project.PLDM");
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600288 IO io(event, socketFd(), EPOLLIN, std::move(callback));
289 event.loop();
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500290
291 result = shutdown(sockfd, SHUT_RDWR);
292 if (-1 == result)
293 {
294 returnCode = -errno;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600295 std::cerr << "Failed to shutdown the socket, RC=" << returnCode << "\n";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500296 exit(EXIT_FAILURE);
297 }
298 exit(EXIT_FAILURE);
299}