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