Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1 | #include "mctp-defines.h" |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 2 | #include "base.h" |
| 3 | #include "container-of.h" |
| 4 | #include "libpldm/pldm.h" |
| 5 | #include "libpldm/transport.h" |
BonnieLo-wiwynn | 36402ff | 2023-08-16 15:38:51 +0800 | [diff] [blame^] | 6 | #include "libpldm/transport/af-mctp.h" |
Rashmica Gupta | 04273e9 | 2023-06-16 16:03:37 +1000 | [diff] [blame] | 7 | #include "socket.h" |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 8 | #include "transport.h" |
| 9 | |
| 10 | #include <errno.h> |
Rashmica Gupta | 04273e9 | 2023-06-16 16:03:37 +1000 | [diff] [blame] | 11 | #include <limits.h> |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 12 | #include <linux/mctp.h> |
| 13 | #include <poll.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | #include <sys/socket.h> |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/un.h> |
| 19 | #include <unistd.h> |
| 20 | |
| 21 | #define AF_MCTP_NAME "AF_MCTP" |
| 22 | struct pldm_transport_af_mctp { |
| 23 | struct pldm_transport transport; |
| 24 | int socket; |
| 25 | pldm_tid_t tid_eid_map[MCTP_MAX_NUM_EID]; |
Rashmica Gupta | 04273e9 | 2023-06-16 16:03:37 +1000 | [diff] [blame] | 26 | struct pldm_socket_sndbuf socket_send_buf; |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | #define transport_to_af_mctp(ptr) \ |
| 30 | container_of(ptr, struct pldm_transport_af_mctp, transport) |
| 31 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 32 | LIBPLDM_ABI_TESTING |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 33 | struct pldm_transport * |
| 34 | pldm_transport_af_mctp_core(struct pldm_transport_af_mctp *ctx) |
| 35 | { |
| 36 | return &ctx->transport; |
| 37 | } |
| 38 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 39 | LIBPLDM_ABI_TESTING |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 40 | int pldm_transport_af_mctp_init_pollfd(struct pldm_transport *t, |
| 41 | struct pollfd *pollfd) |
| 42 | { |
| 43 | struct pldm_transport_af_mctp *ctx = transport_to_af_mctp(t); |
| 44 | pollfd->fd = ctx->socket; |
| 45 | pollfd->events = POLLIN; |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | static int pldm_transport_af_mctp_get_eid(struct pldm_transport_af_mctp *ctx, |
| 50 | pldm_tid_t tid, mctp_eid_t *eid) |
| 51 | { |
| 52 | int i; |
| 53 | for (i = 0; i < MCTP_MAX_NUM_EID; i++) { |
| 54 | if (ctx->tid_eid_map[i] == tid) { |
| 55 | *eid = i; |
| 56 | return 0; |
| 57 | } |
| 58 | } |
| 59 | *eid = -1; |
| 60 | return -1; |
| 61 | } |
| 62 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 63 | LIBPLDM_ABI_TESTING |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 64 | int pldm_transport_af_mctp_map_tid(struct pldm_transport_af_mctp *ctx, |
| 65 | pldm_tid_t tid, mctp_eid_t eid) |
| 66 | { |
| 67 | ctx->tid_eid_map[eid] = tid; |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 72 | LIBPLDM_ABI_TESTING |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 73 | int pldm_transport_af_mctp_unmap_tid(struct pldm_transport_af_mctp *ctx, |
| 74 | __attribute__((unused)) pldm_tid_t tid, |
| 75 | mctp_eid_t eid) |
| 76 | { |
| 77 | ctx->tid_eid_map[eid] = 0; |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static pldm_requester_rc_t pldm_transport_af_mctp_recv(struct pldm_transport *t, |
| 83 | pldm_tid_t tid, |
| 84 | void **pldm_resp_msg, |
| 85 | size_t *resp_msg_len) |
| 86 | { |
| 87 | struct pldm_transport_af_mctp *af_mctp = transport_to_af_mctp(t); |
| 88 | mctp_eid_t eid = 0; |
| 89 | int rc = pldm_transport_af_mctp_get_eid(af_mctp, tid, &eid); |
| 90 | if (rc) { |
| 91 | return PLDM_REQUESTER_RECV_FAIL; |
| 92 | } |
| 93 | |
| 94 | ssize_t length = recv(af_mctp->socket, NULL, 0, MSG_PEEK | MSG_TRUNC); |
| 95 | if (length <= 0) { |
| 96 | return PLDM_REQUESTER_RECV_FAIL; |
| 97 | } |
| 98 | *pldm_resp_msg = malloc(length); |
| 99 | length = recv(af_mctp->socket, *pldm_resp_msg, length, MSG_TRUNC); |
| 100 | if (length < (ssize_t)sizeof(struct pldm_msg_hdr)) { |
| 101 | free(*pldm_resp_msg); |
| 102 | return PLDM_REQUESTER_INVALID_RECV_LEN; |
| 103 | } |
| 104 | *resp_msg_len = length; |
| 105 | return PLDM_REQUESTER_SUCCESS; |
| 106 | } |
| 107 | |
| 108 | static pldm_requester_rc_t pldm_transport_af_mctp_send(struct pldm_transport *t, |
| 109 | pldm_tid_t tid, |
| 110 | const void *pldm_req_msg, |
| 111 | size_t req_msg_len) |
| 112 | { |
| 113 | struct pldm_transport_af_mctp *af_mctp = transport_to_af_mctp(t); |
| 114 | mctp_eid_t eid = 0; |
| 115 | if (pldm_transport_af_mctp_get_eid(af_mctp, tid, &eid)) { |
| 116 | return PLDM_REQUESTER_SEND_FAIL; |
| 117 | } |
| 118 | |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 119 | struct sockaddr_mctp addr = { 0 }; |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 120 | addr.smctp_family = AF_MCTP; |
| 121 | addr.smctp_addr.s_addr = eid; |
| 122 | addr.smctp_type = MCTP_MSG_TYPE_PLDM; |
| 123 | addr.smctp_tag = MCTP_TAG_OWNER; |
| 124 | |
Rashmica Gupta | 04273e9 | 2023-06-16 16:03:37 +1000 | [diff] [blame] | 125 | if (req_msg_len > INT_MAX || |
| 126 | pldm_socket_sndbuf_accomodate(&(af_mctp->socket_send_buf), |
| 127 | (int)req_msg_len)) { |
| 128 | return PLDM_REQUESTER_SEND_FAIL; |
| 129 | } |
| 130 | |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 131 | ssize_t rc = sendto(af_mctp->socket, pldm_req_msg, req_msg_len, 0, |
| 132 | (struct sockaddr *)&addr, sizeof(addr)); |
| 133 | if (rc == -1) { |
| 134 | return PLDM_REQUESTER_SEND_FAIL; |
| 135 | } |
| 136 | return PLDM_REQUESTER_SUCCESS; |
| 137 | } |
| 138 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 139 | LIBPLDM_ABI_TESTING |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 140 | int pldm_transport_af_mctp_init(struct pldm_transport_af_mctp **ctx) |
| 141 | { |
| 142 | if (!ctx || *ctx) { |
| 143 | return -EINVAL; |
| 144 | } |
| 145 | |
| 146 | struct pldm_transport_af_mctp *af_mctp = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 147 | calloc(1, sizeof(struct pldm_transport_af_mctp)); |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 148 | if (!af_mctp) { |
| 149 | return -ENOMEM; |
| 150 | } |
| 151 | |
| 152 | af_mctp->transport.name = AF_MCTP_NAME; |
| 153 | af_mctp->transport.version = 1; |
| 154 | af_mctp->transport.recv = pldm_transport_af_mctp_recv; |
| 155 | af_mctp->transport.send = pldm_transport_af_mctp_send; |
Delphine CC Chiu | b4cd517 | 2023-08-15 11:30:44 +0800 | [diff] [blame] | 156 | af_mctp->transport.init_pollfd = pldm_transport_af_mctp_init_pollfd; |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 157 | af_mctp->socket = socket(AF_MCTP, SOCK_DGRAM, 0); |
| 158 | if (af_mctp->socket == -1) { |
| 159 | free(af_mctp); |
| 160 | return -1; |
| 161 | } |
Rashmica Gupta | 04273e9 | 2023-06-16 16:03:37 +1000 | [diff] [blame] | 162 | |
| 163 | if (pldm_socket_sndbuf_init(&af_mctp->socket_send_buf, |
| 164 | af_mctp->socket)) { |
| 165 | close(af_mctp->socket); |
| 166 | free(af_mctp); |
| 167 | return -1; |
| 168 | } |
| 169 | |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 170 | *ctx = af_mctp; |
| 171 | return 0; |
| 172 | } |
| 173 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 174 | LIBPLDM_ABI_TESTING |
Rashmica Gupta | ba6971b | 2022-12-09 16:27:38 +1100 | [diff] [blame] | 175 | void pldm_transport_af_mctp_destroy(struct pldm_transport_af_mctp *ctx) |
| 176 | { |
| 177 | if (!ctx) { |
| 178 | return; |
| 179 | } |
| 180 | close(ctx->socket); |
| 181 | free(ctx); |
| 182 | } |