Show a patch.

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

{
    "id": 40749,
    "url": "http://patches.dpdk.org/api/patches/40749/?format=api",
    "web_url": "http://patches.dpdk.org/patch/40749/",
    "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"
    },
    "msgid": "<20180607123849.14439-6-qi.z.zhang@intel.com>",
    "date": "2018-06-07T12:38:32",
    "name": "[dpdk-dev,05/22] ethdev: introduce device lock",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": true,
    "hash": "fe1f2310d0986ee867e1134ecec59a939ee5b71e",
    "submitter": {
        "id": 504,
        "url": "http://patches.dpdk.org/api/people/504/?format=api",
        "name": "Zhang, Qi Z",
        "email": "qi.z.zhang@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/patch/40749/mbox/",
    "series": [
        {
            "id": 30,
            "url": "http://patches.dpdk.org/api/series/30/?format=api",
            "web_url": "http://patches.dpdk.org/project/dpdk/list/?series=30",
            "date": "2018-06-07T12:38:27",
            "name": "enable hotplug on multi-process",
            "version": 1,
            "mbox": "http://patches.dpdk.org/series/30/mbox/"
        }
    ],
    "comments": "http://patches.dpdk.org/api/patches/40749/comments/",
    "check": "fail",
    "checks": "http://patches.dpdk.org/api/patches/40749/checks/",
    "tags": {},
    "headers": {
        "List-Subscribe": "<https://dpdk.org/ml/listinfo/dev>,\n\t<mailto:dev-request@dpdk.org?subject=subscribe>",
        "X-IronPort-AV": "E=Sophos;i=\"5.49,486,1520924400\"; d=\"scan'208\";a=\"62605571\"",
        "X-Amp-File-Uploaded": "False",
        "Precedence": "list",
        "X-Mailman-Version": "2.1.15",
        "X-Original-To": "patchwork@dpdk.org",
        "List-Post": "<mailto:dev@dpdk.org>",
        "References": "<20180607123849.14439-1-qi.z.zhang@intel.com>",
        "X-BeenThere": "dev@dpdk.org",
        "List-Id": "DPDK patches and discussions <dev.dpdk.org>",
        "Subject": "[dpdk-dev] [PATCH 05/22] ethdev: introduce device lock",
        "Sender": "\"dev\" <dev-bounces@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 30C831B1E5;\n\tThu,  7 Jun 2018 14:38:36 +0200 (CEST)",
            "from mga04.intel.com (mga04.intel.com [192.55.52.120])\n\tby dpdk.org (Postfix) with ESMTP id 4DA3C1B1B5\n\tfor <dev@dpdk.org>; Thu,  7 Jun 2018 14:38:32 +0200 (CEST)",
            "from orsmga001.jf.intel.com ([10.7.209.18])\n\tby fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384;\n\t07 Jun 2018 05:38:31 -0700",
            "from dpdk51.sh.intel.com ([10.67.110.184])\n\tby orsmga001.jf.intel.com with ESMTP; 07 Jun 2018 05:38:30 -0700"
        ],
        "X-Amp-Result": "SKIPPED(no attachment in message)",
        "List-Archive": "<http://dpdk.org/ml/archives/dev/>",
        "X-Mailer": "git-send-email 2.13.6",
        "List-Unsubscribe": "<https://dpdk.org/ml/options/dev>,\n\t<mailto:dev-request@dpdk.org?subject=unsubscribe>",
        "X-ExtLoop1": "1",
        "Date": "Thu,  7 Jun 2018 20:38:32 +0800",
        "To": "thomas@monjalon.net,\n\tanatoly.burakov@intel.com",
        "From": "Qi Zhang <qi.z.zhang@intel.com>",
        "In-Reply-To": "<20180607123849.14439-1-qi.z.zhang@intel.com>",
        "Cc": "konstantin.ananyev@intel.com, dev@dpdk.org, bruce.richardson@intel.com, \n\tferruh.yigit@intel.com, benjamin.h.shelton@intel.com,\n\tnarender.vangati@intel.com, Qi Zhang <qi.z.zhang@intel.com>",
        "List-Help": "<mailto:dev-request@dpdk.org?subject=help>",
        "Errors-To": "dev-bounces@dpdk.org",
        "Message-Id": "<20180607123849.14439-6-qi.z.zhang@intel.com>",
        "Return-Path": "<dev-bounces@dpdk.org>"
    },
    "content": "Introduce API rte_eth_dev_lock and rte_eth_dev_unlock to let\napplication lock or unlock on specific ethdev, a locked device\ncan't be detached, this help applicaiton to prevent unexpected\ndevice detaching, especially in multi-process envrionment.\n\nAslo the new API let application to register a callback function\nwhich will be invoked before a device is going to be detached,\nthe return value of the function will decide if device will continue\nbe detached or not, this support application to do condition check\nat runtime.\n\nSigned-off-by: Qi Zhang <qi.z.zhang@intel.com>\n---\n lib/librte_ethdev/Makefile          |   1 +\n lib/librte_ethdev/rte_ethdev.c      |  41 ++++++++++++++-\n lib/librte_ethdev/rte_ethdev.h      |  64 ++++++++++++++++++++++\n lib/librte_ethdev/rte_ethdev_lock.c | 102 ++++++++++++++++++++++++++++++++++++\n lib/librte_ethdev/rte_ethdev_lock.h |  23 ++++++++\n lib/librte_ethdev/rte_ethdev_mp.c   |   3 +-\n 6 files changed, 232 insertions(+), 2 deletions(-)\n create mode 100644 lib/librte_ethdev/rte_ethdev_lock.c\n create mode 100644 lib/librte_ethdev/rte_ethdev_lock.h",
    "diff": "diff --git a/lib/librte_ethdev/Makefile b/lib/librte_ethdev/Makefile\nindex 04e93f337..5c4646469 100644\n--- a/lib/librte_ethdev/Makefile\n+++ b/lib/librte_ethdev/Makefile\n@@ -20,6 +20,7 @@ LIBABIVER := 9\n \n SRCS-y += rte_ethdev.c\n SRCS-y += rte_ethdev_mp.c\n+SRCS-y += rte_ethdev_lock.c\n SRCS-y += rte_flow.c\n SRCS-y += rte_tm.c\n SRCS-y += rte_mtr.c\ndiff --git a/lib/librte_ethdev/rte_ethdev.c b/lib/librte_ethdev/rte_ethdev.c\nindex 24360f522..6494e71a4 100644\n--- a/lib/librte_ethdev/rte_ethdev.c\n+++ b/lib/librte_ethdev/rte_ethdev.c\n@@ -42,6 +42,7 @@\n #include \"rte_ethdev_driver.h\"\n #include \"ethdev_profile.h\"\n #include \"rte_ethdev_mp.h\"\n+#include \"rte_ethdev_lock.h\"\n \n int ethdev_logtype;\n \n@@ -787,7 +788,6 @@ rte_eth_dev_attach(const char *devargs, uint16_t *port_id)\n int\n rte_eth_dev_attach_private(const char *devargs, uint16_t *port_id)\n {\n-\n \tif (rte_eal_process_type() == RTE_PROC_PRIMARY)\n \t\treturn -ENOTSUP;\n \n@@ -828,6 +828,10 @@ rte_eth_dev_detach(uint16_t port_id, char *name __rte_unused)\n \t\treturn req.result;\n \t}\n \n+\tret = process_lock_callbacks(port_id);\n+\tif (ret)\n+\t\treturn ret;\n+\n \t/* check pre_detach */\n \treq.t = REQ_TYPE_PRE_DETACH;\n \treq.port_id = port_id;\n@@ -870,6 +874,7 @@ int\n rte_eth_dev_detach_private(uint16_t port_id, char *name __rte_unused)\n {\n \tuint32_t dev_flags;\n+\tint ret;\n \n \tif (rte_eal_process_type() == RTE_PROC_PRIMARY)\n \t\treturn -ENOTSUP;\n@@ -883,6 +888,10 @@ rte_eth_dev_detach_private(uint16_t port_id, char *name __rte_unused)\n \t\treturn -ENOTSUP;\n \t}\n \n+\tret = process_lock_callbacks(port_id);\n+\tif (ret)\n+\t\treturn ret;\n+\n \treturn do_eth_dev_detach(port_id);\n }\n \n@@ -4683,6 +4692,36 @@ rte_eth_devargs_parse(const char *dargs, struct rte_eth_devargs *eth_da)\n \treturn result;\n }\n \n+static int\n+dev_is_busy(uint16_t port_id __rte_unused, void *user_args __rte_unused)\n+{\n+\treturn -EBUSY;\n+}\n+\n+int\n+rte_eth_dev_lock(uint16_t port_id, rte_eth_dev_lock_callback_t callback,\n+\t\t void *user_args)\n+{\n+\tRTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);\n+\n+\tif (callback == NULL)\n+\t\treturn register_lock_callback(port_id, dev_is_busy, NULL);\n+\telse\n+\t\treturn register_lock_callback(port_id, callback, user_args);\n+}\n+\n+int\n+rte_eth_dev_unlock(uint16_t port_id, rte_eth_dev_lock_callback_t callback,\n+\t\t   void *user_args)\n+{\n+\tRTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);\n+\n+\tif (callback == NULL)\n+\t\treturn unregister_lock_callback(port_id, dev_is_busy, NULL);\n+\telse\n+\t\treturn unregister_lock_callback(port_id, callback, user_args);\n+}\n+\n RTE_INIT(ethdev_init_log);\n static void\n ethdev_init_log(void)\ndiff --git a/lib/librte_ethdev/rte_ethdev.h b/lib/librte_ethdev/rte_ethdev.h\nindex bb03d613b..506b6acdd 100644\n--- a/lib/librte_ethdev/rte_ethdev.h\n+++ b/lib/librte_ethdev/rte_ethdev.h\n@@ -4356,6 +4356,70 @@ rte_eth_tx_buffer(uint16_t port_id, uint16_t queue_id,\n \treturn rte_eth_tx_buffer_flush(port_id, queue_id, buffer);\n }\n \n+/**\n+ * Callback function before device is detached.\n+ *\n+ * This type of function will be added into a function list, and will be\n+ * invoked before device be detached. Application can register a callback\n+ * function so it can be notified and do some cleanup before detach happen.\n+ * Also, any callback function return !0 value will prevent device be\n+ * detached(ref. rte_eth_dev_lock and rte_eth_dev_unlock).\n+ *\n+ * @param port_id\n+ *   The port identifier of the Ethernet device.\n+ * @param user_args\n+ *   This is parameter \"user_args\" be saved when callback function is\n+ *   registered(rte_dev_eth_lock).\n+ *\n+ * @return\n+ *   0  device is allowed be detached.\n+ *   !0 device is not allowed be detached.\n+ */\n+typedef int (*rte_eth_dev_lock_callback_t)(uint16_t port_id, void *user_args);\n+\n+/**\n+ * Lock an Ethernet Device directly or register a callback function\n+ * for condition check at runtime, this help application to prevent\n+ * a device be detached unexpectly.\n+ * NOTE: Lock a device mutliple times with same parmeter will increase\n+ * a ref_count, and coresponding unlock decrease the ref_count, the\n+ * device will be unlocked when ref_count reach 0.\n+ *\n+ * @param port_id\n+ *   The port identifier of the Ethernet device.\n+ * @param callback\n+ *   !NULL the callback function will be added into a pre-detach list,\n+ *         it will be invoked when a device is going to be detached. The\n+ *         return value will decide if continue detach the device or not.\n+ *   NULL  lock the device directly, basically this just regiter a empty\n+ *         callback function(dev_is_busy) that return -EBUSY, so we can\n+ *         handle the pre-detach check in unified way.\n+ * @param user_args\n+ *   parameter will be parsed to callback function, only valid when\n+ *   callback != NULL.\n+ * @return\n+ *   0 on success, negative on error.\n+ */\n+int rte_eth_dev_lock(uint16_t port_id, rte_eth_dev_lock_callback_t callback,\n+\t\t     void *user_args);\n+\n+/**\n+ * Reverse operation of rte_eth_dev_lock.\n+ *\n+ * @param port_id\n+ *   The port identifier of the Ethernet device.\n+ * @param callback\n+ *   NULL  decrease the ref_count of default callback function.\n+ *   !NULL decrease the ref_count of specific callback with matched\n+ *         user_args.\n+ * @param user_args\n+ *   parameter to match, only valid when callback != NULL.\n+ * @return\n+ *   0 on success, negative on error.\n+ */\n+int rte_eth_dev_unlock(uint16_t port_id, rte_eth_dev_lock_callback_t callback,\n+\t\t       void *user_args);\n+\n #ifdef __cplusplus\n }\n #endif\ndiff --git a/lib/librte_ethdev/rte_ethdev_lock.c b/lib/librte_ethdev/rte_ethdev_lock.c\nnew file mode 100644\nindex 000000000..688d1d70a\n--- /dev/null\n+++ b/lib/librte_ethdev/rte_ethdev_lock.c\n@@ -0,0 +1,102 @@\n+/* SPDX-License-Identifier: BSD-3-Clause\n+ * Copyright(c) 2018 Intel Corporation\n+ */\n+#include \"rte_ethdev_lock.h\"\n+\n+struct lock_entry {\n+\tTAILQ_ENTRY(lock_entry) next;\n+\trte_eth_dev_lock_callback_t callback;\n+\tuint16_t port_id;\n+\tvoid *user_args;\n+\tint ref_count;\n+};\n+\n+TAILQ_HEAD(lock_entry_list, lock_entry);\n+static struct lock_entry_list lock_entry_list =\n+\tTAILQ_HEAD_INITIALIZER(lock_entry_list);\n+static rte_spinlock_t lock_entry_lock = RTE_SPINLOCK_INITIALIZER;\n+\n+int\n+register_lock_callback(uint16_t port_id,\n+\t\t       rte_eth_dev_lock_callback_t callback,\n+\t\t       void *user_args)\n+{\n+\tstruct lock_entry *le;\n+\n+\trte_spinlock_lock(&lock_entry_lock);\n+\n+\tTAILQ_FOREACH(le, &lock_entry_list, next) {\n+\t\tif (le->port_id == port_id &&\n+\t\t    le->callback == callback &&\n+\t\t    le->user_args == user_args)\n+\t\t\tbreak;\n+\t}\n+\n+\tif (!le) {\n+\t\tle = calloc(1, sizeof(struct lock_entry));\n+\t\tif (!le) {\n+\t\t\trte_spinlock_unlock(&lock_entry_lock);\n+\t\t\treturn -ENOMEM;\n+\t\t}\n+\t\tle->callback = callback;\n+\t\tle->port_id = port_id;\n+\t\tle->user_args = user_args;\n+\t\tTAILQ_INSERT_TAIL(&lock_entry_list, le, next);\n+\t}\n+\tle->ref_count++;\n+\n+\trte_spinlock_unlock(&lock_entry_lock);\n+\treturn 0;\n+}\n+\n+int\n+unregister_lock_callback(uint16_t port_id,\n+\t\t\t rte_eth_dev_lock_callback_t callback,\n+\t\t\t void *user_args)\n+{\n+\tstruct lock_entry *le;\n+\tint ret = 0;\n+\n+\trte_spinlock_lock(&lock_entry_lock);\n+\n+\tTAILQ_FOREACH(le, &lock_entry_list, next) {\n+\t\tif (le->port_id == port_id &&\n+\t\t    le->callback == callback &&\n+\t\t    le->user_args == user_args)\n+\t\t\tbreak;\n+\t}\n+\n+\tif (le) {\n+\t\tle->ref_count--;\n+\t\tif (!le->ref_count) {\n+\t\t\tTAILQ_REMOVE(&lock_entry_list, le, next);\n+\t\t\tfree(le);\n+\t\t}\n+\t} else {\n+\t\tret = -ENOENT;\n+\t}\n+\n+\trte_spinlock_unlock(&lock_entry_lock);\n+\treturn ret;\n+}\n+\n+int\n+process_lock_callbacks(uint16_t port_id)\n+{\n+\tstruct lock_entry *le;\n+\n+\trte_spinlock_lock(&lock_entry_lock);\n+\n+\tTAILQ_FOREACH(le, &lock_entry_list, next) {\n+\t\tif (le->port_id != port_id)\n+\t\t\tcontinue;\n+\n+\t\tif (le->callback(port_id, le->user_args)) {\n+\t\t\trte_spinlock_unlock(&lock_entry_lock);\n+\t\t\treturn -EBUSY;\n+\t\t}\n+\t}\n+\n+\trte_spinlock_unlock(&lock_entry_lock);\n+\treturn 0;\n+}\ndiff --git a/lib/librte_ethdev/rte_ethdev_lock.h b/lib/librte_ethdev/rte_ethdev_lock.h\nnew file mode 100644\nindex 000000000..7b370c926\n--- /dev/null\n+++ b/lib/librte_ethdev/rte_ethdev_lock.h\n@@ -0,0 +1,23 @@\n+/* SPDX-License-Identifier: BSD-3-Clause\n+ * Copyright(c) 2018 Intel Corporation\n+ */\n+\n+#ifndef _RTE_ETHDEV_LOCK_H_\n+#define _RTE_ETHDEV_LOCK_H_\n+\n+#include \"rte_ethdev.h\"\n+\n+int\n+register_lock_callback(uint16_t port_id,\n+\t\t       rte_eth_dev_lock_callback_t callback,\n+\t\t       void *user_args);\n+\n+int\n+unregister_lock_callback(uint16_t port_id,\n+\t\t\t rte_eth_dev_lock_callback_t callback,\n+\t\t\t void *user_args);\n+\n+int\n+process_lock_callbacks(uint16_t port_id);\n+\n+#endif\ndiff --git a/lib/librte_ethdev/rte_ethdev_mp.c b/lib/librte_ethdev/rte_ethdev_mp.c\nindex 8ede8151d..e23c8b010 100644\n--- a/lib/librte_ethdev/rte_ethdev_mp.c\n+++ b/lib/librte_ethdev/rte_ethdev_mp.c\n@@ -4,6 +4,7 @@\n \n #include \"rte_ethdev_driver.h\"\n #include \"rte_ethdev_mp.h\"\n+#include \"rte_ethdev_lock.h\"\n \n static int detach_on_secondary(uint16_t port_id)\n {\n@@ -101,7 +102,7 @@ static int handle_primary_request(const struct rte_mp_msg *msg, const void *peer\n \t\tret = attach_on_secondary(req->devargs, req->port_id);\n \t\tbreak;\n \tcase REQ_TYPE_PRE_DETACH:\n-\t\tret = 0;\n+\t\tret = process_lock_callbacks(req->port_id);\n \t\tbreak;\n \tcase REQ_TYPE_DETACH:\n \tcase REQ_TYPE_ATTACH_ROLLBACK:\n",
    "prefixes": [
        "dpdk-dev",
        "05/22"
    ]
}