requester: Add new APIs to support multiple transports
This patch has two goals: (1) enable consumers to send PLDM messages
over different transports and (2) do this in a way that allows us to
move towards a cleaner and more complete set of requester APIs.
The sole transport option of MCTP via the userspace mctp-demux-daemon is
being deprecated. New transports are being added: MCTP via the kernel
(AF_MCTP) and eventually NC-SI. As such, the requester APIs need
updating to support multiple transports, as well as not having MCTP
specific details in the APIs. To avoid a flag day, the current APIs
(pldm_send, etc) have been rewritten terms of the new APIs.
The current APIs operate at the transport level - they don't implement
all of the behaviour necessary for a requester. As such, the new APIs to
send/recv a message have the prefix `pldm_transport`, rather than
`pldm_requester`. Given the level that these APIs are operating at,
these only send and receive a PLDM message. Any additional logic, such
as looking for a response with a particular instance ID, belongs at the
requester abstraction level.
Some of the missing behaviours to fully be a PLDM requester are:
assigning instance IDs, request retransmission, implementing timeouts,
and enforcing only one PLDM request to a specific TID at a time. These
things are currently implemented in pldmd, meaning any consumer other
than pldmd using the libpldm "requester" APIs doesn't get the full
functionality of a requester and has to implement these things
themselves.
We would like to eventually move these behaviours into libpldm so the
libpldm requester APIs actually implement what is required to be a PLDM
requester.
The next steps to add in a full set of requester APIs, while enabling
the use of multiple transports looks something like this:
1) add instance id APIs into libpldm.
2) convert pldmd to use libpldm instance id APIs - so all users of PLDM
are still using the same instance id allocation method.
3) convert all consumers of libpldm over to using the new libpldm APIs,
including the instance id functions.
4) add in the AF_MCTP transport and move consumers over to it.
5) refactor the encode/decode functions to only encode/decode and not
frame the message (ie, remove the instance id from these functions)
6) add additional requester functionality into libpldm, and have these
use the `pldm_transport` APIs directly.
7) move consumers over to the new `pldm_requester` APIs.
8) remove unused code from pldmd.
Signed-off-by: Rashmica Gupta <rashmica@linux.ibm.com>
Change-Id: I06e602831f360bbd0efda53d410bfb5080b3100d
diff --git a/src/mctp-defines.h b/src/mctp-defines.h
new file mode 100644
index 0000000..6c3d086
--- /dev/null
+++ b/src/mctp-defines.h
@@ -0,0 +1,15 @@
+#ifndef MCTP_DEFINES_H
+#define MCTP_DEFINES_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define MCTP_MSG_TYPE_PLDM 1
+#define MCTP_MAX_NUM_EID 256
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // MCTP_DEFINES_H
diff --git a/src/meson.build b/src/meson.build
index fc44e8a..c1359c4 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -16,4 +16,5 @@
if get_option('requester-api').enabled()
subdir('requester')
+ subdir('transport')
endif
diff --git a/src/requester/pldm.c b/src/requester/pldm.c
index b1916de..c78512f 100644
--- a/src/requester/pldm.c
+++ b/src/requester/pldm.c
@@ -1,138 +1,110 @@
-#include "pldm.h"
+#include "libpldm/requester/pldm.h"
#include "base.h"
+#include "libpldm/transport.h"
#include <bits/types/struct_iovec.h>
+#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
-const uint8_t mctp_msg_type_pldm = 1;
+/* Temporary for old api */
+#include "libpldm/transport/mctp-demux.h"
+extern int
+pldm_transport_mctp_demux_get_socket_fd(struct pldm_transport_mctp_demux *ctx);
+extern struct pldm_transport_mctp_demux *
+pldm_transport_mctp_demux_init_with_fd(int mctp_fd);
+
+/* --- old APIS written in terms of the new API -- */
+/*
+ * pldm_open returns the file descriptor to the MCTP socket, which needs to
+ * persist over api calls (so a consumer can poll it for incoming messages).
+ * So we need a global variable to store the transport struct
+ */
+static struct pldm_transport_mctp_demux *open_transport;
pldm_requester_rc_t pldm_open(void)
{
- ssize_t rc = -1;
- int fd = -1;
+ int fd;
+ int rc;
- fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
- if (-1 == fd) {
- return fd;
+ if (open_transport) {
+ return -1;
}
- const char path[] = "\0mctp-mux";
- struct sockaddr_un addr;
- addr.sun_family = AF_UNIX;
- memcpy(addr.sun_path, path, sizeof(path) - 1);
- rc = connect(fd, (struct sockaddr *)&addr,
- sizeof(path) + sizeof(addr.sun_family) - 1);
- if (-1 == rc) {
- return PLDM_REQUESTER_OPEN_FAIL;
+ struct pldm_transport_mctp_demux *demux = NULL;
+ rc = pldm_transport_mctp_demux_init(&demux);
+ if (rc) {
+ return rc;
}
- rc = write(fd, &mctp_msg_type_pldm, sizeof(mctp_msg_type_pldm));
- if (-1 == rc) {
- return PLDM_REQUESTER_OPEN_FAIL;
- }
+
+ fd = pldm_transport_mctp_demux_get_socket_fd(demux);
+
+ open_transport = demux;
return fd;
}
-/**
- * @brief Read MCTP socket. If there's data available, return success only if
- * data is a PLDM message.
- *
- * @param[in] eid - destination MCTP eid
- * @param[in] mctp_fd - MCTP socket fd
- * @param[out] pldm_resp_msg - *pldm_resp_msg will point to PLDM msg,
- * this function allocates memory, caller to free(*pldm_resp_msg) on
- * success.
- * @param[out] resp_msg_len - caller owned pointer that will be made point to
- * the size of the PLDM msg.
- *
- * @return pldm_requester_rc_t (errno may be set). failure is returned even
- * when data was read, but wasn't a PLDM response message
- */
-static pldm_requester_rc_t mctp_recv(mctp_eid_t eid, int mctp_fd,
- uint8_t **pldm_resp_msg,
- size_t *resp_msg_len)
-{
- ssize_t min_len = sizeof(eid) + sizeof(mctp_msg_type_pldm) +
- sizeof(struct pldm_msg_hdr);
- ssize_t length = recv(mctp_fd, NULL, 0, MSG_PEEK | MSG_TRUNC);
- if (length <= 0) {
- return PLDM_REQUESTER_RECV_FAIL;
- }
- if (length < min_len) {
- /* read and discard */
- uint8_t buf[length];
- recv(mctp_fd, buf, length, 0);
- return PLDM_REQUESTER_INVALID_RECV_LEN;
- }
- struct iovec iov[2];
- size_t mctp_prefix_len = sizeof(eid) + sizeof(mctp_msg_type_pldm);
- uint8_t mctp_prefix[mctp_prefix_len];
- size_t pldm_len = length - mctp_prefix_len;
- iov[0].iov_len = mctp_prefix_len;
- iov[0].iov_base = mctp_prefix;
- *pldm_resp_msg = malloc(pldm_len);
- iov[1].iov_len = pldm_len;
- iov[1].iov_base = *pldm_resp_msg;
- struct msghdr msg = {0};
- msg.msg_iov = iov;
- msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
- ssize_t bytes = recvmsg(mctp_fd, &msg, 0);
- if (length != bytes) {
- free(*pldm_resp_msg);
- return PLDM_REQUESTER_INVALID_RECV_LEN;
- }
- if ((mctp_prefix[0] != eid) || (mctp_prefix[1] != mctp_msg_type_pldm)) {
- free(*pldm_resp_msg);
- return PLDM_REQUESTER_NOT_PLDM_MSG;
- }
- *resp_msg_len = pldm_len;
- return PLDM_REQUESTER_SUCCESS;
-}
+/* This macro does the setup and teardown required for the old API to use the
+ * new API. Since the setup/teardown logic is the same for all four send/recv
+ * functions, it makes sense to only define it once. */
+#define PLDM_REQ_FN(eid, fd, fn, ...) \
+ do { \
+ struct pldm_transport_mctp_demux *demux; \
+ bool using_open_transport = false; \
+ pldm_requester_rc_t rc; \
+ pldm_tid_t tid = 1; \
+ struct pldm_transport *ctx; \
+ /* The fd can be for a socket we opened or one the consumer \
+ * opened. */ \
+ if (open_transport && \
+ mctp_fd == pldm_transport_mctp_demux_get_socket_fd( \
+ open_transport)) { \
+ using_open_transport = true; \
+ demux = open_transport; \
+ } else { \
+ demux = pldm_transport_mctp_demux_init_with_fd(fd); \
+ if (!demux) { \
+ rc = PLDM_REQUESTER_OPEN_FAIL; \
+ goto transport_out; \
+ } \
+ } \
+ ctx = pldm_transport_mctp_demux_core(demux); \
+ rc = pldm_transport_mctp_demux_map_tid(demux, tid, eid); \
+ if (rc) { \
+ rc = PLDM_REQUESTER_OPEN_FAIL; \
+ goto transport_out; \
+ } \
+ rc = fn(ctx, tid, __VA_ARGS__); \
+ transport_out: \
+ if (!using_open_transport) { \
+ pldm_transport_mctp_demux_destroy(demux); \
+ } \
+ return rc; \
+ } while (0)
pldm_requester_rc_t pldm_recv_any(mctp_eid_t eid, int mctp_fd,
uint8_t **pldm_resp_msg, size_t *resp_msg_len)
{
- pldm_requester_rc_t rc =
- mctp_recv(eid, mctp_fd, pldm_resp_msg, resp_msg_len);
- if (rc != PLDM_REQUESTER_SUCCESS) {
- return rc;
- }
-
- struct pldm_msg_hdr *hdr = (struct pldm_msg_hdr *)(*pldm_resp_msg);
- if (hdr->request != PLDM_RESPONSE) {
- free(*pldm_resp_msg);
- return PLDM_REQUESTER_NOT_RESP_MSG;
- }
-
- uint8_t pldm_rc = 0;
- if (*resp_msg_len < (sizeof(struct pldm_msg_hdr) + sizeof(pldm_rc))) {
- free(*pldm_resp_msg);
- return PLDM_REQUESTER_RESP_MSG_TOO_SMALL;
- }
-
- return PLDM_REQUESTER_SUCCESS;
+ PLDM_REQ_FN(eid, mctp_fd, pldm_transport_recv_msg,
+ (void **)pldm_resp_msg, resp_msg_len);
}
-pldm_requester_rc_t pldm_recv(mctp_eid_t eid, int mctp_fd, uint8_t instance_id,
+pldm_requester_rc_t pldm_recv(mctp_eid_t eid, int mctp_fd,
+ __attribute__((unused)) uint8_t instance_id,
uint8_t **pldm_resp_msg, size_t *resp_msg_len)
{
pldm_requester_rc_t rc =
pldm_recv_any(eid, mctp_fd, pldm_resp_msg, resp_msg_len);
- if (rc != PLDM_REQUESTER_SUCCESS) {
- return rc;
- }
-
struct pldm_msg_hdr *hdr = (struct pldm_msg_hdr *)(*pldm_resp_msg);
if (hdr->instance_id != instance_id) {
free(*pldm_resp_msg);
+ *pldm_resp_msg = NULL;
return PLDM_REQUESTER_INSTANCE_ID_MISMATCH;
}
-
- return PLDM_REQUESTER_SUCCESS;
+ return rc;
}
pldm_requester_rc_t pldm_send_recv(mctp_eid_t eid, int mctp_fd,
@@ -140,47 +112,23 @@
size_t req_msg_len, uint8_t **pldm_resp_msg,
size_t *resp_msg_len)
{
- struct pldm_msg_hdr *hdr = (struct pldm_msg_hdr *)pldm_req_msg;
- if ((hdr->request != PLDM_REQUEST) &&
- (hdr->request != PLDM_ASYNC_REQUEST_NOTIFY)) {
- return PLDM_REQUESTER_NOT_REQ_MSG;
- }
-
- pldm_requester_rc_t rc =
- pldm_send(eid, mctp_fd, pldm_req_msg, req_msg_len);
- if (rc != PLDM_REQUESTER_SUCCESS) {
- return rc;
- }
-
- while (1) {
- rc = pldm_recv(eid, mctp_fd, hdr->instance_id, pldm_resp_msg,
- resp_msg_len);
- if (rc == PLDM_REQUESTER_SUCCESS) {
- break;
- }
- }
-
- return rc;
+ PLDM_REQ_FN(eid, mctp_fd, pldm_transport_send_recv_msg, pldm_req_msg,
+ req_msg_len, (void **)pldm_resp_msg, resp_msg_len);
}
pldm_requester_rc_t pldm_send(mctp_eid_t eid, int mctp_fd,
const uint8_t *pldm_req_msg, size_t req_msg_len)
{
- uint8_t hdr[2] = {eid, mctp_msg_type_pldm};
+ PLDM_REQ_FN(eid, mctp_fd, pldm_transport_send_msg, (void *)pldm_req_msg,
+ req_msg_len);
+}
- struct iovec iov[2];
- iov[0].iov_base = hdr;
- iov[0].iov_len = sizeof(hdr);
- iov[1].iov_base = (uint8_t *)pldm_req_msg;
- iov[1].iov_len = req_msg_len;
-
- struct msghdr msg = {0};
- msg.msg_iov = iov;
- msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
-
- ssize_t rc = sendmsg(mctp_fd, &msg, 0);
- if (rc == -1) {
- return PLDM_REQUESTER_SEND_FAIL;
+/* Adding this here for completeness in the case we can't smoothly
+ * transition apps over to the new api */
+void pldm_close()
+{
+ if (open_transport) {
+ pldm_transport_mctp_demux_destroy(open_transport);
}
- return PLDM_REQUESTER_SUCCESS;
+ open_transport = NULL;
}
diff --git a/src/transport/container-of.h b/src/transport/container-of.h
new file mode 100644
index 0000000..facefe5
--- /dev/null
+++ b/src/transport/container-of.h
@@ -0,0 +1,9 @@
+#ifndef CONTAINER_OF_H
+#define CONTAINER_OF_H
+
+#ifndef container_of
+#define container_of(ptr, type, member) \
+ (type *)((char *)(ptr)-offsetof(type, member))
+#endif
+
+#endif
diff --git a/src/transport/mctp-demux.c b/src/transport/mctp-demux.c
new file mode 100644
index 0000000..4b56f2f
--- /dev/null
+++ b/src/transport/mctp-demux.c
@@ -0,0 +1,259 @@
+#include "../mctp-defines.h"
+#include "base.h"
+#include "container-of.h"
+#include "libpldm/pldm.h"
+#include "libpldm/transport.h"
+#include "transport.h"
+
+#include <errno.h>
+#include <poll.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#include <unistd.h>
+
+#define MCTP_DEMUX_NAME "libmctp-demux-daemon"
+const uint8_t mctp_msg_type = MCTP_MSG_TYPE_PLDM;
+
+struct pldm_transport_mctp_demux {
+ struct pldm_transport transport;
+ int socket;
+ /* In the future this probably needs to move to a tid-eid-uuid/network
+ * id mapping for multi mctp networks */
+ pldm_tid_t tid_eid_map[MCTP_MAX_NUM_EID];
+};
+
+#define transport_to_demux(ptr) \
+ container_of(ptr, struct pldm_transport_mctp_demux, transport)
+
+struct pldm_transport *
+pldm_transport_mctp_demux_core(struct pldm_transport_mctp_demux *ctx)
+{
+ return &ctx->transport;
+}
+
+static pldm_requester_rc_t pldm_transport_mctp_demux_open(void)
+{
+ int fd = -1;
+ ssize_t rc = -1;
+
+ fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
+ if (fd == -1) {
+ return fd;
+ }
+
+ const char path[] = "\0mctp-mux";
+ struct sockaddr_un addr;
+ addr.sun_family = AF_UNIX;
+ memcpy(addr.sun_path, path, sizeof(path) - 1);
+ rc = connect(fd, (struct sockaddr *)&addr,
+ sizeof(path) + sizeof(addr.sun_family) - 1);
+ if (rc == -1) {
+ return PLDM_REQUESTER_OPEN_FAIL;
+ }
+ rc = write(fd, &mctp_msg_type, sizeof(mctp_msg_type));
+ if (rc == -1) {
+ return PLDM_REQUESTER_OPEN_FAIL;
+ }
+
+ return fd;
+}
+
+int pldm_transport_mctp_demux_init_pollfd(struct pldm_transport *t,
+ struct pollfd *pollfd)
+{
+ struct pldm_transport_mctp_demux *ctx = transport_to_demux(t);
+ pollfd->fd = ctx->socket;
+ pollfd->events = POLLIN;
+ return 0;
+}
+
+static int
+pldm_transport_mctp_demux_get_eid(struct pldm_transport_mctp_demux *ctx,
+ pldm_tid_t tid, mctp_eid_t *eid)
+{
+ int i;
+ for (i = 0; i < MCTP_MAX_NUM_EID; i++) {
+ if (ctx->tid_eid_map[i] == tid) {
+ *eid = i;
+ return 0;
+ }
+ }
+ *eid = -1;
+ return -1;
+}
+
+int pldm_transport_mctp_demux_map_tid(struct pldm_transport_mctp_demux *ctx,
+ pldm_tid_t tid, mctp_eid_t eid)
+{
+ ctx->tid_eid_map[eid] = tid;
+
+ return 0;
+}
+
+int pldm_transport_mctp_demux_unmap_tid(struct pldm_transport_mctp_demux *ctx,
+ __attribute__((unused)) pldm_tid_t tid,
+ mctp_eid_t eid)
+{
+ ctx->tid_eid_map[eid] = 0;
+
+ return 0;
+}
+
+static pldm_requester_rc_t
+pldm_transport_mctp_demux_recv(struct pldm_transport *t, pldm_tid_t tid,
+ void **pldm_resp_msg, size_t *resp_msg_len)
+{
+ struct pldm_transport_mctp_demux *demux = transport_to_demux(t);
+ mctp_eid_t eid = 0;
+ int rc = pldm_transport_mctp_demux_get_eid(demux, tid, &eid);
+ if (rc) {
+ return PLDM_REQUESTER_RECV_FAIL;
+ }
+
+ ssize_t min_len =
+ sizeof(eid) + sizeof(mctp_msg_type) + sizeof(struct pldm_msg_hdr);
+ ssize_t length = recv(demux->socket, NULL, 0, MSG_PEEK | MSG_TRUNC);
+ if (length <= 0) {
+ return PLDM_REQUESTER_RECV_FAIL;
+ }
+ uint8_t *buf = malloc(length);
+ if (buf == NULL) {
+ return PLDM_REQUESTER_RECV_FAIL;
+ }
+ if (length < min_len) {
+ /* read and discard */
+ recv(demux->socket, buf, length, 0);
+ free(buf);
+ return PLDM_REQUESTER_INVALID_RECV_LEN;
+ }
+ struct iovec iov[2];
+ uint8_t mctp_prefix[2];
+ size_t mctp_prefix_len = 2;
+ size_t pldm_len = length - mctp_prefix_len;
+ iov[0].iov_len = mctp_prefix_len;
+ iov[0].iov_base = mctp_prefix;
+ *pldm_resp_msg = buf;
+ if (*pldm_resp_msg == NULL) {
+ return PLDM_REQUESTER_RECV_FAIL;
+ }
+ iov[1].iov_len = pldm_len;
+ iov[1].iov_base = *pldm_resp_msg;
+ struct msghdr msg = {0};
+ msg.msg_iov = iov;
+ msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
+ ssize_t bytes = recvmsg(demux->socket, &msg, 0);
+ if (length != bytes) {
+ free(*pldm_resp_msg);
+ *pldm_resp_msg = NULL;
+ return PLDM_REQUESTER_INVALID_RECV_LEN;
+ }
+ if ((mctp_prefix[0] != eid) || (mctp_prefix[1] != mctp_msg_type)) {
+ free(*pldm_resp_msg);
+ *pldm_resp_msg = NULL;
+ return PLDM_REQUESTER_NOT_PLDM_MSG;
+ }
+ *resp_msg_len = pldm_len;
+ return PLDM_REQUESTER_SUCCESS;
+}
+
+static pldm_requester_rc_t
+pldm_transport_mctp_demux_send(struct pldm_transport *t, pldm_tid_t tid,
+ const void *pldm_req_msg, size_t req_msg_len)
+{
+ struct pldm_transport_mctp_demux *demux = transport_to_demux(t);
+ mctp_eid_t eid = 0;
+ if (pldm_transport_mctp_demux_get_eid(demux, tid, &eid)) {
+ return PLDM_REQUESTER_SEND_FAIL;
+ }
+
+ uint8_t hdr[2] = {eid, mctp_msg_type};
+
+ struct iovec iov[2];
+ iov[0].iov_base = hdr;
+ iov[0].iov_len = sizeof(hdr);
+ iov[1].iov_base = (uint8_t *)pldm_req_msg;
+ iov[1].iov_len = req_msg_len;
+
+ struct msghdr msg = {0};
+ msg.msg_iov = iov;
+ msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
+
+ ssize_t rc = sendmsg(demux->socket, &msg, 0);
+ if (rc == -1) {
+ return PLDM_REQUESTER_SEND_FAIL;
+ }
+ return PLDM_REQUESTER_SUCCESS;
+}
+
+int pldm_transport_mctp_demux_init(struct pldm_transport_mctp_demux **ctx)
+{
+ if (!ctx || *ctx) {
+ return -EINVAL;
+ }
+
+ struct pldm_transport_mctp_demux *demux =
+ calloc(1, sizeof(struct pldm_transport_mctp_demux));
+ if (!demux) {
+ return -ENOMEM;
+ }
+
+ demux->transport.name = MCTP_DEMUX_NAME;
+ demux->transport.version = 1;
+ demux->transport.recv = pldm_transport_mctp_demux_recv;
+ demux->transport.send = pldm_transport_mctp_demux_send;
+ demux->transport.init_pollfd = pldm_transport_mctp_demux_init_pollfd;
+ demux->socket = pldm_transport_mctp_demux_open();
+ if (demux->socket == -1) {
+ free(demux);
+ return -1;
+ }
+ *ctx = demux;
+ return 0;
+}
+
+void pldm_transport_mctp_demux_destroy(struct pldm_transport_mctp_demux *ctx)
+{
+ if (!ctx) {
+ return;
+ }
+ close(ctx->socket);
+ free(ctx);
+}
+
+/* Temporary for old API */
+struct pldm_transport_mctp_demux *
+pldm_transport_mctp_demux_init_with_fd(int mctp_fd)
+{
+ struct pldm_transport_mctp_demux *demux =
+ calloc(1, sizeof(struct pldm_transport_mctp_demux));
+ if (!demux) {
+ return NULL;
+ }
+
+ demux->transport.name = MCTP_DEMUX_NAME;
+ demux->transport.version = 1;
+ demux->transport.recv = pldm_transport_mctp_demux_recv;
+ demux->transport.send = pldm_transport_mctp_demux_send;
+ demux->transport.init_pollfd = pldm_transport_mctp_demux_init_pollfd;
+ /* dup is so we can call pldm_transport_mctp_demux_destroy which closes
+ * the socket, without closing the fd that is being used by the consumer
+ */
+ demux->socket = dup(mctp_fd);
+ if (demux->socket == -1) {
+ free(demux);
+ return NULL;
+ }
+ return demux;
+}
+
+int pldm_transport_mctp_demux_get_socket_fd(
+ struct pldm_transport_mctp_demux *ctx)
+{
+ if (ctx && ctx->socket) {
+ return ctx->socket;
+ }
+ return -1;
+}
diff --git a/src/transport/meson.build b/src/transport/meson.build
new file mode 100644
index 0000000..1a2acc9
--- /dev/null
+++ b/src/transport/meson.build
@@ -0,0 +1,4 @@
+libpldm_sources += files(
+ 'mctp-demux.c',
+ 'transport.c'
+)
diff --git a/src/transport/transport.c b/src/transport/transport.c
new file mode 100644
index 0000000..0681e23
--- /dev/null
+++ b/src/transport/transport.c
@@ -0,0 +1,130 @@
+#include "libpldm/transport.h"
+#include "base.h"
+#include "libpldm/requester/pldm.h"
+#include "transport.h"
+
+#ifdef PLDM_HAS_POLL
+#include <poll.h>
+#endif
+#include <stdlib.h>
+#include <unistd.h>
+
+#ifndef PLDM_HAS_POLL
+struct pollfd {
+ int fd; /* file descriptor */
+ short events; /* requested events */
+ short revents; /* returned events */
+};
+
+static inline int poll(struct pollfd *fds __attribute__((unused)),
+ int nfds __attribute__((unused)),
+ int timeout __attribute__((unused)))
+{
+ return 0;
+}
+#endif
+
+pldm_requester_rc_t pldm_transport_poll(struct pldm_transport *transport,
+ int timeout)
+{
+ struct pollfd pollfd;
+ int rc = 0;
+ if (!transport) {
+ return PLDM_REQUESTER_INVALID_SETUP;
+ }
+ if (!transport->init_pollfd) {
+ return PLDM_REQUESTER_SUCCESS;
+ }
+
+ transport->init_pollfd(transport, &pollfd);
+ rc = poll(&pollfd, 1, timeout);
+ if (rc < 0) {
+ return PLDM_REQUESTER_POLL_FAIL;
+ }
+
+ return PLDM_REQUESTER_SUCCESS;
+}
+
+pldm_requester_rc_t pldm_transport_send_msg(struct pldm_transport *transport,
+ pldm_tid_t tid,
+ const void *pldm_req_msg,
+ size_t req_msg_len)
+{
+ if (!transport || !pldm_req_msg) {
+ return PLDM_REQUESTER_INVALID_SETUP;
+ }
+
+ if (req_msg_len < sizeof(struct pldm_msg_hdr)) {
+ return PLDM_REQUESTER_NOT_REQ_MSG;
+ }
+
+ const struct pldm_msg_hdr *hdr = pldm_req_msg;
+ if (!hdr->request) {
+ return PLDM_REQUESTER_NOT_REQ_MSG;
+ }
+
+ return transport->send(transport, tid, pldm_req_msg, req_msg_len);
+}
+
+pldm_requester_rc_t pldm_transport_recv_msg(struct pldm_transport *transport,
+ pldm_tid_t tid,
+ void **pldm_resp_msg,
+ size_t *resp_msg_len)
+{
+ if (!transport || !resp_msg_len) {
+ return PLDM_REQUESTER_INVALID_SETUP;
+ }
+
+ pldm_requester_rc_t rc =
+ transport->recv(transport, tid, pldm_resp_msg, resp_msg_len);
+ if (rc != PLDM_REQUESTER_SUCCESS) {
+ return rc;
+ }
+
+ struct pldm_msg_hdr *hdr = *pldm_resp_msg;
+ if (hdr->request || hdr->datagram) {
+ free(*pldm_resp_msg);
+ *pldm_resp_msg = NULL;
+ return PLDM_REQUESTER_NOT_RESP_MSG;
+ }
+
+ uint8_t pldm_rc = 0;
+ if (*resp_msg_len < (sizeof(struct pldm_msg_hdr) + sizeof(pldm_rc))) {
+ free(*pldm_resp_msg);
+ *pldm_resp_msg = NULL;
+ return PLDM_REQUESTER_RESP_MSG_TOO_SMALL;
+ }
+
+ return PLDM_REQUESTER_SUCCESS;
+}
+
+pldm_requester_rc_t
+pldm_transport_send_recv_msg(struct pldm_transport *transport, pldm_tid_t tid,
+ const void *pldm_req_msg, size_t req_msg_len,
+ void **pldm_resp_msg, size_t *resp_msg_len)
+
+{
+ if (!resp_msg_len) {
+ return PLDM_REQUESTER_INVALID_SETUP;
+ }
+
+ pldm_requester_rc_t rc =
+ pldm_transport_send_msg(transport, tid, pldm_req_msg, req_msg_len);
+ if (rc != PLDM_REQUESTER_SUCCESS) {
+ return rc;
+ }
+
+ while (1) {
+ rc = pldm_transport_poll(transport, -1);
+ if (rc != PLDM_REQUESTER_SUCCESS) {
+ break;
+ }
+ rc = pldm_transport_recv_msg(transport, tid, pldm_resp_msg,
+ resp_msg_len);
+ if (rc == PLDM_REQUESTER_SUCCESS) {
+ break;
+ }
+ }
+
+ return rc;
+}
diff --git a/src/transport/transport.h b/src/transport/transport.h
new file mode 100644
index 0000000..28d7f9b
--- /dev/null
+++ b/src/transport/transport.h
@@ -0,0 +1,38 @@
+#ifndef TRANSPORT_H
+#define TRANSPORT_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "libpldm/base.h"
+#include "libpldm/requester/pldm.h"
+struct pollfd;
+
+/**
+ * @brief Generic PLDM transport struct
+ *
+ * @var name - name of the transport
+ * @var version - version of transport to use
+ * @var recv - pointer to the transport specific function to receive a message
+ * @var send - pointer to the transport specific function to send a message
+ * @var init_pollfd - pointer to the transport specific init_pollfd function
+ */
+struct pldm_transport {
+ const char *name;
+ uint8_t version;
+ pldm_requester_rc_t (*recv)(struct pldm_transport *transport,
+ pldm_tid_t tid, void **pldm_resp_msg,
+ size_t *resp_msg_len);
+ pldm_requester_rc_t (*send)(struct pldm_transport *transport,
+ pldm_tid_t tid, const void *pldm_req_msg,
+ size_t req_msg_len);
+ int (*init_pollfd)(struct pldm_transport *transport,
+ struct pollfd *pollfd);
+};
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // TRANSPORT_H