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