George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 1 | #include "libpldm/base.h" |
| 2 | #include "libpldm/bios.h" |
| 3 | #include "libpldm/pdr.h" |
| 4 | #include "libpldm/platform.h" |
| 5 | |
Deepak Kodihalli | d130e1a | 2020-06-17 05:55:32 -0500 | [diff] [blame] | 6 | #include "common/utils.hpp" |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 7 | #include "dbus_impl_requester.hpp" |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 8 | #include "invoker.hpp" |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 9 | #include "requester/handler.hpp" |
| 10 | #include "requester/request.hpp" |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 11 | |
| 12 | #include <err.h> |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 13 | #include <getopt.h> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 14 | #include <poll.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <sys/socket.h> |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/un.h> |
| 19 | #include <unistd.h> |
| 20 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 21 | #include <sdeventplus/event.hpp> |
| 22 | #include <sdeventplus/source/io.hpp> |
| 23 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 24 | #include <cstdio> |
| 25 | #include <cstring> |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 26 | #include <fstream> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 27 | #include <iomanip> |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 28 | #include <iostream> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 29 | #include <iterator> |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 30 | #include <memory> |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 31 | #include <optional> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 32 | #include <sstream> |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 33 | #include <stdexcept> |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 34 | #include <string> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 35 | #include <vector> |
| 36 | |
Tom Joseph | 02b4ee4 | 2021-05-02 22:44:36 -0700 | [diff] [blame] | 37 | #ifdef LIBPLDMRESPONDER |
| 38 | #include "dbus_impl_pdr.hpp" |
| 39 | #include "host-bmc/dbus_to_event_handler.hpp" |
| 40 | #include "host-bmc/dbus_to_host_effecters.hpp" |
| 41 | #include "host-bmc/host_pdr_handler.hpp" |
| 42 | #include "libpldmresponder/base.hpp" |
| 43 | #include "libpldmresponder/bios.hpp" |
| 44 | #include "libpldmresponder/fru.hpp" |
| 45 | #include "libpldmresponder/oem_handler.hpp" |
| 46 | #include "libpldmresponder/platform.hpp" |
| 47 | #include "xyz/openbmc_project/PLDM/Event/server.hpp" |
| 48 | #endif |
| 49 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 50 | #ifdef OEM_IBM |
| 51 | #include "libpldmresponder/file_io.hpp" |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 52 | #include "libpldmresponder/oem_ibm_handler.hpp" |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 53 | #endif |
| 54 | |
| 55 | constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1; |
| 56 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 57 | using namespace pldm; |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 58 | using namespace sdeventplus; |
| 59 | using namespace sdeventplus::source; |
Tom Joseph | 02b4ee4 | 2021-05-02 22:44:36 -0700 | [diff] [blame] | 60 | using namespace pldm::responder; |
| 61 | using namespace pldm::utils; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 62 | |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 63 | static std::optional<Response> |
| 64 | processRxMsg(const std::vector<uint8_t>& requestMsg, Invoker& invoker, |
| 65 | requester::Handler<requester::Request>& handler) |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 66 | { |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 67 | uint8_t eid = requestMsg[0]; |
| 68 | uint8_t type = requestMsg[1]; |
| 69 | pldm_header_info hdrFields{}; |
| 70 | auto hdr = reinterpret_cast<const pldm_msg_hdr*>( |
| 71 | requestMsg.data() + sizeof(eid) + sizeof(type)); |
| 72 | if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields)) |
| 73 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 74 | std::cerr << "Empty PLDM request header \n"; |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 75 | return std::nullopt; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 76 | } |
George Liu | b7095ff | 2021-06-14 16:01:57 +0800 | [diff] [blame] | 77 | |
| 78 | if (PLDM_RESPONSE != hdrFields.msg_type) |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 79 | { |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 80 | Response response; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 81 | auto request = reinterpret_cast<const pldm_msg*>(hdr); |
| 82 | size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) - |
| 83 | sizeof(eid) - sizeof(type); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 84 | try |
| 85 | { |
| 86 | response = invoker.handle(hdrFields.pldm_type, hdrFields.command, |
| 87 | request, requestLen); |
| 88 | } |
| 89 | catch (const std::out_of_range& e) |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 90 | { |
| 91 | uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD; |
| 92 | response.resize(sizeof(pldm_msg_hdr)); |
| 93 | auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data()); |
| 94 | pldm_header_info header{}; |
| 95 | header.msg_type = PLDM_RESPONSE; |
| 96 | header.instance = hdrFields.instance; |
| 97 | header.pldm_type = hdrFields.pldm_type; |
| 98 | header.command = hdrFields.command; |
George Liu | b7095ff | 2021-06-14 16:01:57 +0800 | [diff] [blame] | 99 | if (PLDM_SUCCESS != pack_pldm_header(&header, responseHdr)) |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 100 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 101 | std::cerr << "Failed adding response header \n"; |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 102 | return std::nullopt; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 103 | } |
| 104 | response.insert(response.end(), completion_code); |
| 105 | } |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 106 | return response; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 107 | } |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 108 | else if (PLDM_RESPONSE == hdrFields.msg_type) |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 109 | { |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 110 | auto response = reinterpret_cast<const pldm_msg*>(hdr); |
| 111 | size_t responseLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) - |
| 112 | sizeof(eid) - sizeof(type); |
| 113 | handler.handleResponse(eid, hdrFields.instance, hdrFields.pldm_type, |
| 114 | hdrFields.command, response, responseLen); |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 115 | } |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 116 | return std::nullopt; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 117 | } |
| 118 | |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 119 | void optionUsage(void) |
| 120 | { |
| 121 | std::cerr << "Usage: pldmd [options]\n"; |
| 122 | std::cerr << "Options:\n"; |
| 123 | std::cerr |
| 124 | << " --verbose=<0/1> 0 - Disable verbosity, 1 - Enable verbosity\n"; |
| 125 | std::cerr << "Defaulted settings: --verbose=0 \n"; |
| 126 | } |
| 127 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 128 | int main(int argc, char** argv) |
| 129 | { |
| 130 | |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 131 | bool verbose = false; |
| 132 | static struct option long_options[] = { |
| 133 | {"verbose", required_argument, 0, 'v'}, {0, 0, 0, 0}}; |
| 134 | |
| 135 | auto argflag = getopt_long(argc, argv, "v:", long_options, nullptr); |
| 136 | switch (argflag) |
| 137 | { |
| 138 | case 'v': |
| 139 | switch (std::stoi(optarg)) |
| 140 | { |
| 141 | case 0: |
| 142 | verbose = false; |
| 143 | break; |
| 144 | case 1: |
| 145 | verbose = true; |
| 146 | break; |
| 147 | default: |
| 148 | optionUsage(); |
| 149 | break; |
| 150 | } |
| 151 | break; |
| 152 | default: |
| 153 | optionUsage(); |
| 154 | break; |
| 155 | } |
| 156 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 157 | /* Create local socket. */ |
| 158 | int returnCode = 0; |
| 159 | int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0); |
| 160 | if (-1 == sockfd) |
| 161 | { |
| 162 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 163 | std::cerr << "Failed to create the socket, RC= " << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 164 | exit(EXIT_FAILURE); |
| 165 | } |
| 166 | |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 167 | auto event = Event::get_default(); |
Tom Joseph | 02b4ee4 | 2021-05-02 22:44:36 -0700 | [diff] [blame] | 168 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 169 | dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm"); |
| 170 | Invoker invoker{}; |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 171 | requester::Handler<requester::Request> reqHandler(sockfd, event, |
| 172 | dbusImplReq); |
Tom Joseph | 02b4ee4 | 2021-05-02 22:44:36 -0700 | [diff] [blame] | 173 | |
| 174 | #ifdef LIBPLDMRESPONDER |
| 175 | using namespace pldm::state_sensor; |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 176 | std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo( |
| 177 | pldm_pdr_init(), pldm_pdr_destroy); |
| 178 | std::unique_ptr<pldm_entity_association_tree, |
| 179 | decltype(&pldm_entity_association_tree_destroy)> |
| 180 | entityTree(pldm_entity_association_tree_init(), |
| 181 | pldm_entity_association_tree_destroy); |
Sampa Misra | c073a20 | 2021-05-08 10:56:05 -0500 | [diff] [blame] | 182 | std::unique_ptr<pldm_entity_association_tree, |
| 183 | decltype(&pldm_entity_association_tree_destroy)> |
| 184 | bmcEntityTree(pldm_entity_association_tree_init(), |
| 185 | pldm_entity_association_tree_destroy); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 186 | std::unique_ptr<HostPDRHandler> hostPDRHandler; |
Sampa Misra | c0c6054 | 2020-07-01 02:34:25 -0500 | [diff] [blame] | 187 | std::unique_ptr<pldm::host_effecters::HostEffecterParser> |
| 188 | hostEffecterParser; |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 189 | std::unique_ptr<DbusToPLDMEvent> dbusToPLDMEventHandler; |
Deepak Kodihalli | b5c227e | 2020-07-13 06:58:34 -0500 | [diff] [blame] | 190 | auto dbusHandler = std::make_unique<DBusHandler>(); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 191 | auto hostEID = pldm::utils::readHostEID(); |
| 192 | if (hostEID) |
| 193 | { |
| 194 | hostPDRHandler = std::make_unique<HostPDRHandler>( |
Pavithra Barithaya | 3aec997 | 2020-12-14 01:55:44 -0600 | [diff] [blame] | 195 | sockfd, hostEID, event, pdrRepo.get(), EVENTS_JSONS_DIR, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 196 | entityTree.get(), bmcEntityTree.get(), dbusImplReq, &reqHandler, |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 197 | verbose); |
Sampa Misra | c0c6054 | 2020-07-01 02:34:25 -0500 | [diff] [blame] | 198 | hostEffecterParser = |
| 199 | std::make_unique<pldm::host_effecters::HostEffecterParser>( |
Deepak Kodihalli | b5c227e | 2020-07-13 06:58:34 -0500 | [diff] [blame] | 200 | &dbusImplReq, sockfd, pdrRepo.get(), dbusHandler.get(), |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 201 | HOST_JSONS_DIR, &reqHandler, verbose); |
| 202 | dbusToPLDMEventHandler = std::make_unique<DbusToPLDMEvent>( |
| 203 | sockfd, hostEID, dbusImplReq, &reqHandler); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 204 | } |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 205 | std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; |
| 206 | |
| 207 | #ifdef OEM_IBM |
| 208 | std::unique_ptr<pldm::responder::CodeUpdate> codeUpdate = |
| 209 | std::make_unique<pldm::responder::CodeUpdate>(dbusHandler.get()); |
Varsha Kaverappa | 3ca29df | 2020-09-27 12:39:22 -0500 | [diff] [blame] | 210 | codeUpdate->clearDirPath(LID_STAGING_DIR); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 211 | oemPlatformHandler = std::make_unique<oem_ibm_platform::Handler>( |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 212 | dbusHandler.get(), codeUpdate.get(), sockfd, hostEID, dbusImplReq, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 213 | event, &reqHandler); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 214 | codeUpdate->setOemPlatformHandler(oemPlatformHandler.get()); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 215 | invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>( |
| 216 | oemPlatformHandler.get(), sockfd, |
| 217 | hostEID, &dbusImplReq, &reqHandler)); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 218 | #endif |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 219 | invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>()); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 220 | invoker.registerHandler( |
| 221 | PLDM_BIOS, std::make_unique<bios::Handler>(sockfd, hostEID, |
| 222 | &dbusImplReq, &reqHandler)); |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 223 | auto fruHandler = std::make_unique<fru::Handler>( |
Manojkiran Eda | 03b01ca | 2021-06-29 08:55:09 +0530 | [diff] [blame] | 224 | FRU_JSONS_DIR, FRU_MASTER_JSON, pdrRepo.get(), entityTree.get(), |
| 225 | bmcEntityTree.get()); |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 226 | // FRU table is built lazily when a FRU command or Get PDR command is |
| 227 | // handled. To enable building FRU table, the FRU handler is passed to the |
| 228 | // Platform handler. |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 229 | auto platformHandler = std::make_unique<platform::Handler>( |
| 230 | dbusHandler.get(), PDR_JSONS_DIR, pdrRepo.get(), hostPDRHandler.get(), |
| 231 | dbusToPLDMEventHandler.get(), fruHandler.get(), |
Sampa Misra | 5fb37d5 | 2021-03-06 07:26:00 -0600 | [diff] [blame] | 232 | oemPlatformHandler.get(), event, true); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 233 | #ifdef OEM_IBM |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 234 | pldm::responder::oem_ibm_platform::Handler* oemIbmPlatformHandler = |
| 235 | dynamic_cast<pldm::responder::oem_ibm_platform::Handler*>( |
| 236 | oemPlatformHandler.get()); |
| 237 | oemIbmPlatformHandler->setPlatformHandler(platformHandler.get()); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 238 | #endif |
| 239 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 240 | invoker.registerHandler(PLDM_PLATFORM, std::move(platformHandler)); |
| 241 | invoker.registerHandler(PLDM_FRU, std::move(fruHandler)); |
Tom Joseph | 02b4ee4 | 2021-05-02 22:44:36 -0700 | [diff] [blame] | 242 | dbus_api::Pdr dbusImplPdr(bus, "/xyz/openbmc_project/pldm", pdrRepo.get()); |
| 243 | sdbusplus::xyz::openbmc_project::PLDM::server::Event dbusImplEvent( |
| 244 | bus, "/xyz/openbmc_project/pldm"); |
| 245 | #endif |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 246 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 247 | pldm::utils::CustomFD socketFd(sockfd); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 248 | |
| 249 | struct sockaddr_un addr |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 250 | {}; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 251 | addr.sun_family = AF_UNIX; |
| 252 | const char path[] = "\0mctp-mux"; |
| 253 | memcpy(addr.sun_path, path, sizeof(path) - 1); |
| 254 | int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr), |
| 255 | sizeof(path) + sizeof(addr.sun_family) - 1); |
| 256 | if (-1 == result) |
| 257 | { |
| 258 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 259 | std::cerr << "Failed to connect to the socket, RC= " << returnCode |
| 260 | << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 261 | exit(EXIT_FAILURE); |
| 262 | } |
| 263 | |
| 264 | result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM)); |
| 265 | if (-1 == result) |
| 266 | { |
| 267 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 268 | std::cerr << "Failed to send message type as pldm to mctp, RC= " |
| 269 | << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 270 | exit(EXIT_FAILURE); |
| 271 | } |
| 272 | |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 273 | auto callback = [verbose, &invoker, &reqHandler](IO& io, int fd, |
| 274 | uint32_t revents) { |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 275 | if (!(revents & EPOLLIN)) |
| 276 | { |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | // Outgoing message. |
| 281 | struct iovec iov[2]{}; |
| 282 | |
| 283 | // This structure contains the parameter information for the response |
| 284 | // message. |
| 285 | struct msghdr msg |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 286 | {}; |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 287 | |
| 288 | int returnCode = 0; |
| 289 | ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 290 | if (0 == peekedLength) |
| 291 | { |
Deepak Kodihalli | 23c5204 | 2020-09-01 03:04:32 -0500 | [diff] [blame] | 292 | // MCTP daemon has closed the socket this daemon is connected to. |
| 293 | // This may or may not be an error scenario, in either case the |
| 294 | // recovery mechanism for this daemon is to restart, and hence exit |
| 295 | // the event loop, that will cause this daemon to exit with a |
| 296 | // failure code. |
| 297 | io.get_event().exit(0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 298 | } |
| 299 | else if (peekedLength <= -1) |
| 300 | { |
| 301 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 302 | std::cerr << "recv system call failed, RC= " << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 303 | } |
| 304 | else |
| 305 | { |
| 306 | std::vector<uint8_t> requestMsg(peekedLength); |
| 307 | auto recvDataLength = recv( |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 308 | fd, static_cast<void*>(requestMsg.data()), peekedLength, 0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 309 | if (recvDataLength == peekedLength) |
| 310 | { |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 311 | if (verbose) |
| 312 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 313 | std::cout << "Received Msg" << std::endl; |
Sridevi Ramesh | ae28bc7 | 2020-12-10 07:21:16 -0600 | [diff] [blame] | 314 | printBuffer(requestMsg, verbose); |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 315 | } |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 316 | if (MCTP_MSG_TYPE_PLDM != requestMsg[1]) |
| 317 | { |
| 318 | // Skip this message and continue. |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 319 | std::cerr << "Encountered Non-PLDM type message" |
| 320 | << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 321 | } |
| 322 | else |
| 323 | { |
| 324 | // process message and send response |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 325 | auto response = |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 326 | processRxMsg(requestMsg, invoker, reqHandler); |
| 327 | if (response.has_value()) |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 328 | { |
Deepak Kodihalli | 8ffbbe0 | 2019-08-14 06:51:38 -0500 | [diff] [blame] | 329 | if (verbose) |
| 330 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 331 | std::cout << "Sending Msg" << std::endl; |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 332 | printBuffer(*response, verbose); |
Deepak Kodihalli | 8ffbbe0 | 2019-08-14 06:51:38 -0500 | [diff] [blame] | 333 | } |
| 334 | |
Zahed Hossain | 09a96e0 | 2019-08-06 07:42:37 -0500 | [diff] [blame] | 335 | iov[0].iov_base = &requestMsg[0]; |
| 336 | iov[0].iov_len = |
| 337 | sizeof(requestMsg[0]) + sizeof(requestMsg[1]); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 338 | iov[1].iov_base = (*response).data(); |
| 339 | iov[1].iov_len = (*response).size(); |
Zahed Hossain | 09a96e0 | 2019-08-06 07:42:37 -0500 | [diff] [blame] | 340 | |
| 341 | msg.msg_iov = iov; |
| 342 | msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]); |
| 343 | |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 344 | int result = sendmsg(fd, &msg, 0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 345 | if (-1 == result) |
| 346 | { |
| 347 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 348 | std::cerr << "sendto system call failed, RC= " |
| 349 | << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | else |
| 355 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 356 | std::cerr |
| 357 | << "Failure to read peeked length packet. peekedLength= " |
| 358 | << peekedLength << " recvDataLength=" << recvDataLength |
| 359 | << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 360 | } |
| 361 | } |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 362 | }; |
| 363 | |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 364 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 365 | bus.request_name("xyz.openbmc_project.PLDM"); |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 366 | IO io(event, socketFd(), EPOLLIN, std::move(callback)); |
| 367 | event.loop(); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 368 | |
| 369 | result = shutdown(sockfd, SHUT_RDWR); |
| 370 | if (-1 == result) |
| 371 | { |
| 372 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 373 | std::cerr << "Failed to shutdown the socket, RC=" << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 374 | exit(EXIT_FAILURE); |
| 375 | } |
| 376 | exit(EXIT_FAILURE); |
| 377 | } |