get:
Show a patch.

patch:
Update a patch.

put:
Update a patch.

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

{
    "id": 1525272,
    "url": "http://patchwork.ozlabs.org/api/patches/1525272/",
    "web_url": "http://patchwork.ozlabs.org/project/openvswitch/patch/20210907111725.43672-2-cian.ferriter@intel.com/",
    "project": {
        "id": 47,
        "url": "http://patchwork.ozlabs.org/api/projects/47/",
        "name": "Open vSwitch",
        "link_name": "openvswitch",
        "list_id": "ovs-dev.openvswitch.org",
        "list_email": "ovs-dev@openvswitch.org",
        "web_url": "http://openvswitch.org/",
        "scm_url": "git@github.com:openvswitch/ovs.git",
        "webscm_url": "https://github.com/openvswitch/ovs",
        "list_archive_url": "",
        "list_archive_url_format": "",
        "commit_url_format": ""
    },
    "msgid": "<20210907111725.43672-2-cian.ferriter@intel.com>",
    "list_archive_url": null,
    "date": "2021-09-07T11:17:23",
    "name": "[ovs-dev,v2,1/3] dpif-netdev: Add a per thread work ring",
    "commit_ref": null,
    "pull_url": null,
    "state": "rejected",
    "archived": false,
    "hash": "6b03887bed173396859450e2b57fd55264166635",
    "submitter": {
        "id": 70444,
        "url": "http://patchwork.ozlabs.org/api/people/70444/",
        "name": "Ferriter, Cian",
        "email": "cian.ferriter@intel.com"
    },
    "delegate": null,
    "mbox": "http://patchwork.ozlabs.org/project/openvswitch/patch/20210907111725.43672-2-cian.ferriter@intel.com/mbox/",
    "series": [
        {
            "id": 261267,
            "url": "http://patchwork.ozlabs.org/api/series/261267/",
            "web_url": "http://patchwork.ozlabs.org/project/openvswitch/list/?series=261267",
            "date": "2021-09-07T11:17:22",
            "name": "Userspace deferral of work",
            "version": 2,
            "mbox": "http://patchwork.ozlabs.org/series/261267/mbox/"
        }
    ],
    "comments": "http://patchwork.ozlabs.org/api/patches/1525272/comments/",
    "check": "success",
    "checks": "http://patchwork.ozlabs.org/api/patches/1525272/checks/",
    "tags": {},
    "related": [],
    "headers": {
        "Return-Path": "<ovs-dev-bounces@openvswitch.org>",
        "X-Original-To": [
            "incoming@patchwork.ozlabs.org",
            "ovs-dev@openvswitch.org"
        ],
        "Delivered-To": [
            "patchwork-incoming@bilbo.ozlabs.org",
            "ovs-dev@lists.linuxfoundation.org"
        ],
        "Authentication-Results": "ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org\n (client-ip=140.211.166.136; helo=smtp3.osuosl.org;\n envelope-from=ovs-dev-bounces@openvswitch.org; receiver=<UNKNOWN>)",
        "Received": [
            "from smtp3.osuosl.org (smtp3.osuosl.org [140.211.166.136])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest\n SHA256)\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 4H3jRB65fGz9sf8\n\tfor <incoming@patchwork.ozlabs.org>; Tue,  7 Sep 2021 21:17:26 +1000 (AEST)",
            "from localhost (localhost [127.0.0.1])\n\tby smtp3.osuosl.org (Postfix) with ESMTP id 4DAE060BBF;\n\tTue,  7 Sep 2021 11:17:24 +0000 (UTC)",
            "from smtp3.osuosl.org ([127.0.0.1])\n\tby localhost (smtp3.osuosl.org [127.0.0.1]) (amavisd-new, port 10024)\n\twith ESMTP id sWfSWxi_nFsF; Tue,  7 Sep 2021 11:17:22 +0000 (UTC)",
            "from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56])\n\tby smtp3.osuosl.org (Postfix) with ESMTPS id D28E160BA3;\n\tTue,  7 Sep 2021 11:17:20 +0000 (UTC)",
            "from lf-lists.osuosl.org (localhost [127.0.0.1])\n\tby lists.linuxfoundation.org (Postfix) with ESMTP id 02747C0025;\n\tTue,  7 Sep 2021 11:17:19 +0000 (UTC)",
            "from smtp4.osuosl.org (smtp4.osuosl.org [IPv6:2605:bc80:3010::137])\n by lists.linuxfoundation.org (Postfix) with ESMTP id 3526BC000F\n for <ovs-dev@openvswitch.org>; Tue,  7 Sep 2021 11:17:17 +0000 (UTC)",
            "from localhost (localhost [127.0.0.1])\n by smtp4.osuosl.org (Postfix) with ESMTP id C358F40527\n for <ovs-dev@openvswitch.org>; Tue,  7 Sep 2021 11:17:16 +0000 (UTC)",
            "from smtp4.osuosl.org ([127.0.0.1])\n by localhost (smtp4.osuosl.org [127.0.0.1]) (amavisd-new, port 10024)\n with ESMTP id i_Y_sPA0vpmu for <ovs-dev@openvswitch.org>;\n Tue,  7 Sep 2021 11:17:15 +0000 (UTC)",
            "from mga17.intel.com (mga17.intel.com [192.55.52.151])\n by smtp4.osuosl.org (Postfix) with ESMTPS id 1A3B04039F\n for <ovs-dev@openvswitch.org>; Tue,  7 Sep 2021 11:17:14 +0000 (UTC)",
            "from fmsmga003.fm.intel.com ([10.253.24.29])\n by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384;\n 07 Sep 2021 04:17:14 -0700",
            "from silpixa00399779.ir.intel.com (HELO\n silpixa00399779.ger.corp.intel.com) ([10.237.222.105])\n by FMSMGA003.fm.intel.com with ESMTP; 07 Sep 2021 04:17:12 -0700"
        ],
        "X-Virus-Scanned": [
            "amavisd-new at osuosl.org",
            "amavisd-new at osuosl.org"
        ],
        "X-Greylist": "domain auto-whitelisted by SQLgrey-1.8.0",
        "X-IronPort-AV": [
            "E=McAfee;i=\"6200,9189,10099\"; a=\"200378292\"",
            "E=Sophos;i=\"5.85,274,1624345200\"; d=\"scan'208\";a=\"200378292\"",
            "E=Sophos;i=\"5.85,274,1624345200\"; d=\"scan'208\";a=\"537926491\""
        ],
        "X-ExtLoop1": "1",
        "From": "Cian Ferriter <cian.ferriter@intel.com>",
        "To": "ovs-dev@openvswitch.org",
        "Date": "Tue,  7 Sep 2021 12:17:23 +0100",
        "Message-Id": "<20210907111725.43672-2-cian.ferriter@intel.com>",
        "X-Mailer": "git-send-email 2.32.0",
        "In-Reply-To": "<20210907111725.43672-1-cian.ferriter@intel.com>",
        "References": "<20210907111725.43672-1-cian.ferriter@intel.com>",
        "MIME-Version": "1.0",
        "Subject": "[ovs-dev] [PATCH v2 1/3] dpif-netdev: Add a per thread work ring",
        "X-BeenThere": "ovs-dev@openvswitch.org",
        "X-Mailman-Version": "2.1.15",
        "Precedence": "list",
        "List-Id": "<ovs-dev.openvswitch.org>",
        "List-Unsubscribe": "<https://mail.openvswitch.org/mailman/options/ovs-dev>,\n <mailto:ovs-dev-request@openvswitch.org?subject=unsubscribe>",
        "List-Archive": "<http://mail.openvswitch.org/pipermail/ovs-dev/>",
        "List-Post": "<mailto:ovs-dev@openvswitch.org>",
        "List-Help": "<mailto:ovs-dev-request@openvswitch.org?subject=help>",
        "List-Subscribe": "<https://mail.openvswitch.org/mailman/listinfo/ovs-dev>,\n <mailto:ovs-dev-request@openvswitch.org?subject=subscribe>",
        "Content-Type": "text/plain; charset=\"us-ascii\"",
        "Content-Transfer-Encoding": "7bit",
        "Errors-To": "ovs-dev-bounces@openvswitch.org",
        "Sender": "\"dev\" <ovs-dev-bounces@openvswitch.org>"
    },
    "content": "These work rings help with handling the asynchronous TX usecase. In this\nusecase, netdev_send will be called, but packets won't be immediately\nsent by the thread calling netdev_send, but instead handled by a\ndifferent resource. Since the TX is not instantaneous, the thread\ncalling netdev_send can't immediately free the packets being sent, or\nreport them as sent. Rather than the thread polling for completion of\nthe TX, it is desirable that the thread to move on and process more\npackets.\n\nThe work ring serves as a FIFO queue to keep track of the asynchronous\nTX calls that have been kicked off. The work ring is added/queued to when\nnetdev_send returns '-EINPROGRESS' indicating it kicked off an\nasynchronous TX. The work ring is taken/dequeued from in 2 main cases:\n1. In pmd_thread_main after processing every rxq assigned to the thread.\n2. When the ring is full while trying to queue work.\n\nSome dp_defer functions are defined in dpif-netev.c. It would be nice to\ndefine these in dpif-netdev-private-defer.h, but these functions rely on\nthe cycle_time_stop/start() functions and dp_netdev_rxq_add_cycles(). I\ntried to move these to a header so they could be included, but they rely\non more structs. I stopped this because I would have to move bigger\nstructs like \"struct dp_netdev_rxq\" which rely on even more structs all\ndefined in dpif-netdev.c.\n\nSigned-off-by: Cian Ferriter <cian.ferriter@intel.com>\nCo-authored-by: Harry van Haaren <harry.van.haaren@intel.com>\nSigned-off-by: Harry van Haaren <harry.van.haaren@intel.com>\nCo-authored-by: Sunil Pai G <sunil.pai.g@intel.com>\nSigned-off-by: Sunil Pai G <sunil.pai.g@intel.com>\n\n---\n\nv2:\n- Implement and use a simpler ring buffer in OVS, rather than using the\n  DPDK implementation.\n- Only print work defer stats if some work has actually been deferred.\n- Add a \"force\" flag to the \"process_async()\" API to implement an\n  attempt limit on the number of times an asynchronous piece of work\n  should be attempted.\n- Do all outstanding work on a PMD thread before allowing a reload to\n  occur.\n---\n lib/automake.mk                  |   1 +\n lib/dpif-netdev-perf.c           |  20 ++++-\n lib/dpif-netdev-perf.h           |   9 ++\n lib/dpif-netdev-private-defer.h  |  84 +++++++++++++++++++\n lib/dpif-netdev-private-thread.h |   4 +\n lib/dpif-netdev.c                | 139 +++++++++++++++++++++++++++++--\n lib/netdev-dpdk.c                |  22 +++--\n lib/netdev-provider.h            |  19 ++++-\n lib/netdev.c                     |   3 +-\n 9 files changed, 286 insertions(+), 15 deletions(-)\n create mode 100644 lib/dpif-netdev-private-defer.h",
    "diff": "diff --git a/lib/automake.mk b/lib/automake.mk\nindex 46f869a33..0d910bc92 100644\n--- a/lib/automake.mk\n+++ b/lib/automake.mk\n@@ -115,6 +115,7 @@ lib_libopenvswitch_la_SOURCES = \\\n \tlib/dpif-netdev-lookup-generic.c \\\n \tlib/dpif-netdev.c \\\n \tlib/dpif-netdev.h \\\n+\tlib/dpif-netdev-private-defer.h \\\n \tlib/dpif-netdev-private-dfc.c \\\n \tlib/dpif-netdev-private-dfc.h \\\n \tlib/dpif-netdev-private-dpcls.h \\\ndiff --git a/lib/dpif-netdev-perf.c b/lib/dpif-netdev-perf.c\nindex d7676ea2b..859ef300c 100644\n--- a/lib/dpif-netdev-perf.c\n+++ b/lib/dpif-netdev-perf.c\n@@ -230,6 +230,7 @@ pmd_perf_format_overall_stats(struct ds *str, struct pmd_perf_stats *s,\n     uint64_t tot_iter = histogram_samples(&s->pkts);\n     uint64_t idle_iter = s->pkts.bin[0];\n     uint64_t busy_iter = tot_iter >= idle_iter ? tot_iter - idle_iter : 0;\n+    uint64_t work_deferred = stats[PMD_STAT_WORK_DEFER];\n \n     ds_put_format(str,\n             \"  Iterations:        %12\"PRIu64\"  (%.2f us/it)\\n\"\n@@ -284,7 +285,24 @@ pmd_perf_format_overall_stats(struct ds *str, struct pmd_perf_stats *s,\n             tx_packets, (tx_packets / duration) / 1000,\n             tx_batches, 1.0 * tx_packets / tx_batches);\n     } else {\n-        ds_put_format(str, \"  Tx packets:        %12d\\n\\n\", 0);\n+        ds_put_format(str, \"  Tx packets:        %12d\\n\", 0);\n+    }\n+    if (work_deferred > 0) {\n+        uint64_t work_compl_checks = stats[PMD_STAT_WORK_IN_PROG] +\n+                                     stats[PMD_STAT_WORK_DONE];\n+\n+        ds_put_format(str,\n+            \"  Work deferred:                   %12\"PRIu64\"\\n\"\n+            \"  - Deferred work done:            %12\"PRIu64\"\\n\"\n+            \"  - Work completion checks:        %12\"PRIu64\n+                                                \"  (%.2f checks/work item)\\n\"\n+            \"  - Ring full when deferring work: %12\"PRIu64\"\\n\"\n+            \"  - Deferred work dropped:         %12\"PRIu64\"\\n\",\n+            work_deferred, stats[PMD_STAT_WORK_DONE], work_compl_checks,\n+            1.0 * work_compl_checks / stats[PMD_STAT_WORK_DONE],\n+            stats[PMD_STAT_WORK_R_FULL], stats[PMD_STAT_WORK_DROPPED]);\n+    } else {\n+        ds_put_format(str, \"  Work deferred:     %12d\\n\\n\", 0);\n     }\n }\n \ndiff --git a/lib/dpif-netdev-perf.h b/lib/dpif-netdev-perf.h\nindex 834c26260..e9c02a866 100644\n--- a/lib/dpif-netdev-perf.h\n+++ b/lib/dpif-netdev-perf.h\n@@ -76,6 +76,15 @@ enum pmd_stat_type {\n                              * recirculation. */\n     PMD_STAT_SENT_PKTS,     /* Packets that have been sent. */\n     PMD_STAT_SENT_BATCHES,  /* Number of batches sent. */\n+    PMD_STAT_WORK_DEFER,    /* Number of times that work was deferred. */\n+    PMD_STAT_WORK_IN_PROG,  /* Number of times that work was still in progress\n+                               when checked by a thread. */\n+    PMD_STAT_WORK_R_FULL,   /* Number of times work ring was full when\n+                             * deferring work. */\n+    PMD_STAT_WORK_DONE,     /* Number of times that deferred work was\n+                             * completed. */\n+    PMD_STAT_WORK_DROPPED,  /* Number of times that deferred work was dropped.\n+                             */\n     PMD_CYCLES_ITER_IDLE,   /* Cycles spent in idle iterations. */\n     PMD_CYCLES_ITER_BUSY,   /* Cycles spent in busy iterations. */\n     PMD_CYCLES_UPCALL,      /* Cycles spent processing upcalls. */\ndiff --git a/lib/dpif-netdev-private-defer.h b/lib/dpif-netdev-private-defer.h\nnew file mode 100644\nindex 000000000..78c140f56\n--- /dev/null\n+++ b/lib/dpif-netdev-private-defer.h\n@@ -0,0 +1,84 @@\n+/*\n+ * Copyright (c) 2021 Intel Corporation.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at:\n+ *\n+ *     http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+\n+#ifndef DPIF_NETDEV_PRIVATE_DEFER_H\n+#define DPIF_NETDEV_PRIVATE_DEFER_H 1\n+\n+#include <stdbool.h>\n+#include <stdint.h>\n+\n+#include \"dpif.h\"\n+#include \"dpif-netdev-perf.h\"\n+#include \"cmap.h\"\n+\n+#ifdef  __cplusplus\n+extern \"C\" {\n+#endif\n+\n+/* Function definition for deferred work. */\n+typedef int (*dp_defer_work_func)(struct netdev *netdev, int qid, bool force);\n+\n+/* Structure to track outstanding work to be done. */\n+struct dp_defer_work_item {\n+    dp_defer_work_func work_func;\n+    void *netdev;\n+    int qid;\n+    uint32_t attempts;\n+};\n+\n+#define WORK_RING_SIZE 128\n+#define WORK_RING_MASK (WORK_RING_SIZE - 1)\n+\n+#define ATTEMPT_LIMIT 1000\n+\n+/* The read and write indexes are between 0 and 2^32, and we mask their value\n+ * when we access the work_ring[] array. */\n+struct dp_defer {\n+    uint32_t read_idx;\n+    uint32_t write_idx;\n+    struct dp_defer_work_item work_ring[WORK_RING_SIZE];\n+};\n+\n+static inline void\n+dp_defer_init(struct dp_defer *defer)\n+{\n+    defer->read_idx = 0;\n+    defer->write_idx = 0;\n+}\n+\n+static inline int\n+dp_defer_work_ring_empty(const struct dp_defer *defer)\n+{\n+    return defer->write_idx == defer->read_idx;\n+}\n+\n+static inline int\n+dp_defer_work_ring_full(const struct dp_defer *defer)\n+{\n+    /* When the write index is exactly (WORK_RING_SIZE - 1) or WORK_RING_MASK\n+     * elements ahead of the read index, the ring is full. When calculating the\n+     * difference between the indexes, wraparound is not an issue since\n+     * unsigned ints are used. */\n+    uint16_t count = (defer->write_idx - defer->read_idx) & WORK_RING_MASK;\n+\n+    return count == WORK_RING_MASK;\n+}\n+\n+#ifdef  __cplusplus\n+}\n+#endif\n+\n+#endif /* dpif-netdev-private-defer.h */\ndiff --git a/lib/dpif-netdev-private-thread.h b/lib/dpif-netdev-private-thread.h\nindex a782d9678..d14a5ade7 100644\n--- a/lib/dpif-netdev-private-thread.h\n+++ b/lib/dpif-netdev-private-thread.h\n@@ -20,6 +20,7 @@\n \n #include \"dpif.h\"\n #include \"dpif-netdev-perf.h\"\n+#include \"dpif-netdev-private-defer.h\"\n #include \"dpif-netdev-private-dfc.h\"\n #include \"dpif-netdev-private-dpif.h\"\n \n@@ -219,6 +220,9 @@ struct dp_netdev_pmd_thread {\n \n     /* Next time when PMD should try RCU quiescing. */\n     long long next_rcu_quiesce;\n+\n+    /* Structure to track deferred work in this thread. */\n+    struct dp_defer defer;\n };\n \n #ifdef  __cplusplus\ndiff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c\nindex b3e57bb95..f4143a93a 100644\n--- a/lib/dpif-netdev.c\n+++ b/lib/dpif-netdev.c\n@@ -435,6 +435,7 @@ struct tx_port {\n     long long flush_time;\n     struct dp_packet_batch output_pkts;\n     struct dp_netdev_rxq *output_pkts_rxqs[NETDEV_MAX_BURST];\n+    dp_defer_work_func cached_work_func;\n };\n \n /* Contained by struct tx_bond 'member_buckets'. */\n@@ -4591,6 +4592,97 @@ pmd_perf_metrics_enabled(const struct dp_netdev_pmd_thread *pmd OVS_UNUSED)\n }\n #endif\n \n+/* Try to do one piece of work from the work ring.\n+ *\n+ * Returns:\n+ * -ENOENT: No work to do. The ring is empty.\n+ * -EINPROGRESS: The work is still in progress, I can't do it.\n+ * 0: One piece of work was taken from the ring. It was either successfully\n+ *    handled, or dropped if attempted too many times.\n+ */\n+static inline unsigned int\n+dp_defer_do_work(struct dp_defer *defer, struct pmd_perf_stats *perf_stats)\n+{\n+    struct dp_defer_work_item *work;\n+    uint32_t read_idx;\n+    int ret;\n+\n+    /* Check that there's a piece of work in the ring to do. */\n+    if (dp_defer_work_ring_empty(defer)) {\n+        return -ENOENT;\n+    }\n+\n+    read_idx = defer->read_idx & WORK_RING_MASK;\n+    work = &defer->work_ring[read_idx];\n+    ret = work->work_func(work->netdev, work->qid, false);\n+\n+    if (ret == -EINPROGRESS) {\n+        pmd_perf_update_counter(perf_stats, PMD_STAT_WORK_IN_PROG, 1);\n+\n+        work->attempts++;\n+        if (work->attempts > ATTEMPT_LIMIT) {\n+            ret = work->work_func(work->netdev, work->qid, true);\n+            defer->read_idx++;\n+\n+            if (ret) {\n+                pmd_perf_update_counter(perf_stats, PMD_STAT_WORK_DROPPED, 1);\n+            } else {\n+                pmd_perf_update_counter(perf_stats, PMD_STAT_WORK_DONE, 1);\n+            }\n+\n+            return 0;\n+        }\n+\n+        return ret;\n+    }\n+\n+    defer->read_idx++;\n+\n+    pmd_perf_update_counter(perf_stats, PMD_STAT_WORK_DONE, 1);\n+\n+    return 0;\n+}\n+\n+static inline void\n+dp_defer_work(struct dp_defer *defer, struct pmd_perf_stats *perf_stats,\n+              struct dp_defer_work_item *work)\n+{\n+    struct dp_defer_work_item *ring_item;\n+    uint32_t write_idx;\n+\n+    /* Check that we have enough room in ring. */\n+    if (dp_defer_work_ring_full(defer)) {\n+        /* The work ring is full, try to make room by doing work. Doing work\n+         * can fail to make room if the work has to be requeued. Keep trying to\n+         * do work until there is room in the ring. */\n+        pmd_perf_update_counter(perf_stats, PMD_STAT_WORK_R_FULL, 1);\n+\n+        while (dp_defer_do_work(defer, perf_stats)) {\n+            continue;\n+        }\n+    }\n+\n+    write_idx = defer->write_idx & WORK_RING_MASK;\n+    ring_item = &defer->work_ring[write_idx];\n+\n+    ring_item->work_func = work->work_func;\n+    ring_item->netdev = work->netdev;\n+    ring_item->qid = work->qid;\n+    ring_item->attempts = 0;\n+\n+    defer->write_idx++;\n+\n+    pmd_perf_update_counter(perf_stats, PMD_STAT_WORK_DEFER, 1);\n+}\n+\n+static inline void\n+dp_defer_do_all_work(struct dp_defer *defer, struct pmd_perf_stats *perf_stats)\n+{\n+    while (dp_defer_do_work(defer, perf_stats) != -ENOENT) {\n+        continue;\n+    }\n+}\n+\n static int\n dp_netdev_pmd_flush_output_on_port(struct dp_netdev_pmd_thread *pmd,\n                                    struct tx_port *p)\n@@ -4600,10 +4692,12 @@ dp_netdev_pmd_flush_output_on_port(struct dp_netdev_pmd_thread *pmd,\n     int output_cnt;\n     bool dynamic_txqs;\n     struct cycle_timer timer;\n+    struct netdev *netdev;\n     uint64_t cycles;\n     uint32_t tx_flush_interval;\n+    struct pmd_perf_stats *perf_stats = &pmd->perf_stats;\n \n-    cycle_timer_start(&pmd->perf_stats, &timer);\n+    cycle_timer_start(perf_stats, &timer);\n \n     dynamic_txqs = p->port->dynamic_txqs;\n     if (dynamic_txqs) {\n@@ -4615,7 +4709,21 @@ dp_netdev_pmd_flush_output_on_port(struct dp_netdev_pmd_thread *pmd,\n     output_cnt = dp_packet_batch_size(&p->output_pkts);\n     ovs_assert(output_cnt > 0);\n \n-    netdev_send(p->port->netdev, tx_qid, &p->output_pkts, dynamic_txqs);\n+    netdev = p->port->netdev;\n+    int ret = netdev_send(netdev, tx_qid, &p->output_pkts,\n+                          dynamic_txqs);\n+\n+    if (ret == -EINPROGRESS) {\n+        struct dp_defer_work_item work = {\n+            .work_func = p->cached_work_func,\n+            .netdev = netdev,\n+            .qid = tx_qid,\n+        };\n+\n+        /* Defer the work. */\n+        dp_defer_work(&pmd->defer, perf_stats, &work);\n+    }\n+\n     dp_packet_batch_init(&p->output_pkts);\n \n     /* Update time of the next flush. */\n@@ -4625,12 +4733,15 @@ dp_netdev_pmd_flush_output_on_port(struct dp_netdev_pmd_thread *pmd,\n     ovs_assert(pmd->n_output_batches > 0);\n     pmd->n_output_batches--;\n \n-    pmd_perf_update_counter(&pmd->perf_stats, PMD_STAT_SENT_PKTS, output_cnt);\n-    pmd_perf_update_counter(&pmd->perf_stats, PMD_STAT_SENT_BATCHES, 1);\n+    /* The batch and number of packets are updated as sent here, even though\n+     * some packets might have been dropped, or are in transit asynchronously.\n+     */\n+    pmd_perf_update_counter(perf_stats, PMD_STAT_SENT_PKTS, output_cnt);\n+    pmd_perf_update_counter(perf_stats, PMD_STAT_SENT_BATCHES, 1);\n \n     /* Distribute send cycles evenly among transmitted packets and assign to\n      * their respective rx queues. */\n-    cycles = cycle_timer_stop(&pmd->perf_stats, &timer) / output_cnt;\n+    cycles = cycle_timer_stop(perf_stats, &timer) / output_cnt;\n     for (i = 0; i < output_cnt; i++) {\n         if (p->output_pkts_rxqs[i]) {\n             dp_netdev_rxq_add_cycles(p->output_pkts_rxqs[i],\n@@ -6196,6 +6307,7 @@ reload:\n     ovs_mutex_lock(&pmd->perf_stats.stats_mutex);\n     for (;;) {\n         uint64_t rx_packets = 0, tx_packets = 0;\n+        struct dp_defer *defer = &pmd->defer;\n \n         pmd_perf_start_iteration(s);\n \n@@ -6228,10 +6340,20 @@ reload:\n             tx_packets = dp_netdev_pmd_flush_output_packets(pmd, false);\n         }\n \n+        /* Try to clear the work ring. If a piece of work is still in progress,\n+         * don't attempt to do the remaining work items. They will be postponed\n+         * to the next interation of pmd_thread_main(). */\n+        while (!dp_defer_do_work(defer, s)) {\n+            continue;\n+        }\n+\n         /* Do RCU synchronization at fixed interval.  This ensures that\n          * synchronization would not be delayed long even at high load of\n          * packet processing. */\n         if (pmd->ctx.now > pmd->next_rcu_quiesce) {\n+            /* Do any work outstanding on this PMD thread. */\n+            dp_defer_do_all_work(defer, s);\n+\n             if (!ovsrcu_try_quiesce()) {\n                 pmd->next_rcu_quiesce =\n                     pmd->ctx.now + PMD_RCU_QUIESCE_INTERVAL;\n@@ -6240,6 +6362,8 @@ reload:\n \n         if (lc++ > 1024) {\n             lc = 0;\n+            /* Do any work outstanding on this PMD thread. */\n+            dp_defer_do_all_work(defer, s);\n \n             coverage_try_clear();\n             dp_netdev_pmd_try_optimize(pmd, poll_list, poll_cnt);\n@@ -6262,6 +6386,8 @@ reload:\n \n         atomic_read_explicit(&pmd->reload, &reload, memory_order_acquire);\n         if (OVS_UNLIKELY(reload)) {\n+            /* Do any work outstanding on this PMD thread. */\n+            dp_defer_do_all_work(defer, s);\n             break;\n         }\n \n@@ -6748,6 +6874,8 @@ dp_netdev_configure_pmd(struct dp_netdev_pmd_thread *pmd, struct dp_netdev *dp,\n     pmd_perf_stats_init(&pmd->perf_stats);\n     cmap_insert(&dp->poll_threads, CONST_CAST(struct cmap_node *, &pmd->node),\n                 hash_int(core_id, 0));\n+\n+    dp_defer_init(&pmd->defer);\n }\n \n static void\n@@ -6918,6 +7046,7 @@ dp_netdev_add_port_tx_to_pmd(struct dp_netdev_pmd_thread *pmd,\n     tx->qid = -1;\n     tx->flush_time = 0LL;\n     dp_packet_batch_init(&tx->output_pkts);\n+    tx->cached_work_func = port->netdev->netdev_class->process_async;\n \n     hmap_insert(&pmd->tx_ports, &tx->node, hash_port_no(tx->port->port_no));\n     pmd->need_reload = true;\ndiff --git a/lib/netdev-dpdk.c b/lib/netdev-dpdk.c\nindex 45a96b9be..96d8210e3 100644\n--- a/lib/netdev-dpdk.c\n+++ b/lib/netdev-dpdk.c\n@@ -2585,7 +2585,7 @@ netdev_dpdk_vhost_update_tx_counters(struct netdev_dpdk *dev,\n     }\n }\n \n-static void\n+static int\n __netdev_dpdk_vhost_send(struct netdev *netdev, int qid,\n                          struct dp_packet **pkts, int cnt)\n {\n@@ -2667,6 +2667,8 @@ out:\n     for (i = 0; i < n_packets_to_free; i++) {\n         dp_packet_delete(pkts[i]);\n     }\n+\n+    return 0;\n }\n \n static void\n@@ -2774,7 +2776,7 @@ dpdk_copy_dp_packet_to_mbuf(struct rte_mempool *mp, struct dp_packet *pkt_orig)\n }\n \n /* Tx function. Transmit packets indefinitely */\n-static void\n+static int\n dpdk_do_tx_copy(struct netdev *netdev, int qid, struct dp_packet_batch *batch)\n     OVS_NO_THREAD_SAFETY_ANALYSIS\n {\n@@ -2793,6 +2795,7 @@ dpdk_do_tx_copy(struct netdev *netdev, int qid, struct dp_packet_batch *batch)\n     uint32_t tx_failure = 0;\n     uint32_t mtu_drops = 0;\n     uint32_t qos_drops = 0;\n+    int ret = 0;\n \n     if (dev->type != DPDK_DEV_VHOST) {\n         /* Check if QoS has been configured for this netdev. */\n@@ -2826,7 +2829,7 @@ dpdk_do_tx_copy(struct netdev *netdev, int qid, struct dp_packet_batch *batch)\n \n     if (OVS_LIKELY(txcnt)) {\n         if (dev->type == DPDK_DEV_VHOST) {\n-            __netdev_dpdk_vhost_send(netdev, qid, pkts, txcnt);\n+            ret = __netdev_dpdk_vhost_send(netdev, qid, pkts, txcnt);\n         } else {\n             tx_failure += netdev_dpdk_eth_tx_burst(dev, qid,\n                                                    (struct rte_mbuf **)pkts,\n@@ -2843,6 +2846,8 @@ dpdk_do_tx_copy(struct netdev *netdev, int qid, struct dp_packet_batch *batch)\n         sw_stats->tx_qos_drops += qos_drops;\n         rte_spinlock_unlock(&dev->stats_lock);\n     }\n+\n+    return ret;\n }\n \n static int\n@@ -2851,14 +2856,15 @@ netdev_dpdk_vhost_send(struct netdev *netdev, int qid,\n                        bool concurrent_txq OVS_UNUSED)\n {\n \n+    int ret = 0;\n     if (OVS_UNLIKELY(batch->packets[0]->source != DPBUF_DPDK)) {\n-        dpdk_do_tx_copy(netdev, qid, batch);\n+        ret = dpdk_do_tx_copy(netdev, qid, batch);\n         dp_packet_delete_batch(batch, true);\n     } else {\n-        __netdev_dpdk_vhost_send(netdev, qid, batch->packets,\n-                                 dp_packet_batch_size(batch));\n+        ret = __netdev_dpdk_vhost_send(netdev, qid, batch->packets,\n+                                       dp_packet_batch_size(batch));\n     }\n-    return 0;\n+    return ret;\n }\n \n static inline void\n@@ -5468,6 +5474,7 @@ static const struct netdev_class dpdk_vhost_class = {\n     .construct = netdev_dpdk_vhost_construct,\n     .destruct = netdev_dpdk_vhost_destruct,\n     .send = netdev_dpdk_vhost_send,\n+    .process_async = NULL,\n     .get_carrier = netdev_dpdk_vhost_get_carrier,\n     .get_stats = netdev_dpdk_vhost_get_stats,\n     .get_custom_stats = netdev_dpdk_get_sw_custom_stats,\n@@ -5484,6 +5491,7 @@ static const struct netdev_class dpdk_vhost_client_class = {\n     .destruct = netdev_dpdk_vhost_destruct,\n     .set_config = netdev_dpdk_vhost_client_set_config,\n     .send = netdev_dpdk_vhost_send,\n+    .process_async = NULL,\n     .get_carrier = netdev_dpdk_vhost_get_carrier,\n     .get_stats = netdev_dpdk_vhost_get_stats,\n     .get_custom_stats = netdev_dpdk_get_sw_custom_stats,\ndiff --git a/lib/netdev-provider.h b/lib/netdev-provider.h\nindex b5420947d..a448328e7 100644\n--- a/lib/netdev-provider.h\n+++ b/lib/netdev-provider.h\n@@ -384,7 +384,11 @@ struct netdev_class {\n      * if it would always return EOPNOTSUPP anyhow.  (This will prevent the\n      * network device from being usefully used by the netdev-based \"userspace\n      * datapath\".  It will also prevent the OVS implementation of bonding from\n-     * working properly over 'netdev'.) */\n+     * working properly over 'netdev'.)\n+     *\n+     * May return EINPROGRESS. This indicates that the netdev has more work to\n+     * do, and needs to have process_async called before sending buffers is\n+     * totally completed. */\n     int (*send)(struct netdev *netdev, int qid, struct dp_packet_batch *batch,\n                 bool concurrent_txq);\n \n@@ -402,6 +406,19 @@ struct netdev_class {\n      * implement packet transmission through the 'send' member function. */\n     void (*send_wait)(struct netdev *netdev, int qid);\n \n+    /* Performs asynchronous work required by the netdev to complete sending\n+     * buffers. The work done in the process_async function is netdev specific,\n+     * but could include freeing packets or updating port stats.\n+     *\n+     * If called with force = false, may return EINPROGRESS if the async call\n+     * still hasn't completed, indicating process_async should be called on\n+     * this netdev + qid again in the future.\n+     *\n+     * If called with force = true, can't return EINPROGRESS. Must handle stats\n+     * updates and any freeing of buffers even if they haven't been sent yet.\n+     */\n+    int (*process_async)(struct netdev *netdev, int qid, bool force);\n+\n     /* Sets 'netdev''s Ethernet address to 'mac' */\n     int (*set_etheraddr)(struct netdev *netdev, const struct eth_addr mac);\n \ndiff --git a/lib/netdev.c b/lib/netdev.c\nindex 8305f6c42..e122441cf 100644\n--- a/lib/netdev.c\n+++ b/lib/netdev.c\n@@ -892,7 +892,8 @@ netdev_send(struct netdev *netdev, int qid, struct dp_packet_batch *batch,\n     }\n \n     error = netdev->netdev_class->send(netdev, qid, batch, concurrent_txq);\n-    if (!error) {\n+    /* For async, treat netdev_send as called when -EINPROGRESS is returned. */\n+    if (!error || error == -EINPROGRESS) {\n         COVERAGE_INC(netdev_sent);\n     }\n     return error;\n",
    "prefixes": [
        "ovs-dev",
        "v2",
        "1/3"
    ]
}