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