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