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 | { |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame^] | 136 | instanceId = instanceIdDb.next(mctp_eid); |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 137 | auto [rc, requestMsg] = createRequestMsg(); |
| 138 | if (rc != PLDM_SUCCESS) |
| 139 | { |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame^] | 140 | instanceIdDb.free(mctp_eid, instanceId); |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 141 | std::cerr << "Failed to encode request message for " << pldmType << ":" |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 142 | << commandName << " rc = " << rc << "\n"; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 143 | return; |
| 144 | } |
| 145 | |
John Wang | b754eee | 2020-02-15 16:10:25 +0800 | [diff] [blame] | 146 | std::vector<uint8_t> responseMsg; |
| 147 | rc = pldmSendRecv(requestMsg, responseMsg); |
| 148 | |
| 149 | if (rc != PLDM_SUCCESS) |
| 150 | { |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame^] | 151 | instanceIdDb.free(mctp_eid, instanceId); |
John Wang | b754eee | 2020-02-15 16:10:25 +0800 | [diff] [blame] | 152 | std::cerr << "pldmSendRecv: Failed to receive RC = " << rc << "\n"; |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | auto responsePtr = reinterpret_cast<struct pldm_msg*>(responseMsg.data()); |
| 157 | parseResponseMsg(responsePtr, responseMsg.size() - sizeof(pldm_msg_hdr)); |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame^] | 158 | instanceIdDb.free(mctp_eid, instanceId); |
John Wang | b754eee | 2020-02-15 16:10:25 +0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | int CommandInterface::pldmSendRecv(std::vector<uint8_t>& requestMsg, |
| 162 | std::vector<uint8_t>& responseMsg) |
| 163 | { |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 164 | // Insert the PLDM message type and EID at the beginning of the |
| 165 | // msg. |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 166 | requestMsg.insert(requestMsg.begin(), MCTP_MSG_TYPE_PLDM); |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 167 | requestMsg.insert(requestMsg.begin(), mctp_eid); |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 168 | |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 169 | bool mctpVerbose = pldmVerbose; |
| 170 | |
| 171 | // By default enable request/response msgs for pldmtool raw commands. |
| 172 | if (CommandInterface::pldmType == "raw") |
| 173 | { |
| 174 | pldmVerbose = true; |
| 175 | } |
| 176 | |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 177 | if (pldmVerbose) |
| 178 | { |
| 179 | std::cout << "pldmtool: "; |
| 180 | printBuffer(Tx, requestMsg); |
| 181 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 182 | |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 183 | if (mctp_eid != PLDM_ENTITY_ID) |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 184 | { |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 185 | int fd = pldm_open(); |
| 186 | if (-1 == fd) |
| 187 | { |
| 188 | std::cerr << "failed to init mctp " |
| 189 | << "\n"; |
| 190 | return -1; |
| 191 | } |
| 192 | uint8_t* responseMessage = nullptr; |
| 193 | size_t responseMessageSize{}; |
Sampa Misra | 9f8d2b0 | 2021-03-24 08:33:14 +0000 | [diff] [blame] | 194 | pldm_send_recv(mctp_eid, fd, requestMsg.data() + 2, |
| 195 | requestMsg.size() - 2, &responseMessage, |
| 196 | &responseMessageSize); |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 197 | |
| 198 | responseMsg.resize(responseMessageSize); |
| 199 | memcpy(responseMsg.data(), responseMessage, responseMsg.size()); |
| 200 | |
Dung Cao | 2b85d78 | 2022-05-24 09:28:48 +0000 | [diff] [blame] | 201 | shutdown(fd, SHUT_RDWR); |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 202 | free(responseMessage); |
Dung Cao | 2b85d78 | 2022-05-24 09:28:48 +0000 | [diff] [blame] | 203 | |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 204 | if (pldmVerbose) |
| 205 | { |
| 206 | std::cout << "pldmtool: "; |
| 207 | printBuffer(Rx, responseMsg); |
| 208 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 209 | } |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 210 | else |
| 211 | { |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 212 | mctpSockSendRecv(requestMsg, responseMsg, mctpVerbose); |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 213 | if (pldmVerbose) |
| 214 | { |
| 215 | std::cout << "pldmtool: "; |
| 216 | printBuffer(Rx, responseMsg); |
| 217 | } |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 218 | responseMsg.erase(responseMsg.begin(), |
| 219 | responseMsg.begin() + 2 /* skip the mctp header */); |
| 220 | } |
John Wang | b754eee | 2020-02-15 16:10:25 +0800 | [diff] [blame] | 221 | return PLDM_SUCCESS; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 222 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 223 | } // namespace helper |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 224 | } // namespace pldmtool |