Patrick Williams | 691668f | 2023-11-01 08:19:10 -0500 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 2 | #include "libpldm/transport.h" |
Andrew Jeffery | 4c1ea56 | 2023-09-22 18:01:05 +0930 | [diff] [blame] | 3 | #include "libpldm/base.h" |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 4 | #include "libpldm/requester/pldm.h" |
| 5 | #include "transport.h" |
| 6 | |
Dung Cao | abaf61f | 2022-11-15 20:48:50 +0700 | [diff] [blame] | 7 | #include <errno.h> |
| 8 | #include <limits.h> |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 9 | #ifdef PLDM_HAS_POLL |
| 10 | #include <poll.h> |
| 11 | #endif |
Dung Cao | abaf61f | 2022-11-15 20:48:50 +0700 | [diff] [blame] | 12 | #include <stdbool.h> |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 13 | #include <stdlib.h> |
Dung Cao | abaf61f | 2022-11-15 20:48:50 +0700 | [diff] [blame] | 14 | #include <sys/time.h> |
| 15 | #include <time.h> |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 16 | #include <unistd.h> |
| 17 | |
| 18 | #ifndef PLDM_HAS_POLL |
| 19 | struct pollfd { |
| 20 | int fd; /* file descriptor */ |
| 21 | short events; /* requested events */ |
| 22 | short revents; /* returned events */ |
| 23 | }; |
| 24 | |
| 25 | static inline int poll(struct pollfd *fds __attribute__((unused)), |
| 26 | int nfds __attribute__((unused)), |
| 27 | int timeout __attribute__((unused))) |
| 28 | { |
| 29 | return 0; |
| 30 | } |
| 31 | #endif |
| 32 | |
Andrew Jeffery | 0a6d682 | 2023-08-22 21:40:32 +0930 | [diff] [blame] | 33 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 3380a6c | 2023-07-12 13:57:33 +0930 | [diff] [blame] | 34 | int pldm_transport_poll(struct pldm_transport *transport, int timeout) |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 35 | { |
| 36 | struct pollfd pollfd; |
| 37 | int rc = 0; |
| 38 | if (!transport) { |
| 39 | return PLDM_REQUESTER_INVALID_SETUP; |
| 40 | } |
Andrew Jeffery | 3380a6c | 2023-07-12 13:57:33 +0930 | [diff] [blame] | 41 | |
| 42 | /* If polling isn't supported then always indicate the transport is ready */ |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 43 | if (!transport->init_pollfd) { |
Andrew Jeffery | 3380a6c | 2023-07-12 13:57:33 +0930 | [diff] [blame] | 44 | return 1; |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 45 | } |
| 46 | |
Andrew Jeffery | 98085fa | 2023-07-19 00:35:42 +0930 | [diff] [blame] | 47 | rc = transport->init_pollfd(transport, &pollfd); |
| 48 | if (rc < 0) { |
| 49 | return PLDM_REQUESTER_POLL_FAIL; |
| 50 | } |
| 51 | |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 52 | rc = poll(&pollfd, 1, timeout); |
| 53 | if (rc < 0) { |
| 54 | return PLDM_REQUESTER_POLL_FAIL; |
| 55 | } |
| 56 | |
Andrew Jeffery | 3380a6c | 2023-07-12 13:57:33 +0930 | [diff] [blame] | 57 | /* rc is 0 if poll(2) times out, or 1 if pollfd becomes active. */ |
| 58 | return rc; |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 59 | } |
| 60 | |
Andrew Jeffery | 0a6d682 | 2023-08-22 21:40:32 +0930 | [diff] [blame] | 61 | LIBPLDM_ABI_STABLE |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 62 | pldm_requester_rc_t pldm_transport_send_msg(struct pldm_transport *transport, |
| 63 | pldm_tid_t tid, |
Rashmica Gupta | f1ebde4 | 2023-07-31 14:17:57 +1000 | [diff] [blame] | 64 | const void *pldm_msg, |
| 65 | size_t msg_len) |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 66 | { |
Rashmica Gupta | f1ebde4 | 2023-07-31 14:17:57 +1000 | [diff] [blame] | 67 | if (!transport || !pldm_msg) { |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 68 | return PLDM_REQUESTER_INVALID_SETUP; |
| 69 | } |
| 70 | |
Rashmica Gupta | f1ebde4 | 2023-07-31 14:17:57 +1000 | [diff] [blame] | 71 | if (msg_len < sizeof(struct pldm_msg_hdr)) { |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 72 | return PLDM_REQUESTER_NOT_REQ_MSG; |
| 73 | } |
| 74 | |
Rashmica Gupta | f1ebde4 | 2023-07-31 14:17:57 +1000 | [diff] [blame] | 75 | return transport->send(transport, tid, pldm_msg, msg_len); |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 76 | } |
| 77 | |
Andrew Jeffery | 0a6d682 | 2023-08-22 21:40:32 +0930 | [diff] [blame] | 78 | LIBPLDM_ABI_STABLE |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 79 | pldm_requester_rc_t pldm_transport_recv_msg(struct pldm_transport *transport, |
Rashmica Gupta | 2457629 | 2023-07-31 14:02:41 +1000 | [diff] [blame] | 80 | pldm_tid_t *tid, void **pldm_msg, |
Rashmica Gupta | f1ebde4 | 2023-07-31 14:17:57 +1000 | [diff] [blame] | 81 | size_t *msg_len) |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 82 | { |
Rashmica Gupta | f1ebde4 | 2023-07-31 14:17:57 +1000 | [diff] [blame] | 83 | if (!transport || !msg_len) { |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 84 | return PLDM_REQUESTER_INVALID_SETUP; |
| 85 | } |
| 86 | |
| 87 | pldm_requester_rc_t rc = |
Rashmica Gupta | f1ebde4 | 2023-07-31 14:17:57 +1000 | [diff] [blame] | 88 | transport->recv(transport, tid, pldm_msg, msg_len); |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 89 | if (rc != PLDM_REQUESTER_SUCCESS) { |
| 90 | return rc; |
| 91 | } |
| 92 | |
Rashmica Gupta | f1ebde4 | 2023-07-31 14:17:57 +1000 | [diff] [blame] | 93 | if (*msg_len < sizeof(struct pldm_msg_hdr)) { |
| 94 | free(*pldm_msg); |
| 95 | *pldm_msg = NULL; |
Rashmica Gupta | 0411b71 | 2023-05-30 16:43:08 +1000 | [diff] [blame] | 96 | return PLDM_REQUESTER_INVALID_RECV_LEN; |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 97 | } |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 98 | return PLDM_REQUESTER_SUCCESS; |
| 99 | } |
| 100 | |
Dung Cao | abaf61f | 2022-11-15 20:48:50 +0700 | [diff] [blame] | 101 | static void timespec_to_timeval(const struct timespec *ts, struct timeval *tv) |
| 102 | { |
| 103 | tv->tv_sec = ts->tv_sec; |
| 104 | tv->tv_usec = ts->tv_nsec / 1000; |
| 105 | } |
| 106 | |
| 107 | /* Overflow safety must be upheld before call */ |
| 108 | static long timeval_to_msec(const struct timeval *tv) |
| 109 | { |
| 110 | return tv->tv_sec * 1000 + tv->tv_usec / 1000; |
| 111 | } |
| 112 | |
| 113 | /* If calculations on `tv` don't overflow then operations on derived |
| 114 | * intervals can't either. |
| 115 | */ |
| 116 | static bool timeval_is_valid(const struct timeval *tv) |
| 117 | { |
| 118 | if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000) { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | if (tv->tv_sec > (LONG_MAX - tv->tv_usec / 1000) / 1000) { |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | static int clock_gettimeval(clockid_t clockid, struct timeval *tv) |
| 130 | { |
| 131 | struct timespec now; |
| 132 | int rc; |
| 133 | |
| 134 | rc = clock_gettime(clockid, &now); |
| 135 | if (rc < 0) { |
| 136 | return rc; |
| 137 | } |
| 138 | |
| 139 | timespec_to_timeval(&now, tv); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Andrew Jeffery | 0a6d682 | 2023-08-22 21:40:32 +0930 | [diff] [blame] | 144 | LIBPLDM_ABI_STABLE |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 145 | pldm_requester_rc_t |
| 146 | pldm_transport_send_recv_msg(struct pldm_transport *transport, pldm_tid_t tid, |
| 147 | const void *pldm_req_msg, size_t req_msg_len, |
| 148 | void **pldm_resp_msg, size_t *resp_msg_len) |
| 149 | |
| 150 | { |
Dung Cao | abaf61f | 2022-11-15 20:48:50 +0700 | [diff] [blame] | 151 | /** |
| 152 | * Section "Requirements for requesters" in DSP0240, define the Time-out |
| 153 | * waiting for a response of the requester. |
| 154 | * PT2max = PT3min - 2*PT4max = 4800ms |
| 155 | */ |
| 156 | static const struct timeval max_response_interval = { |
| 157 | .tv_sec = 4, .tv_usec = 800000 |
| 158 | }; |
Thu Nguyen | b01fb1c | 2023-05-28 09:16:46 +0700 | [diff] [blame] | 159 | const struct pldm_msg_hdr *req_hdr; |
Dung Cao | abaf61f | 2022-11-15 20:48:50 +0700 | [diff] [blame] | 160 | struct timeval remaining; |
Thu Nguyen | b01fb1c | 2023-05-28 09:16:46 +0700 | [diff] [blame] | 161 | pldm_requester_rc_t rc; |
Dung Cao | abaf61f | 2022-11-15 20:48:50 +0700 | [diff] [blame] | 162 | struct timeval now; |
| 163 | struct timeval end; |
Dung Cao | abaf61f | 2022-11-15 20:48:50 +0700 | [diff] [blame] | 164 | int ret; |
Thu Nguyen | f56e4dc | 2023-07-24 10:39:47 +0700 | [diff] [blame] | 165 | int cnt; |
Dung Cao | abaf61f | 2022-11-15 20:48:50 +0700 | [diff] [blame] | 166 | |
Thu Nguyen | b01fb1c | 2023-05-28 09:16:46 +0700 | [diff] [blame] | 167 | if (req_msg_len < sizeof(*req_hdr) || !resp_msg_len) { |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 168 | return PLDM_REQUESTER_INVALID_SETUP; |
| 169 | } |
| 170 | |
Thu Nguyen | b01fb1c | 2023-05-28 09:16:46 +0700 | [diff] [blame] | 171 | req_hdr = pldm_req_msg; |
| 172 | |
Andrew Jeffery | 486d85d | 2023-08-31 11:12:08 +0930 | [diff] [blame] | 173 | if (!req_hdr->request) { |
| 174 | return PLDM_REQUESTER_NOT_REQ_MSG; |
| 175 | } |
| 176 | |
Thu Nguyen | f56e4dc | 2023-07-24 10:39:47 +0700 | [diff] [blame] | 177 | for (cnt = 0; cnt <= (PLDM_INSTANCE_MAX + 1) * PLDM_MAX_TIDS && |
| 178 | pldm_transport_poll(transport, 0) == 1; |
| 179 | cnt++) { |
Rashmica Gupta | 2457629 | 2023-07-31 14:02:41 +1000 | [diff] [blame] | 180 | pldm_tid_t l_tid; |
| 181 | rc = pldm_transport_recv_msg(transport, &l_tid, pldm_resp_msg, |
Thu Nguyen | f56e4dc | 2023-07-24 10:39:47 +0700 | [diff] [blame] | 182 | resp_msg_len); |
| 183 | if (rc == PLDM_REQUESTER_SUCCESS) { |
| 184 | /* This isn't the message we wanted */ |
| 185 | free(*pldm_resp_msg); |
| 186 | } |
| 187 | } |
| 188 | if (cnt == (PLDM_INSTANCE_MAX + 1) * PLDM_MAX_TIDS) { |
| 189 | return PLDM_REQUESTER_TRANSPORT_BUSY; |
| 190 | } |
| 191 | |
Dung Cao | abaf61f | 2022-11-15 20:48:50 +0700 | [diff] [blame] | 192 | rc = pldm_transport_send_msg(transport, tid, pldm_req_msg, req_msg_len); |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 193 | if (rc != PLDM_REQUESTER_SUCCESS) { |
| 194 | return rc; |
| 195 | } |
| 196 | |
Dung Cao | abaf61f | 2022-11-15 20:48:50 +0700 | [diff] [blame] | 197 | ret = clock_gettimeval(CLOCK_MONOTONIC, &now); |
| 198 | if (ret < 0) { |
| 199 | return PLDM_REQUESTER_POLL_FAIL; |
| 200 | } |
| 201 | |
| 202 | timeradd(&now, &max_response_interval, &end); |
| 203 | if (!timeval_is_valid(&end)) { |
| 204 | return PLDM_REQUESTER_POLL_FAIL; |
| 205 | } |
| 206 | |
Andrew Jeffery | 1d0e2d4 | 2023-09-22 18:02:44 +0930 | [diff] [blame] | 207 | while (timercmp(&now, &end, <)) { |
| 208 | pldm_tid_t src_tid; |
| 209 | |
Dung Cao | abaf61f | 2022-11-15 20:48:50 +0700 | [diff] [blame] | 210 | timersub(&end, &now, &remaining); |
Andrew Jeffery | 1d0e2d4 | 2023-09-22 18:02:44 +0930 | [diff] [blame] | 211 | |
Dung Cao | abaf61f | 2022-11-15 20:48:50 +0700 | [diff] [blame] | 212 | /* 0 <= `timeval_to_msec()` <= 4800, and 4800 < INT_MAX */ |
Andrew Jeffery | 3380a6c | 2023-07-12 13:57:33 +0930 | [diff] [blame] | 213 | ret = pldm_transport_poll(transport, |
| 214 | (int)(timeval_to_msec(&remaining))); |
| 215 | if (ret <= 0) { |
Andrew Jeffery | 1184f09 | 2023-09-22 17:44:46 +0930 | [diff] [blame] | 216 | return PLDM_REQUESTER_RECV_FAIL; |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 217 | } |
Dung Cao | abaf61f | 2022-11-15 20:48:50 +0700 | [diff] [blame] | 218 | |
Andrew Jeffery | 5038268 | 2023-09-22 17:46:54 +0930 | [diff] [blame] | 219 | ret = clock_gettimeval(CLOCK_MONOTONIC, &now); |
| 220 | if (ret < 0) { |
| 221 | return PLDM_REQUESTER_POLL_FAIL; |
| 222 | } |
| 223 | |
Rashmica Gupta | 2457629 | 2023-07-31 14:02:41 +1000 | [diff] [blame] | 224 | rc = pldm_transport_recv_msg(transport, &src_tid, pldm_resp_msg, |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 225 | resp_msg_len); |
Andrew Jeffery | 1d0e2d4 | 2023-09-22 18:02:44 +0930 | [diff] [blame] | 226 | if (rc != PLDM_REQUESTER_SUCCESS) { |
| 227 | continue; |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 228 | } |
Andrew Jeffery | 1d0e2d4 | 2023-09-22 18:02:44 +0930 | [diff] [blame] | 229 | |
| 230 | if (src_tid != tid || !pldm_msg_hdr_correlate_response( |
| 231 | pldm_req_msg, *pldm_resp_msg)) { |
| 232 | free(*pldm_resp_msg); |
| 233 | continue; |
| 234 | } |
| 235 | |
| 236 | return PLDM_REQUESTER_SUCCESS; |
| 237 | } |
Dung Cao | abaf61f | 2022-11-15 20:48:50 +0700 | [diff] [blame] | 238 | |
| 239 | return PLDM_REQUESTER_RECV_FAIL; |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 240 | } |