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