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