blob: 1b37726e3458c5b7313755c80eaa316d81b77d0f [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"
Sampa Misraa2fa0702019-05-31 01:28:55 -05005#include "libpldmresponder/platform.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -05006#include "libpldmresponder/utils.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -05007
8#include <err.h>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +05309#include <getopt.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050010#include <poll.h>
11#include <stdlib.h>
12#include <sys/socket.h>
13#include <sys/types.h>
14#include <sys/un.h>
15#include <unistd.h>
16
17#include <cstdio>
18#include <cstring>
19#include <iomanip>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053020#include <iostream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050021#include <iterator>
22#include <phosphor-logging/log.hpp>
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 phosphor::logging;
42using namespace pldm;
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060043using namespace sdeventplus;
44using namespace sdeventplus::source;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050045
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060046static Response processRxMsg(const std::vector<uint8_t>& requestMsg,
Deepak Kodihallibc669f12019-11-28 08:52:07 -060047 Invoker& invoker, dbus_api::Requester& requester)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050048{
49
50 Response response;
51 uint8_t eid = requestMsg[0];
52 uint8_t type = requestMsg[1];
53 pldm_header_info hdrFields{};
54 auto hdr = reinterpret_cast<const pldm_msg_hdr*>(
55 requestMsg.data() + sizeof(eid) + sizeof(type));
56 if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields))
57 {
58 log<level::ERR>("Empty PLDM request header");
59 }
60 else if (PLDM_RESPONSE != hdrFields.msg_type)
61 {
62 auto request = reinterpret_cast<const pldm_msg*>(hdr);
63 size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) -
64 sizeof(eid) - sizeof(type);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060065 try
66 {
67 response = invoker.handle(hdrFields.pldm_type, hdrFields.command,
68 request, requestLen);
69 }
70 catch (const std::out_of_range& e)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050071 {
72 uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
73 response.resize(sizeof(pldm_msg_hdr));
74 auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data());
75 pldm_header_info header{};
76 header.msg_type = PLDM_RESPONSE;
77 header.instance = hdrFields.instance;
78 header.pldm_type = hdrFields.pldm_type;
79 header.command = hdrFields.command;
80 auto result = pack_pldm_header(&header, responseHdr);
81 if (PLDM_SUCCESS != result)
82 {
83 log<level::ERR>("Failed adding response header");
84 }
85 response.insert(response.end(), completion_code);
86 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050087 }
Deepak Kodihalli4de4d002019-11-11 02:41:43 -060088 else
89 {
90 requester.markFree(eid, hdr->instance_id);
91 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050092 return response;
93}
94
95void printBuffer(const std::vector<uint8_t>& buffer)
96{
97 std::ostringstream tempStream;
98 tempStream << "Buffer Data: ";
99 if (!buffer.empty())
100 {
101 for (int byte : buffer)
102 {
103 tempStream << std::setfill('0') << std::setw(2) << std::hex << byte
104 << " ";
105 }
106 }
107 log<level::INFO>(tempStream.str().c_str());
108}
109
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530110void optionUsage(void)
111{
112 std::cerr << "Usage: pldmd [options]\n";
113 std::cerr << "Options:\n";
114 std::cerr
115 << " --verbose=<0/1> 0 - Disable verbosity, 1 - Enable verbosity\n";
116 std::cerr << "Defaulted settings: --verbose=0 \n";
117}
118
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500119int main(int argc, char** argv)
120{
121
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530122 bool verbose = false;
123 static struct option long_options[] = {
124 {"verbose", required_argument, 0, 'v'}, {0, 0, 0, 0}};
125
126 auto argflag = getopt_long(argc, argv, "v:", long_options, nullptr);
127 switch (argflag)
128 {
129 case 'v':
130 switch (std::stoi(optarg))
131 {
132 case 0:
133 verbose = false;
134 break;
135 case 1:
136 verbose = true;
137 break;
138 default:
139 optionUsage();
140 break;
141 }
142 break;
143 default:
144 optionUsage();
145 break;
146 }
147
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600148 Invoker invoker{};
149 invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>());
150 invoker.registerHandler(PLDM_BIOS, std::make_unique<bios::Handler>());
151 invoker.registerHandler(PLDM_PLATFORM,
152 std::make_unique<platform::Handler>());
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500153
154#ifdef OEM_IBM
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600155 invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>());
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500156#endif
157
158 /* Create local socket. */
159 int returnCode = 0;
160 int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
161 if (-1 == sockfd)
162 {
163 returnCode = -errno;
164 log<level::ERR>("Failed to create the socket",
165 entry("RC=%d", returnCode));
166 exit(EXIT_FAILURE);
167 }
168
169 responder::utils::CustomFD socketFd(sockfd);
170
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;
182 log<level::ERR>("Failed to connect to the socket",
183 entry("RC=%d", returnCode));
184 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;
191 log<level::ERR>("Failed to send message type as pldm to mctp",
192 entry("RC=%d", returnCode));
193 exit(EXIT_FAILURE);
194 }
195
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600196 auto bus = sdbusplus::bus::new_default();
197 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 {
218 log<level::ERR>("Socket has been closed");
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500219 }
220 else if (peekedLength <= -1)
221 {
222 returnCode = -errno;
223 log<level::ERR>("recv system call failed",
224 entry("RC=%d", returnCode));
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500225 }
226 else
227 {
228 std::vector<uint8_t> requestMsg(peekedLength);
229 auto recvDataLength = recv(
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600230 fd, static_cast<void*>(requestMsg.data()), peekedLength, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500231 if (recvDataLength == peekedLength)
232 {
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530233 if (verbose)
234 {
235 log<level::INFO>("Received Msg ",
236 entry("LENGTH=%zu", recvDataLength),
237 entry("EID=0x%02x", requestMsg[0]),
238 entry("TYPE=0x%02x", requestMsg[1]));
239 printBuffer(requestMsg);
240 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500241 if (MCTP_MSG_TYPE_PLDM != requestMsg[1])
242 {
243 // Skip this message and continue.
244 log<level::ERR>("Encountered Non-PLDM type message",
245 entry("TYPE=0x%02x", requestMsg[1]));
246 }
247 else
248 {
249 // process message and send response
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600250 auto response =
251 processRxMsg(requestMsg, invoker, dbusImplReq);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500252 if (!response.empty())
253 {
Deepak Kodihalli8ffbbe02019-08-14 06:51:38 -0500254 if (verbose)
255 {
256 log<level::INFO>("Sending Msg ");
257 printBuffer(response);
258 }
259
Zahed Hossain09a96e02019-08-06 07:42:37 -0500260 iov[0].iov_base = &requestMsg[0];
261 iov[0].iov_len =
262 sizeof(requestMsg[0]) + sizeof(requestMsg[1]);
263 iov[1].iov_base = response.data();
264 iov[1].iov_len = response.size();
265
266 msg.msg_iov = iov;
267 msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
268
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600269 int result = sendmsg(fd, &msg, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500270 if (-1 == result)
271 {
272 returnCode = -errno;
273 log<level::ERR>("sendto system call failed",
274 entry("RC=%d", returnCode));
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500275 }
276 }
277 }
278 }
279 else
280 {
281 log<level::ERR>("Failure to read peeked length packet",
282 entry("PEEKED_LENGTH=%zu", peekedLength),
283 entry("READ_LENGTH=%zu", recvDataLength));
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500284 }
285 }
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600286 };
287
288 auto event = Event::get_default();
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600289 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
290 bus.request_name("xyz.openbmc_project.PLDM");
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600291 IO io(event, socketFd(), EPOLLIN, std::move(callback));
292 event.loop();
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500293
294 result = shutdown(sockfd, SHUT_RDWR);
295 if (-1 == result)
296 {
297 returnCode = -errno;
298 log<level::ERR>("Failed to shutdown the socket",
299 entry("RC=%d", returnCode));
300 exit(EXIT_FAILURE);
301 }
302 exit(EXIT_FAILURE);
303}