get:
Show a patch.

patch:
Update a patch.

put:
Update a patch.

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

{
    "id": 41555,
    "url": "http://patches.dpdk.org/api/patches/41555/?format=api",
    "web_url": "http://patches.dpdk.org/project/dpdk/patch/3951b507713bf8848afdcd3c45f040bd2acc3663.1530009564.git.anatoly.burakov@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": "<3951b507713bf8848afdcd3c45f040bd2acc3663.1530009564.git.anatoly.burakov@intel.com>",
    "list_archive_url": "https://inbox.dpdk.org/dev/3951b507713bf8848afdcd3c45f040bd2acc3663.1530009564.git.anatoly.burakov@intel.com",
    "date": "2018-06-26T10:53:15",
    "name": "[v2,4/7] eal/bsdapp: add alarm support",
    "commit_ref": null,
    "pull_url": null,
    "state": "accepted",
    "archived": true,
    "hash": "6324adf4be9e1bb376a72930c3a60ffcda185afd",
    "submitter": {
        "id": 4,
        "url": "http://patches.dpdk.org/api/people/4/?format=api",
        "name": "Anatoly Burakov",
        "email": "anatoly.burakov@intel.com"
    },
    "delegate": {
        "id": 1,
        "url": "http://patches.dpdk.org/api/users/1/?format=api",
        "username": "tmonjalo",
        "first_name": "Thomas",
        "last_name": "Monjalon",
        "email": "thomas@monjalon.net"
    },
    "mbox": "http://patches.dpdk.org/project/dpdk/patch/3951b507713bf8848afdcd3c45f040bd2acc3663.1530009564.git.anatoly.burakov@intel.com/mbox/",
    "series": [
        {
            "id": 238,
            "url": "http://patches.dpdk.org/api/series/238/?format=api",
            "web_url": "http://patches.dpdk.org/project/dpdk/list/?series=238",
            "date": "2018-06-26T10:53:12",
            "name": "Remove asynchronous IPC thread",
            "version": 2,
            "mbox": "http://patches.dpdk.org/series/238/mbox/"
        }
    ],
    "comments": "http://patches.dpdk.org/api/patches/41555/comments/",
    "check": "success",
    "checks": "http://patches.dpdk.org/api/patches/41555/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 [127.0.0.1])\n\tby dpdk.org (Postfix) with ESMTP id 488A61B522;\n\tTue, 26 Jun 2018 12:53:29 +0200 (CEST)",
            "from mga18.intel.com (mga18.intel.com [134.134.136.126])\n\tby dpdk.org (Postfix) with ESMTP id 816B61B4A4\n\tfor <dev@dpdk.org>; Tue, 26 Jun 2018 12:53:23 +0200 (CEST)",
            "from fmsmga002.fm.intel.com ([10.253.24.26])\n\tby orsmga106.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384;\n\t26 Jun 2018 03:53:22 -0700",
            "from irvmail001.ir.intel.com ([163.33.26.43])\n\tby fmsmga002.fm.intel.com with ESMTP; 26 Jun 2018 03:53:20 -0700",
            "from sivswdev01.ir.intel.com (sivswdev01.ir.intel.com\n\t[10.237.217.45])\n\tby irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id\n\tw5QArKWG026445; Tue, 26 Jun 2018 11:53:20 +0100",
            "from sivswdev01.ir.intel.com (localhost [127.0.0.1])\n\tby sivswdev01.ir.intel.com with ESMTP id w5QArJ87021210;\n\tTue, 26 Jun 2018 11:53:19 +0100",
            "(from aburakov@localhost)\n\tby sivswdev01.ir.intel.com with LOCAL id w5QArJuC021206;\n\tTue, 26 Jun 2018 11:53:19 +0100"
        ],
        "X-Amp-Result": "SKIPPED(no attachment in message)",
        "X-Amp-File-Uploaded": "False",
        "X-ExtLoop1": "1",
        "X-IronPort-AV": "E=Sophos;i=\"5.51,274,1526367600\"; d=\"scan'208\";a=\"61375858\"",
        "From": "Anatoly Burakov <anatoly.burakov@intel.com>",
        "To": "dev@dpdk.org",
        "Cc": "Bruce Richardson <bruce.richardson@intel.com>,\n\tkonstantin.ananyev@intel.com, thomas@monjalon.net, qi.z.zhang@intel.com",
        "Date": "Tue, 26 Jun 2018 11:53:15 +0100",
        "Message-Id": "<3951b507713bf8848afdcd3c45f040bd2acc3663.1530009564.git.anatoly.burakov@intel.com>",
        "X-Mailer": "git-send-email 1.7.0.7",
        "In-Reply-To": [
            "<cover.1530009564.git.anatoly.burakov@intel.com>",
            "<cover.1530009564.git.anatoly.burakov@intel.com>"
        ],
        "References": [
            "<cover.1530009564.git.anatoly.burakov@intel.com>",
            "<cover.1529071026.git.anatoly.burakov@intel.com>\n\t<cover.1530009564.git.anatoly.burakov@intel.com>"
        ],
        "Subject": "[dpdk-dev] [PATCH v2 4/7] eal/bsdapp: add alarm support",
        "X-BeenThere": "dev@dpdk.org",
        "X-Mailman-Version": "2.1.15",
        "Precedence": "list",
        "List-Id": "DPDK patches and discussions <dev.dpdk.org>",
        "List-Unsubscribe": "<https://mails.dpdk.org/options/dev>,\n\t<mailto:dev-request@dpdk.org?subject=unsubscribe>",
        "List-Archive": "<http://mails.dpdk.org/archives/dev/>",
        "List-Post": "<mailto:dev@dpdk.org>",
        "List-Help": "<mailto:dev-request@dpdk.org?subject=help>",
        "List-Subscribe": "<https://mails.dpdk.org/listinfo/dev>,\n\t<mailto:dev-request@dpdk.org?subject=subscribe>",
        "Errors-To": "dev-bounces@dpdk.org",
        "Sender": "\"dev\" <dev-bounces@dpdk.org>"
    },
    "content": "Implement EAL alarm API support for FreeBSD. The implementation\nis largely identical to that of Linux version, with one key\ndifference.\n\nThe alarm API is a little Linux-centric in that it is expecting\nthe alarm API to manage alarm timeouts without involvement of the\ninterrupt thread. This works on Linux because in Linux, there's\ntimerfd API which allows waiting for timer events on an fd.\n\nOn FreeBSD, however, there are no timerfd's, and timer events are\nset up directly in kevent. There is no way to pass information from\nthe alarm API to the interrupt thread, so we also add a little\nback-channel magic to get soonest alarm timeout from the alarm API.\n\nSigned-off-by: Anatoly Burakov <anatoly.burakov@intel.com>\n---\n lib/librte_eal/bsdapp/eal/eal_alarm.c         | 299 +++++++++++++++++-\n lib/librte_eal/bsdapp/eal/eal_alarm_private.h |  19 ++\n lib/librte_eal/bsdapp/eal/eal_interrupts.c    |  29 +-\n 3 files changed, 334 insertions(+), 13 deletions(-)\n create mode 100644 lib/librte_eal/bsdapp/eal/eal_alarm_private.h",
    "diff": "diff --git a/lib/librte_eal/bsdapp/eal/eal_alarm.c b/lib/librte_eal/bsdapp/eal/eal_alarm.c\nindex eb3913c97..51ea4b8c0 100644\n--- a/lib/librte_eal/bsdapp/eal/eal_alarm.c\n+++ b/lib/librte_eal/bsdapp/eal/eal_alarm.c\n@@ -1,31 +1,314 @@\n /* SPDX-License-Identifier: BSD-3-Clause\n- * Copyright(c) 2010-2014 Intel Corporation\n+ * Copyright(c) 2010-2018 Intel Corporation\n  */\n+\n+#include <sys/types.h>\n+#include <sys/stat.h>\n+#include <fcntl.h>\n+#include <stdio.h>\n #include <stdlib.h>\n+#include <string.h>\n+#include <time.h>\n #include <errno.h>\n \n #include <rte_alarm.h>\n+#include <rte_cycles.h>\n #include <rte_common.h>\n+#include <rte_errno.h>\n+#include <rte_interrupts.h>\n+#include <rte_spinlock.h>\n+\n #include \"eal_private.h\"\n+#include \"eal_alarm_private.h\"\n+\n+#define NS_PER_US 1000\n+\n+#ifdef CLOCK_MONOTONIC_RAW /* Defined in glibc bits/time.h */\n+#define CLOCK_TYPE_ID CLOCK_MONOTONIC_RAW\n+#else\n+#define CLOCK_TYPE_ID CLOCK_MONOTONIC\n+#endif\n+\n+struct alarm_entry {\n+\tLIST_ENTRY(alarm_entry) next;\n+\tstruct rte_intr_handle handle;\n+\tstruct timespec time;\n+\trte_eal_alarm_callback cb_fn;\n+\tvoid *cb_arg;\n+\tvolatile uint8_t executing;\n+\tvolatile pthread_t executing_id;\n+};\n+\n+static LIST_HEAD(alarm_list, alarm_entry) alarm_list = LIST_HEAD_INITIALIZER();\n+static rte_spinlock_t alarm_list_lk = RTE_SPINLOCK_INITIALIZER;\n+\n+static struct rte_intr_handle intr_handle = {.fd = -1 };\n+static void eal_alarm_callback(void *arg);\n \n int\n rte_eal_alarm_init(void)\n {\n+\tintr_handle.type = RTE_INTR_HANDLE_ALARM;\n+\n+\t/* on FreeBSD, timers don't use fd's, and their identifiers are stored\n+\t * in separate namespace from fd's, so using any value is OK. however,\n+\t * EAL interrupts handler expects fd's to be unique, so use an actual fd\n+\t * to guarantee unique timer identifier.\n+\t */\n+\tintr_handle.fd = open(\"/dev/zero\", O_RDONLY);\n+\n+\treturn 0;\n+}\n+\n+static inline int\n+timespec_cmp(const struct timespec *now, const struct timespec *at)\n+{\n+\tif (now->tv_sec < at->tv_sec)\n+\t\treturn -1;\n+\tif (now->tv_sec > at->tv_sec)\n+\t\treturn 1;\n+\tif (now->tv_nsec < at->tv_nsec)\n+\t\treturn -1;\n+\tif (now->tv_nsec > at->tv_nsec)\n+\t\treturn 1;\n+\treturn 0;\n+}\n+\n+static inline uint64_t\n+diff_ns(struct timespec *now, struct timespec *at)\n+{\n+\tuint64_t now_ns, at_ns;\n+\n+\tif (timespec_cmp(now, at) >= 0)\n+\t\treturn 0;\n+\n+\tnow_ns = now->tv_sec * NS_PER_S + now->tv_nsec;\n+\tat_ns = at->tv_sec * NS_PER_S + at->tv_nsec;\n+\n+\treturn at_ns - now_ns;\n+}\n+\n+int\n+eal_alarm_get_timeout_ns(uint64_t *val)\n+{\n+\tstruct alarm_entry *ap;\n+\tstruct timespec now;\n+\n+\tif (clock_gettime(CLOCK_TYPE_ID, &now) < 0)\n+\t\treturn -1;\n+\n+\tif (LIST_EMPTY(&alarm_list))\n+\t\treturn -1;\n+\n+\tap = LIST_FIRST(&alarm_list);\n+\n+\t*val = diff_ns(&now, &ap->time);\n+\n \treturn 0;\n }\n \n+static int\n+unregister_current_callback(void)\n+{\n+\tstruct alarm_entry *ap;\n+\tint ret = 0;\n+\n+\tif (!LIST_EMPTY(&alarm_list)) {\n+\t\tap = LIST_FIRST(&alarm_list);\n+\n+\t\tdo {\n+\t\t\tret = rte_intr_callback_unregister(&intr_handle,\n+\t\t\t\teal_alarm_callback, &ap->time);\n+\t\t} while (ret == -EAGAIN);\n+\t}\n+\n+\treturn ret;\n+}\n+\n+static int\n+register_first_callback(void)\n+{\n+\tstruct alarm_entry *ap;\n+\tint ret = 0;\n+\n+\tif (!LIST_EMPTY(&alarm_list)) {\n+\t\tap = LIST_FIRST(&alarm_list);\n+\n+\t\t/* register a new callback */\n+\t\tret = rte_intr_callback_register(&intr_handle,\n+\t\t\t\teal_alarm_callback, &ap->time);\n+\t}\n+\treturn ret;\n+}\n+\n+static void\n+eal_alarm_callback(void *arg __rte_unused)\n+{\n+\tstruct timespec now;\n+\tstruct alarm_entry *ap;\n+\n+\trte_spinlock_lock(&alarm_list_lk);\n+\tap = LIST_FIRST(&alarm_list);\n+\n+\tif (clock_gettime(CLOCK_TYPE_ID, &now) < 0)\n+\t\treturn;\n+\n+\twhile (ap != NULL && timespec_cmp(&now, &ap->time) >= 0) {\n+\t\tap->executing = 1;\n+\t\tap->executing_id = pthread_self();\n+\t\trte_spinlock_unlock(&alarm_list_lk);\n+\n+\t\tap->cb_fn(ap->cb_arg);\n+\n+\t\trte_spinlock_lock(&alarm_list_lk);\n+\n+\t\tLIST_REMOVE(ap, next);\n+\t\tfree(ap);\n+\n+\t\tap = LIST_FIRST(&alarm_list);\n+\t}\n+\n+\t/* timer has been deleted from the kqueue, so recreate it if needed */\n+\tregister_first_callback();\n+\n+\trte_spinlock_unlock(&alarm_list_lk);\n+}\n+\n \n int\n-rte_eal_alarm_set(uint64_t us __rte_unused,\n-\t\trte_eal_alarm_callback cb_fn __rte_unused,\n-\t\tvoid *cb_arg __rte_unused)\n+rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg)\n {\n-\treturn -ENOTSUP;\n+\tstruct alarm_entry *ap, *new_alarm;\n+\tstruct timespec now;\n+\tuint64_t ns;\n+\tint ret = 0;\n+\n+\t/* check parameters, also ensure us won't cause a uint64_t overflow */\n+\tif (us < 1 || us > (UINT64_MAX - US_PER_S) || cb_fn == NULL)\n+\t\treturn -EINVAL;\n+\n+\tnew_alarm = calloc(1, sizeof(*new_alarm));\n+\tif (new_alarm == NULL)\n+\t\treturn -ENOMEM;\n+\n+\t/* use current time to calculate absolute time of alarm */\n+\tclock_gettime(CLOCK_TYPE_ID, &now);\n+\n+\tns = us * NS_PER_US;\n+\n+\tnew_alarm->cb_fn = cb_fn;\n+\tnew_alarm->cb_arg = cb_arg;\n+\tnew_alarm->time.tv_nsec = (now.tv_nsec + ns) % NS_PER_S;\n+\tnew_alarm->time.tv_sec = now.tv_sec + ((now.tv_nsec + ns) / NS_PER_S);\n+\n+\trte_spinlock_lock(&alarm_list_lk);\n+\n+\tif (LIST_EMPTY(&alarm_list))\n+\t\tLIST_INSERT_HEAD(&alarm_list, new_alarm, next);\n+\telse {\n+\t\tLIST_FOREACH(ap, &alarm_list, next) {\n+\t\t\tif (timespec_cmp(&new_alarm->time, &ap->time) < 0) {\n+\t\t\t\tLIST_INSERT_BEFORE(ap, new_alarm, next);\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t\tif (LIST_NEXT(ap, next) == NULL) {\n+\t\t\t\tLIST_INSERT_AFTER(ap, new_alarm, next);\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\t/* re-register first callback just in case */\n+\tregister_first_callback();\n+\n+\trte_spinlock_unlock(&alarm_list_lk);\n+\n+\treturn ret;\n }\n \n int\n-rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn __rte_unused,\n-\t\tvoid *cb_arg __rte_unused)\n+rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)\n {\n-\treturn -ENOTSUP;\n+\tstruct alarm_entry *ap, *ap_prev;\n+\tint count = 0;\n+\tint err = 0;\n+\tint executing;\n+\n+\tif (!cb_fn) {\n+\t\trte_errno = EINVAL;\n+\t\treturn -1;\n+\t}\n+\n+\tdo {\n+\t\texecuting = 0;\n+\t\trte_spinlock_lock(&alarm_list_lk);\n+\t\t/* remove any matches at the start of the list */\n+\t\twhile (1) {\n+\t\t\tap = LIST_FIRST(&alarm_list);\n+\t\t\tif (ap == NULL)\n+\t\t\t\tbreak;\n+\t\t\tif (cb_fn != ap->cb_fn)\n+\t\t\t\tbreak;\n+\t\t\tif (cb_arg != ap->cb_arg && cb_arg != (void *) -1)\n+\t\t\t\tbreak;\n+\t\t\tif (ap->executing == 0) {\n+\t\t\t\tLIST_REMOVE(ap, next);\n+\t\t\t\tfree(ap);\n+\t\t\t\tcount++;\n+\t\t\t} else {\n+\t\t\t\t/* If calling from other context, mark that\n+\t\t\t\t * alarm is executing so loop can spin till it\n+\t\t\t\t * finish. Otherwise we are trying to cancel\n+\t\t\t\t * ourselves - mark it by EINPROGRESS.\n+\t\t\t\t */\n+\t\t\t\tif (pthread_equal(ap->executing_id,\n+\t\t\t\t\t\tpthread_self()) == 0)\n+\t\t\t\t\texecuting++;\n+\t\t\t\telse\n+\t\t\t\t\terr = EINPROGRESS;\n+\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\t\tap_prev = ap;\n+\n+\t\t/* now go through list, removing entries not at start */\n+\t\tLIST_FOREACH(ap, &alarm_list, next) {\n+\t\t\t/* this won't be true first time through */\n+\t\t\tif (cb_fn == ap->cb_fn &&\n+\t\t\t\t\t(cb_arg == (void *)-1 ||\n+\t\t\t\t\t cb_arg == ap->cb_arg)) {\n+\t\t\t\tif (ap->executing == 0) {\n+\t\t\t\t\tLIST_REMOVE(ap, next);\n+\t\t\t\t\tfree(ap);\n+\t\t\t\t\tcount++;\n+\t\t\t\t\tap = ap_prev;\n+\t\t\t\t} else if (pthread_equal(ap->executing_id,\n+\t\t\t\t\t\t\t pthread_self()) == 0) {\n+\t\t\t\t\texecuting++;\n+\t\t\t\t} else {\n+\t\t\t\t\terr = EINPROGRESS;\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\tap_prev = ap;\n+\t\t}\n+\t\trte_spinlock_unlock(&alarm_list_lk);\n+\t} while (executing != 0);\n+\n+\tif (count == 0 && err == 0)\n+\t\trte_errno = ENOENT;\n+\telse if (err)\n+\t\trte_errno = err;\n+\n+\trte_spinlock_lock(&alarm_list_lk);\n+\n+\t/* unregister if no alarms left, otherwise re-register first */\n+\tif (LIST_EMPTY(&alarm_list))\n+\t\tunregister_current_callback();\n+\telse\n+\t\tregister_first_callback();\n+\n+\trte_spinlock_unlock(&alarm_list_lk);\n+\n+\treturn count;\n }\ndiff --git a/lib/librte_eal/bsdapp/eal/eal_alarm_private.h b/lib/librte_eal/bsdapp/eal/eal_alarm_private.h\nnew file mode 100644\nindex 000000000..65c711518\n--- /dev/null\n+++ b/lib/librte_eal/bsdapp/eal/eal_alarm_private.h\n@@ -0,0 +1,19 @@\n+/* SPDX-License-Identifier: BSD-3-Clause\n+ * Copyright(c) 2018 Intel Corporation\n+ */\n+\n+#ifndef EAL_ALARM_PRIVATE_H\n+#define EAL_ALARM_PRIVATE_H\n+\n+#include <inttypes.h>\n+\n+/*\n+ * FreeBSD needs a back-channel communication mechanism between interrupt and\n+ * alarm thread, because on FreeBSD, timer period is set up inside the interrupt\n+ * API and not inside alarm API like on Linux.\n+ */\n+\n+int\n+eal_alarm_get_timeout_ns(uint64_t *val);\n+\n+#endif // EAL_ALARM_PRIVATE_H\ndiff --git a/lib/librte_eal/bsdapp/eal/eal_interrupts.c b/lib/librte_eal/bsdapp/eal/eal_interrupts.c\nindex d0db6c6ff..a4aac606e 100644\n--- a/lib/librte_eal/bsdapp/eal/eal_interrupts.c\n+++ b/lib/librte_eal/bsdapp/eal/eal_interrupts.c\n@@ -16,6 +16,7 @@\n #include <rte_interrupts.h>\n \n #include \"eal_private.h\"\n+#include \"eal_alarm_private.h\"\n \n #define MAX_INTR_EVENTS 16\n \n@@ -56,7 +57,22 @@ static volatile int kq = -1;\n static int\n intr_source_to_kevent(const struct rte_intr_handle *ih, struct kevent *ke)\n {\n-\tke->filter = EVFILT_READ;\n+\t/* alarm callbacks are special case */\n+\tif (ih->type == RTE_INTR_HANDLE_ALARM) {\n+\t\tuint64_t timeout_ns;\n+\n+\t\t/* get soonest alarm timeout */\n+\t\tif (eal_alarm_get_timeout_ns(&timeout_ns) < 0)\n+\t\t\treturn -1;\n+\n+\t\tke->filter = EVFILT_TIMER;\n+\t\t/* timers are one shot */\n+\t\tke->flags |= EV_ONESHOT;\n+\t\tke->fflags = NOTE_NSECONDS;\n+\t\tke->data = timeout_ns;\n+\t} else {\n+\t\tke->filter = EVFILT_READ;\n+\t}\n \tke->ident = ih->fd;\n \n \treturn 0;\n@@ -122,8 +138,10 @@ rte_intr_callback_register(const struct rte_intr_handle *intr_handle,\n \t\t}\n \t}\n \n-\t/* add events to the queue */\n-\tif (add_event) {\n+\t/* add events to the queue. timer events are special as we need to\n+\t * re-set the timer.\n+\t */\n+\tif (add_event || src->intr_handle.type == RTE_INTR_HANDLE_ALARM) {\n \t\tstruct kevent ke;\n \n \t\tmemset(&ke, 0, sizeof(ke));\n@@ -218,8 +236,9 @@ rte_intr_callback_unregister(const struct rte_intr_handle *intr_handle,\n \t\tif (kevent(kq, &ke, 1, NULL, 0, NULL) < 0) {\n \t\t\tRTE_LOG(ERR, EAL, \"Error removing fd %d kevent, %s\\n\",\n \t\t\t\tsrc->intr_handle.fd, strerror(errno));\n-\t\t\tret = -errno;\n-\t\t\tgoto out;\n+\t\t\t/* removing non-existent even is an expected condition\n+\t\t\t * in some circumstances (e.g. oneshot events).\n+\t\t\t */\n \t\t}\n \n \t\t/*walk through the callbacks and remove all that match. */\n",
    "prefixes": [
        "v2",
        "4/7"
    ]
}