blob: 21e402e2f43d0c3993c811d8382e60be36359835 [file] [log] [blame]
Rashmica Guptac1b66f42022-12-09 16:24:45 +11001#include "libpldm/requester/pldm.h"
Andrew Jeffery9c766792022-08-10 23:12:49 +09302#include "base.h"
Rashmica Guptac1b66f42022-12-09 16:24:45 +11003#include "libpldm/transport.h"
Andrew Jeffery9c766792022-08-10 23:12:49 +09304
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05305#include <bits/types/struct_iovec.h>
Rashmica Guptac1b66f42022-12-09 16:24:45 +11006#include <stdbool.h>
Andrew Jeffery9c766792022-08-10 23:12:49 +09307#include <stdlib.h>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05308#include <string.h>
Andrew Jeffery9c766792022-08-10 23:12:49 +09309#include <sys/socket.h>
Andrew Jeffery9c766792022-08-10 23:12:49 +093010#include <sys/un.h>
11#include <unistd.h>
12
Rashmica Guptac1b66f42022-12-09 16:24:45 +110013/* Temporary for old api */
14#include "libpldm/transport/mctp-demux.h"
15extern int
16pldm_transport_mctp_demux_get_socket_fd(struct pldm_transport_mctp_demux *ctx);
17extern struct pldm_transport_mctp_demux *
18pldm_transport_mctp_demux_init_with_fd(int mctp_fd);
19
20/* --- old APIS written in terms of the new API -- */
21/*
22 * pldm_open returns the file descriptor to the MCTP socket, which needs to
23 * persist over api calls (so a consumer can poll it for incoming messages).
24 * So we need a global variable to store the transport struct
25 */
26static struct pldm_transport_mctp_demux *open_transport;
Andrew Jeffery9c766792022-08-10 23:12:49 +093027
Andrew Jeffery319304f2023-04-05 13:53:18 +093028pldm_requester_rc_t pldm_open(void)
Andrew Jeffery9c766792022-08-10 23:12:49 +093029{
Rashmica Guptac1b66f42022-12-09 16:24:45 +110030 int fd;
31 int rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +093032
Rashmica Guptac1b66f42022-12-09 16:24:45 +110033 if (open_transport) {
Rashmica Gupta39f88322023-05-12 15:54:12 +100034 fd = pldm_transport_mctp_demux_get_socket_fd(open_transport);
35 return fd;
Andrew Jeffery9c766792022-08-10 23:12:49 +093036 }
37
Rashmica Guptac1b66f42022-12-09 16:24:45 +110038 struct pldm_transport_mctp_demux *demux = NULL;
39 rc = pldm_transport_mctp_demux_init(&demux);
40 if (rc) {
41 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +093042 }
Rashmica Guptac1b66f42022-12-09 16:24:45 +110043
44 fd = pldm_transport_mctp_demux_get_socket_fd(demux);
45
46 open_transport = demux;
Andrew Jeffery9c766792022-08-10 23:12:49 +093047
48 return fd;
49}
50
Rashmica Guptac1b66f42022-12-09 16:24:45 +110051/* This macro does the setup and teardown required for the old API to use the
52 * new API. Since the setup/teardown logic is the same for all four send/recv
53 * functions, it makes sense to only define it once. */
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093054#define PLDM_REQ_FN(eid, fd, fn, ...) \
55 do { \
56 struct pldm_transport_mctp_demux *demux; \
57 bool using_open_transport = false; \
58 pldm_requester_rc_t rc; \
59 pldm_tid_t tid = 1; \
60 struct pldm_transport *ctx; \
Rashmica Guptac1b66f42022-12-09 16:24:45 +110061 /* The fd can be for a socket we opened or one the consumer \
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093062 * opened. */ \
63 if (open_transport && \
64 mctp_fd == pldm_transport_mctp_demux_get_socket_fd( \
65 open_transport)) { \
66 using_open_transport = true; \
67 demux = open_transport; \
68 } else { \
69 demux = pldm_transport_mctp_demux_init_with_fd(fd); \
70 if (!demux) { \
71 rc = PLDM_REQUESTER_OPEN_FAIL; \
72 goto transport_out; \
73 } \
74 } \
75 ctx = pldm_transport_mctp_demux_core(demux); \
76 rc = pldm_transport_mctp_demux_map_tid(demux, tid, eid); \
77 if (rc) { \
78 rc = PLDM_REQUESTER_OPEN_FAIL; \
79 goto transport_out; \
80 } \
81 rc = fn(ctx, tid, __VA_ARGS__); \
82 transport_out: \
83 if (!using_open_transport) { \
84 pldm_transport_mctp_demux_destroy(demux); \
85 } \
86 return rc; \
Rashmica Guptac1b66f42022-12-09 16:24:45 +110087 } while (0)
Andrew Jeffery9c766792022-08-10 23:12:49 +093088
89pldm_requester_rc_t pldm_recv_any(mctp_eid_t eid, int mctp_fd,
90 uint8_t **pldm_resp_msg, size_t *resp_msg_len)
91{
Rashmica Guptac1b66f42022-12-09 16:24:45 +110092 PLDM_REQ_FN(eid, mctp_fd, pldm_transport_recv_msg,
93 (void **)pldm_resp_msg, resp_msg_len);
Andrew Jeffery9c766792022-08-10 23:12:49 +093094}
95
Rashmica Guptac1b66f42022-12-09 16:24:45 +110096pldm_requester_rc_t pldm_recv(mctp_eid_t eid, int mctp_fd,
97 __attribute__((unused)) uint8_t instance_id,
Andrew Jeffery9c766792022-08-10 23:12:49 +093098 uint8_t **pldm_resp_msg, size_t *resp_msg_len)
99{
100 pldm_requester_rc_t rc =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930101 pldm_recv_any(eid, mctp_fd, pldm_resp_msg, resp_msg_len);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930102 struct pldm_msg_hdr *hdr = (struct pldm_msg_hdr *)(*pldm_resp_msg);
103 if (hdr->instance_id != instance_id) {
104 free(*pldm_resp_msg);
Rashmica Guptac1b66f42022-12-09 16:24:45 +1100105 *pldm_resp_msg = NULL;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930106 return PLDM_REQUESTER_INSTANCE_ID_MISMATCH;
107 }
Rashmica Guptac1b66f42022-12-09 16:24:45 +1100108 return rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930109}
110
111pldm_requester_rc_t pldm_send_recv(mctp_eid_t eid, int mctp_fd,
112 const uint8_t *pldm_req_msg,
113 size_t req_msg_len, uint8_t **pldm_resp_msg,
114 size_t *resp_msg_len)
115{
Rashmica Guptac1b66f42022-12-09 16:24:45 +1100116 PLDM_REQ_FN(eid, mctp_fd, pldm_transport_send_recv_msg, pldm_req_msg,
117 req_msg_len, (void **)pldm_resp_msg, resp_msg_len);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930118}
119
120pldm_requester_rc_t pldm_send(mctp_eid_t eid, int mctp_fd,
121 const uint8_t *pldm_req_msg, size_t req_msg_len)
122{
Rashmica Guptac1b66f42022-12-09 16:24:45 +1100123 PLDM_REQ_FN(eid, mctp_fd, pldm_transport_send_msg, (void *)pldm_req_msg,
124 req_msg_len);
125}
Andrew Jeffery9c766792022-08-10 23:12:49 +0930126
Rashmica Guptac1b66f42022-12-09 16:24:45 +1100127/* Adding this here for completeness in the case we can't smoothly
128 * transition apps over to the new api */
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930129void pldm_close(void)
Rashmica Guptac1b66f42022-12-09 16:24:45 +1100130{
131 if (open_transport) {
132 pldm_transport_mctp_demux_destroy(open_transport);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930133 }
Rashmica Guptac1b66f42022-12-09 16:24:45 +1100134 open_transport = NULL;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930135}