Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 1 | #include "pldm_cmd_helper.hpp" |
| 2 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 3 | #include "libpldm/requester/pldm.h" |
| 4 | |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 5 | #include "xyz/openbmc_project/Common/error.hpp" |
| 6 | |
| 7 | #include <systemd/sd-bus.h> |
| 8 | |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 9 | #include <sdbusplus/server.hpp> |
| 10 | #include <xyz/openbmc_project/Logging/Entry/server.hpp> |
| 11 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 12 | #include <exception> |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 13 | |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 14 | using namespace pldm::utils; |
| 15 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 16 | namespace pldmtool |
| 17 | { |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 18 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 19 | namespace helper |
| 20 | { |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 21 | /* |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 22 | * Initialize the socket, send pldm command & recieve response from socket |
| 23 | * |
| 24 | */ |
| 25 | int mctpSockSendRecv(const std::vector<uint8_t>& requestMsg, |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 26 | std::vector<uint8_t>& responseMsg, bool pldmVerbose) |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 27 | { |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 28 | |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 29 | const char devPath[] = "\0mctp-mux"; |
| 30 | int returnCode = 0; |
| 31 | |
| 32 | int sockFd = socket(AF_UNIX, SOCK_SEQPACKET, 0); |
| 33 | if (-1 == sockFd) |
| 34 | { |
| 35 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 36 | std::cerr << "Failed to create the socket : RC = " << sockFd << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 37 | return returnCode; |
| 38 | } |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 39 | Logger(pldmVerbose, "Success in creating the socket : RC = ", sockFd); |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 40 | |
| 41 | struct sockaddr_un addr |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 42 | {}; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 43 | addr.sun_family = AF_UNIX; |
| 44 | |
| 45 | memcpy(addr.sun_path, devPath, sizeof(devPath) - 1); |
| 46 | |
| 47 | CustomFD socketFd(sockFd); |
| 48 | int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr), |
| 49 | sizeof(devPath) + sizeof(addr.sun_family) - 1); |
| 50 | if (-1 == result) |
| 51 | { |
| 52 | returnCode = -errno; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 53 | std::cerr << "Failed to connect to socket : RC = " << returnCode |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 54 | << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 55 | return returnCode; |
| 56 | } |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 57 | Logger(pldmVerbose, "Success in connecting to socket : RC = ", returnCode); |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 58 | |
| 59 | auto pldmType = MCTP_MSG_TYPE_PLDM; |
| 60 | result = write(socketFd(), &pldmType, sizeof(pldmType)); |
| 61 | if (-1 == result) |
| 62 | { |
| 63 | returnCode = -errno; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 64 | std::cerr << "Failed to send message type as pldm to mctp : RC = " |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 65 | << returnCode << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 66 | return returnCode; |
| 67 | } |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 68 | Logger( |
| 69 | pldmVerbose, |
| 70 | "Success in sending message type as pldm to mctp : RC = ", returnCode); |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 71 | |
| 72 | result = send(socketFd(), requestMsg.data(), requestMsg.size(), 0); |
| 73 | if (-1 == result) |
| 74 | { |
| 75 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 76 | std::cerr << "Write to socket failure : RC = " << returnCode << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 77 | return returnCode; |
| 78 | } |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 79 | Logger(pldmVerbose, "Write to socket successful : RC = ", result); |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 80 | |
| 81 | // Read the response from socket |
| 82 | ssize_t peekedLength = recv(socketFd(), nullptr, 0, MSG_TRUNC | MSG_PEEK); |
| 83 | if (0 == peekedLength) |
| 84 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 85 | std::cerr << "Socket is closed : peekedLength = " << peekedLength |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 86 | << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 87 | return returnCode; |
| 88 | } |
| 89 | else if (peekedLength <= -1) |
| 90 | { |
| 91 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 92 | std::cerr << "recv() system call failed : RC = " << returnCode << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 93 | return returnCode; |
| 94 | } |
| 95 | else |
| 96 | { |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 97 | auto reqhdr = reinterpret_cast<const pldm_msg_hdr*>(&requestMsg[2]); |
| 98 | do |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 99 | { |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 100 | ssize_t peekedLength = |
| 101 | recv(socketFd(), nullptr, 0, MSG_PEEK | MSG_TRUNC); |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 102 | responseMsg.resize(peekedLength); |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 103 | auto recvDataLength = |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 104 | recv(socketFd(), reinterpret_cast<void*>(responseMsg.data()), |
| 105 | peekedLength, 0); |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 106 | auto resphdr = |
| 107 | reinterpret_cast<const pldm_msg_hdr*>(&responseMsg[2]); |
| 108 | if (recvDataLength == peekedLength && |
| 109 | resphdr->instance_id == reqhdr->instance_id && |
| 110 | resphdr->request != PLDM_REQUEST) |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 111 | { |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 112 | Logger(pldmVerbose, "Total length:", recvDataLength); |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 113 | break; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 114 | } |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 115 | else if (recvDataLength != peekedLength) |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 116 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 117 | std::cerr << "Failure to read response length packet: length = " |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 118 | << recvDataLength << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 119 | return returnCode; |
| 120 | } |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 121 | } while (1); |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 122 | } |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 123 | |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 124 | returnCode = shutdown(socketFd(), SHUT_RDWR); |
| 125 | if (-1 == returnCode) |
| 126 | { |
| 127 | returnCode = -errno; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 128 | std::cerr << "Failed to shutdown the socket : RC = " << returnCode |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 129 | << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 130 | return returnCode; |
| 131 | } |
| 132 | |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 133 | Logger(pldmVerbose, "Shutdown Socket successful : RC = ", returnCode); |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 134 | return PLDM_SUCCESS; |
| 135 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 136 | |
| 137 | void CommandInterface::exec() |
| 138 | { |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 139 | static constexpr auto pldmObjPath = "/xyz/openbmc_project/pldm"; |
| 140 | static constexpr auto pldmRequester = "xyz.openbmc_project.PLDM.Requester"; |
| 141 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 142 | try |
| 143 | { |
| 144 | auto service = |
| 145 | pldm::utils::DBusHandler().getService(pldmObjPath, pldmRequester); |
| 146 | auto method = bus.new_method_call(service.c_str(), pldmObjPath, |
| 147 | pldmRequester, "GetInstanceId"); |
| 148 | method.append(mctp_eid); |
| 149 | auto reply = bus.call(method); |
| 150 | reply.read(instanceId); |
| 151 | } |
| 152 | catch (const std::exception& e) |
| 153 | { |
| 154 | std::cerr << "GetInstanceId D-Bus call failed, MCTP id = " << mctp_eid |
| 155 | << ", error = " << e.what() << "\n"; |
| 156 | return; |
| 157 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 158 | auto [rc, requestMsg] = createRequestMsg(); |
| 159 | if (rc != PLDM_SUCCESS) |
| 160 | { |
| 161 | std::cerr << "Failed to encode request message for " << pldmType << ":" |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 162 | << commandName << " rc = " << rc << "\n"; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 163 | return; |
| 164 | } |
| 165 | |
John Wang | b754eee | 2020-02-15 16:10:25 +0800 | [diff] [blame] | 166 | std::vector<uint8_t> responseMsg; |
| 167 | rc = pldmSendRecv(requestMsg, responseMsg); |
| 168 | |
| 169 | if (rc != PLDM_SUCCESS) |
| 170 | { |
| 171 | std::cerr << "pldmSendRecv: Failed to receive RC = " << rc << "\n"; |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | auto responsePtr = reinterpret_cast<struct pldm_msg*>(responseMsg.data()); |
| 176 | parseResponseMsg(responsePtr, responseMsg.size() - sizeof(pldm_msg_hdr)); |
| 177 | } |
| 178 | |
| 179 | int CommandInterface::pldmSendRecv(std::vector<uint8_t>& requestMsg, |
| 180 | std::vector<uint8_t>& responseMsg) |
| 181 | { |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 182 | |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 183 | // Insert the PLDM message type and EID at the beginning of the |
| 184 | // msg. |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 185 | requestMsg.insert(requestMsg.begin(), MCTP_MSG_TYPE_PLDM); |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 186 | requestMsg.insert(requestMsg.begin(), mctp_eid); |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 187 | |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 188 | bool mctpVerbose = pldmVerbose; |
| 189 | |
| 190 | // By default enable request/response msgs for pldmtool raw commands. |
| 191 | if (CommandInterface::pldmType == "raw") |
| 192 | { |
| 193 | pldmVerbose = true; |
| 194 | } |
| 195 | |
| 196 | Logger(pldmVerbose, "Request Message:", ""); |
| 197 | printBuffer(requestMsg, pldmVerbose); |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 198 | |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 199 | if (mctp_eid != PLDM_ENTITY_ID) |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 200 | { |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 201 | int fd = pldm_open(); |
| 202 | if (-1 == fd) |
| 203 | { |
| 204 | std::cerr << "failed to init mctp " |
| 205 | << "\n"; |
| 206 | return -1; |
| 207 | } |
| 208 | uint8_t* responseMessage = nullptr; |
| 209 | size_t responseMessageSize{}; |
Sampa Misra | 9f8d2b0 | 2021-03-24 08:33:14 +0000 | [diff] [blame] | 210 | pldm_send_recv(mctp_eid, fd, requestMsg.data() + 2, |
| 211 | requestMsg.size() - 2, &responseMessage, |
| 212 | &responseMessageSize); |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 213 | |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 214 | Logger(pldmVerbose, "Response Message:", ""); |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 215 | responseMsg.resize(responseMessageSize); |
| 216 | memcpy(responseMsg.data(), responseMessage, responseMsg.size()); |
| 217 | |
| 218 | free(responseMessage); |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 219 | printBuffer(responseMsg, pldmVerbose); |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 220 | } |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 221 | else |
| 222 | { |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 223 | mctpSockSendRecv(requestMsg, responseMsg, mctpVerbose); |
| 224 | Logger(pldmVerbose, "Response Message:", ""); |
| 225 | printBuffer(responseMsg, pldmVerbose); |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 226 | responseMsg.erase(responseMsg.begin(), |
| 227 | responseMsg.begin() + 2 /* skip the mctp header */); |
| 228 | } |
John Wang | b754eee | 2020-02-15 16:10:25 +0800 | [diff] [blame] | 229 | return PLDM_SUCCESS; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 230 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 231 | } // namespace helper |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 232 | } // namespace pldmtool |