Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1 | #include "config.h" |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 2 | #include "libpldm/requester/pldm.h" |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 3 | #include "base.h" |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 4 | #include "libpldm/transport.h" |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 5 | |
Manojkiran Eda | 9a8e497 | 2022-11-28 16:38:21 +0530 | [diff] [blame] | 6 | #include <bits/types/struct_iovec.h> |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 7 | #include <stdbool.h> |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 8 | #include <stdlib.h> |
Manojkiran Eda | 9a8e497 | 2022-11-28 16:38:21 +0530 | [diff] [blame] | 9 | #include <string.h> |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 10 | #include <sys/socket.h> |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 11 | #include <sys/un.h> |
| 12 | #include <unistd.h> |
| 13 | |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 14 | /* Temporary for old api */ |
| 15 | #include "libpldm/transport/mctp-demux.h" |
| 16 | extern int |
| 17 | pldm_transport_mctp_demux_get_socket_fd(struct pldm_transport_mctp_demux *ctx); |
| 18 | extern struct pldm_transport_mctp_demux * |
| 19 | pldm_transport_mctp_demux_init_with_fd(int mctp_fd); |
| 20 | |
| 21 | /* --- old APIS written in terms of the new API -- */ |
| 22 | /* |
| 23 | * pldm_open returns the file descriptor to the MCTP socket, which needs to |
| 24 | * persist over api calls (so a consumer can poll it for incoming messages). |
| 25 | * So we need a global variable to store the transport struct |
| 26 | */ |
| 27 | static struct pldm_transport_mctp_demux *open_transport; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 28 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 29 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 319304f | 2023-04-05 13:53:18 +0930 | [diff] [blame] | 30 | pldm_requester_rc_t pldm_open(void) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 31 | { |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 32 | int fd; |
| 33 | int rc; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 34 | |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 35 | if (open_transport) { |
Rashmica Gupta | 39f8832 | 2023-05-12 15:54:12 +1000 | [diff] [blame] | 36 | fd = pldm_transport_mctp_demux_get_socket_fd(open_transport); |
| 37 | return fd; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 38 | } |
| 39 | |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 40 | struct pldm_transport_mctp_demux *demux = NULL; |
| 41 | rc = pldm_transport_mctp_demux_init(&demux); |
| 42 | if (rc) { |
| 43 | return rc; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 44 | } |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 45 | |
| 46 | fd = pldm_transport_mctp_demux_get_socket_fd(demux); |
| 47 | |
| 48 | open_transport = demux; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 49 | |
| 50 | return fd; |
| 51 | } |
| 52 | |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 53 | /* This macro does the setup and teardown required for the old API to use the |
| 54 | * new API. Since the setup/teardown logic is the same for all four send/recv |
| 55 | * functions, it makes sense to only define it once. */ |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 56 | #define PLDM_REQ_FN(eid, fd, fn, ...) \ |
| 57 | do { \ |
| 58 | struct pldm_transport_mctp_demux *demux; \ |
| 59 | bool using_open_transport = false; \ |
| 60 | pldm_requester_rc_t rc; \ |
| 61 | pldm_tid_t tid = 1; \ |
| 62 | struct pldm_transport *ctx; \ |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 63 | /* The fd can be for a socket we opened or one the consumer \ |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 64 | * opened. */ \ |
| 65 | if (open_transport && \ |
| 66 | mctp_fd == pldm_transport_mctp_demux_get_socket_fd( \ |
| 67 | open_transport)) { \ |
| 68 | using_open_transport = true; \ |
| 69 | demux = open_transport; \ |
| 70 | } else { \ |
| 71 | demux = pldm_transport_mctp_demux_init_with_fd(fd); \ |
| 72 | if (!demux) { \ |
| 73 | rc = PLDM_REQUESTER_OPEN_FAIL; \ |
| 74 | goto transport_out; \ |
| 75 | } \ |
| 76 | } \ |
| 77 | ctx = pldm_transport_mctp_demux_core(demux); \ |
| 78 | rc = pldm_transport_mctp_demux_map_tid(demux, tid, eid); \ |
| 79 | if (rc) { \ |
| 80 | rc = PLDM_REQUESTER_OPEN_FAIL; \ |
| 81 | goto transport_out; \ |
| 82 | } \ |
| 83 | rc = fn(ctx, tid, __VA_ARGS__); \ |
| 84 | transport_out: \ |
| 85 | if (!using_open_transport) { \ |
| 86 | pldm_transport_mctp_demux_destroy(demux); \ |
| 87 | } \ |
| 88 | return rc; \ |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 89 | } while (0) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 90 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 91 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 92 | pldm_requester_rc_t pldm_recv_any(mctp_eid_t eid, int mctp_fd, |
| 93 | uint8_t **pldm_resp_msg, size_t *resp_msg_len) |
| 94 | { |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 95 | PLDM_REQ_FN(eid, mctp_fd, pldm_transport_recv_msg, |
| 96 | (void **)pldm_resp_msg, resp_msg_len); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 97 | } |
| 98 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 99 | LIBPLDM_ABI_STABLE |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 100 | pldm_requester_rc_t pldm_recv(mctp_eid_t eid, int mctp_fd, |
| 101 | __attribute__((unused)) uint8_t instance_id, |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 102 | uint8_t **pldm_resp_msg, size_t *resp_msg_len) |
| 103 | { |
| 104 | pldm_requester_rc_t rc = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 105 | pldm_recv_any(eid, mctp_fd, pldm_resp_msg, resp_msg_len); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 106 | struct pldm_msg_hdr *hdr = (struct pldm_msg_hdr *)(*pldm_resp_msg); |
| 107 | if (hdr->instance_id != instance_id) { |
| 108 | free(*pldm_resp_msg); |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 109 | *pldm_resp_msg = NULL; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 110 | return PLDM_REQUESTER_INSTANCE_ID_MISMATCH; |
| 111 | } |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 112 | return rc; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 113 | } |
| 114 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 115 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 116 | pldm_requester_rc_t pldm_send_recv(mctp_eid_t eid, int mctp_fd, |
| 117 | const uint8_t *pldm_req_msg, |
| 118 | size_t req_msg_len, uint8_t **pldm_resp_msg, |
| 119 | size_t *resp_msg_len) |
| 120 | { |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 121 | PLDM_REQ_FN(eid, mctp_fd, pldm_transport_send_recv_msg, pldm_req_msg, |
| 122 | req_msg_len, (void **)pldm_resp_msg, resp_msg_len); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 123 | } |
| 124 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 125 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 126 | pldm_requester_rc_t pldm_send(mctp_eid_t eid, int mctp_fd, |
| 127 | const uint8_t *pldm_req_msg, size_t req_msg_len) |
| 128 | { |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 129 | PLDM_REQ_FN(eid, mctp_fd, pldm_transport_send_msg, (void *)pldm_req_msg, |
| 130 | req_msg_len); |
| 131 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 132 | |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 133 | /* Adding this here for completeness in the case we can't smoothly |
| 134 | * transition apps over to the new api */ |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 135 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 136 | void pldm_close(void) |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 137 | { |
| 138 | if (open_transport) { |
| 139 | pldm_transport_mctp_demux_destroy(open_transport); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 140 | } |
Rashmica Gupta | c1b66f4 | 2022-12-09 16:24:45 +1100 | [diff] [blame] | 141 | open_transport = NULL; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 142 | } |