1 #include "libpldm/base.h" 2 #include "libpldm/bios.h" 3 #include "libpldm/pdr.h" 4 #include "libpldm/platform.h" 5 6 #include "common/flight_recorder.hpp" 7 #include "common/utils.hpp" 8 #include "dbus_impl_requester.hpp" 9 #include "fw-update/manager.hpp" 10 #include "invoker.hpp" 11 #include "requester/handler.hpp" 12 #include "requester/mctp_endpoint_discovery.hpp" 13 #include "requester/request.hpp" 14 15 #include <err.h> 16 #include <getopt.h> 17 #include <poll.h> 18 #include <stdlib.h> 19 #include <sys/socket.h> 20 #include <sys/types.h> 21 #include <sys/un.h> 22 #include <unistd.h> 23 24 #include <sdeventplus/event.hpp> 25 #include <sdeventplus/source/io.hpp> 26 #include <sdeventplus/source/signal.hpp> 27 #include <stdplus/signal.hpp> 28 29 #include <cstdio> 30 #include <cstring> 31 #include <fstream> 32 #include <iomanip> 33 #include <iostream> 34 #include <iterator> 35 #include <memory> 36 #include <optional> 37 #include <sstream> 38 #include <stdexcept> 39 #include <string> 40 #include <vector> 41 42 #ifdef LIBPLDMRESPONDER 43 #include "dbus_impl_pdr.hpp" 44 #include "host-bmc/dbus_to_event_handler.hpp" 45 #include "host-bmc/dbus_to_host_effecters.hpp" 46 #include "host-bmc/host_condition.hpp" 47 #include "host-bmc/host_pdr_handler.hpp" 48 #include "libpldmresponder/base.hpp" 49 #include "libpldmresponder/bios.hpp" 50 #include "libpldmresponder/fru.hpp" 51 #include "libpldmresponder/oem_handler.hpp" 52 #include "libpldmresponder/platform.hpp" 53 #include "xyz/openbmc_project/PLDM/Event/server.hpp" 54 #endif 55 56 #ifdef OEM_IBM 57 #include "libpldmresponder/file_io.hpp" 58 #include "libpldmresponder/oem_ibm_handler.hpp" 59 #endif 60 61 constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1; 62 63 using namespace pldm; 64 using namespace sdeventplus; 65 using namespace sdeventplus::source; 66 using namespace pldm::responder; 67 using namespace pldm::utils; 68 using sdeventplus::source::Signal; 69 using namespace pldm::flightrecorder; 70 71 void interruptFlightRecorderCallBack(Signal& /*signal*/, 72 const struct signalfd_siginfo*) 73 { 74 std::cerr << "\nReceived SIGUR1(10) Signal interrupt\n"; 75 76 // obtain the flight recorder instance and dump the recorder 77 FlightRecorder::GetInstance().playRecorder(); 78 } 79 80 static std::optional<Response> 81 processRxMsg(const std::vector<uint8_t>& requestMsg, Invoker& invoker, 82 requester::Handler<requester::Request>& handler, 83 fw_update::Manager* fwManager) 84 { 85 using type = uint8_t; 86 uint8_t eid = requestMsg[0]; 87 pldm_header_info hdrFields{}; 88 auto hdr = reinterpret_cast<const pldm_msg_hdr*>( 89 requestMsg.data() + sizeof(eid) + sizeof(type)); 90 if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields)) 91 { 92 std::cerr << "Empty PLDM request header \n"; 93 return std::nullopt; 94 } 95 96 if (PLDM_RESPONSE != hdrFields.msg_type) 97 { 98 Response response; 99 auto request = reinterpret_cast<const pldm_msg*>(hdr); 100 size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) - 101 sizeof(eid) - sizeof(type); 102 try 103 { 104 if (hdrFields.pldm_type != PLDM_FWUP) 105 { 106 response = 107 invoker.handle(hdrFields.pldm_type, hdrFields.command, 108 request, requestLen); 109 } 110 else 111 { 112 response = fwManager->handleRequest(eid, hdrFields.command, 113 request, requestLen); 114 } 115 } 116 catch (const std::out_of_range& e) 117 { 118 uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD; 119 response.resize(sizeof(pldm_msg_hdr)); 120 auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data()); 121 pldm_header_info header{}; 122 header.msg_type = PLDM_RESPONSE; 123 header.instance = hdrFields.instance; 124 header.pldm_type = hdrFields.pldm_type; 125 header.command = hdrFields.command; 126 if (PLDM_SUCCESS != pack_pldm_header(&header, responseHdr)) 127 { 128 std::cerr << "Failed adding response header \n"; 129 return std::nullopt; 130 } 131 response.insert(response.end(), completion_code); 132 } 133 return response; 134 } 135 else if (PLDM_RESPONSE == hdrFields.msg_type) 136 { 137 auto response = reinterpret_cast<const pldm_msg*>(hdr); 138 size_t responseLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) - 139 sizeof(eid) - sizeof(type); 140 handler.handleResponse(eid, hdrFields.instance, hdrFields.pldm_type, 141 hdrFields.command, response, responseLen); 142 } 143 return std::nullopt; 144 } 145 146 void optionUsage(void) 147 { 148 std::cerr << "Usage: pldmd [options]\n"; 149 std::cerr << "Options:\n"; 150 std::cerr << " [--verbose] - would enable verbosity\n"; 151 } 152 153 int main(int argc, char** argv) 154 { 155 bool verbose = false; 156 static struct option long_options[] = {{"verbose", no_argument, 0, 'v'}, 157 {0, 0, 0, 0}}; 158 159 auto argflag = getopt_long(argc, argv, "v", long_options, nullptr); 160 switch (argflag) 161 { 162 case 'v': 163 verbose = true; 164 break; 165 case -1: 166 break; 167 default: 168 optionUsage(); 169 exit(EXIT_FAILURE); 170 } 171 172 /* Create local socket. */ 173 int returnCode = 0; 174 int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0); 175 if (-1 == sockfd) 176 { 177 returnCode = -errno; 178 std::cerr << "Failed to create the socket, RC= " << returnCode << "\n"; 179 exit(EXIT_FAILURE); 180 } 181 socklen_t optlen; 182 int currentSendbuffSize; 183 184 // Get Current send buffer size 185 optlen = sizeof(currentSendbuffSize); 186 187 int res = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, ¤tSendbuffSize, 188 &optlen); 189 if (res == -1) 190 { 191 std::cerr << "Error in obtaining the default send buffer size, Error : " 192 << strerror(errno) << std::endl; 193 } 194 auto event = Event::get_default(); 195 auto& bus = pldm::utils::DBusHandler::getBus(); 196 sdbusplus::server::manager_t objManager(bus, 197 "/xyz/openbmc_project/software"); 198 dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm"); 199 200 Invoker invoker{}; 201 requester::Handler<requester::Request> reqHandler( 202 sockfd, event, dbusImplReq, currentSendbuffSize, verbose); 203 204 #ifdef LIBPLDMRESPONDER 205 using namespace pldm::state_sensor; 206 dbus_api::Host dbusImplHost(bus, "/xyz/openbmc_project/pldm"); 207 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo( 208 pldm_pdr_init(), pldm_pdr_destroy); 209 std::unique_ptr<pldm_entity_association_tree, 210 decltype(&pldm_entity_association_tree_destroy)> 211 entityTree(pldm_entity_association_tree_init(), 212 pldm_entity_association_tree_destroy); 213 std::unique_ptr<pldm_entity_association_tree, 214 decltype(&pldm_entity_association_tree_destroy)> 215 bmcEntityTree(pldm_entity_association_tree_init(), 216 pldm_entity_association_tree_destroy); 217 std::shared_ptr<HostPDRHandler> hostPDRHandler; 218 std::unique_ptr<pldm::host_effecters::HostEffecterParser> 219 hostEffecterParser; 220 std::unique_ptr<DbusToPLDMEvent> dbusToPLDMEventHandler; 221 DBusHandler dbusHandler; 222 auto hostEID = pldm::utils::readHostEID(); 223 if (hostEID) 224 { 225 hostPDRHandler = std::make_shared<HostPDRHandler>( 226 sockfd, hostEID, event, pdrRepo.get(), EVENTS_JSONS_DIR, 227 entityTree.get(), bmcEntityTree.get(), dbusImplReq, &reqHandler); 228 // HostFirmware interface needs access to hostPDR to know if host 229 // is running 230 dbusImplHost.setHostPdrObj(hostPDRHandler); 231 232 hostEffecterParser = 233 std::make_unique<pldm::host_effecters::HostEffecterParser>( 234 &dbusImplReq, sockfd, pdrRepo.get(), &dbusHandler, 235 HOST_JSONS_DIR, &reqHandler); 236 dbusToPLDMEventHandler = std::make_unique<DbusToPLDMEvent>( 237 sockfd, hostEID, dbusImplReq, &reqHandler); 238 } 239 std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; 240 241 #ifdef OEM_IBM 242 std::unique_ptr<pldm::responder::CodeUpdate> codeUpdate = 243 std::make_unique<pldm::responder::CodeUpdate>(&dbusHandler); 244 codeUpdate->clearDirPath(LID_STAGING_DIR); 245 oemPlatformHandler = std::make_unique<oem_ibm_platform::Handler>( 246 &dbusHandler, codeUpdate.get(), sockfd, hostEID, dbusImplReq, event, 247 &reqHandler); 248 codeUpdate->setOemPlatformHandler(oemPlatformHandler.get()); 249 invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>( 250 oemPlatformHandler.get(), sockfd, 251 hostEID, &dbusImplReq, &reqHandler)); 252 #endif 253 invoker.registerHandler( 254 PLDM_BIOS, std::make_unique<bios::Handler>(sockfd, hostEID, 255 &dbusImplReq, &reqHandler)); 256 auto fruHandler = std::make_unique<fru::Handler>( 257 FRU_JSONS_DIR, FRU_MASTER_JSON, pdrRepo.get(), entityTree.get(), 258 bmcEntityTree.get()); 259 // FRU table is built lazily when a FRU command or Get PDR command is 260 // handled. To enable building FRU table, the FRU handler is passed to the 261 // Platform handler. 262 auto platformHandler = std::make_unique<platform::Handler>( 263 &dbusHandler, PDR_JSONS_DIR, pdrRepo.get(), hostPDRHandler.get(), 264 dbusToPLDMEventHandler.get(), fruHandler.get(), 265 oemPlatformHandler.get(), event, true); 266 #ifdef OEM_IBM 267 pldm::responder::oem_ibm_platform::Handler* oemIbmPlatformHandler = 268 dynamic_cast<pldm::responder::oem_ibm_platform::Handler*>( 269 oemPlatformHandler.get()); 270 oemIbmPlatformHandler->setPlatformHandler(platformHandler.get()); 271 #endif 272 273 invoker.registerHandler(PLDM_PLATFORM, std::move(platformHandler)); 274 invoker.registerHandler( 275 PLDM_BASE, 276 std::make_unique<base::Handler>(hostEID, dbusImplReq, event, 277 oemPlatformHandler.get(), &reqHandler)); 278 invoker.registerHandler(PLDM_FRU, std::move(fruHandler)); 279 dbus_api::Pdr dbusImplPdr(bus, "/xyz/openbmc_project/pldm", pdrRepo.get()); 280 sdbusplus::xyz::openbmc_project::PLDM::server::Event dbusImplEvent( 281 bus, "/xyz/openbmc_project/pldm"); 282 283 #endif 284 285 pldm::utils::CustomFD socketFd(sockfd); 286 287 struct sockaddr_un addr 288 {}; 289 addr.sun_family = AF_UNIX; 290 const char path[] = "\0mctp-mux"; 291 memcpy(addr.sun_path, path, sizeof(path) - 1); 292 int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr), 293 sizeof(path) + sizeof(addr.sun_family) - 1); 294 if (-1 == result) 295 { 296 returnCode = -errno; 297 std::cerr << "Failed to connect to the socket, RC= " << returnCode 298 << "\n"; 299 exit(EXIT_FAILURE); 300 } 301 302 result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM)); 303 if (-1 == result) 304 { 305 returnCode = -errno; 306 std::cerr << "Failed to send message type as pldm to mctp, RC= " 307 << returnCode << "\n"; 308 exit(EXIT_FAILURE); 309 } 310 311 std::unique_ptr<fw_update::Manager> fwManager = 312 std::make_unique<fw_update::Manager>(event, reqHandler, dbusImplReq); 313 std::unique_ptr<MctpDiscovery> mctpDiscoveryHandler = 314 std::make_unique<MctpDiscovery>(bus, fwManager.get()); 315 316 auto callback = [verbose, &invoker, &reqHandler, currentSendbuffSize, 317 &fwManager](IO& io, int fd, uint32_t revents) mutable { 318 if (!(revents & EPOLLIN)) 319 { 320 return; 321 } 322 323 // Outgoing message. 324 struct iovec iov[2]{}; 325 326 // This structure contains the parameter information for the response 327 // message. 328 struct msghdr msg 329 {}; 330 331 int returnCode = 0; 332 ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC); 333 if (0 == peekedLength) 334 { 335 // MCTP daemon has closed the socket this daemon is connected to. 336 // This may or may not be an error scenario, in either case the 337 // recovery mechanism for this daemon is to restart, and hence exit 338 // the event loop, that will cause this daemon to exit with a 339 // failure code. 340 io.get_event().exit(0); 341 } 342 else if (peekedLength <= -1) 343 { 344 returnCode = -errno; 345 std::cerr << "recv system call failed, RC= " << returnCode << "\n"; 346 } 347 else 348 { 349 std::vector<uint8_t> requestMsg(peekedLength); 350 auto recvDataLength = recv( 351 fd, static_cast<void*>(requestMsg.data()), peekedLength, 0); 352 if (recvDataLength == peekedLength) 353 { 354 FlightRecorder::GetInstance().saveRecord(requestMsg, false); 355 if (verbose) 356 { 357 printBuffer(Rx, requestMsg); 358 } 359 360 if (MCTP_MSG_TYPE_PLDM != requestMsg[1]) 361 { 362 // Skip this message and continue. 363 } 364 else 365 { 366 // process message and send response 367 auto response = processRxMsg(requestMsg, invoker, 368 reqHandler, fwManager.get()); 369 if (response.has_value()) 370 { 371 FlightRecorder::GetInstance().saveRecord(*response, 372 true); 373 if (verbose) 374 { 375 printBuffer(Tx, *response); 376 } 377 378 iov[0].iov_base = &requestMsg[0]; 379 iov[0].iov_len = 380 sizeof(requestMsg[0]) + sizeof(requestMsg[1]); 381 iov[1].iov_base = (*response).data(); 382 iov[1].iov_len = (*response).size(); 383 384 msg.msg_iov = iov; 385 msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]); 386 if (currentSendbuffSize >= 0 && 387 (size_t)currentSendbuffSize < (*response).size()) 388 { 389 int oldBuffSize = currentSendbuffSize; 390 currentSendbuffSize = (*response).size(); 391 int res = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, 392 ¤tSendbuffSize, 393 sizeof(currentSendbuffSize)); 394 if (res == -1) 395 { 396 std::cerr 397 << "Responder : Failed to set the new send buffer size [bytes] : " 398 << currentSendbuffSize 399 << " from current size [bytes] : " 400 << oldBuffSize 401 << ", Error : " << strerror(errno) 402 << std::endl; 403 return; 404 } 405 } 406 407 int result = sendmsg(fd, &msg, 0); 408 if (-1 == result) 409 { 410 returnCode = -errno; 411 std::cerr << "sendto system call failed, RC= " 412 << returnCode << "\n"; 413 } 414 } 415 } 416 } 417 else 418 { 419 std::cerr 420 << "Failure to read peeked length packet. peekedLength= " 421 << peekedLength << " recvDataLength=" << recvDataLength 422 << "\n"; 423 } 424 } 425 }; 426 427 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); 428 bus.request_name("xyz.openbmc_project.PLDM"); 429 IO io(event, socketFd(), EPOLLIN, std::move(callback)); 430 #ifdef LIBPLDMRESPONDER 431 if (hostPDRHandler) 432 { 433 hostPDRHandler->setHostFirmwareCondition(); 434 } 435 #endif 436 stdplus::signal::block(SIGUSR1); 437 sdeventplus::source::Signal sigUsr1( 438 event, SIGUSR1, std::bind_front(&interruptFlightRecorderCallBack)); 439 returnCode = event.loop(); 440 441 if (shutdown(sockfd, SHUT_RDWR)) 442 { 443 std::perror("Failed to shutdown the socket"); 444 } 445 if (returnCode) 446 { 447 exit(EXIT_FAILURE); 448 } 449 450 exit(EXIT_SUCCESS); 451 } 452