Show a patch.

Update a patch.

Update a patch.

GET /api/patches/1528344/
Content-Type: application/json
Vary: Accept

    "id": 1528344,
    "url": "",
    "web_url": "",
    "project": {
        "id": 47,
        "url": "",
        "name": "Open vSwitch",
        "link_name": "openvswitch",
        "list_id": "",
        "list_email": "",
        "web_url": "",
        "scm_url": "",
        "webscm_url": "",
        "list_archive_url": "",
        "list_archive_url_format": "",
        "commit_url_format": ""
    "msgid": "<>",
    "list_archive_url": null,
    "date": "2021-09-15T12:43:16",
    "name": "[ovs-dev,v15,4/7] netdev-offload-tc: Introduce group ID management API",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "31556427ed294f93e61e97d84d089ced85648919",
    "submitter": {
        "id": 80086,
        "url": "",
        "name": "Chris Mi",
        "email": ""
    "delegate": null,
    "mbox": "",
    "series": [
            "id": 262447,
            "url": "",
            "web_url": "",
            "date": "2021-09-15T12:43:12",
            "name": "Add offload support for sFlow",
            "version": 15,
            "mbox": ""
    "comments": "",
    "check": "success",
    "checks": "",
    "tags": {},
    "related": [],
    "headers": {
        "Return-Path": "<>",
        "X-Original-To": [
        "Delivered-To": [
        "Authentication-Results": ";\n spf=pass (sender SPF authorized)\n (client-ip=;;\n; receiver=<UNKNOWN>)",
        "Received": [
            "from ( [])\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 (Postfix) with ESMTPS id 4H8fzJ3Jz0z9sjJ\n\tfor <>; Wed, 15 Sep 2021 22:43:56 +1000 (AEST)",
            "from localhost (localhost [])\n\tby (Postfix) with ESMTP id 3422F82CC7;\n\tWed, 15 Sep 2021 12:43:53 +0000 (UTC)",
            "from ([])\n\tby localhost ( []) (amavisd-new, port 10024)\n\twith ESMTP id DBuHctzuj0ui; Wed, 15 Sep 2021 12:43:44 +0000 (UTC)",
            "from ( [])\n\tby (Postfix) with ESMTPS id 3064F82BE7;\n\tWed, 15 Sep 2021 12:43:41 +0000 (UTC)",
            "from (localhost [])\n\tby (Postfix) with ESMTP id 8BF3DC0039;\n\tWed, 15 Sep 2021 12:43:36 +0000 (UTC)",
            "from ( [])\n by (Postfix) with ESMTP id 4493BC0027\n for <>; Wed, 15 Sep 2021 12:43:32 +0000 (UTC)",
            "from localhost (localhost [])\n by (Postfix) with ESMTP id DF76060B89\n for <>; Wed, 15 Sep 2021 12:43:29 +0000 (UTC)",
            "from ([])\n by localhost ( []) (amavisd-new, port 10024)\n with ESMTP id uD2t2DbC-2ua for <>;\n Wed, 15 Sep 2021 12:43:29 +0000 (UTC)",
            "from ( [])\n by (Postfix) with ESMTP id 5A4D460B90\n for <>; Wed, 15 Sep 2021 12:43:28 +0000 (UTC)",
            "from Internal Mail-Server by MTLPINE1 (envelope-from\n with SMTP; 15 Sep 2021 15:43:21 +0300",
            "from c-141-46-1-010.mtl.labs.mlnx (c-141-46-1-010.mtl.labs.mlnx\n [])\n by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id 18FChLbY010242;\n Wed, 15 Sep 2021 15:43:21 +0300"
        "X-Virus-Scanned": [
            "amavisd-new at",
            "amavisd-new at"
        "X-Greylist": "domain auto-whitelisted by SQLgrey-1.8.0",
        "To": "",
        "Date": "Wed, 15 Sep 2021 15:43:16 +0300",
        "Message-Id": "<>",
        "X-Mailer": "git-send-email 2.27.0",
        "In-Reply-To": "<>",
        "References": "<>",
        "MIME-Version": "1.0",
        "Cc": ",,,\n",
        "Subject": "[ovs-dev] [PATCH v15 4/7] netdev-offload-tc: Introduce group ID\n\tmanagement API",
        "X-BeenThere": "",
        "X-Mailman-Version": "2.1.15",
        "Precedence": "list",
        "List-Id": "<>",
        "List-Unsubscribe": "<>,\n <>",
        "List-Archive": "<>",
        "List-Post": "<>",
        "List-Help": "<>",
        "List-Subscribe": "<>,\n <>",
        "From": "Chris Mi via dev <>",
        "Reply-To": "Chris Mi <>",
        "Content-Type": "text/plain; charset=\"us-ascii\"",
        "Content-Transfer-Encoding": "7bit",
        "Errors-To": "",
        "Sender": "\"dev\" <>"
    "content": "When offloading sample action to TC, userspace creates a unique ID\nto map sFlow action and tunnel info and passes this ID to kernel instead\nof the sFlow info. psample will send this ID and sampled packet to\nuserspace. Using the ID, userspace can recover the sFlow info and send\nsampled packet to the right sFlow monitoring host.\n\nSigned-off-by: Chris Mi <>\nReviewed-by: Eli Britstein <>\n---\n lib/dpif-offload-provider.h |  17 +++\n lib/netdev-offload-tc.c     | 247 +++++++++++++++++++++++++++++++++++-\n lib/netdev-offload.h        |   1 +\n lib/tc.h                    |   1 +\n 4 files changed, 260 insertions(+), 6 deletions(-)",
    "diff": "diff --git a/lib/dpif-offload-provider.h b/lib/dpif-offload-provider.h\nindex 75238c4cb..95c29c0d8 100644\n--- a/lib/dpif-offload-provider.h\n+++ b/lib/dpif-offload-provider.h\n@@ -17,9 +17,26 @@\n #ifndef DPIF_OFFLOAD_PROVIDER_H\n #define DPIF_OFFLOAD_PROVIDER_H\n \n+#include \"netlink-protocol.h\"\n+#include \"openvswitch/packets.h\"\n+#include \"openvswitch/types.h\"\n+\n struct dpif;\n struct dpif_offload_sflow;\n \n+/* When offloading sample action, userspace creates a unique ID to map\n+ * sFlow action and tunnel info and passes this ID to datapath instead\n+ * of the sFlow info. Datapath will send this ID and sampled packet to\n+ * userspace. Using the ID, userspace can recover the sFlow info and send\n+ * sampled packet to the right sFlow monitoring host.\n+ */\n+struct dpif_sflow_attr {\n+    const struct nlattr *action;    /* SFlow action. */\n+    const struct nlattr *userdata;  /* Struct user_action_cookie. */\n+    struct flow_tnl *tunnel;        /* Tunnel info. */\n+    ovs_u128 ufid;                  /* Flow ufid. */\n+};\n+\n /* Datapath interface offload structure, to be defined by each implementation\n  * of a datapath interface.\n  */\ndiff --git a/lib/netdev-offload-tc.c b/lib/netdev-offload-tc.c\nindex 9845e8d3f..5d817b6cf 100644\n--- a/lib/netdev-offload-tc.c\n+++ b/lib/netdev-offload-tc.c\n@@ -40,6 +40,7 @@\n #include \"unaligned.h\"\n #include \"util.h\"\n #include \"dpif-provider.h\"\n+#include \"cmap.h\"\n \n VLOG_DEFINE_THIS_MODULE(netdev_offload_tc);\n \n@@ -62,6 +63,234 @@ struct chain_node {\n     uint32_t chain;\n };\n \n+/* This maps a psample group ID to struct dpif_sflow_attr for sFlow */\n+struct sgid_node {\n+    struct ovs_list exp_node OVS_GUARDED;\n+    struct cmap_node metadata_node;\n+    struct cmap_node id_node;\n+    struct ovs_refcount refcount;\n+    uint32_t hash;\n+    uint32_t id;\n+    const struct dpif_sflow_attr sflow;\n+};\n+\n+static struct ovs_rwlock sgid_rwlock = OVS_RWLOCK_INITIALIZER;\n+static struct cmap sgid_map = CMAP_INITIALIZER;\n+static struct cmap sgid_metadata_map = CMAP_INITIALIZER;\n+static struct ovs_list sgid_expiring OVS_GUARDED_BY(sgid_rwlock)\n+    = OVS_LIST_INITIALIZER(&sgid_expiring);\n+static uint32_t next_sample_group_id OVS_GUARDED_BY(sgid_rwlock) = 1;\n+\n+static void\n+sgid_node_free(struct sgid_node *node)\n+{\n+    free(node->sflow.tunnel);\n+    free(CONST_CAST(void *, node->sflow.action));\n+    free(CONST_CAST(void *, node->sflow.userdata));\n+    free(node);\n+}\n+\n+/* Lockless RCU protected lookup.  If node is needed accross RCU quiescent\n+ * state, caller should copy the contents. */\n+static const struct sgid_node *\n+sgid_find(uint32_t id)\n+{\n+    const struct cmap_node *node = cmap_find(&sgid_map, id);\n+\n+    return node ? CONTAINER_OF(node, const struct sgid_node, id_node) : NULL;\n+}\n+\n+const struct dpif_sflow_attr *\n+dpif_offload_sflow_attr_find(uint32_t id)\n+{\n+    const struct sgid_node *node;\n+\n+    node = sgid_find(id);\n+    if (!node) {\n+        return NULL;\n+    }\n+\n+    return &node->sflow;\n+}\n+\n+static uint32_t\n+dpif_sflow_attr_hash(const struct dpif_sflow_attr *sflow)\n+{\n+    return hash_bytes(&sflow->ufid, sizeof sflow->ufid, 0);\n+}\n+\n+static bool\n+dpif_sflow_attr_equal(const struct dpif_sflow_attr *a,\n+                      const struct dpif_sflow_attr *b)\n+{\n+    if (!ovs_u128_equals(a->ufid, b->ufid)) {\n+        return false;\n+    }\n+    if (!a->action || !b->action) {\n+        return false;\n+    }\n+    if (a->action->nla_len != b->action->nla_len\n+        || memcmp(a->action, b->action, a->action->nla_len)) {\n+        return false;\n+    }\n+    if (!a->tunnel && !b->tunnel) {\n+        return true;\n+    }\n+    if (!a->tunnel || !b->tunnel) {\n+        return false;\n+    }\n+\n+    return !memcmp(a->tunnel, b->tunnel, sizeof *a->tunnel);\n+}\n+\n+/* Lockless RCU protected lookup.  If node is needed accross RCU quiescent\n+ * state, caller should take a reference. */\n+static struct sgid_node *\n+sgid_find_equal(const struct dpif_sflow_attr *target, uint32_t hash)\n+{\n+    struct sgid_node *node;\n+\n+    CMAP_FOR_EACH_WITH_HASH (node, metadata_node, hash, &sgid_metadata_map) {\n+        if (dpif_sflow_attr_equal(&node->sflow, target)) {\n+            return node;\n+        }\n+    }\n+    return NULL;\n+}\n+\n+static struct sgid_node *\n+sgid_ref_equal(const struct dpif_sflow_attr *target, uint32_t hash)\n+{\n+    struct sgid_node *node;\n+\n+    do {\n+        node = sgid_find_equal(target, hash);\n+        /* Try again if the node was released before we get the reference. */\n+    } while (node && !ovs_refcount_try_ref_rcu(&node->refcount));\n+\n+    return node;\n+}\n+\n+static void\n+dpif_sflow_attr_clone(struct dpif_sflow_attr *new,\n+                      const struct dpif_sflow_attr *old)\n+{\n+    new->action = xmemdup(old->action, old->action->nla_len);\n+    new->userdata = xmemdup(old->userdata, old->userdata->nla_len);\n+    new->tunnel = old->tunnel\n+                  ? xmemdup(old->tunnel, sizeof *old->tunnel)\n+                  : NULL;\n+    new->ufid = old->ufid;\n+}\n+\n+/* We use the id as the hash value, which works due to cmap internal rehashing.\n+ * We also only insert nodes with unique IDs, so all possible hash collisions\n+ * remain internal to the cmap. */\n+static struct sgid_node *\n+sgid_find__(uint32_t id)\n+    OVS_REQUIRES(sgid_rwlock)\n+{\n+    struct cmap_node *node = cmap_find_protected(&sgid_map, id);\n+\n+    return node ? CONTAINER_OF(node, struct sgid_node, id_node) : NULL;\n+}\n+\n+/* Allocate a unique group id for the given set of flow metadata. The id\n+ * space is 2^^32, but if looping too many times, we still can't find a\n+ * free one, that means something is wrong, return NULL.\n+ */\n+#define SGID_ALLOC_RETRY_MAX 8192\n+static struct sgid_node *\n+sgid_alloc__(const struct dpif_sflow_attr *sflow, uint32_t hash)\n+{\n+    struct sgid_node *node = xzalloc(sizeof *node);\n+    int i = 0;\n+\n+    node->hash = hash;\n+    ovs_refcount_init(&node->refcount);\n+    dpif_sflow_attr_clone(CONST_CAST(struct dpif_sflow_attr *,\n+                                     &node->sflow), sflow);\n+\n+    ovs_rwlock_wrlock(&sgid_rwlock);\n+    for (;;) {\n+        node->id = next_sample_group_id++;\n+        if (OVS_UNLIKELY(!node->id)) {\n+            next_sample_group_id = 1;\n+            node->id = next_sample_group_id++;\n+        }\n+        /* Find if the id is free. */\n+        if (OVS_LIKELY(!sgid_find__(node->id))) {\n+            break;\n+        }\n+        if (++i == SGID_ALLOC_RETRY_MAX) {\n+            VLOG_ERR(\"%s: Can't find a free group ID after %d retries\",\n+                     __func__, i);\n+            ovs_rwlock_unlock(&sgid_rwlock);\n+            sgid_node_free(node);\n+            return NULL;\n+        }\n+    }\n+    cmap_insert(&sgid_map, &node->id_node, node->id);\n+    cmap_insert(&sgid_metadata_map, &node->metadata_node, node->hash);\n+    ovs_rwlock_unlock(&sgid_rwlock);\n+    return node;\n+}\n+\n+/* Allocate a unique group id for the given set of flow metadata and\n+   optional actions. */\n+static uint32_t\n+sgid_alloc_ctx(const struct dpif_sflow_attr *sflow)\n+{\n+    uint32_t hash = dpif_sflow_attr_hash(sflow);\n+    struct sgid_node *node = sgid_ref_equal(sflow, hash);\n+\n+    if (!node) {\n+        node = sgid_alloc__(sflow, hash);\n+    }\n+\n+    return node ? node->id : 0;\n+}\n+\n+static void\n+sgid_node_unref(const struct sgid_node *node_)\n+    OVS_EXCLUDED(sgid_rwlock)\n+{\n+    struct sgid_node *node = CONST_CAST(struct sgid_node *, node_);\n+\n+    ovs_rwlock_wrlock(&sgid_rwlock);\n+    if (node && ovs_refcount_unref(&node->refcount) == 1) {\n+        cmap_remove(&sgid_metadata_map, &node->metadata_node, node->hash);\n+        cmap_remove(&sgid_map, &node->id_node, node->id);\n+        ovsrcu_postpone(sgid_node_free, node);\n+    }\n+    ovs_rwlock_unlock(&sgid_rwlock);\n+}\n+\n+static void\n+sgid_free(uint32_t id)\n+{\n+    const struct sgid_node *node;\n+\n+    if (!id) {\n+        return;\n+    }\n+\n+    node = sgid_find(id);\n+    if (node) {\n+        sgid_node_unref(node);\n+    } else {\n+        VLOG_ERR(\"%s: Freeing nonexistent group ID: %\"PRIu32, __func__, id);\n+    }\n+}\n+\n+static int\n+del_filter_and_sgid(struct tcf_id *id)\n+{\n+    sgid_free(id->sample_group_id);\n+    id->sample_group_id = 0;\n+    return tc_del_filter(id);\n+}\n+\n static bool\n is_internal_port(const char *type)\n {\n@@ -198,13 +427,13 @@ del_ufid_tc_mapping(const ovs_u128 *ufid)\n     ovs_mutex_unlock(&ufid_lock);\n }\n \n-/* Wrapper function to delete filter and ufid tc mapping */\n+/* Wrapper function to delete filter, sgid and ufid tc mapping */\n static int\n-del_filter_and_ufid_mapping(struct tcf_id *id, const ovs_u128 *ufid)\n+del_filter_sgid_ufid_mapping(struct tcf_id *id, const ovs_u128 *ufid)\n {\n     int err;\n \n-    err = tc_del_filter(id);\n+    err = del_filter_and_sgid(id);\n     if (!err) {\n         del_ufid_tc_mapping(ufid);\n     }\n@@ -426,7 +655,7 @@ netdev_tc_flow_flush(struct netdev *netdev)\n             continue;\n         }\n \n-        err = tc_del_filter(&data->id);\n+        err = del_filter_and_sgid(&data->id);\n         if (!err) {\n             del_ufid_tc_mapping_unlocked(&data->ufid);\n         }\n@@ -1917,6 +2146,11 @@ netdev_tc_flow_put(struct netdev *netdev, struct match *match,\n             action->type = TC_ACT_GOTO;\n             action->chain = 0;  /* 0 is reserved and not used by recirc. */\n             flower.action_count++;\n+        } else if (nl_attr_type(nla) == OVS_ACTION_ATTR_SAMPLE) {\n+            struct dpif_sflow_attr sflow_attr;\n+\n+            memset(&sflow_attr, 0, sizeof sflow_attr);\n+            sgid_alloc_ctx(&sflow_attr);\n         } else {\n             VLOG_DBG_RL(&rl, \"unsupported put action type: %d\",\n                         nl_attr_type(nla));\n@@ -1932,7 +2166,8 @@ netdev_tc_flow_put(struct netdev *netdev, struct match *match,\n     if (get_ufid_tc_mapping(ufid, &id) == 0) {\n         VLOG_DBG_RL(&rl, \"updating old handle: %d prio: %d\",\n                     id.handle, id.prio);\n-        info->tc_modify_flow_deleted = !del_filter_and_ufid_mapping(&id, ufid);\n+        info->tc_modify_flow_deleted = !del_filter_sgid_ufid_mapping(&id,\n+                                                                     ufid);\n     }\n \n     prio = get_prio_for_tc_flower(&flower);\n@@ -2021,7 +2256,7 @@ netdev_tc_flow_del(struct netdev *netdev OVS_UNUSED,\n         }\n     }\n \n-    error = del_filter_and_ufid_mapping(&id, ufid);\n+    error = del_filter_sgid_ufid_mapping(&id, ufid);\n \n     return error;\n }\ndiff --git a/lib/netdev-offload.h b/lib/netdev-offload.h\nindex e7fcedae9..d57d70470 100644\n--- a/lib/netdev-offload.h\n+++ b/lib/netdev-offload.h\n@@ -138,6 +138,7 @@ int netdev_ports_flow_get(const char *dpif_type, struct match *match,\n int netdev_ports_get_n_flows(const char *dpif_type,\n                              odp_port_t port_no, uint64_t *n_flows);\n \n+const struct dpif_sflow_attr *dpif_offload_sflow_attr_find(uint32_t id);\n #ifdef  __cplusplus\n }\n #endif\ndiff --git a/lib/tc.h b/lib/tc.h\nindex a147ca461..2e4084f48 100644\n--- a/lib/tc.h\n+++ b/lib/tc.h\n@@ -276,6 +276,7 @@ struct tcf_id {\n     uint32_t chain;\n     uint16_t prio;\n     uint32_t handle;\n+    uint32_t sample_group_id;\n };\n \n static inline struct tcf_id\n",
    "prefixes": [