From patchwork Sun Sep 29 17:09:58 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tonghao Zhang X-Patchwork-Id: 1169098 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="lwRa9sJl"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46hQnW71Ptz9sPJ for ; Mon, 30 Sep 2019 12:09:19 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729298AbfI3CJC (ORCPT ); Sun, 29 Sep 2019 22:09:02 -0400 Received: from mail-pf1-f175.google.com ([209.85.210.175]:44287 "EHLO mail-pf1-f175.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726360AbfI3CJC (ORCPT ); Sun, 29 Sep 2019 22:09:02 -0400 Received: by mail-pf1-f175.google.com with SMTP id q21so4654629pfn.11 for ; Sun, 29 Sep 2019 19:09:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=t6XjOWdkdSKhiXjRuo+Xi7CGq4ZXtRbOkaCMfbncl8c=; b=lwRa9sJlRjjK5kOiuJFFK5QJiU5gRx4V9xVfiQp9sA/QC/fSdKtw1aqvWKYma/ZECf aAPWDfsvjHLEd0AhtcVq/8JMuTdUZ8JcspxhMdxwWReGQk3GLfDu/RcyqWczTHU9djkc iW2LmDU6ZGWJmXya9A/F4N/ZYCnPDM7pTbB1qjHtHv7iFmmHk6gH1W5mJal0T2kx6fgA NDwVun3czjjHyoOx2Yt8vJecVXIN4Gvy5qnraPI3ch2/Zb80kL8mlhqrXWZ+i7rlYvSj 8tPEVOBkFG+kP5zamwW40WP+bV3HLG3UCWstfhul8BtxDsfOlvXJgLI7W9PT+KGk9VOI N2cA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=t6XjOWdkdSKhiXjRuo+Xi7CGq4ZXtRbOkaCMfbncl8c=; b=NJgrOrDq79nKpW9Ma1xdjZm/zuIIQOKREnSToP3qzWECUKHwX59eY/llDGYUoXMzQe yQ3ngfABj/6lVIu/Ca/Fc+7Z1if+YgCbd6kcD1UglP4arYsO4Yr+ExUZ0Q+L0b82aagZ DRi6YTy6IqeOTG+EvP1Y54kKhw1qYYWTd/BvJ5/Vse1YeCbEatmkJ6zlj6UlHCok+YXO purWQudKLDaLjz9iF0qKbMTnBifJb6U08KagZg5A3+IyaESqB3zvSxAkpc831CkCJRqE P14CXmVocXTwbKVVe13rbcPc+FGPUT5bAP76xSBQ7/Xqb5sYO+4XFmALHysrHae58Sg1 mfLA== X-Gm-Message-State: APjAAAUet9wYzT6l/dhyzNMs3JbX/C85GxxZLgTSc7d58Hj1/Ucm3w+Q 6OFYACzWc0D9i8btV0C+wis= X-Google-Smtp-Source: APXvYqwzHtcKpK2HcHEC6ZNzWiW/tlzWuvqqx9JG+iDc9Qth7xyyA5DMyxZm0gAawndM3Bmk8rwqSw== X-Received: by 2002:a63:186:: with SMTP id 128mr21629075pgb.157.1569809341158; Sun, 29 Sep 2019 19:09:01 -0700 (PDT) Received: from local.opencloud.tech.localdomain ([203.100.54.194]) by smtp.gmail.com with ESMTPSA id d69sm9941635pfd.175.2019.09.29.19.08.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 29 Sep 2019 19:09:00 -0700 (PDT) From: xiangxia.m.yue@gmail.com To: gvrose8192@gmail.com, pshelar@ovn.org Cc: netdev@vger.kernel.org, Tonghao Zhang Subject: [PATCH net-next 1/9] net: openvswitch: add flow-mask cache for performance Date: Mon, 30 Sep 2019 01:09:58 +0800 Message-Id: <1569777006-7435-2-git-send-email-xiangxia.m.yue@gmail.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1569777006-7435-1-git-send-email-xiangxia.m.yue@gmail.com> References: <1569777006-7435-1-git-send-email-xiangxia.m.yue@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Tonghao Zhang The idea of this optimization comes from a patch which is committed in 2014, openvswitch community. The author is Pravin B Shelar. In order to get high performance, I implement it again. Later patches will use it. Pravin B Shelar, says: | On every packet OVS needs to lookup flow-table with every | mask until it finds a match. The packet flow-key is first | masked with mask in the list and then the masked key is | looked up in flow-table. Therefore number of masks can | affect packet processing performance. Link: https://github.com/openvswitch/ovs/commit/5604935e4e1cbc16611d2d97f50b717aa31e8ec5 Signed-off-by: Tonghao Zhang --- net/openvswitch/datapath.c | 3 +- net/openvswitch/flow_table.c | 109 +++++++++++++++++++++++++++++++++++++------ net/openvswitch/flow_table.h | 11 ++++- 3 files changed, 107 insertions(+), 16 deletions(-) diff --git a/net/openvswitch/datapath.c b/net/openvswitch/datapath.c index dde9d76..3d7b1c4 100644 --- a/net/openvswitch/datapath.c +++ b/net/openvswitch/datapath.c @@ -227,7 +227,8 @@ void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key) stats = this_cpu_ptr(dp->stats_percpu); /* Look up flow. */ - flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit); + flow = ovs_flow_tbl_lookup_stats(&dp->table, key, skb_get_hash(skb), + &n_mask_hit); if (unlikely(!flow)) { struct dp_upcall_info upcall; diff --git a/net/openvswitch/flow_table.c b/net/openvswitch/flow_table.c index cf3582c..3d515c0 100644 --- a/net/openvswitch/flow_table.c +++ b/net/openvswitch/flow_table.c @@ -36,6 +36,10 @@ #define TBL_MIN_BUCKETS 1024 #define REHASH_INTERVAL (10 * 60 * HZ) +#define MC_HASH_SHIFT 8 +#define MC_HASH_ENTRIES (1u << MC_HASH_SHIFT) +#define MC_HASH_SEGS ((sizeof(uint32_t) * 8) / MC_HASH_SHIFT) + static struct kmem_cache *flow_cache; struct kmem_cache *flow_stats_cache __read_mostly; @@ -168,10 +172,15 @@ int ovs_flow_tbl_init(struct flow_table *table) { struct table_instance *ti, *ufid_ti; - ti = table_instance_alloc(TBL_MIN_BUCKETS); + table->mask_cache = __alloc_percpu(sizeof(struct mask_cache_entry) * + MC_HASH_ENTRIES, + __alignof__(struct mask_cache_entry)); + if (!table->mask_cache) + return -ENOMEM; + ti = table_instance_alloc(TBL_MIN_BUCKETS); if (!ti) - return -ENOMEM; + goto free_mask_cache; ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS); if (!ufid_ti) @@ -187,6 +196,8 @@ int ovs_flow_tbl_init(struct flow_table *table) free_ti: __table_instance_destroy(ti); +free_mask_cache: + free_percpu(table->mask_cache); return -ENOMEM; } @@ -243,6 +254,7 @@ void ovs_flow_tbl_destroy(struct flow_table *table) struct table_instance *ti = rcu_dereference_raw(table->ti); struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti); + free_percpu(table->mask_cache); table_instance_destroy(ti, ufid_ti, false); } @@ -425,7 +437,8 @@ static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow, static struct sw_flow *masked_flow_lookup(struct table_instance *ti, const struct sw_flow_key *unmasked, - const struct sw_flow_mask *mask) + const struct sw_flow_mask *mask, + u32 *n_mask_hit) { struct sw_flow *flow; struct hlist_head *head; @@ -435,6 +448,8 @@ static struct sw_flow *masked_flow_lookup(struct table_instance *ti, ovs_flow_mask_key(&masked_key, unmasked, false, mask); hash = flow_hash(&masked_key, &mask->range); head = find_bucket(ti, hash); + (*n_mask_hit)++; + hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver]) { if (flow->mask == mask && flow->flow_table.hash == hash && flow_cmp_masked_key(flow, &masked_key, &mask->range)) @@ -443,30 +458,97 @@ static struct sw_flow *masked_flow_lookup(struct table_instance *ti, return NULL; } -struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl, - const struct sw_flow_key *key, - u32 *n_mask_hit) +static struct sw_flow *flow_lookup(struct flow_table *tbl, + struct table_instance *ti, + const struct sw_flow_key *key, + u32 *n_mask_hit) { - struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); struct sw_flow_mask *mask; struct sw_flow *flow; - *n_mask_hit = 0; list_for_each_entry_rcu(mask, &tbl->mask_list, list) { - (*n_mask_hit)++; - flow = masked_flow_lookup(ti, key, mask); + flow = masked_flow_lookup(ti, key, mask, n_mask_hit); if (flow) /* Found */ return flow; } return NULL; } +/* + * mask_cache maps flow to probable mask. This cache is not tightly + * coupled cache, It means updates to mask list can result in inconsistent + * cache entry in mask cache. + * This is per cpu cache and is divided in MC_HASH_SEGS segments. + * In case of a hash collision the entry is hashed in next segment. + * */ +struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl, + const struct sw_flow_key *key, + u32 skb_hash, + u32 *n_mask_hit) +{ + struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); + struct mask_cache_entry *entries, *ce, *del; + struct sw_flow *flow; + u32 hash = skb_hash; + int seg; + + *n_mask_hit = 0; + if (unlikely(!skb_hash)) + return flow_lookup(tbl, ti, key, n_mask_hit); + + del = NULL; + entries = this_cpu_ptr(tbl->mask_cache); + + for (seg = 0; seg < MC_HASH_SEGS; seg++) { + int index; + + index = hash & (MC_HASH_ENTRIES - 1); + ce = &entries[index]; + + if (ce->skb_hash == skb_hash) { + struct sw_flow_mask *mask; + int i; + + i = 0; + list_for_each_entry_rcu(mask, &tbl->mask_list, list) { + if (ce->mask_index == i++) { + flow = masked_flow_lookup(ti, key, mask, + n_mask_hit); + if (flow) /* Found */ + return flow; + + break; + } + } + + del = ce; + break; + } + + if (!del || (del->skb_hash && !ce->skb_hash)) { + del = ce; + } + + hash >>= MC_HASH_SHIFT; + } + + flow = flow_lookup(tbl, ti, key, n_mask_hit); + + if (flow) { + del->skb_hash = skb_hash; + del->mask_index = (*n_mask_hit - 1); + } + + return flow; +} + struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl, const struct sw_flow_key *key) { + struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); u32 __always_unused n_mask_hit; - return ovs_flow_tbl_lookup_stats(tbl, key, &n_mask_hit); + return flow_lookup(tbl, ti, key, &n_mask_hit); } struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl, @@ -475,10 +557,11 @@ struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl, struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); struct sw_flow_mask *mask; struct sw_flow *flow; + u32 __always_unused n_mask_hit; /* Always called under ovs-mutex. */ list_for_each_entry(mask, &tbl->mask_list, list) { - flow = masked_flow_lookup(ti, match->key, mask); + flow = masked_flow_lookup(ti, match->key, mask, &n_mask_hit); if (flow && ovs_identifier_is_key(&flow->id) && ovs_flow_cmp_unmasked_key(flow, match)) return flow; @@ -631,7 +714,7 @@ static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow, return -ENOMEM; mask->key = new->key; mask->range = new->range; - list_add_rcu(&mask->list, &tbl->mask_list); + list_add_tail_rcu(&mask->list, &tbl->mask_list); } else { BUG_ON(!mask->ref_count); mask->ref_count++; diff --git a/net/openvswitch/flow_table.h b/net/openvswitch/flow_table.h index bc52045..04b6b1c 100644 --- a/net/openvswitch/flow_table.h +++ b/net/openvswitch/flow_table.h @@ -22,6 +22,11 @@ #include "flow.h" +struct mask_cache_entry { + u32 skb_hash; + u32 mask_index; +}; + struct table_instance { struct hlist_head *buckets; unsigned int n_buckets; @@ -34,6 +39,7 @@ struct table_instance { struct flow_table { struct table_instance __rcu *ti; struct table_instance __rcu *ufid_ti; + struct mask_cache_entry __percpu *mask_cache; struct list_head mask_list; unsigned long last_rehash; unsigned int count; @@ -60,8 +66,9 @@ int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow, struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *table, u32 *bucket, u32 *idx); struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *, - const struct sw_flow_key *, - u32 *n_mask_hit); + const struct sw_flow_key *, + u32 skb_hash, + u32 *n_mask_hit); struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *, const struct sw_flow_key *); struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl, From patchwork Sun Sep 29 17:09:59 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tonghao Zhang X-Patchwork-Id: 1169100 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="JniwiYgY"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46hQnX6CVgz9sPL for ; Mon, 30 Sep 2019 12:09:20 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729328AbfI3CJE (ORCPT ); Sun, 29 Sep 2019 22:09:04 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:35776 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726360AbfI3CJE (ORCPT ); Sun, 29 Sep 2019 22:09:04 -0400 Received: by mail-pf1-f196.google.com with SMTP id 205so4682177pfw.2 for ; Sun, 29 Sep 2019 19:09:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=FOXGs3dGs/tTHBHBhZJVHOJPRZHVi5a8OXwYOCUK8lk=; b=JniwiYgYK7XfQLwdfKWSWDhpvdAw5DPJh23zEsWXE5q7ajMR7aWqkgrMMBa9emrNUu 44E8aSx44MyR1xEtyQYlf+3qTH1ETd4wPPcp2VYMIZdkIsH+gbXOnXzSpL/oExgeFsvY Fr2qcvIkSqpRRH04Xg9apL/Lcr3yVrbAcsfQkrVYbv2oxIgHQtrMPmD8moyQA6h5A2wG NWFa+ulT5wSr4hcp35osoGeKH7QceFrVvZ1Z4pdFQPOwl68jQPLkk0TLA5JzoUjGZXEa aCYffWIR074cpQJOVjfkLM4u7m3yRaenGCEeFlWb1uztbvHucl5cHjT8IX4bhs5ODsvK RaGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=FOXGs3dGs/tTHBHBhZJVHOJPRZHVi5a8OXwYOCUK8lk=; b=VQ50ZV/rJRDr4s9nzuo7PU0TjTyJNZrC7P01iiWkkzrPzt1mBHNJXEQIW1qH888Bhy D//yj9s/8OM6qgAdKKSatFJ1Oq7Ki+JxBd8YrOitViV0CuAQY+fG9HyjeR9sxhr0w3TV +cK0XzAuETI8KAMhA+Ln2Z3WYMcaZijnAQSGLCbij40lqcZ5QlWgS+1kibbB1YZb2pL0 2PzYwFoH6UHDptk2GtexD+P6jt+tMgVISnjYSRCcVdRykdiMBuWcyHJYPKX6tZpDP3WE D4w7VfDJjJF8TUFxNy4ubRGg54kPl+mPMHFqfM65m1oLFcEC7cwj+UJ4c/7n4B9aH7cw BnRg== X-Gm-Message-State: APjAAAVMLzkrH1lRf6QUmn/T+ht1OsM+tcm+xmPdkVBO1Tti4QIwOd+i AaXwPU0sEfn96GvHE7WNq5M= X-Google-Smtp-Source: APXvYqxLLik60VtXTaUsKesm4RXwE3TQhs3J/b9EFtfE1bN2mtRBAsbFen05mf9m3fSenld89HEsoA== X-Received: by 2002:a65:66cd:: with SMTP id c13mr22280281pgw.412.1569809343269; Sun, 29 Sep 2019 19:09:03 -0700 (PDT) Received: from local.opencloud.tech.localdomain ([203.100.54.194]) by smtp.gmail.com with ESMTPSA id d69sm9941635pfd.175.2019.09.29.19.09.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 29 Sep 2019 19:09:02 -0700 (PDT) From: xiangxia.m.yue@gmail.com To: gvrose8192@gmail.com, pshelar@ovn.org Cc: netdev@vger.kernel.org, Tonghao Zhang Subject: [PATCH net-next 2/9] net: openvswitch: convert mask list in mask array Date: Mon, 30 Sep 2019 01:09:59 +0800 Message-Id: <1569777006-7435-3-git-send-email-xiangxia.m.yue@gmail.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1569777006-7435-1-git-send-email-xiangxia.m.yue@gmail.com> References: <1569777006-7435-1-git-send-email-xiangxia.m.yue@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Tonghao Zhang Port the codes to linux upstream and with little changes. Pravin B Shelar, says: | mask caches index of mask in mask_list. On packet recv OVS | need to traverse mask-list to get cached mask. Therefore array | is better for retrieving cached mask. This also allows better | cache replacement algorithm by directly checking mask's existence. Link: https://github.com/openvswitch/ovs/commit/d49fc3ff53c65e4eca9cabd52ac63396746a7ef5 Signed-off-by: Tonghao Zhang --- net/openvswitch/flow.h | 1 - net/openvswitch/flow_table.c | 218 +++++++++++++++++++++++++++++++++---------- net/openvswitch/flow_table.h | 8 +- 3 files changed, 175 insertions(+), 52 deletions(-) diff --git a/net/openvswitch/flow.h b/net/openvswitch/flow.h index b830d5f..8080518 100644 --- a/net/openvswitch/flow.h +++ b/net/openvswitch/flow.h @@ -166,7 +166,6 @@ struct sw_flow_key_range { struct sw_flow_mask { int ref_count; struct rcu_head rcu; - struct list_head list; struct sw_flow_key_range range; struct sw_flow_key key; }; diff --git a/net/openvswitch/flow_table.c b/net/openvswitch/flow_table.c index 3d515c0..99954fa 100644 --- a/net/openvswitch/flow_table.c +++ b/net/openvswitch/flow_table.c @@ -34,6 +34,7 @@ #include #define TBL_MIN_BUCKETS 1024 +#define MASK_ARRAY_SIZE_MIN 16 #define REHASH_INTERVAL (10 * 60 * HZ) #define MC_HASH_SHIFT 8 @@ -168,9 +169,59 @@ static struct table_instance *table_instance_alloc(int new_size) return ti; } +static void mask_array_rcu_cb(struct rcu_head *rcu) +{ + struct mask_array *ma = container_of(rcu, struct mask_array, rcu); + + kfree(ma); +} + +static struct mask_array *tbl_mask_array_alloc(int size) +{ + struct mask_array *new; + + size = max(MASK_ARRAY_SIZE_MIN, size); + new = kzalloc(sizeof(struct mask_array) + + sizeof(struct sw_flow_mask *) * size, GFP_KERNEL); + if (!new) + return NULL; + + new->count = 0; + new->max = size; + + return new; +} + +static int tbl_mask_array_realloc(struct flow_table *tbl, int size) +{ + struct mask_array *old; + struct mask_array *new; + + new = tbl_mask_array_alloc(size); + if (!new) + return -ENOMEM; + + old = ovsl_dereference(tbl->mask_array); + if (old) { + int i; + + for (i = 0; i < old->max; i++) { + if (ovsl_dereference(old->masks[i])) + new->masks[new->count++] = old->masks[i]; + } + } + rcu_assign_pointer(tbl->mask_array, new); + + if (old) + call_rcu(&old->rcu, mask_array_rcu_cb); + + return 0; +} + int ovs_flow_tbl_init(struct flow_table *table) { struct table_instance *ti, *ufid_ti; + struct mask_array *ma; table->mask_cache = __alloc_percpu(sizeof(struct mask_cache_entry) * MC_HASH_ENTRIES, @@ -178,9 +229,13 @@ int ovs_flow_tbl_init(struct flow_table *table) if (!table->mask_cache) return -ENOMEM; + ma = tbl_mask_array_alloc(MASK_ARRAY_SIZE_MIN); + if (!ma) + goto free_mask_cache; + ti = table_instance_alloc(TBL_MIN_BUCKETS); if (!ti) - goto free_mask_cache; + goto free_mask_array; ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS); if (!ufid_ti) @@ -188,7 +243,7 @@ int ovs_flow_tbl_init(struct flow_table *table) rcu_assign_pointer(table->ti, ti); rcu_assign_pointer(table->ufid_ti, ufid_ti); - INIT_LIST_HEAD(&table->mask_list); + rcu_assign_pointer(table->mask_array, ma); table->last_rehash = jiffies; table->count = 0; table->ufid_count = 0; @@ -196,6 +251,8 @@ int ovs_flow_tbl_init(struct flow_table *table) free_ti: __table_instance_destroy(ti); +free_mask_array: + kfree(ma); free_mask_cache: free_percpu(table->mask_cache); return -ENOMEM; @@ -255,6 +312,7 @@ void ovs_flow_tbl_destroy(struct flow_table *table) struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti); free_percpu(table->mask_cache); + kfree(rcu_dereference_raw(table->mask_array)); table_instance_destroy(ti, ufid_ti, false); } @@ -460,17 +518,27 @@ static struct sw_flow *masked_flow_lookup(struct table_instance *ti, static struct sw_flow *flow_lookup(struct flow_table *tbl, struct table_instance *ti, + struct mask_array *ma, const struct sw_flow_key *key, - u32 *n_mask_hit) + u32 *n_mask_hit, + u32 *index) { - struct sw_flow_mask *mask; struct sw_flow *flow; + int i; - list_for_each_entry_rcu(mask, &tbl->mask_list, list) { - flow = masked_flow_lookup(ti, key, mask, n_mask_hit); - if (flow) /* Found */ - return flow; + for (i = 0; i < ma->max; i++) { + struct sw_flow_mask *mask; + + mask = rcu_dereference_ovsl(ma->masks[i]); + if (mask) { + flow = masked_flow_lookup(ti, key, mask, n_mask_hit); + if (flow) { /* Found */ + *index = i; + return flow; + } + } } + return NULL; } @@ -486,6 +554,7 @@ struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl, u32 skb_hash, u32 *n_mask_hit) { + struct mask_array *ma = rcu_dereference_ovsl(tbl->mask_array); struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); struct mask_cache_entry *entries, *ce, *del; struct sw_flow *flow; @@ -493,8 +562,11 @@ struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl, int seg; *n_mask_hit = 0; - if (unlikely(!skb_hash)) - return flow_lookup(tbl, ti, key, n_mask_hit); + if (unlikely(!skb_hash)) { + u32 __always_unused mask_index; + + return flow_lookup(tbl, ti, ma, key, n_mask_hit, &mask_index); + } del = NULL; entries = this_cpu_ptr(tbl->mask_cache); @@ -507,37 +579,33 @@ struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl, if (ce->skb_hash == skb_hash) { struct sw_flow_mask *mask; - int i; - - i = 0; - list_for_each_entry_rcu(mask, &tbl->mask_list, list) { - if (ce->mask_index == i++) { - flow = masked_flow_lookup(ti, key, mask, - n_mask_hit); - if (flow) /* Found */ - return flow; - - break; - } + struct sw_flow *flow; + + mask = rcu_dereference_ovsl(ma->masks[ce->mask_index]); + if (mask) { + flow = masked_flow_lookup(ti, key, mask, + n_mask_hit); + if (flow) /* Found */ + return flow; } del = ce; break; } - if (!del || (del->skb_hash && !ce->skb_hash)) { + if (!del || (del->skb_hash && !ce->skb_hash) || + (rcu_dereference_ovsl(ma->masks[del->mask_index]) && + !rcu_dereference_ovsl(ma->masks[ce->mask_index]))) { del = ce; } hash >>= MC_HASH_SHIFT; } - flow = flow_lookup(tbl, ti, key, n_mask_hit); + flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, &del->mask_index); - if (flow) { + if (flow) del->skb_hash = skb_hash; - del->mask_index = (*n_mask_hit - 1); - } return flow; } @@ -546,26 +614,38 @@ struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl, const struct sw_flow_key *key) { struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); + struct mask_array *ma = rcu_dereference_ovsl(tbl->mask_array); + u32 __always_unused n_mask_hit; + u32 __always_unused index; - return flow_lookup(tbl, ti, key, &n_mask_hit); + return flow_lookup(tbl, ti, ma, key, &n_mask_hit, &index); } struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl, const struct sw_flow_match *match) { - struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); - struct sw_flow_mask *mask; - struct sw_flow *flow; - u32 __always_unused n_mask_hit; + struct mask_array *ma = ovsl_dereference(tbl->mask_array); + int i; /* Always called under ovs-mutex. */ - list_for_each_entry(mask, &tbl->mask_list, list) { + for (i = 0; i < ma->max; i++) { + struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); + u32 __always_unused n_mask_hit; + struct sw_flow_mask *mask; + struct sw_flow *flow; + + mask = ovsl_dereference(ma->masks[i]); + if (!mask) + continue; + flow = masked_flow_lookup(ti, match->key, mask, &n_mask_hit); if (flow && ovs_identifier_is_key(&flow->id) && - ovs_flow_cmp_unmasked_key(flow, match)) + ovs_flow_cmp_unmasked_key(flow, match)) { return flow; + } } + return NULL; } @@ -611,13 +691,8 @@ struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl, int ovs_flow_tbl_num_masks(const struct flow_table *table) { - struct sw_flow_mask *mask; - int num = 0; - - list_for_each_entry(mask, &table->mask_list, list) - num++; - - return num; + struct mask_array *ma = rcu_dereference_ovsl(table->mask_array); + return ma->count; } static struct table_instance *table_instance_expand(struct table_instance *ti, @@ -638,8 +713,19 @@ static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask) mask->ref_count--; if (!mask->ref_count) { - list_del_rcu(&mask->list); - kfree_rcu(mask, rcu); + struct mask_array *ma; + int i; + + ma = ovsl_dereference(tbl->mask_array); + for (i = 0; i < ma->max; i++) { + if (mask == ovsl_dereference(ma->masks[i])) { + RCU_INIT_POINTER(ma->masks[i], NULL); + ma->count--; + kfree_rcu(mask, rcu); + return; + } + } + BUG(); } } } @@ -689,13 +775,16 @@ static bool mask_equal(const struct sw_flow_mask *a, static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl, const struct sw_flow_mask *mask) { - struct list_head *ml; + struct mask_array *ma; + int i; - list_for_each(ml, &tbl->mask_list) { - struct sw_flow_mask *m; - m = container_of(ml, struct sw_flow_mask, list); - if (mask_equal(mask, m)) - return m; + ma = ovsl_dereference(tbl->mask_array); + for (i = 0; i < ma->max; i++) { + struct sw_flow_mask *t; + t = ovsl_dereference(ma->masks[i]); + + if (t && mask_equal(mask, t)) + return t; } return NULL; @@ -706,15 +795,44 @@ static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow, const struct sw_flow_mask *new) { struct sw_flow_mask *mask; + mask = flow_mask_find(tbl, new); if (!mask) { + struct mask_array *ma; + int i; + /* Allocate a new mask if none exsits. */ mask = mask_alloc(); if (!mask) return -ENOMEM; mask->key = new->key; mask->range = new->range; - list_add_tail_rcu(&mask->list, &tbl->mask_list); + + /* Add mask to mask-list. */ + ma = ovsl_dereference(tbl->mask_array); + if (ma->count >= ma->max) { + int err; + + err = tbl_mask_array_realloc(tbl, ma->max + + MASK_ARRAY_SIZE_MIN); + if (err) { + kfree(mask); + return err; + } + + ma = ovsl_dereference(tbl->mask_array); + } + + for (i = 0; i < ma->max; i++) { + const struct sw_flow_mask *t; + + t = ovsl_dereference(ma->masks[i]); + if (!t) { + rcu_assign_pointer(ma->masks[i], mask); + ma->count++; + break; + } + } } else { BUG_ON(!mask->ref_count); mask->ref_count++; diff --git a/net/openvswitch/flow_table.h b/net/openvswitch/flow_table.h index 04b6b1c..8a5cea6 100644 --- a/net/openvswitch/flow_table.h +++ b/net/openvswitch/flow_table.h @@ -27,6 +27,12 @@ struct mask_cache_entry { u32 mask_index; }; +struct mask_array { + struct rcu_head rcu; + int count, max; + struct sw_flow_mask __rcu *masks[]; +}; + struct table_instance { struct hlist_head *buckets; unsigned int n_buckets; @@ -40,7 +46,7 @@ struct flow_table { struct table_instance __rcu *ti; struct table_instance __rcu *ufid_ti; struct mask_cache_entry __percpu *mask_cache; - struct list_head mask_list; + struct mask_array __rcu *mask_array; unsigned long last_rehash; unsigned int count; unsigned int ufid_count; From patchwork Sun Sep 29 17:10:00 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tonghao Zhang X-Patchwork-Id: 1169101 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="FThiTkpz"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46hQnY5FFWz9s7T for ; Mon, 30 Sep 2019 12:09:21 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729368AbfI3CJG (ORCPT ); Sun, 29 Sep 2019 22:09:06 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:35777 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726360AbfI3CJG (ORCPT ); Sun, 29 Sep 2019 22:09:06 -0400 Received: by mail-pf1-f194.google.com with SMTP id 205so4682224pfw.2 for ; Sun, 29 Sep 2019 19:09:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=LRRofKZdw88izW3jLkOBrYfzBBphlunfCEFOpEUKOZc=; b=FThiTkpz1jdeTcngb9zc2rR37hNxd3ZF6kT3nnLWr911t5r7HLCduSFUPCQ/meEpJI U3WItU/UKbNY4syojIbBzxNV1cnwWd8WoaAnoJrZ7YcyMK4WmeUNtTfe7Hy/0HjPLpfE 48l8Vj2LayjibjDThA1hDL8K+VAnjkv6YTGwZ8hSZFcYSksIswhlf82miXYRQENc9f7a kQ2r1Cd61DOvXVoTSowFCTwR6/xrRbliwwmwt9KCjalMQzYy5rLn3lgFuAj3oilelAei qIisuY/M7fOtUoR6shgi1Ga10cLelJ4aizgctlmRcP09mZwzVDuVHZCvqaGUwuh/PTTR 8c+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=LRRofKZdw88izW3jLkOBrYfzBBphlunfCEFOpEUKOZc=; b=kPSJoqEo7QcC4rAT2WbmYpXcIvI31Odc1fTXhcaOsDGJ+nslnryNAhQpxhcQoIUp+l x/gZXXTr1sRoDj7Gc23eK71kxtmrS0/DytR7Sw3hB1n7ZYJKgCokw1t6IQxWIX6rYvMx p5SrmKnPD76YUYysTwJBiqyvvnAMcavYJn0/CNKEx9lN9tgDG2ePDJE3qlE0luqO+XOU r98GDoEp1YeTqWY+cTBpA1pol1ca/BRO2l1VXOHzISycSG4bKgw3bFaJFqjOZAFpLW+a Ijj0mlXA7Gl9SZ6FnGqb4kqDCkcJt6qFyZyeJSV/WLa6SIbzrqflHirIpUMlyW2YdkB3 bULA== X-Gm-Message-State: APjAAAXpnK+pZmBctMIlwjSXrfxx8DwJRwHYB5gOSDqdCMDswUwvHfCn hnW/AKLEKlOaaEoztssmO66VSghU X-Google-Smtp-Source: APXvYqzZCDauxWDRk3rv2PGH6+UUdT2oX6N7Mn/Ry/0hTp0RSF8sqP/gQ+1uRMUiQzrq7/7RTWzwTw== X-Received: by 2002:a17:90b:8d1:: with SMTP id ds17mr23644381pjb.105.1569809345355; Sun, 29 Sep 2019 19:09:05 -0700 (PDT) Received: from local.opencloud.tech.localdomain ([203.100.54.194]) by smtp.gmail.com with ESMTPSA id d69sm9941635pfd.175.2019.09.29.19.09.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 29 Sep 2019 19:09:04 -0700 (PDT) From: xiangxia.m.yue@gmail.com To: gvrose8192@gmail.com, pshelar@ovn.org Cc: netdev@vger.kernel.org, Tonghao Zhang Subject: [PATCH net-next 3/9] net: openvswitch: shrink the mask array if necessary Date: Mon, 30 Sep 2019 01:10:00 +0800 Message-Id: <1569777006-7435-4-git-send-email-xiangxia.m.yue@gmail.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1569777006-7435-1-git-send-email-xiangxia.m.yue@gmail.com> References: <1569777006-7435-1-git-send-email-xiangxia.m.yue@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Tonghao Zhang When creating and inserting flow-mask, if there is no available flow-mask, we realloc the mask array. When removing flow-mask, if necessary, we shrink mask array. Signed-off-by: Tonghao Zhang --- net/openvswitch/flow_table.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/net/openvswitch/flow_table.c b/net/openvswitch/flow_table.c index 99954fa..9c72aab 100644 --- a/net/openvswitch/flow_table.c +++ b/net/openvswitch/flow_table.c @@ -701,6 +701,23 @@ static struct table_instance *table_instance_expand(struct table_instance *ti, return table_instance_rehash(ti, ti->n_buckets * 2, ufid); } +static void tbl_mask_array_delete_mask(struct mask_array *ma, + struct sw_flow_mask *mask) +{ + int i; + + /* Remove the deleted mask pointers from the array */ + for (i = 0; i < ma->max; i++) { + if (mask == ovsl_dereference(ma->masks[i])) { + RCU_INIT_POINTER(ma->masks[i], NULL); + ma->count--; + kfree_rcu(mask, rcu); + return; + } + } + BUG(); +} + /* Remove 'mask' from the mask list, if it is not needed any more. */ static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask) { @@ -714,18 +731,14 @@ static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask) if (!mask->ref_count) { struct mask_array *ma; - int i; ma = ovsl_dereference(tbl->mask_array); - for (i = 0; i < ma->max; i++) { - if (mask == ovsl_dereference(ma->masks[i])) { - RCU_INIT_POINTER(ma->masks[i], NULL); - ma->count--; - kfree_rcu(mask, rcu); - return; - } - } - BUG(); + tbl_mask_array_delete_mask(ma, mask); + + /* Shrink the mask array if necessary. */ + if (ma->max >= (MASK_ARRAY_SIZE_MIN * 2) && + ma->count <= (ma->max / 3)) + tbl_mask_array_realloc(tbl, ma->max / 2); } } } From patchwork Sun Sep 29 17:10:01 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tonghao Zhang X-Patchwork-Id: 1169102 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="G0r8JUgg"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46hQnZ38Gnz9sPJ for ; Mon, 30 Sep 2019 12:09:22 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729384AbfI3CJJ (ORCPT ); Sun, 29 Sep 2019 22:09:09 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:35778 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729329AbfI3CJI (ORCPT ); Sun, 29 Sep 2019 22:09:08 -0400 Received: by mail-pf1-f194.google.com with SMTP id 205so4682252pfw.2 for ; Sun, 29 Sep 2019 19:09:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=MfKiaD7BLDCj9sb393V4hNHiOobedPu8xnBx7OnK1E0=; b=G0r8JUgg586iEMATkDQ+j1c9J/emmG4kV1zt/OhoATyW6h8FWVgFyDBQz1PA5sOs56 ezdW4TpT/AVH6n7ke8oqriVEbzMBpZhkxR6FZN3WMWQCnAyiiAweKu9TYz1WH1wxKToZ Sz4q6fdiWkX2DGasAoj8RsYNFbxknTJtwh3Q3egkM68nvvaMrSfi/FjLdbxakollXte9 AeEOo4w+EBk1RNRdRTpw5ZmhPzlW8V4RR6W2ZZdkve/gHeHzcHrhIWU8w95idgO/smKp 8zj/LJHmUpnMGj/cnby190cGhYt0S2lIYUU+hxdBZgg8ROaS1ewr+9yktYv8d7d4/HnX u6fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=MfKiaD7BLDCj9sb393V4hNHiOobedPu8xnBx7OnK1E0=; b=qF0U++sVT+mtqJ4NQTNnevXwBpxrulz3FkFnYRTmW1sMBXao6ZXCZLEajd6sRfJkRX OHA6RkHugE2nZyjsRo5nGQilZtc3PH3BvqbK1vT9nM3V+z89HGUEQqJA4lsqiOI+yIoK ytOcrlnl7z706S1Y3VY4TIhMKfWKxayOzJ+645pBNBLwCOmZk4/Tmx5pnuNSGWysXC5A ckopBTmRMA4/suzt8Qqnhj/9zSm0xHIUtJyVWm+3xVV87Xe/At8Q45n3xHQv0t/qTDnK ydbqF+pUBr66HekoFKY4+NOKSnc6CJ3mkmmsxHHJGLw/XnjGNIsO43xZVGBFaeKxfFBK 8+Kg== X-Gm-Message-State: APjAAAWrieGmzobtETPBFAcBrIwt+wf6U2IMx1I746JErGz6HBuf6J5I essZLzxgRy48B8Kwf6AfTNk= X-Google-Smtp-Source: APXvYqxerPlKvn7LzUcK8DmsolEscO37X2QOUlWxthYrg1nyHl5fnBXh78+U4CTtgqheNkPpbFypPw== X-Received: by 2002:aa7:95a7:: with SMTP id a7mr18906916pfk.158.1569809347249; Sun, 29 Sep 2019 19:09:07 -0700 (PDT) Received: from local.opencloud.tech.localdomain ([203.100.54.194]) by smtp.gmail.com with ESMTPSA id d69sm9941635pfd.175.2019.09.29.19.09.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 29 Sep 2019 19:09:06 -0700 (PDT) From: xiangxia.m.yue@gmail.com To: gvrose8192@gmail.com, pshelar@ovn.org Cc: netdev@vger.kernel.org, Tonghao Zhang Subject: [PATCH net-next 4/9] net: openvswitch: optimize flow mask cache hash collision Date: Mon, 30 Sep 2019 01:10:01 +0800 Message-Id: <1569777006-7435-5-git-send-email-xiangxia.m.yue@gmail.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1569777006-7435-1-git-send-email-xiangxia.m.yue@gmail.com> References: <1569777006-7435-1-git-send-email-xiangxia.m.yue@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Tonghao Zhang Port the codes to linux upstream and with little changes. Pravin B Shelar, says: | In case hash collision on mask cache, OVS does extra flow | lookup. Following patch avoid it. Link: https://github.com/openvswitch/ovs/commit/0e6efbe2712da03522532dc5e84806a96f6a0dd1 Signed-off-by: Tonghao Zhang --- net/openvswitch/flow_table.c | 95 ++++++++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 42 deletions(-) diff --git a/net/openvswitch/flow_table.c b/net/openvswitch/flow_table.c index 9c72aab..e59fac5 100644 --- a/net/openvswitch/flow_table.c +++ b/net/openvswitch/flow_table.c @@ -516,6 +516,9 @@ static struct sw_flow *masked_flow_lookup(struct table_instance *ti, return NULL; } +/* Flow lookup does full lookup on flow table. It starts with + * mask from index passed in *index. + */ static struct sw_flow *flow_lookup(struct flow_table *tbl, struct table_instance *ti, struct mask_array *ma, @@ -524,18 +527,31 @@ static struct sw_flow *flow_lookup(struct flow_table *tbl, u32 *index) { struct sw_flow *flow; + struct sw_flow_mask *mask; int i; - for (i = 0; i < ma->max; i++) { - struct sw_flow_mask *mask; - - mask = rcu_dereference_ovsl(ma->masks[i]); + if (*index < ma->max) { + mask = rcu_dereference_ovsl(ma->masks[*index]); if (mask) { flow = masked_flow_lookup(ti, key, mask, n_mask_hit); - if (flow) { /* Found */ - *index = i; + if (flow) return flow; - } + } + } + + for (i = 0; i < ma->max; i++) { + + if (i == *index) + continue; + + mask = rcu_dereference_ovsl(ma->masks[i]); + if (!mask) + continue; + + flow = masked_flow_lookup(ti, key, mask, n_mask_hit); + if (flow) { /* Found */ + *index = i; + return flow; } } @@ -554,58 +570,54 @@ struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl, u32 skb_hash, u32 *n_mask_hit) { - struct mask_array *ma = rcu_dereference_ovsl(tbl->mask_array); - struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); - struct mask_cache_entry *entries, *ce, *del; + struct mask_array *ma = rcu_dereference(tbl->mask_array); + struct table_instance *ti = rcu_dereference(tbl->ti); + struct mask_cache_entry *entries, *ce; struct sw_flow *flow; - u32 hash = skb_hash; + u32 hash; int seg; *n_mask_hit = 0; if (unlikely(!skb_hash)) { - u32 __always_unused mask_index; + u32 mask_index = 0; return flow_lookup(tbl, ti, ma, key, n_mask_hit, &mask_index); } - del = NULL; + /* Pre and post recirulation flows usually have the same skb_hash + * value. To avoid hash collisions, rehash the 'skb_hash' with + * 'recirc_id'. */ + if (key->recirc_id) + skb_hash = jhash_1word(skb_hash, key->recirc_id); + + ce = NULL; + hash = skb_hash; entries = this_cpu_ptr(tbl->mask_cache); + /* Find the cache entry 'ce' to operate on. */ for (seg = 0; seg < MC_HASH_SEGS; seg++) { - int index; - - index = hash & (MC_HASH_ENTRIES - 1); - ce = &entries[index]; - - if (ce->skb_hash == skb_hash) { - struct sw_flow_mask *mask; - struct sw_flow *flow; - - mask = rcu_dereference_ovsl(ma->masks[ce->mask_index]); - if (mask) { - flow = masked_flow_lookup(ti, key, mask, - n_mask_hit); - if (flow) /* Found */ - return flow; - } - - del = ce; - break; + int index = hash & (MC_HASH_ENTRIES - 1); + struct mask_cache_entry *e; + + e = &entries[index]; + if (e->skb_hash == skb_hash) { + flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, + &e->mask_index); + if (!flow) + e->skb_hash = 0; + return flow; } - if (!del || (del->skb_hash && !ce->skb_hash) || - (rcu_dereference_ovsl(ma->masks[del->mask_index]) && - !rcu_dereference_ovsl(ma->masks[ce->mask_index]))) { - del = ce; - } + if (!ce || e->skb_hash < ce->skb_hash) + ce = e; /* A better replacement cache candidate. */ hash >>= MC_HASH_SHIFT; } - flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, &del->mask_index); - + /* Cache miss, do full lookup. */ + flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, &ce->mask_index); if (flow) - del->skb_hash = skb_hash; + ce->skb_hash = skb_hash; return flow; } @@ -615,9 +627,8 @@ struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl, { struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); struct mask_array *ma = rcu_dereference_ovsl(tbl->mask_array); - u32 __always_unused n_mask_hit; - u32 __always_unused index; + u32 index = 0; return flow_lookup(tbl, ti, ma, key, &n_mask_hit, &index); } From patchwork Sun Sep 29 17:10:02 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tonghao Zhang X-Patchwork-Id: 1169104 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="vGGv5H8w"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46hQnb1q0cz9sPL for ; Mon, 30 Sep 2019 12:09:23 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729390AbfI3CJK (ORCPT ); Sun, 29 Sep 2019 22:09:10 -0400 Received: from mail-pl1-f195.google.com ([209.85.214.195]:34009 "EHLO mail-pl1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729329AbfI3CJJ (ORCPT ); Sun, 29 Sep 2019 22:09:09 -0400 Received: by mail-pl1-f195.google.com with SMTP id k7so3282630pll.1 for ; Sun, 29 Sep 2019 19:09:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=smH45prXaWa99xV+deagFBljpnUmJJvQg3D5ytEuJBw=; b=vGGv5H8wpc7GElG0QGj0ddKH2iIbG/RlZix3qUgJBp4UMXo63z9Pwpr6+J2ipCBe/o /hQ55FSENg3qdTyFd+8slGnHRZC70tnji1qk+rcNkpbTVZ5JJn3bOsOkO1bau4leNx4e eLK3q5EYKJUSa9CtRL8RB8fvAVxpG0HxAMamdpXhG2JWuDHq8+XS2RtuOkLo7DpX1H6h NbcBE8/tJSwiSx7o7I7r3OqjGp9Qqe6bJ/1HcjwFTmJ6SP0A4hUm+u3AWWpt8LvVCRHY sXz62NQWs2onZYDGqtRwL4ZYVpqVMNJIdCSyHcm7hdYv9UDPY5jtUUw+3MM9uNZh46HA hdTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=smH45prXaWa99xV+deagFBljpnUmJJvQg3D5ytEuJBw=; b=IHPWqhjax49iyPUgDj/T7Cql5NrkEM/gkcLYBzVgo5BgrQTFdz0ccTBzAZAkVRgRA7 BdxaBPm54kOpwpzlSct+IRwVVknze+EMfH+APKi7cBIE9XDBtUoZr+zX0/cE94Msa7Dy QSkkKFKB0EKwcccPO3x1ZiLiMCnMdhABrdwYh4WekDUEVABq2lpmwRIyafO5Z7z2ws4g bWF2wgDJS0JLqCZxIG9PcRrWm03S8f+NnVxiJmnPB+crohTB99r4u9edAXW5/B8Ru65k DHbbNG9VAkJQlW2TdMEeXihakWG/nLZwWh7Xmz/N3lLyCcWSf5g4M25UL0I+h56F/XZM IxkQ== X-Gm-Message-State: APjAAAVj/P24JS7N6uf/gFyYpU2ahWgK98I3Vhg3qYYFl3SFXjcSYZW9 SiqCFTrVK/viYIcaL2JlAeV7HMnU X-Google-Smtp-Source: APXvYqxiwcpaOZKb9PScR5UlsRF0mGxGoSABklllPnz5zXrnC+fBb5rEQubgp8ohbx/KJ8AErh9kIQ== X-Received: by 2002:a17:902:b10d:: with SMTP id q13mr4398552plr.109.1569809349219; Sun, 29 Sep 2019 19:09:09 -0700 (PDT) Received: from local.opencloud.tech.localdomain ([203.100.54.194]) by smtp.gmail.com with ESMTPSA id d69sm9941635pfd.175.2019.09.29.19.09.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 29 Sep 2019 19:09:08 -0700 (PDT) From: xiangxia.m.yue@gmail.com To: gvrose8192@gmail.com, pshelar@ovn.org Cc: netdev@vger.kernel.org, Tonghao Zhang Subject: [PATCH net-next 5/9] net: openvswitch: optimize flow-mask looking up Date: Mon, 30 Sep 2019 01:10:02 +0800 Message-Id: <1569777006-7435-6-git-send-email-xiangxia.m.yue@gmail.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1569777006-7435-1-git-send-email-xiangxia.m.yue@gmail.com> References: <1569777006-7435-1-git-send-email-xiangxia.m.yue@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Tonghao Zhang The full looking up on flow table traverses all mask array. If mask-array is too large, the number of invalid flow-mask increase, performance will be drop. This patch optimizes mask-array operation: * Inserting, insert it [ma->count- 1] directly. * Removing, only change last and current mask point, and free current mask. * Looking up, full looking up will break if mask is NULL. The function which changes or gets *count* of struct mask_array, is protected by ovs_lock, but flow_lookup (not protected) should use *max* of struct mask_array. Functions protected by ovs_lock: * tbl_mask_array_del_mask * tbl_mask_array_add_mask * flow_mask_find * ovs_flow_tbl_lookup_exact * ovs_flow_tbl_num_masks Signed-off-by: Tonghao Zhang --- net/openvswitch/flow_table.c | 101 +++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 52 deletions(-) diff --git a/net/openvswitch/flow_table.c b/net/openvswitch/flow_table.c index e59fac5..5257e4a 100644 --- a/net/openvswitch/flow_table.c +++ b/net/openvswitch/flow_table.c @@ -546,7 +546,7 @@ static struct sw_flow *flow_lookup(struct flow_table *tbl, mask = rcu_dereference_ovsl(ma->masks[i]); if (!mask) - continue; + break; flow = masked_flow_lookup(ti, key, mask, n_mask_hit); if (flow) { /* Found */ @@ -640,15 +640,13 @@ struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl, int i; /* Always called under ovs-mutex. */ - for (i = 0; i < ma->max; i++) { + for (i = 0; i < ma->count; i++) { struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); u32 __always_unused n_mask_hit; struct sw_flow_mask *mask; struct sw_flow *flow; mask = ovsl_dereference(ma->masks[i]); - if (!mask) - continue; flow = masked_flow_lookup(ti, match->key, mask, &n_mask_hit); if (flow && ovs_identifier_is_key(&flow->id) && @@ -712,21 +710,31 @@ static struct table_instance *table_instance_expand(struct table_instance *ti, return table_instance_rehash(ti, ti->n_buckets * 2, ufid); } -static void tbl_mask_array_delete_mask(struct mask_array *ma, - struct sw_flow_mask *mask) +static void tbl_mask_array_del_mask(struct flow_table *tbl, + struct sw_flow_mask *mask) { + struct mask_array *ma = ovsl_dereference(tbl->mask_array); int i; /* Remove the deleted mask pointers from the array */ - for (i = 0; i < ma->max; i++) { - if (mask == ovsl_dereference(ma->masks[i])) { - RCU_INIT_POINTER(ma->masks[i], NULL); - ma->count--; - kfree_rcu(mask, rcu); - return; - } + for (i = 0; i < ma->count; i++) { + if (mask == ovsl_dereference(ma->masks[i])) + goto found; } + BUG(); + return; + +found: + rcu_assign_pointer(ma->masks[i], ma->masks[ma->count -1]); + RCU_INIT_POINTER(ma->masks[ma->count -1], NULL); + ma->count--; + kfree_rcu(mask, rcu); + + /* Shrink the mask array if necessary. */ + if (ma->max >= (MASK_ARRAY_SIZE_MIN * 2) && + ma->count <= (ma->max / 3)) + tbl_mask_array_realloc(tbl, ma->max / 2); } /* Remove 'mask' from the mask list, if it is not needed any more. */ @@ -740,17 +748,8 @@ static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask) BUG_ON(!mask->ref_count); mask->ref_count--; - if (!mask->ref_count) { - struct mask_array *ma; - - ma = ovsl_dereference(tbl->mask_array); - tbl_mask_array_delete_mask(ma, mask); - - /* Shrink the mask array if necessary. */ - if (ma->max >= (MASK_ARRAY_SIZE_MIN * 2) && - ma->count <= (ma->max / 3)) - tbl_mask_array_realloc(tbl, ma->max / 2); - } + if (!mask->ref_count) + tbl_mask_array_del_mask(tbl, mask); } } @@ -803,17 +802,38 @@ static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl, int i; ma = ovsl_dereference(tbl->mask_array); - for (i = 0; i < ma->max; i++) { + for (i = 0; i < ma->count; i++) { struct sw_flow_mask *t; t = ovsl_dereference(ma->masks[i]); - if (t && mask_equal(mask, t)) + if (mask_equal(mask, t)) return t; } return NULL; } +static int tbl_mask_array_add_mask(struct flow_table *tbl, + struct sw_flow_mask *new) +{ + struct mask_array *ma = ovsl_dereference(tbl->mask_array); + int err; + + if (ma->count >= ma->max) { + err = tbl_mask_array_realloc(tbl, ma->max + + MASK_ARRAY_SIZE_MIN); + if (err) + return err; + } + + BUG_ON(ovsl_dereference(ma->masks[ma->count])); + + rcu_assign_pointer(ma->masks[ma->count], new); + ma->count++; + + return 0; +} + /* Add 'mask' into the mask list, if it is not already there. */ static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow, const struct sw_flow_mask *new) @@ -822,9 +842,6 @@ static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow, mask = flow_mask_find(tbl, new); if (!mask) { - struct mask_array *ma; - int i; - /* Allocate a new mask if none exsits. */ mask = mask_alloc(); if (!mask) @@ -833,29 +850,9 @@ static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow, mask->range = new->range; /* Add mask to mask-list. */ - ma = ovsl_dereference(tbl->mask_array); - if (ma->count >= ma->max) { - int err; - - err = tbl_mask_array_realloc(tbl, ma->max + - MASK_ARRAY_SIZE_MIN); - if (err) { - kfree(mask); - return err; - } - - ma = ovsl_dereference(tbl->mask_array); - } - - for (i = 0; i < ma->max; i++) { - const struct sw_flow_mask *t; - - t = ovsl_dereference(ma->masks[i]); - if (!t) { - rcu_assign_pointer(ma->masks[i], mask); - ma->count++; - break; - } + if (tbl_mask_array_add_mask(tbl, mask)) { + kfree(mask); + return -ENOMEM; } } else { BUG_ON(!mask->ref_count); From patchwork Sun Sep 29 17:10:03 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tonghao Zhang X-Patchwork-Id: 1169105 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="iCkatR+E"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46hQnb71Cfz9s7T for ; Mon, 30 Sep 2019 12:09:23 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729398AbfI3CJN (ORCPT ); Sun, 29 Sep 2019 22:09:13 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:35912 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729394AbfI3CJL (ORCPT ); Sun, 29 Sep 2019 22:09:11 -0400 Received: by mail-pf1-f193.google.com with SMTP id y22so4681864pfr.3 for ; Sun, 29 Sep 2019 19:09:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=6HgZPQKZxL7r1/wQsfui/0kmuRXOheA/g5FosFtqYiw=; b=iCkatR+EiXQmd0aOKiofwhZDVV90FVsy0Clt1u/z1dAoYAj1CVv+DpJthktY3Bm03u LnK2wWAOdm0Fn0kyy5XPtz72PtG3Iga1YN6UWsKQGysonStODP2x3R617gGwsbwujqWL GQgxNqqe/fSi2CvKCql4ROElB3L9pP9Hn1bIj4J9M6Ewvb1PivMwGqskds9fTM0PDnnF +/qbrmrh1XhvtT8t8yyP85mNlBjCio4aNm1RkGlRlecTu6MtCSuGtlP4an3MPSHFdVHS bM/hNp3CmPHX+k4xAowP8ZrbyYiJdESoCCjEsp8ryNikowq+myurZ4qBorNaObfSR/+c 8Cfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=6HgZPQKZxL7r1/wQsfui/0kmuRXOheA/g5FosFtqYiw=; b=V2LiuxqAc6ThBO6wGFKJDbxi4efxwBqQmqPAVAdih+L8upZO+4PeV+aMxu1ZUevR98 7QwFGsMhhe93vj4uTYGkM93s7yBuBF5HCSq3faeJGGatgF7E+7sqth7oqJD2badaBjzX AcJblA4NEbID1gtzx9I6f19X9gtEh760/kIEMMp+d2/MCbQCCy1BxzE8rXP/nqWagsSP 6WaCQYmXNVzPUWcb5Tppurm0WFM4fyjKtyuj+ZEUXhlCuSVR83GBBAQUjbAMLoOgHZlW fKpngTFMrEd1cC7Yp0J8bwDKA+kV/X1cD6g/I/4jQ/h8lh3HyWYfx0Ik72qUfkX8oOA7 hVmA== X-Gm-Message-State: APjAAAUxNbZEN6ty9hfvzo2vOx/JjAk3iLFvalCkWXTw/oOL7yoT9WQ0 y5iVQaqOrLoyrsPWL2xymCLDj3/q X-Google-Smtp-Source: APXvYqwPL2XwElQHeiKkz2ytP7Dn7K6d2rc1ujax5nEWofrx2yXa+UhrhBGrxoEJL9SEodiiceQiQg== X-Received: by 2002:a63:e116:: with SMTP id z22mr22347565pgh.424.1569809351056; Sun, 29 Sep 2019 19:09:11 -0700 (PDT) Received: from local.opencloud.tech.localdomain ([203.100.54.194]) by smtp.gmail.com with ESMTPSA id d69sm9941635pfd.175.2019.09.29.19.09.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 29 Sep 2019 19:09:10 -0700 (PDT) From: xiangxia.m.yue@gmail.com To: gvrose8192@gmail.com, pshelar@ovn.org Cc: netdev@vger.kernel.org, Tonghao Zhang Subject: [PATCH net-next 6/9] net: openvswitch: simplify the flow_hash Date: Mon, 30 Sep 2019 01:10:03 +0800 Message-Id: <1569777006-7435-7-git-send-email-xiangxia.m.yue@gmail.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1569777006-7435-1-git-send-email-xiangxia.m.yue@gmail.com> References: <1569777006-7435-1-git-send-email-xiangxia.m.yue@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Tonghao Zhang Simplify the code and remove the unnecessary BUILD_BUG_ON. Signed-off-by: Tonghao Zhang --- net/openvswitch/flow_table.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/net/openvswitch/flow_table.c b/net/openvswitch/flow_table.c index 5257e4a..c8e79c1 100644 --- a/net/openvswitch/flow_table.c +++ b/net/openvswitch/flow_table.c @@ -440,13 +440,9 @@ int ovs_flow_tbl_flush(struct flow_table *flow_table) static u32 flow_hash(const struct sw_flow_key *key, const struct sw_flow_key_range *range) { - int key_start = range->start; - int key_end = range->end; - const u32 *hash_key = (const u32 *)((const u8 *)key + key_start); - int hash_u32s = (key_end - key_start) >> 2; - + const u32 *hash_key = (const u32 *)((const u8 *)key + range->start); /* Make sure number of hash bytes are multiple of u32. */ - BUILD_BUG_ON(sizeof(long) % sizeof(u32)); + int hash_u32s = range_n_bytes(range) >> 2; return jhash2(hash_key, hash_u32s, 0); } From patchwork Sun Sep 29 17:10:04 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tonghao Zhang X-Patchwork-Id: 1169107 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="HrrBHCQD"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46hQnd21NZz9sPJ for ; Mon, 30 Sep 2019 12:09:25 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729408AbfI3CJQ (ORCPT ); Sun, 29 Sep 2019 22:09:16 -0400 Received: from mail-pl1-f196.google.com ([209.85.214.196]:41468 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729394AbfI3CJP (ORCPT ); Sun, 29 Sep 2019 22:09:15 -0400 Received: by mail-pl1-f196.google.com with SMTP id t10so3273150plr.8 for ; Sun, 29 Sep 2019 19:09:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=3N8AYSTn8Cd+SI4v+ldKV2ux7udULZm8tazu3f3NRMU=; b=HrrBHCQDhJmBXEjuLbPHoUgefvdA2y0VHPf9FM8OYi8/0uji9GhbSLEuKGZC2YED0Z AR61r6hZLK+GCWjyxywAxI8C0DzGBCQ9ZJAEpmuWAVWE9PRHmi9kP86TmQQDlutMAPSd cNaHCksAUlMZPQw4XLFeIygIzBUWgOqOqy5zyJOjjTnSKG6svATFWq5d5B7ZsThKkJkd iwDUdpkgOyoeyiURnmzifONijnxCwQO/w6KCMM5n0s5m+14wQDk7flx6DhknYCzi9s44 Mq1fQwJJlZ/sNHoHh0fpaI044iQbjziT1sRji/4WTQ78dGnGr242cmp5Ew1B5DyeqQS6 SvUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=3N8AYSTn8Cd+SI4v+ldKV2ux7udULZm8tazu3f3NRMU=; b=aN14oSDiMbVVyeLKdHweDmNxfW/xZksD1FPwEmVIFQ6iMBD4JHg8jWSpL5YfofLJHG Gtiti/qtIj8igbt5ps1g93aDjQVZW0UZTnVoRzXItoyw6If8mdvl6dGmKYJljNecVNPL u81hycInzWCDTp8ucAUHZvESVh4tzZDo9VZjNxHF5n5wFBzibXnMuRuTjC4GitWHsN94 W8JeR5YfdnlR5cUF9nsB8KkD9k7JgBElGAfWnbRq34o8KUq8WYrMP5plnTL2KyoFvBxn hobzRiM9EwXQThKzmf55O3Gzxb5HiP07ToB0BDdGBe45CtLVsiRPfwLkFpUgw6rzeNVn PF7A== X-Gm-Message-State: APjAAAX2KqCBHWDXwzljpBDTRYVry41zZAgmiSebQYp3Q4zNEgnIv7GW zMRVKiFXdggjNJddldvJQdg= X-Google-Smtp-Source: APXvYqxECPw/IcERwc8ewUNy32DH4WaGdq36hnbm8y67+Dxwo4vxNP6L02ivCLnA4RO2cDS5MNMe/A== X-Received: by 2002:a17:902:6e17:: with SMTP id u23mr17624863plk.205.1569809353136; Sun, 29 Sep 2019 19:09:13 -0700 (PDT) Received: from local.opencloud.tech.localdomain ([203.100.54.194]) by smtp.gmail.com with ESMTPSA id d69sm9941635pfd.175.2019.09.29.19.09.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 29 Sep 2019 19:09:12 -0700 (PDT) From: xiangxia.m.yue@gmail.com To: gvrose8192@gmail.com, pshelar@ovn.org Cc: netdev@vger.kernel.org, Tonghao Zhang Subject: [PATCH net-next 7/9] net: openvswitch: add likely in flow_lookup Date: Mon, 30 Sep 2019 01:10:04 +0800 Message-Id: <1569777006-7435-8-git-send-email-xiangxia.m.yue@gmail.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1569777006-7435-1-git-send-email-xiangxia.m.yue@gmail.com> References: <1569777006-7435-1-git-send-email-xiangxia.m.yue@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Tonghao Zhang The most case *index < ma->max, we add likely for performance. Signed-off-by: Tonghao Zhang --- net/openvswitch/flow_table.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/openvswitch/flow_table.c b/net/openvswitch/flow_table.c index c8e79c1..c21fd52 100644 --- a/net/openvswitch/flow_table.c +++ b/net/openvswitch/flow_table.c @@ -526,7 +526,7 @@ static struct sw_flow *flow_lookup(struct flow_table *tbl, struct sw_flow_mask *mask; int i; - if (*index < ma->max) { + if (likely(*index < ma->max)) { mask = rcu_dereference_ovsl(ma->masks[*index]); if (mask) { flow = masked_flow_lookup(ti, key, mask, n_mask_hit); From patchwork Sun Sep 29 17:10:05 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tonghao Zhang X-Patchwork-Id: 1169106 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="nFvSJ9Hn"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46hQnc4r47z9sPG for ; Mon, 30 Sep 2019 12:09:24 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729414AbfI3CJQ (ORCPT ); Sun, 29 Sep 2019 22:09:16 -0400 Received: from mail-pl1-f195.google.com ([209.85.214.195]:38448 "EHLO mail-pl1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729399AbfI3CJP (ORCPT ); Sun, 29 Sep 2019 22:09:15 -0400 Received: by mail-pl1-f195.google.com with SMTP id w8so2819359plq.5 for ; Sun, 29 Sep 2019 19:09:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=fKkPfxwPiJP7/KUjVDqoH89/DSdrM52RI3ZG4KqqgR0=; b=nFvSJ9HncOZA5a0j+1cvjbI0qLxOX69Lswb17JlfcN2UUfYO9pwD7FE2q40PBwKWri px9e76s2lCeVuReq339dI0btYP0hdXQ/wODmig2J/U3OcaTJqjNIjdT/cEKyvBiAVh/3 Ml5hYEBARUbtKbHL9xru1+466TRTEzq832Uhj0Vya49pKWPAeOqCk822SMyGE3MFesuq J4pnXR3HKvbYM+bYgk74Q0RmHlW/2ve6qknG1u6AeeSNc2pS649cT0qCkAywhsKfJ0Df v/XYgnNyNWrODIqYerdyK5U7ydVEXoliCExM5y6WtC9pcQ1cAph6jhKca9YqxaNaAfs8 a0QQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=fKkPfxwPiJP7/KUjVDqoH89/DSdrM52RI3ZG4KqqgR0=; b=hNanipXbRDCPHQK55qDRRZ42oB703WV+cRIqzd1vyuMBVkzfwFLb+lZF6CmEUjIxoJ gudAH0/EhsOGwo6D8cCdh4046AvxlCKvL7FiKdC45OMpN/CcuWIYWYXZDfK+3iLj1I7c jffxlxFMAWv1UaJXRa6TsAwwfcN3Emv2waEJy3qgMvfG3H1xokUZkxiOEMcxc0UbhVDl pIEq7DPrJCFC3hrJImaF24O5TpExEvLZZI9ZFYSGGBGjmyEgWq1S37HLaGO/CFsjf0tN i+Hm124/sXV7zYol81RVC95T4zaWp5s68xS83tqOcqpcFsY+RNraAdNHEr08BF3mFF3z XGQA== X-Gm-Message-State: APjAAAVgUL7ICto3Fyx93OEug3/VBHucCMSdU0IoGl01H66O6l9G/fR9 OVAreuD8fWQYQFGieVH1k+c= X-Google-Smtp-Source: APXvYqyFNSyPPymBRtoeW3Z25Tcv0q5SO/V+arKJwS1SAcpfyugHLhvVzxDATArHlvi/71QH5i3jkg== X-Received: by 2002:a17:902:b08f:: with SMTP id p15mr17970210plr.158.1569809355099; Sun, 29 Sep 2019 19:09:15 -0700 (PDT) Received: from local.opencloud.tech.localdomain ([203.100.54.194]) by smtp.gmail.com with ESMTPSA id d69sm9941635pfd.175.2019.09.29.19.09.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 29 Sep 2019 19:09:14 -0700 (PDT) From: xiangxia.m.yue@gmail.com To: gvrose8192@gmail.com, pshelar@ovn.org Cc: netdev@vger.kernel.org, Tonghao Zhang Subject: [PATCH net-next 8/9] net: openvswitch: fix possible memleak on destroy flow table Date: Mon, 30 Sep 2019 01:10:05 +0800 Message-Id: <1569777006-7435-9-git-send-email-xiangxia.m.yue@gmail.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1569777006-7435-1-git-send-email-xiangxia.m.yue@gmail.com> References: <1569777006-7435-1-git-send-email-xiangxia.m.yue@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Tonghao Zhang When we destroy the flow tables which may contain the flow_mask, so release the flow mask struct. Signed-off-by: Tonghao Zhang --- net/openvswitch/flow_table.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/net/openvswitch/flow_table.c b/net/openvswitch/flow_table.c index c21fd52..a2e0cb1 100644 --- a/net/openvswitch/flow_table.c +++ b/net/openvswitch/flow_table.c @@ -218,6 +218,18 @@ static int tbl_mask_array_realloc(struct flow_table *tbl, int size) return 0; } +static void tbl_mask_array_destroy(struct flow_table *tbl) +{ + struct mask_array *ma = ovsl_dereference(tbl->mask_array); + int i; + + /* Free the flow-mask and kfree_rcu the NULL is allowed. */ + for (i = 0; i < ma->count; i++) + kfree_rcu(ma->masks[i], rcu); + + kfree_rcu(tbl->mask_array, rcu); +} + int ovs_flow_tbl_init(struct flow_table *table) { struct table_instance *ti, *ufid_ti; @@ -312,7 +324,7 @@ void ovs_flow_tbl_destroy(struct flow_table *table) struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti); free_percpu(table->mask_cache); - kfree(rcu_dereference_raw(table->mask_array)); + tbl_mask_array_destroy(table); table_instance_destroy(ti, ufid_ti, false); } From patchwork Sun Sep 29 17:10:06 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tonghao Zhang X-Patchwork-Id: 1169108 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Q6UWVsWi"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 46hQnd5SP7z9s7T for ; Mon, 30 Sep 2019 12:09:25 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729424AbfI3CJT (ORCPT ); Sun, 29 Sep 2019 22:09:19 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:38791 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729399AbfI3CJR (ORCPT ); Sun, 29 Sep 2019 22:09:17 -0400 Received: by mail-pg1-f194.google.com with SMTP id x10so6488775pgi.5 for ; Sun, 29 Sep 2019 19:09:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=m+q8qfRrZeBgpd9Kpe/d6ur2XjjScNHiyDB8B58qb+8=; b=Q6UWVsWig247yw6KYC8PhS/e+rmJ9Fe8vtesjHCXe9U3OzkJnIVRbiRjbRI2SOZYaD LX/wg0NgNR9mZQhCVnAWbaXR+uQeS51USeCpf38p9nqsi5LERhCY+dRSoJWHCjrmd0CB lQXRbsQaxAkzyKMJfTywkAHelK/qWCCuyi7w/sUxoSaNXolIK7l1bc8l/V3JBIp/PLGy XKrym9KSmd7OoCx4TldZzo8JlLEM7JpIg1OZ93txox7Sw4Pn+lciyjOlCdSpL70jXKsl 1C84DFtxMzkzro3jGmmD5AUDiaumioBJ3A/2Tra4CWK/g2hg3D2PPXrJQXqTWh1l84FQ V6Bw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=m+q8qfRrZeBgpd9Kpe/d6ur2XjjScNHiyDB8B58qb+8=; b=HFDYSKduPTqAniqXwLCqxhS1gvYTxp1X8jMLLKe8xMB2zkKk9xi7IJNA88YSj3bYq4 3/2KSV33UZDAKnqakMC8nIm7l4dChqPHyrdGxX9+cSv5ibWLJduuIUz2dMzMgtM/AtMg 7j9vPONzDUw3hTma15a4G986g2X9VwDFK9JZvSPwZlihLXiU/E7/LFHZfglmQSELdrFa YZwiNc2Tp09Qdbmdw6s//RB3zIffDxdo6Vq7EJ/334KwKsiZ/BLHVuxZ6duc+7VeCRss AYs4gnG+p7866bASg9ZNla6aAlFCt/oRpriCx3VFtwTchbxlwkI/a+bepAe+o+EXx+kg 2s+w== X-Gm-Message-State: APjAAAVPrWYwSZlUl506/AnrnospT3C3tR8F4YZIlWdqRosjQH1bYW5H hppug7yHJhkttzVglAghgqXaeB5e X-Google-Smtp-Source: APXvYqz62RA0jaHfdYJt5XHJfh7BvRgI+2veCCSuZPpWNfQt3X3UEf8z3hSuxqQqS79zXmGUGfAHiQ== X-Received: by 2002:a63:a369:: with SMTP id v41mr22140918pgn.148.1569809357067; Sun, 29 Sep 2019 19:09:17 -0700 (PDT) Received: from local.opencloud.tech.localdomain ([203.100.54.194]) by smtp.gmail.com with ESMTPSA id d69sm9941635pfd.175.2019.09.29.19.09.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 29 Sep 2019 19:09:16 -0700 (PDT) From: xiangxia.m.yue@gmail.com To: gvrose8192@gmail.com, pshelar@ovn.org Cc: netdev@vger.kernel.org, Tonghao Zhang Subject: [PATCH net-next 9/9] net: openvswitch: simplify the ovs_dp_cmd_new Date: Mon, 30 Sep 2019 01:10:06 +0800 Message-Id: <1569777006-7435-10-git-send-email-xiangxia.m.yue@gmail.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1569777006-7435-1-git-send-email-xiangxia.m.yue@gmail.com> References: <1569777006-7435-1-git-send-email-xiangxia.m.yue@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Tonghao Zhang use the specified functions to init resource. Signed-off-by: Tonghao Zhang --- net/openvswitch/datapath.c | 60 +++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/net/openvswitch/datapath.c b/net/openvswitch/datapath.c index 3d7b1c4..78b442e 100644 --- a/net/openvswitch/datapath.c +++ b/net/openvswitch/datapath.c @@ -1576,6 +1576,31 @@ static int ovs_dp_change(struct datapath *dp, struct nlattr *a[]) return 0; } +static int ovs_dp_stats_init(struct datapath *dp) +{ + dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu); + if (!dp->stats_percpu) + return -ENOMEM; + + return 0; +} + +static int ovs_dp_vport_init(struct datapath *dp) +{ + int i; + + dp->ports = kmalloc_array(DP_VPORT_HASH_BUCKETS, + sizeof(struct hlist_head), + GFP_KERNEL); + if (!dp->ports) + return -ENOMEM; + + for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) + INIT_HLIST_HEAD(&dp->ports[i]); + + return 0; +} + static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info) { struct nlattr **a = info->attrs; @@ -1584,7 +1609,7 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info) struct datapath *dp; struct vport *vport; struct ovs_net *ovs_net; - int err, i; + int err; err = -EINVAL; if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID]) @@ -1597,35 +1622,26 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info) err = -ENOMEM; dp = kzalloc(sizeof(*dp), GFP_KERNEL); if (dp == NULL) - goto err_free_reply; + goto err_destroy_reply; ovs_dp_set_net(dp, sock_net(skb->sk)); /* Allocate table. */ err = ovs_flow_tbl_init(&dp->table); if (err) - goto err_free_dp; + goto err_destroy_dp; - dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu); - if (!dp->stats_percpu) { - err = -ENOMEM; + err = ovs_dp_stats_init(dp); + if (err) goto err_destroy_table; - } - dp->ports = kmalloc_array(DP_VPORT_HASH_BUCKETS, - sizeof(struct hlist_head), - GFP_KERNEL); - if (!dp->ports) { - err = -ENOMEM; - goto err_destroy_percpu; - } - - for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) - INIT_HLIST_HEAD(&dp->ports[i]); + err = ovs_dp_vport_init(dp); + if (err) + goto err_destroy_stats; err = ovs_meters_init(dp); if (err) - goto err_destroy_ports_array; + goto err_destroy_ports; /* Set up our datapath device. */ parms.name = nla_data(a[OVS_DP_ATTR_NAME]); @@ -1675,15 +1691,15 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info) err_destroy_meters: ovs_unlock(); ovs_meters_exit(dp); -err_destroy_ports_array: +err_destroy_ports: kfree(dp->ports); -err_destroy_percpu: +err_destroy_stats: free_percpu(dp->stats_percpu); err_destroy_table: ovs_flow_tbl_destroy(&dp->table); -err_free_dp: +err_destroy_dp: kfree(dp); -err_free_reply: +err_destroy_reply: kfree_skb(reply); err: return err;