get:
Show a patch.

patch:
Update a patch.

put:
Update a patch.

GET /api/patches/8343/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 8343,
    "url": "http://patches.dpdk.org/api/patches/8343/?format=api",
    "web_url": "http://patches.dpdk.org/project/dpdk/patch/1446198204-9852-6-git-send-email-danielx.t.mrzyglod@intel.com/",
    "project": {
        "id": 1,
        "url": "http://patches.dpdk.org/api/projects/1/?format=api",
        "name": "DPDK",
        "link_name": "dpdk",
        "list_id": "dev.dpdk.org",
        "list_email": "dev@dpdk.org",
        "web_url": "http://core.dpdk.org",
        "scm_url": "git://dpdk.org/dpdk",
        "webscm_url": "http://git.dpdk.org/dpdk",
        "list_archive_url": "https://inbox.dpdk.org/dev",
        "list_archive_url_format": "https://inbox.dpdk.org/dev/{}",
        "commit_url_format": ""
    },
    "msgid": "<1446198204-9852-6-git-send-email-danielx.t.mrzyglod@intel.com>",
    "list_archive_url": "https://inbox.dpdk.org/dev/1446198204-9852-6-git-send-email-danielx.t.mrzyglod@intel.com",
    "date": "2015-10-30T09:43:23",
    "name": "[dpdk-dev,v2,5/6] example: PTP client slave minimal implementation",
    "commit_ref": null,
    "pull_url": null,
    "state": "changes-requested",
    "archived": true,
    "hash": "a23ff37707f8e7b023e9143c5c97466cf336ed74",
    "submitter": {
        "id": 23,
        "url": "http://patches.dpdk.org/api/people/23/?format=api",
        "name": "Daniel Mrzyglod",
        "email": "danielx.t.mrzyglod@intel.com"
    },
    "delegate": null,
    "mbox": "http://patches.dpdk.org/project/dpdk/patch/1446198204-9852-6-git-send-email-danielx.t.mrzyglod@intel.com/mbox/",
    "series": [],
    "comments": "http://patches.dpdk.org/api/patches/8343/comments/",
    "check": "pending",
    "checks": "http://patches.dpdk.org/api/patches/8343/checks/",
    "tags": {},
    "related": [],
    "headers": {
        "Return-Path": "<dev-bounces@dpdk.org>",
        "X-Original-To": "patchwork@dpdk.org",
        "Delivered-To": "patchwork@dpdk.org",
        "Received": [
            "from [92.243.14.124] (localhost [IPv6:::1])\n\tby dpdk.org (Postfix) with ESMTP id 2D0488E72;\n\tFri, 30 Oct 2015 10:48:22 +0100 (CET)",
            "from mga02.intel.com (mga02.intel.com [134.134.136.20])\n\tby dpdk.org (Postfix) with ESMTP id 204358E70\n\tfor <dev@dpdk.org>; Fri, 30 Oct 2015 10:48:19 +0100 (CET)",
            "from orsmga002.jf.intel.com ([10.7.209.21])\n\tby orsmga101.jf.intel.com with ESMTP; 30 Oct 2015 02:48:19 -0700",
            "from unknown ([10.217.248.15])\n\tby orsmga002.jf.intel.com with SMTP; 30 Oct 2015 02:48:17 -0700",
            "by  (sSMTP sendmail emulation); Fri, 30 Oct 2015 10:47:34 +0100"
        ],
        "X-ExtLoop1": "1",
        "X-IronPort-AV": "E=Sophos;i=\"5.20,217,1444719600\"; d=\"scan'208\";a=\"838723862\"",
        "From": "Daniel Mrzyglod <danielx.t.mrzyglod@intel.com>",
        "To": "dev@dpdk.org",
        "Date": "Fri, 30 Oct 2015 10:43:23 +0100",
        "Message-Id": "<1446198204-9852-6-git-send-email-danielx.t.mrzyglod@intel.com>",
        "X-Mailer": "git-send-email 2.1.4",
        "In-Reply-To": "<1446198204-9852-1-git-send-email-danielx.t.mrzyglod@intel.com>",
        "References": "<1443799208-9408-1-git-send-email-danielx.t.mrzyglod@intel.com>\n\t<1446198204-9852-1-git-send-email-danielx.t.mrzyglod@intel.com>",
        "Subject": "[dpdk-dev] [PATCH v2 5/6] example: PTP client slave minimal\n\timplementation",
        "X-BeenThere": "dev@dpdk.org",
        "X-Mailman-Version": "2.1.15",
        "Precedence": "list",
        "List-Id": "patches and discussions about DPDK <dev.dpdk.org>",
        "List-Unsubscribe": "<http://dpdk.org/ml/options/dev>,\n\t<mailto:dev-request@dpdk.org?subject=unsubscribe>",
        "List-Archive": "<http://dpdk.org/ml/archives/dev/>",
        "List-Post": "<mailto:dev@dpdk.org>",
        "List-Help": "<mailto:dev-request@dpdk.org?subject=help>",
        "List-Subscribe": "<http://dpdk.org/ml/listinfo/dev>,\n\t<mailto:dev-request@dpdk.org?subject=subscribe>",
        "Errors-To": "dev-bounces@dpdk.org",
        "Sender": "\"dev\" <dev-bounces@dpdk.org>"
    },
    "content": "Add a sample application that acts as a PTP slave using the\nDPDK ieee1588 functions.\n\nSigned-off-by: Daniel Mrzyglod <danielx.t.mrzyglod@intel.com>\n---\n MAINTAINERS                          |   3 +\n doc/guides/rel_notes/release_2_2.rst |   5 +\n examples/Makefile                    |   1 +\n examples/ptpclient/Makefile          |  57 +++\n examples/ptpclient/ptpclient.c       | 788 +++++++++++++++++++++++++++++++++++\n 5 files changed, 854 insertions(+)\n create mode 100644 examples/ptpclient/Makefile\n create mode 100644 examples/ptpclient/ptpclient.c",
    "diff": "diff --git a/MAINTAINERS b/MAINTAINERS\nindex 080a8e8..3e446d5 100644\n--- a/MAINTAINERS\n+++ b/MAINTAINERS\n@@ -514,3 +514,6 @@ F: examples/tep_termination/\n F: examples/vmdq/\n F: examples/vmdq_dcb/\n F: doc/guides/sample_app_ug/vmdq_dcb_forwarding.rst\n+\n+M: Daniel Mrzyglod <danielx.t.mrzyglod@intel.com>\n+F: examples/ptpclient\ndiff --git a/doc/guides/rel_notes/release_2_2.rst b/doc/guides/rel_notes/release_2_2.rst\nindex b83ef7f..840d8ea 100644\n--- a/doc/guides/rel_notes/release_2_2.rst\n+++ b/doc/guides/rel_notes/release_2_2.rst\n@@ -92,6 +92,11 @@ Libraries\n Examples\n ~~~~~~~~\n \n+* **ptpclient: simple PTP slave client.**\n+\n+  Add a sample application that acts as a PTP slave using the\n+  DPDK ieee1588 functions.\n+\n \n Other\n ~~~~~\ndiff --git a/examples/Makefile b/examples/Makefile\nindex b4eddbd..4672534 100644\n--- a/examples/Makefile\n+++ b/examples/Makefile\n@@ -74,5 +74,6 @@ DIRS-$(CONFIG_RTE_LIBRTE_XEN_DOM0) += vhost_xen\n DIRS-y += vmdq\n DIRS-y += vmdq_dcb\n DIRS-$(CONFIG_RTE_LIBRTE_POWER) += vm_power_manager\n+DIRS-$(CONFIG_RTE_LIBRTE_IEEE1588) += ptpclient\n \n include $(RTE_SDK)/mk/rte.extsubdir.mk\ndiff --git a/examples/ptpclient/Makefile b/examples/ptpclient/Makefile\nnew file mode 100644\nindex 0000000..1a8efb3\n--- /dev/null\n+++ b/examples/ptpclient/Makefile\n@@ -0,0 +1,57 @@\n+#   BSD LICENSE\n+#\n+#   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.\n+#   All rights reserved.\n+#\n+#   Redistribution and use in source and binary forms, with or without\n+#   modification, are permitted provided that the following conditions\n+#   are met:\n+#\n+#     * Redistributions of source code must retain the above copyright\n+#       notice, this list of conditions and the following disclaimer.\n+#     * Redistributions in binary form must reproduce the above copyright\n+#       notice, this list of conditions and the following disclaimer in\n+#       the documentation and/or other materials provided with the\n+#       distribution.\n+#     * Neither the name of Intel Corporation nor the names of its\n+#       contributors may be used to endorse or promote products derived\n+#       from this software without specific prior written permission.\n+#\n+#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n+#   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n+#   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n+#   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n+#   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n+#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n+#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n+#   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n+#   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n+#   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n+#   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n+\n+ifeq ($(RTE_SDK),)\n+$(error \"Please define RTE_SDK environment variable\")\n+endif\n+\n+# Default target, can be overriddegitn by command line or environment\n+RTE_TARGET ?= x86_64-native-linuxapp-gcc\n+\n+include $(RTE_SDK)/mk/rte.vars.mk\n+\n+# binary name\n+APP = ptpclient\n+\n+# all source are stored in SRCS-y\n+SRCS-y := ptpclient.c\n+\n+CFLAGS += $(WERROR_FLAGS)\n+\n+# workaround for a gcc bug with noreturn attribute\n+# http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12603\n+ifeq ($(CONFIG_RTE_TOOLCHAIN_GCC),y)\n+CFLAGS_main.o += -Wno-return-type\n+endif\n+\n+EXTRA_CFLAGS += -O3\n+\n+include $(RTE_SDK)/mk/rte.extapp.mk\ndiff --git a/examples/ptpclient/ptpclient.c b/examples/ptpclient/ptpclient.c\nnew file mode 100644\nindex 0000000..453c20b\n--- /dev/null\n+++ b/examples/ptpclient/ptpclient.c\n@@ -0,0 +1,788 @@\n+/*-\n+ *   BSD LICENSE\n+ *\n+ *   Copyright(c) 2015 Intel Corporation. All rights reserved.\n+ *   All rights reserved.\n+ *\n+ *   Redistribution and use in source and binary forms, with or without\n+ *   modification, are permitted provided that the following conditions\n+ *   are met:\n+ *\n+ *     * Redistributions of source code must retain the above copyright\n+ *       notice, this list of conditions and the following disclaimer.\n+ *     * Redistributions in binary form must reproduce the above copyright\n+ *       notice, this list of conditions and the following disclaimer in\n+ *       the documentation and/or other materials provided with the\n+ *       distribution.\n+ *     * Neither the name of Intel Corporation nor the names of its\n+ *       contributors may be used to endorse or promote products derived\n+ *       from this software without specific prior written permission.\n+ *\n+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n+ *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n+ */\n+\n+/*\n+ * This Application is a simple Layer 2 PTP v2 client\n+ * IT shows T1-T4 values wchich are used to synchronize PHC clock.\n+ * if -T 1 parameter is used Linux kernel Clock is sychronized with ptp clock\n+ */\n+\n+#include <stdint.h>\n+#include <inttypes.h>\n+#include <rte_eal.h>\n+#include <rte_ethdev.h>\n+#include <rte_cycles.h>\n+#include <rte_lcore.h>\n+#include <rte_mbuf.h>\n+#include <rte_ip.h>\n+#include <limits.h>\n+#include <sys/time.h>\n+#include <getopt.h>\n+\n+#define RX_RING_SIZE 128\n+#define TX_RING_SIZE 512\n+\n+#define NUM_MBUFS            8191\n+#define MBUF_CACHE_SIZE       250\n+\n+/* Values for the PTP messageType field */\n+#define SYNC                  0x0\n+#define DELAY_REQ             0x1\n+#define PDELAY_REQ            0x2\n+#define PDELAY_RESP           0x3\n+#define FOLLOW_UP             0x8\n+#define DELAY_RESP            0x9\n+#define PDELAY_RESP_FOLLOW_UP 0xA\n+#define ANNOUNCE              0xB\n+#define SIGNALING             0xC\n+#define MANAGEMENT            0xD\n+\n+#define NSEC_PER_SEC  1000000000L\n+#define PTP_PROTOCOL       0x88F7\n+#define KERNEL_UPDATE         100\n+#define DATE_IN_2015   1444924130\n+#define MAX_PORT               64\n+\n+\n+\n+struct rte_mempool *mbuf_pool;\n+uint32_t ptp_enabled_port_mask;\n+uint8_t ptp_enabled_port_nb;\n+static uint8_t ptp_enabled_ports[MAX_PORT];\n+\n+static const struct rte_eth_conf port_conf_default = {\n+\t.rxmode = { .max_rx_pkt_len = ETHER_MAX_LEN }\n+};\n+\n+static const struct ether_addr ether_multicast = {\n+\t.addr_bytes = {0x01, 0x1b, 0x19, 0x0, 0x0, 0x0}\n+};\n+\n+/* Structs used for PTP handling */\n+struct tstamp {\n+\tuint16_t   sec_msb;\n+\tuint32_t   sec_lsb;\n+\tuint32_t   ns;\n+}  __attribute__((packed));\n+\n+struct clock_identity {\n+\tuint8_t id[8];\n+};\n+\n+struct port_identity {\n+\tstruct clock_identity  clock_identity;\n+\tuint16_t               port_number;\n+}  __attribute__((packed));\n+\n+struct ptp_header {\n+\tuint8_t              msgtype;\n+\tuint8_t              ver;\n+\tuint16_t             message_length;\n+\tuint8_t              domain_number;\n+\tuint8_t              reserved1;\n+\tuint8_t              flag_field[2];\n+\tint64_t              correction;\n+\tuint32_t             reserved2;\n+\tstruct port_identity source_port_identity;\n+\tuint16_t             sequenceId;\n+\tuint8_t              control;\n+\tint8_t               log_message_interval;\n+} __attribute__((packed));\n+\n+struct sync_msg {\n+\tstruct ptp_header   hdr;\n+\tstruct tstamp       origin_timestamp;\n+} __attribute__((packed));\n+\n+struct follow_up_msg {\n+\tstruct ptp_header   hdr;\n+\tstruct tstamp       precise_origin_timestamp;\n+\tuint8_t             suffix[0];\n+} __attribute__((packed));\n+\n+struct delay_req_msg {\n+\tstruct ptp_header   hdr;\n+\tstruct tstamp       origin_timestamp;\n+} __attribute__((packed));\n+\n+struct delay_resp_msg {\n+\tstruct ptp_header    hdr;\n+\tstruct tstamp        rx_tstamp;\n+\tstruct port_identity requesting_port_identity;\n+\tuint8_t              suffix[0];\n+} __attribute__((packed));\n+\n+struct ptp_message {\n+\tunion {\n+\t\tstruct ptp_header          header;\n+\t\tstruct sync_msg            sync;\n+\t\tstruct delay_req_msg       delay_req;\n+\t\tstruct follow_up_msg       follow_up;\n+\t\tstruct delay_resp_msg      delay_resp;\n+\t} __attribute__((packed));\n+};\n+\n+struct ptpv2_data_slave_ordinary {\n+\tstruct rte_mbuf *m;\n+\tstruct timespec timestamp;\n+\tstruct timespec timestamp1;\n+\tstruct timespec timestamp2;\n+\tstruct timespec timestamp3;\n+\tstruct timespec timestamp4;\n+\tstruct timespec systime;\n+\tstruct clock_identity client_clock_id;\n+\tstruct clock_identity master_clock_id;\n+\tstruct timeval new_adj;\n+\tstruct timeval old_adj;\n+\tint64_t delta;\n+\tuint8_t portid;\n+\tuint8_t clock_portid;\n+\tuint16_t seqID_SYNC;\n+\tuint16_t seqID_FOLLOWUP;\n+\tuint8_t ptpset;\n+\tuint8_t kernel_time_set;\n+\tuint8_t current_ptp_port;\n+};\n+\n+static struct ptpv2_data_slave_ordinary ptp_data;\n+\n+static inline uint64_t timespec64_to_ns(const struct timespec *ts)\n+{\n+\treturn ((uint64_t) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;\n+}\n+\n+static struct timeval\n+ns_to_timeval(int64_t nsec)\n+{\n+\tstruct timespec t_spec = {0, 0};\n+\tstruct timeval t_eval = {0, 0};\n+\tint32_t rem;\n+\n+\tif (nsec == 0)\n+\t\treturn t_eval;\n+\trem = nsec % NSEC_PER_SEC;\n+\tt_spec.tv_sec = nsec / NSEC_PER_SEC;\n+\n+\tif (rem < 0) {\n+\t\tt_spec.tv_sec--;\n+\t\trem += NSEC_PER_SEC;\n+\t}\n+\n+\tt_spec.tv_nsec = rem;\n+\tt_eval.tv_sec = t_spec.tv_sec;\n+\tt_eval.tv_usec = t_spec.tv_nsec / 1000;\n+\n+\treturn t_eval;\n+}\n+\n+/*\n+ * Initializes a given port using global settings and with the RX buffers\n+ * coming from the mbuf_pool passed as a parameter.\n+ */\n+static inline int\n+port_init(uint8_t port, struct rte_mempool *mbuf_pool)\n+{\n+\tstruct rte_eth_dev_info dev_info;\n+\tstruct rte_eth_conf port_conf = port_conf_default;\n+\tconst uint16_t rx_rings = 1;\n+\tconst uint16_t tx_rings = 1;\n+\tint retval;\n+\tuint16_t q;\n+\n+\tif (port >= rte_eth_dev_count())\n+\t\treturn -1;\n+\n+\t/* Configure the Ethernet device. */\n+\tretval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);\n+\tif (retval != 0)\n+\t\treturn retval;\n+\n+\t/* Allocate and set up 1 RX queue per Ethernet port. */\n+\tfor (q = 0; q < rx_rings; q++) {\n+\t\tretval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE,\n+\t\t\t\trte_eth_dev_socket_id(port), NULL, mbuf_pool);\n+\n+\t\tif (retval < 0)\n+\t\t\treturn retval;\n+\t}\n+\n+\t/* Allocate and set up 1 TX queue per Ethernet port. */\n+\tfor (q = 0; q < tx_rings; q++) {\n+\t\t/* Setup txq_flags */\n+\t\tstruct rte_eth_txconf *txconf;\n+\n+\t\trte_eth_dev_info_get(q, &dev_info);\n+\t\ttxconf = &dev_info.default_txconf;\n+\t\ttxconf->txq_flags = 0;\n+\n+\t\tretval = rte_eth_tx_queue_setup(port, q, TX_RING_SIZE,\n+\t\t\t\trte_eth_dev_socket_id(port), txconf);\n+\t\tif (retval < 0)\n+\t\t\treturn retval;\n+\t}\n+\n+\t/* Start the Ethernet port. */\n+\tretval = rte_eth_dev_start(port);\n+\tif (retval < 0)\n+\t\treturn retval;\n+\n+\t/* Enable timesync timestamping for the Ethernet device */\n+\trte_eth_timesync_enable(port);\n+\n+\t/* Enable RX in promiscuous mode for the Ethernet device. */\n+\trte_eth_promiscuous_enable(port);\n+\n+\treturn 0;\n+}\n+\n+static void\n+print_clock_info(struct ptpv2_data_slave_ordinary *ptp_data)\n+{\n+\tint64_t nsec;\n+\n+\tprintf(\"Master Clock id: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\",\n+\t\tptp_data->master_clock_id.id[0],\n+\t\tptp_data->master_clock_id.id[1],\n+\t\tptp_data->master_clock_id.id[2],\n+\t\tptp_data->master_clock_id.id[3],\n+\t\tptp_data->master_clock_id.id[4],\n+\t\tptp_data->master_clock_id.id[5],\n+\t\tptp_data->master_clock_id.id[6],\n+\t\tptp_data->master_clock_id.id[7]);\n+\n+\tprintf(\"\\nT2 - Slave  Clock.  %lds %ldns\",\n+\t\t\t(ptp_data->timestamp2.tv_sec),\n+\t\t\t(ptp_data->timestamp2.tv_nsec));\n+\n+\tprintf(\"\\nT1 - Master Clock.  %lds %ldns \",\n+\t\t\tptp_data->timestamp1.tv_sec,\n+\t\t\t(ptp_data->timestamp1.tv_nsec));\n+\n+\tprintf(\"\\nT3 - Slave  Clock.  %lds %ldns\",\n+\t\t\tptp_data->timestamp3.tv_sec,\n+\t\t\t(ptp_data->timestamp3.tv_nsec));\n+\n+\tprintf(\"\\nT4 - Master Clock.  %lds %ldns \",\n+\t\t\tptp_data->timestamp4.tv_sec,\n+\t\t\t(ptp_data->timestamp4.tv_nsec));\n+\n+\tprintf(\"\\nDelta between master and slave clocks:%\"PRId64\"ns\\n\",\n+\t\t\tptp_data->delta);\n+\n+\tclock_gettime(CLOCK_REALTIME, &ptp_data->systime);\n+\trte_eth_timesync_time_get(ptp_data->clock_portid,\n+\t\t\t&ptp_data->timestamp);\n+\ttime_t ts = ptp_data->timestamp.tv_sec;\n+\n+\tprintf(\"\\n\\nComparison between Linux kernel Time and PTP:\");\n+\n+\tprintf(\"\\nCurrent PTP Time: %.24s %.9ld ns\",\n+\t\t\tctime(&ts), ptp_data->timestamp.tv_nsec);\n+\n+\tnsec = (int64_t)timespec64_to_ns(&ptp_data->timestamp) -\n+\t\t\t(int64_t)timespec64_to_ns(&ptp_data->systime);\n+\tptp_data->new_adj = ns_to_timeval(nsec);\n+\n+\tif (ptp_data->kernel_time_set == 1)\n+\t\tadjtime(&ptp_data->new_adj, NULL);\n+\n+\tgettimeofday(&ptp_data->new_adj, NULL);\n+\ttime_t tp = ptp_data->new_adj.tv_sec;\n+\n+\tprintf(\"\\nCurrent SYS Time: %.24s %.6ld ns\",\n+\t\t\t\tctime(&tp), ptp_data->new_adj.tv_usec);\n+\n+\tprintf(\"\\nDelta between PTP and Linux Kernel time:%\"PRId64\"ns\\n\",\n+\t\t\t\tnsec);\n+\tprintf(\"[Ctrl+C to quit]\\n\");\n+\n+\t/* Clear screen and put cursor in column 1, row 1 */\n+\tprintf(\"\\033[2J\\033[1;1H\");\n+}\n+\n+static int64_t\n+delta_eval(struct ptpv2_data_slave_ordinary *ptp_data)\n+{\n+\tint64_t delta;\n+\tuint64_t t1 = 0;\n+\tuint64_t t2 = 0;\n+\tuint64_t t3 = 0;\n+\tuint64_t t4 = 0;\n+\n+\tt1 = timespec64_to_ns(&ptp_data->timestamp1);\n+\tt2 = timespec64_to_ns(&ptp_data->timestamp2);\n+\tt3 = timespec64_to_ns(&ptp_data->timestamp3);\n+\tt4 = timespec64_to_ns(&ptp_data->timestamp4);\n+\n+\t/* delta = -[(T2-T1) - (T4-T3)]/2 */\n+\tdelta = -((int64_t)((t2 - t1) - (t4 - t3))) / 2;\n+\n+\treturn delta;\n+}\n+\n+/*\n+ * Parse PTP SYNC message\n+ */\n+static void\n+parse_sync(struct ptpv2_data_slave_ordinary *ptp_data)\n+{\n+\tstruct ptp_header *ptp_hdr;\n+\n+\tptp_hdr = (struct ptp_header *)(rte_pktmbuf_mtod(ptp_data->m, char *)\n+\t\t\t+ sizeof(struct ether_hdr));\n+\tptp_data->seqID_SYNC = rte_be_to_cpu_16(ptp_hdr->sequenceId);\n+\n+\tif (ptp_data->ptpset == 0) {\n+\t\trte_memcpy(&ptp_data->master_clock_id,\n+\t\t\t\t&ptp_hdr->source_port_identity.clock_identity,\n+\t\t\t\tsizeof(struct clock_identity));\n+\t\tptp_data->ptpset = 1;\n+\t}\n+\n+\tif (memcmp(&ptp_hdr->source_port_identity.clock_identity,\n+\t\t\t&ptp_hdr->source_port_identity.clock_identity,\n+\t\t\tsizeof(struct clock_identity)) == 0) {\n+\n+\t\tif (ptp_data->ptpset == 1)\n+\t\t\trte_eth_timesync_read_rx_timestamp(ptp_data->portid,\n+\t\t\t\t\t&ptp_data->timestamp2, 0);\n+\t}\n+\n+}\n+\n+/*\n+ * Parse PTP FOLLOWUP message & Send DELAY_REQ to master clock.\n+ */\n+static void\n+parse_fup(struct ptpv2_data_slave_ordinary *ptp_data)\n+{\n+\tstruct ether_hdr *eth_hdr;\n+\tstruct ptp_header *ptp_hdr;\n+\tstruct clock_identity *client_clkid;\n+\tstruct ptp_message *ptp_msg;\n+\tstruct rte_mbuf *created_pkt;\n+\tstruct ether_addr eth_multicast = ether_multicast;\n+\tsize_t pkt_size;\n+\tint wait_us;\n+\tstruct rte_mbuf *m = ptp_data->m;\n+\n+\teth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);\n+\tptp_hdr = (struct ptp_header *)(rte_pktmbuf_mtod(m, char *)\n+\t\t\t+ sizeof(struct ether_hdr));\n+\tif (memcmp(&ptp_data->master_clock_id,\n+\t\t\t&ptp_hdr->source_port_identity.clock_identity,\n+\t\t\tsizeof(struct clock_identity)) != 0)\n+\t\treturn;\n+\n+\tptp_data->seqID_FOLLOWUP = rte_be_to_cpu_16(ptp_hdr->sequenceId);\n+\tptp_msg = (struct ptp_message *)\n+\t\t\t(rte_pktmbuf_mtod(m, char *) + sizeof(struct ether_hdr));\n+\tptp_data->timestamp1.tv_nsec =\n+\t\tntohl(ptp_msg->follow_up.precise_origin_timestamp.ns);\n+\tptp_data->timestamp1.tv_sec =\n+\t\t((uint64_t)ntohl\n+\t\t\t\t(ptp_msg->follow_up.precise_origin_timestamp.sec_lsb)) |\n+\t\t(((uint64_t)ntohs\n+\t\t\t\t(ptp_msg->follow_up.precise_origin_timestamp.sec_msb))\n+\t\t\t\t<< 32);\n+\n+\tif (ptp_data->seqID_FOLLOWUP == ptp_data->seqID_SYNC) {\n+\n+\t\tcreated_pkt = rte_pktmbuf_alloc(mbuf_pool);\n+\t\tpkt_size = sizeof(struct ether_hdr) + sizeof(struct ptp_message);\n+\t\tcreated_pkt->data_len = pkt_size;\n+\t\tcreated_pkt->pkt_len = pkt_size;\n+\t\teth_hdr = rte_pktmbuf_mtod(created_pkt, struct ether_hdr *);\n+\t\trte_eth_macaddr_get(ptp_data->portid, &eth_hdr->s_addr);\n+\n+\t\t/* Set multicast address 01-1B-19-00-00-00 */\n+\t\tether_addr_copy(&eth_multicast, &eth_hdr->d_addr);\n+\n+\t\teth_hdr->ether_type = htons(PTP_PROTOCOL);\n+\t\tptp_msg = (struct ptp_message *)\n+\t\t\t(rte_pktmbuf_mtod(created_pkt, char *) +\n+\t\t\tsizeof(struct ether_hdr));\n+\n+\t\tptp_msg->delay_req.hdr.sequenceId = htons(ptp_data->seqID_SYNC);\n+\t\tptp_msg->delay_req.hdr.msgtype = DELAY_REQ;\n+\t\tptp_msg->delay_req.hdr.ver = 2;\n+\t\tptp_msg->delay_req.hdr.control = 1;\n+\t\tptp_msg->delay_req.hdr.log_message_interval = 127;\n+\n+\t\t/* Set up clock id */\n+\t\tclient_clkid =\n+\t\t\t&ptp_msg->delay_req.hdr.source_port_identity.clock_identity;\n+\n+\t\tclient_clkid->id[0] = eth_hdr->s_addr.addr_bytes[0];\n+\t\tclient_clkid->id[1] = eth_hdr->s_addr.addr_bytes[1];\n+\t\tclient_clkid->id[2] = eth_hdr->s_addr.addr_bytes[2];\n+\t\tclient_clkid->id[3] = 0xFF;\n+\t\tclient_clkid->id[4] = 0xFE;\n+\t\tclient_clkid->id[5] = eth_hdr->s_addr.addr_bytes[3];\n+\t\tclient_clkid->id[6] = eth_hdr->s_addr.addr_bytes[4];\n+\t\tclient_clkid->id[7] = eth_hdr->s_addr.addr_bytes[5];\n+\n+\t\trte_memcpy(&ptp_data->client_clock_id,\n+\t\t\t\tclient_clkid,\n+\t\t\t\tsizeof(struct clock_identity));\n+\n+\t\t/* Enable Flag for Hardware Timestamping */\n+\t\tcreated_pkt->ol_flags |= PKT_TX_IEEE1588_TMST;\n+\n+\t\t/* We read value from NIC to prevent latching with old value */\n+\t\trte_eth_timesync_read_tx_timestamp(ptp_data->portid,\n+\t\t\t\t&ptp_data->timestamp3);\n+\n+\t\t/* the packet is being transmitted */\n+\t\trte_eth_tx_burst(ptp_data->portid, 0, &created_pkt, 1);\n+\n+\t\twait_us = 0;\n+\t\tptp_data->timestamp3.tv_nsec = 0;\n+\t\tptp_data->timestamp3.tv_sec = 0;\n+\n+\t\t/* we must wait at least 1us to read TX Timestamp */\n+\t\twhile ((rte_eth_timesync_read_tx_timestamp(ptp_data->portid,\n+\t\t\t\t&ptp_data->timestamp3) < 0) && (wait_us < 1000)) {\n+\t\t\trte_delay_us(1);\n+\t\t\twait_us++;\n+\t\t}\n+\t}\n+}\n+\n+/*\n+ * Parse DELAY_RESP message\n+ */\n+static void\n+parse_drsp(struct ptpv2_data_slave_ordinary *ptp_data)\n+{\n+\tstruct rte_mbuf *m = ptp_data->m;\n+\tstruct ptp_message *ptp_msg;\n+\tuint16_t seqid;\n+\tint64_t nsec;\n+\tstruct timespec systime;\n+\tstruct timespec nettime;\n+\n+\tptp_msg = (struct ptp_message *) (rte_pktmbuf_mtod(m, char *) +\n+\t\t\t\t\tsizeof(struct ether_hdr));\n+\tseqid =  rte_be_to_cpu_16(ptp_msg->delay_resp.hdr.sequenceId);\n+\tif (memcmp(&ptp_data->client_clock_id,\n+\t\t\t&ptp_msg->delay_resp.requesting_port_identity.clock_identity,\n+\t\t\tsizeof(struct clock_identity)) == 0) {\n+\n+\t\tif (seqid == ptp_data->seqID_FOLLOWUP) {\n+\t\t\tptp_data->timestamp4.tv_nsec =\n+\t\t\t\t\tntohl(ptp_msg->delay_resp.rx_tstamp.ns);\n+\t\t\tptp_data->timestamp4.tv_sec =\n+\t\t\t\t((uint64_t)ntohl\n+\t\t\t\t\t(ptp_msg->delay_resp.rx_tstamp.sec_lsb)) |\n+\t\t\t\t(((uint64_t)ntohs\n+\t\t\t\t\t(ptp_msg->delay_resp.rx_tstamp.sec_msb)) << 32);\n+\n+\t\t\t/* Evaluate delta for adjustment */\n+\t\t\tptp_data->delta = delta_eval(ptp_data);\n+\t\t\trte_eth_timesync_time_adjust(ptp_data->portid, ptp_data->delta);\n+\t\t\tptp_data->clock_portid = ptp_data->portid;\n+\t\t\tptp_data->current_ptp_port = ptp_data->clock_portid;\n+\n+\t\t\tif (ptp_data->kernel_time_set == 1) {\n+\t\t\t\tclock_gettime(CLOCK_REALTIME, &systime);\n+\t\t\t\trte_eth_timesync_time_get(ptp_data->current_ptp_port,\n+\t\t\t\t\t\t&nettime);\n+\t\t\t\tnsec = (int64_t)timespec64_to_ns(&nettime) -\n+\t\t\t\t\t\t(int64_t)timespec64_to_ns(&systime);\n+\n+\t\t\t\tif (nsec > 2*NSEC_PER_SEC || nsec < -2*NSEC_PER_SEC) {\n+\n+\t\t\t\t\t/*\n+\t\t\t\t\t * This epoch time is set for first pass when\n+\t\t\t\t\t * right clock is not setup and values\n+\t\t\t\t\t * take us back to '70s\n+\t\t\t\t\t */\n+\t\t\t\t\tif (likely(nettime.tv_sec > DATE_IN_2015))\n+\t\t\t\t\t\tclock_settime(CLOCK_REALTIME, &nettime);\n+\t\t\t\t}\n+\t\t\t}\n+\t\t}\n+\t}\n+}\n+\n+/* This function processes PTP packets, implementing\n+ * slave PTP IEEE1588 L2 functionality\n+ */\n+static void\n+parse_ptp_frames(uint8_t portid, struct rte_mbuf *m) {\n+\tstruct ptp_header *ptp_hdr;\n+\tstruct ether_hdr *eth_hdr;\n+\tuint16_t eth_type;\n+\n+\teth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);\n+\teth_type = rte_be_to_cpu_16(eth_hdr->ether_type);\n+\n+\tif (eth_type == PTP_PROTOCOL) {\n+\t\tptp_data.m = m;\n+\t\tptp_data.portid = portid;\n+\t\tptp_hdr = (struct ptp_header *)(rte_pktmbuf_mtod(m, char *)\n+\t\t\t\t\t+ sizeof(struct ether_hdr));\n+\n+\t\tswitch (ptp_hdr->msgtype) {\n+\t\tcase SYNC:\n+\t\t\tparse_sync(&ptp_data);\n+\t\t\tbreak;\n+\t\tcase FOLLOW_UP:\n+\t\t\tparse_fup(&ptp_data);\n+\t\t\tbreak;\n+\t\tcase DELAY_RESP:\n+\t\t\tparse_drsp(&ptp_data);\n+\t\t\tprint_clock_info(&ptp_data);\n+\t\t\tbreak;\n+\t\tdefault:\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+}\n+\n+/*\n+ * The lcore main. This is the main thread that does the work, reading from\n+ * an input port and writing to an output port.\n+ */\n+static __attribute__((noreturn)) void\n+lcore_main(void)\n+{\n+\tuint8_t portid;\n+\tunsigned nb_rx;\n+\tstruct rte_mbuf *m;\n+\tint64_t i = 0;\n+\tint64_t nsec = 0;\n+\n+\t/*\n+\t * Check that the port is on the same NUMA node as the polling thread\n+\t * for best performance.\n+\t */\n+\tprintf(\"\\nCore %u Waiting for SYNC packets. [Ctrl+C to quit]\\n\",\n+\t\t\trte_lcore_id());\n+\n+\t/* Run until the application is quit or killed. */\n+\n+\twhile (1) {\n+\t\t/*\n+\t\t * Read packet from RX queues\n+\t\t */\n+\t\tfor (portid = 0; portid < ptp_enabled_port_nb; portid++) {\n+\n+\t\t\tportid = ptp_enabled_ports[portid];\n+\t\t\tnb_rx = rte_eth_rx_burst(portid, 0, &m, 1);\n+\n+\t\t\tif (ptp_data.kernel_time_set == 1) {\n+\t\t\t\t/* Update every KERNEL_UPDATE cycle */\n+\t\t\t\tif (i % KERNEL_UPDATE == 0) {\n+\t\t\t\t\tclock_gettime(CLOCK_REALTIME,\n+\t\t\t\t\t\t\t&ptp_data.systime);\n+\n+\t\t\t\t\trte_eth_timesync_time_get(ptp_data.current_ptp_port,\n+\t\t\t\t\t\t\t&ptp_data.timestamp);\n+\n+\t\t\t\t\tnsec = (int64_t)timespec64_to_ns(&ptp_data.timestamp) -\n+\t\t\t\t\t\t(int64_t)timespec64_to_ns(&ptp_data.systime);\n+\n+\t\t\t\t\tptp_data.new_adj = ns_to_timeval(nsec);\n+\t\t\t\t\tadjtime(&ptp_data.new_adj, 0);\n+\t\t\t\t}\n+\t\t\t\ti++;\n+\t\t\t}\n+\n+\t\t\tif (likely(nb_rx == 0))\n+\t\t\t\tcontinue;\n+\n+\t\t\tif (m->ol_flags & PKT_RX_IEEE1588_PTP)\n+\t\t\t\tparse_ptp_frames(portid, m);\n+\n+\t\t\trte_pktmbuf_free(m);\n+\t\t}\n+\t}\n+}\n+\n+static void\n+print_usage(const char *prgname)\n+{\n+\tprintf(\"%s [EAL options] -- -p PORTMASK -T VALUE\\n\"\n+\t\t\"  -T VALUE: 0 - Disable, 1 - Enable Linux Clock Synchronization\"\n+\t\t\" (0 default)\\n\"\n+\t\t\"  -p PORTMASK: hexadecimal bitmask of ports to configure\\n\",\n+\t\t\tprgname);\n+}\n+\n+static int\n+ptp_parse_portmask(const char *portmask)\n+{\n+\tchar *end = NULL;\n+\tunsigned long pm;\n+\n+\t/* parse hexadecimal string */\n+\tpm = strtoul(portmask, &end, 16);\n+\tif ((portmask[0] == '\\0') || (end == NULL) || (*end != '\\0'))\n+\t\treturn -1;\n+\n+\tif (pm == 0)\n+\t\treturn -1;\n+\n+\treturn pm;\n+}\n+\n+static int\n+parse_ptp_kernel(const char *param)\n+{\n+\tchar *end = NULL;\n+\tunsigned long pm;\n+\n+\tpm = strtoul(param, &end, 16);\n+\tif ((param[0] == '\\0') || (end == NULL) || (*end != '\\0'))\n+\t\treturn -1;\n+\tif (pm == 0)\n+\t\treturn 0;\n+\n+\treturn 1;\n+}\n+\n+/* Parse the argument given in the command line of the application */\n+static int\n+ptp_parse_args(int argc, char **argv)\n+{\n+\tint opt, ret;\n+\tchar **argvopt;\n+\tint option_index;\n+\tchar *prgname = argv[0];\n+\tstatic struct option lgopts[] = { {NULL, 0, 0, 0} };\n+\n+\targvopt = argv;\n+\n+\twhile ((opt = getopt_long(argc, argvopt, \"p:T:\",\n+\t\t\t\t  lgopts, &option_index)) != EOF) {\n+\n+\t\tswitch (opt) {\n+\n+\t\t/* portmask */\n+\t\tcase 'p':\n+\t\t\tptp_enabled_port_mask = ptp_parse_portmask(optarg);\n+\t\t\tif (ptp_enabled_port_mask == 0) {\n+\t\t\t\tprintf(\"invalid portmask\\n\");\n+\t\t\t\tprint_usage(prgname);\n+\t\t\t\treturn -1;\n+\t\t\t}\n+\t\t\tbreak;\n+\t\t/* time synchronization */\n+\t\tcase 'T':\n+\t\t\tret = parse_ptp_kernel(optarg);\n+\t\t\tif (ret < 0) {\n+\t\t\t\tprint_usage(prgname);\n+\t\t\t\treturn -1;\n+\t\t\t}\n+\n+\t\t\tptp_data.kernel_time_set = ret;\n+\t\t\tbreak;\n+\n+\t\tdefault:\n+\t\t\tprint_usage(prgname);\n+\t\t\treturn -1;\n+\t\t}\n+\t}\n+\n+\targv[optind-1] = prgname;\n+\n+\toptind = 0; /* reset getopt lib */\n+\treturn 0;\n+}\n+\n+/*\n+ * The main function, which does initialization and calls the per-lcore\n+ * functions.\n+ */\n+int\n+main(int argc, char *argv[])\n+{\n+\tunsigned nb_ports;\n+\n+\tuint8_t portid;\n+\n+\t/* Initialize the Environment Abstraction Layer (EAL). */\n+\tint ret = rte_eal_init(argc, argv);\n+\n+\tif (ret < 0)\n+\t\trte_exit(EXIT_FAILURE, \"Error with EAL initialization\\n\");\n+\n+\tmemset(&ptp_data, '\\0', sizeof(struct ptpv2_data_slave_ordinary));\n+\n+\targc -= ret;\n+\targv += ret;\n+\n+\tret = ptp_parse_args(argc, argv);\n+\tif (ret < 0)\n+\t\trte_exit(EXIT_FAILURE, \"Error with PTP initialization\\n\");\n+\n+\t/* Check that there is an even number of ports to send/receive on. */\n+\tnb_ports = rte_eth_dev_count();\n+\n+\t/* Creates a new mempool in memory to hold the mbufs. */\n+\tmbuf_pool = rte_pktmbuf_pool_create(\"MBUF_POOL\", NUM_MBUFS * nb_ports,\n+\t\tMBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());\n+\n+\tif (mbuf_pool == NULL)\n+\t\trte_exit(EXIT_FAILURE, \"Cannot create mbuf pool\\n\");\n+\n+\t/* Initialize all ports. */\n+\tfor (portid = 0; portid < nb_ports; portid++) {\n+\t\tif ((ptp_enabled_port_mask & (1 << portid)) != 0) {\n+\n+\t\t\tif (port_init(portid, mbuf_pool) == 0) {\n+\t\t\t\tptp_enabled_ports[ptp_enabled_port_nb] = portid;\n+\t\t\t\tptp_enabled_port_nb++;\n+\t\t\t} else {\n+\t\t\t\trte_exit(EXIT_FAILURE, \"Cannot init port %\"PRIu8 \"\\n\",\n+\t\t\t\t\t\tportid);\n+\t\t\t}\n+\t\t} else\n+\t\t\tprintf(\"Skipping disabled port %u\\n\", portid);\n+\t}\n+\n+\tif (ptp_enabled_port_nb == 0) {\n+\t\trte_exit(EXIT_FAILURE,\n+\t\t\t\"All available ports are disabled. Please set portmask.\\n\");\n+\t}\n+\n+\tif (rte_lcore_count() > 1)\n+\t\tprintf(\"\\nWARNING: Too many lcores enabled. Only 1 used.\\n\");\n+\n+\t/* Call lcore_main on the master core only. */\n+\tlcore_main();\n+\n+\treturn 0;\n+}\n",
    "prefixes": [
        "dpdk-dev",
        "v2",
        "5/6"
    ]
}