From: Arkadiusz Kubalewski <arkadiusz.kubalew...@intel.com> Interface is used to create and store an opened socket for sending and receiving SyncE PDU's on a given NIC interface.
Co-developed-by: Michal Michalik <michal.micha...@intel.com> Signed-off-by: Michal Michalik <michal.micha...@intel.com> Signed-off-by: Arkadiusz Kubalewski <arkadiusz.kubalew...@intel.com> --- v4: - changed order of patch in patch-series - changed naming 'Sync-E' -> 'SyncE' v3: - rebase patch series v2: - added buffer length parameter to recv_raw_esmc_frame to avoid buffer overruns - updated license headers synce_transport.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++ synce_transport.h | 55 +++++++++++++++++++++++++ synce_transport_private.h | 18 ++++++++ 3 files changed, 175 insertions(+) create mode 100644 synce_transport.c create mode 100644 synce_transport.h create mode 100644 synce_transport_private.h diff --git a/synce_transport.c b/synce_transport.c new file mode 100644 index 0000000..350270e --- /dev/null +++ b/synce_transport.c @@ -0,0 +1,102 @@ +/** + * @file synce_transport.c + * @brief Implements the SyncE transport interface. + * @note SPDX-FileCopyrightText: Copyright 2022 Intel Corporation + * @note SPDX-License-Identifier: GPL-2.0+ + */ +#include <errno.h> +#include <net/if.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <sys/socket.h> +#include <linux/if_ether.h> + +#include "sk.h" +#include "print.h" +#include "synce_msg.h" +#include "synce_msg_private.h" +#include "esmc_socket.h" +#include "synce_transport.h" +#include "synce_transport_private.h" + +struct synce_transport *synce_transport_create(const char *iface) +{ + struct synce_transport *transport; + + transport = malloc(sizeof(struct synce_transport)); + if (!transport) { + pr_err("transport creation in SyncE failed"); + return NULL; + } + + if (snprintf(transport->iface, IFNAMSIZ, "%s", iface) >= IFNAMSIZ) { + pr_err("interface name too long"); + goto err; + } + + transport->raw_socket_fd = open_esmc_socket(iface); + if (transport->raw_socket_fd < 0) { + pr_err("socket creation in SyncE transport failed"); + goto err; + } + + transport->iface_index = sk_interface_index(transport->raw_socket_fd, + iface); + if (transport->iface_index < 0) { + goto err_socket; + } + + return transport; + +err_socket: + close(transport->raw_socket_fd); +err: + free(transport); + + return NULL; +} + +void synce_transport_delete(struct synce_transport *transport) +{ + close(transport->raw_socket_fd); + free(transport); +} + +int synce_transport_send_pdu(struct synce_transport *transport, + struct synce_pdu *pdu) +{ + int tlvs_size, pdu_size; + + tlvs_size = synce_msg_get_esmc_tlvs_size(pdu); + + pdu_size = sizeof(pdu->header) + tlvs_size; + + if (send_raw_esmc_frame(transport->raw_socket_fd, (void *)pdu, + pdu_size, transport->iface_index)) { + return -EIO; + } + + return 0; +} + +int synce_transport_recv_pdu(struct synce_transport *transport, + struct synce_pdu *pdu) +{ + int ret; + + synce_msg_reset_tlvs(pdu); + + ret = recv_raw_esmc_frame(transport->raw_socket_fd, &pdu->data, + sizeof(pdu->data)); + if (ret < 0) { + return -EAGAIN; + } else if (ret > 0 && ret < ETH_ZLEN) { + pr_err("%s failed: received only %i bytes", __func__, ret); + return -EBADMSG; + } + + synce_msg_recover_tlvs(pdu); + + return 0; +} diff --git a/synce_transport.h b/synce_transport.h new file mode 100644 index 0000000..0d0e8e4 --- /dev/null +++ b/synce_transport.h @@ -0,0 +1,55 @@ +/** + * @file synce_transport.h + * @brief Implements the SyncE transport interface. + * @note SPDX-FileCopyrightText: Copyright 2022 Intel Corporation + * @note SPDX-License-Identifier: GPL-2.0+ + */ +#ifndef HAVE_SYNCE_TRANSPORT_H +#define HAVE_SYNCE_TRANSPORT_H + +#include <stdint.h> + +struct synce_transport; +struct ClockIdentity; +struct synce_pdu; + +/** + * Create a SyncE transport. + * + * This high level API creates SyncE transport but and initialize it. + * + * @param iface A name of interface to create transport for + * @return A SyncE transport structure + */ +struct synce_transport *synce_transport_create(const char *iface); + +/** + * Delete a SyncE transport. + * + * This high level API deletes SyncE transport and frees all memory allocations. + * + * @param transport A SyncE transport interface + */ +void synce_transport_delete(struct synce_transport *transport); + +/** + * Send PDU via SyncE transport. + * + * @param transport A SyncE transport interface + * @param pdu A pointer to a ESMC SyncE PDU + * @return Zero on success, non-zero if failure + */ +int synce_transport_send_pdu(struct synce_transport *transport, + struct synce_pdu *pdu); + +/** + * Recv PDU via SyncE transport. + * + * @param transport A SyncE transport interface + * @param pdu A pointer to a ESMC SyncE PDU + * @return Zero on success, non-zero if failure + */ +int synce_transport_recv_pdu(struct synce_transport *transport, + struct synce_pdu *pdu); + +#endif diff --git a/synce_transport_private.h b/synce_transport_private.h new file mode 100644 index 0000000..3304c9a --- /dev/null +++ b/synce_transport_private.h @@ -0,0 +1,18 @@ +/** + * @file synce_transport_private.h + * @brief Implements the SyncE transport private structures. + * @note SPDX-FileCopyrightText: Copyright 2022 Intel Corporation + * @note SPDX-License-Identifier: GPL-2.0+ + */ +#ifndef HAVE_SYNCE_TRANSPORT_PRIVATE_H +#define HAVE_SYNCE_TRANSPORT_PRIVATE_H + +#include <net/if.h> + +struct synce_transport { + char iface[IFNAMSIZ]; + int iface_index; + int raw_socket_fd; +}; + +#endif -- 2.9.5 _______________________________________________ Linuxptp-devel mailing list Linuxptp-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linuxptp-devel