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