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>
---
 synce_transport.c         | 116 ++++++++++++++++++++++++++++++++++++++
 synce_transport.h         |  67 ++++++++++++++++++++++
 synce_transport_private.h |  30 ++++++++++
 3 files changed, 213 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 000000000000..d32e63a6e72b
--- /dev/null
+++ b/synce_transport.c
@@ -0,0 +1,116 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/**
+ * @file synce_transport.c
+ * @brief Implements the Sync-E transport interface.
+ * @note Copyright (C) 2022 Intel Corporation
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2, as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+#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 (strnlen(iface, IFNAMSIZ) == IFNAMSIZ) {
+               pr_err("interface name too long");
+               goto err;
+       }
+       strncpy(transport->iface, iface,
+               (sizeof(transport->iface)/sizeof(*transport->iface)));
+
+       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);
+       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 000000000000..a74b5329d949
--- /dev/null
+++ b/synce_transport.h
@@ -0,0 +1,67 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/**
+ * @file synce_transport.h
+ * @brief Implements the Sync-E transport interface.
+ * @note Copyright (C) 2022 Intel Corporation
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2, as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+#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 000000000000..f73e879bf6ca
--- /dev/null
+++ b/synce_transport_private.h
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/**
+ * @file synce_transport_private.h
+ * @brief Implements the Sync-E transport private structures.
+ * @note Copyright (C) 2021 Intel Corporation
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2, as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+#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.34.1



_______________________________________________
Linuxptp-devel mailing list
Linuxptp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel

Reply via email to