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> --- v2: - changed strnlen/strncpy to snprintf for increased readability - added buffer size for recieving ESMC frames - updated license headers v3: - rebase patch series 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..06ff145 --- /dev/null +++ b/synce_transport.c @@ -0,0 +1,102 @@ +/** + * @file synce_transport.c + * @brief Implements the Sync-E 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 Sync-E 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 Sync-E 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..fb7ae34 --- /dev/null +++ b/synce_transport.h @@ -0,0 +1,55 @@ +/** + * @file synce_transport.h + * @brief Implements the Sync-E 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 Sync-E transport. + * + * This high level API creates Sync-E transport but and initialize it. + * + * @param iface A name of interface to create transport for + * @return A Sync-E transport structure + */ +struct synce_transport *synce_transport_create(const char *iface); + +/** + * Delete a Sync-E transport. + * + * This high level API deletes Sync-E transport and frees all memory allocations. + * + * @param transport A Sync-E transport interface + */ +void synce_transport_delete(struct synce_transport *transport); + +/** + * Send PDU via Sync-E transport. + * + * @param transport A Sync-E transport interface + * @param pdu A pointer to a ESMC Sync-E 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 Sync-E transport. + * + * @param transport A Sync-E transport interface + * @param pdu A pointer to a ESMC Sync-E 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..f99a240 --- /dev/null +++ b/synce_transport_private.h @@ -0,0 +1,18 @@ +/** + * @file synce_transport_private.h + * @brief Implements the Sync-E 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.26.0 _______________________________________________ Linuxptp-devel mailing list Linuxptp-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linuxptp-devel