blob: b50dbbfe1d309a42ee8ebb203325ff75c86aad2d [file] [log] [blame]
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301#include "config.h"
Rashmica Guptac1b66f42022-12-09 16:24:45 +11002#include "libpldm/requester/pldm.h"
Andrew Jeffery9c766792022-08-10 23:12:49 +09303#include "base.h"
Rashmica Guptac1b66f42022-12-09 16:24:45 +11004#include "libpldm/transport.h"
Andrew Jeffery9c766792022-08-10 23:12:49 +09305
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05306#include <bits/types/struct_iovec.h>
Rashmica Guptac1b66f42022-12-09 16:24:45 +11007#include <stdbool.h>
Andrew Jeffery9c766792022-08-10 23:12:49 +09308#include <stdlib.h>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05309#include <string.h>
Andrew Jeffery9c766792022-08-10 23:12:49 +093010#include <sys/socket.h>
Andrew Jeffery9c766792022-08-10 23:12:49 +093011#include <sys/un.h>
12#include <unistd.h>
13
Rashmica Guptac1b66f42022-12-09 16:24:45 +110014/* Temporary for old api */
15#include "libpldm/transport/mctp-demux.h"
16extern int
17pldm_transport_mctp_demux_get_socket_fd(struct pldm_transport_mctp_demux *ctx);
18extern struct pldm_transport_mctp_demux *
19pldm_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 */
27static struct pldm_transport_mctp_demux *open_transport;
Andrew Jeffery9c766792022-08-10 23:12:49 +093028
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +093029LIBPLDM_ABI_STABLE
Andrew Jeffery319304f2023-04-05 13:53:18 +093030pldm_requester_rc_t pldm_open(void)
Andrew Jeffery9c766792022-08-10 23:12:49 +093031{
Rashmica Guptac1b66f42022-12-09 16:24:45 +110032 int fd;
33 int rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +093034
Rashmica Guptac1b66f42022-12-09 16:24:45 +110035 if (open_transport) {
Rashmica Gupta39f88322023-05-12 15:54:12 +100036 fd = pldm_transport_mctp_demux_get_socket_fd(open_transport);
37 return fd;
Andrew Jeffery9c766792022-08-10 23:12:49 +093038 }
39
Rashmica Guptac1b66f42022-12-09 16:24:45 +110040 struct pldm_transport_mctp_demux *demux = NULL;
41 rc = pldm_transport_mctp_demux_init(&demux);
42 if (rc) {
43 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +093044 }
Rashmica Guptac1b66f42022-12-09 16:24:45 +110045
46 fd = pldm_transport_mctp_demux_get_socket_fd(demux);
47
48 open_transport = demux;
Andrew Jeffery9c766792022-08-10 23:12:49 +093049
50 return fd;
51}
52
Rashmica Guptac1b66f42022-12-09 16:24:45 +110053/* 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 Jeffery37dd6a32023-05-12 16:04:06 +093056#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 Guptac1b66f42022-12-09 16:24:45 +110063 /* The fd can be for a socket we opened or one the consumer \
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093064 * 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 Guptac1b66f42022-12-09 16:24:45 +110089 } while (0)
Andrew Jeffery9c766792022-08-10 23:12:49 +093090
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +093091LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +093092pldm_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 Guptac1b66f42022-12-09 16:24:45 +110095 PLDM_REQ_FN(eid, mctp_fd, pldm_transport_recv_msg,
96 (void **)pldm_resp_msg, resp_msg_len);
Andrew Jeffery9c766792022-08-10 23:12:49 +093097}
98
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +093099LIBPLDM_ABI_STABLE
Rashmica Guptac1b66f42022-12-09 16:24:45 +1100100pldm_requester_rc_t pldm_recv(mctp_eid_t eid, int mctp_fd,
101 __attribute__((unused)) uint8_t instance_id,
Andrew Jeffery9c766792022-08-10 23:12:49 +0930102 uint8_t **pldm_resp_msg, size_t *resp_msg_len)
103{
104 pldm_requester_rc_t rc =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930105 pldm_recv_any(eid, mctp_fd, pldm_resp_msg, resp_msg_len);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930106 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 Guptac1b66f42022-12-09 16:24:45 +1100109 *pldm_resp_msg = NULL;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930110 return PLDM_REQUESTER_INSTANCE_ID_MISMATCH;
111 }
Rashmica Guptac1b66f42022-12-09 16:24:45 +1100112 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930113}
114
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930115LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930116pldm_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 Guptac1b66f42022-12-09 16:24:45 +1100121 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 Jeffery9c766792022-08-10 23:12:49 +0930123}
124
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930125LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930126pldm_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 Guptac1b66f42022-12-09 16:24:45 +1100129 PLDM_REQ_FN(eid, mctp_fd, pldm_transport_send_msg, (void *)pldm_req_msg,
130 req_msg_len);
131}
Andrew Jeffery9c766792022-08-10 23:12:49 +0930132
Rashmica Guptac1b66f42022-12-09 16:24:45 +1100133/* Adding this here for completeness in the case we can't smoothly
134 * transition apps over to the new api */
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930135LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930136void pldm_close(void)
Rashmica Guptac1b66f42022-12-09 16:24:45 +1100137{
138 if (open_transport) {
139 pldm_transport_mctp_demux_destroy(open_transport);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930140 }
Rashmica Guptac1b66f42022-12-09 16:24:45 +1100141 open_transport = NULL;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930142}