Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 1 | #include "dbus_impl_requester.hpp" |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 2 | #include "invoker.hpp" |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 3 | #include "libpldmresponder/base.hpp" |
| 4 | #include "libpldmresponder/bios.hpp" |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 5 | #include "libpldmresponder/platform.hpp" |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 6 | #include "libpldmresponder/utils.hpp" |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 7 | |
| 8 | #include <err.h> |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 9 | #include <getopt.h> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 10 | #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 Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 20 | #include <iostream> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 21 | #include <iterator> |
| 22 | #include <phosphor-logging/log.hpp> |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 23 | #include <sdeventplus/event.hpp> |
| 24 | #include <sdeventplus/source/io.hpp> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 25 | #include <sstream> |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 26 | #include <stdexcept> |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 27 | #include <string> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
| 30 | #include "libpldm/base.h" |
| 31 | #include "libpldm/bios.h" |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 32 | #include "libpldm/platform.h" |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 33 | |
| 34 | #ifdef OEM_IBM |
| 35 | #include "libpldmresponder/file_io.hpp" |
| 36 | #endif |
| 37 | |
| 38 | constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1; |
| 39 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 40 | using namespace pldm::responder; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 41 | using namespace phosphor::logging; |
| 42 | using namespace pldm; |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 43 | using namespace sdeventplus; |
| 44 | using namespace sdeventplus::source; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 45 | |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 46 | static Response processRxMsg(const std::vector<uint8_t>& requestMsg, |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 47 | Invoker& invoker, dbus_api::Requester& requester) |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 48 | { |
| 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 Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 65 | 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 Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 71 | { |
| 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 Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 87 | } |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 88 | else |
| 89 | { |
| 90 | requester.markFree(eid, hdr->instance_id); |
| 91 | } |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 92 | return response; |
| 93 | } |
| 94 | |
| 95 | void 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 Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 110 | void 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 Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 119 | int main(int argc, char** argv) |
| 120 | { |
| 121 | |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 122 | 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 Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 148 | 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 Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 153 | |
| 154 | #ifdef OEM_IBM |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 155 | invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>()); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 156 | #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 Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 196 | auto bus = sdbusplus::bus::new_default(); |
| 197 | dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm"); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 198 | auto callback = [verbose, &invoker, &dbusImplReq](IO& /*io*/, int fd, |
| 199 | uint32_t revents) { |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 200 | 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 Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 216 | if (0 == peekedLength) |
| 217 | { |
| 218 | log<level::ERR>("Socket has been closed"); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 219 | } |
| 220 | else if (peekedLength <= -1) |
| 221 | { |
| 222 | returnCode = -errno; |
| 223 | log<level::ERR>("recv system call failed", |
| 224 | entry("RC=%d", returnCode)); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 225 | } |
| 226 | else |
| 227 | { |
| 228 | std::vector<uint8_t> requestMsg(peekedLength); |
| 229 | auto recvDataLength = recv( |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 230 | fd, static_cast<void*>(requestMsg.data()), peekedLength, 0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 231 | if (recvDataLength == peekedLength) |
| 232 | { |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 233 | 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 Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 241 | 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 Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 250 | auto response = |
| 251 | processRxMsg(requestMsg, invoker, dbusImplReq); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 252 | if (!response.empty()) |
| 253 | { |
Deepak Kodihalli | 8ffbbe0 | 2019-08-14 06:51:38 -0500 | [diff] [blame] | 254 | if (verbose) |
| 255 | { |
| 256 | log<level::INFO>("Sending Msg "); |
| 257 | printBuffer(response); |
| 258 | } |
| 259 | |
Zahed Hossain | 09a96e0 | 2019-08-06 07:42:37 -0500 | [diff] [blame] | 260 | 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 Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 269 | int result = sendmsg(fd, &msg, 0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 270 | if (-1 == result) |
| 271 | { |
| 272 | returnCode = -errno; |
| 273 | log<level::ERR>("sendto system call failed", |
| 274 | entry("RC=%d", returnCode)); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 275 | } |
| 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 Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 284 | } |
| 285 | } |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | auto event = Event::get_default(); |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 289 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 290 | bus.request_name("xyz.openbmc_project.PLDM"); |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 291 | IO io(event, socketFd(), EPOLLIN, std::move(callback)); |
| 292 | event.loop(); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 293 | |
| 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 | } |