From patchwork Mon Mar 23 13:10:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tonghao Zhang X-Patchwork-Id: 1263255 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.137; helo=fraxinus.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=dHkkb55f; dkim-atps=neutral Received: from fraxinus.osuosl.org (smtp4.osuosl.org [140.211.166.137]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48qNPf2bKZz9sSN for ; Sun, 29 Mar 2020 02:46:40 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by fraxinus.osuosl.org (Postfix) with ESMTP id B1BB387063; Sat, 28 Mar 2020 15:46:37 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from fraxinus.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id rkZKM90Lckeu; Sat, 28 Mar 2020 15:46:35 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by fraxinus.osuosl.org (Postfix) with ESMTP id CB9E386FC3; Sat, 28 Mar 2020 15:46:35 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id B2C1AC18DA; Sat, 28 Mar 2020 15:46:35 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from hemlock.osuosl.org (smtp2.osuosl.org [140.211.166.133]) by lists.linuxfoundation.org (Postfix) with ESMTP id 912D2C0177 for ; Sat, 28 Mar 2020 15:46:34 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by hemlock.osuosl.org (Postfix) with ESMTP id 8D8D088007 for ; Sat, 28 Mar 2020 15:46:34 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from hemlock.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id sA2AV3ivQqhf for ; Sat, 28 Mar 2020 15:46:33 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-pl1-f193.google.com (mail-pl1-f193.google.com [209.85.214.193]) by hemlock.osuosl.org (Postfix) with ESMTPS id ADD7884F80 for ; Sat, 28 Mar 2020 15:46:33 +0000 (UTC) Received: by mail-pl1-f193.google.com with SMTP id b9so4690325pls.12 for ; Sat, 28 Mar 2020 08:46:33 -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; bh=cWk4oFQDqrwsP/Dl2f22kQXEVJYhtGHvOy5gZec+d/s=; b=dHkkb55f5xh6c9Grrt2+aPZGQ4ugta6Y+zEtQpE7nyDBc5W7mLtf+3o2t/IeCY14ao p2REl3KK6A2UmDLwVhYb11Ut/lj2Skrire+/1Wt82yop4RCce0j+BetVQiR5EVo+36nG Bji2ezi3UnM2AViUb1HbYMjzYS4+CIDAS2j8ZgNd+D/tQZeolAazzdLSj4Pnb/aE7IRw kA+4jssmV9BcUgC3k1JpQHyzzgN0Q5zpvVQIkVcuQZuFjiDcl0I6Rf9BvjUgpu4YbK4x R6REWFczM66ulZB+IA95XXWljDvoFQwoaWiKFm9HL5UxMxqd4Ym4y/PuKCo5RU0BNltj QKEw== 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; bh=cWk4oFQDqrwsP/Dl2f22kQXEVJYhtGHvOy5gZec+d/s=; b=GATm7RZsVIoqUrfrLmcBME2PAdngoYlW4UxdvUdZ4/9Xp9is4/7gGqN1yp+aZqa8h7 lHVFqdbpm+9p3FhJJkvuRweKdBMN+qb2FGTj0G0HIZLNHZ7+UG8xip8RBmQZqaOTK8s6 WZW22BChPXRffXVslTdF82VtFSQFKj8vz1+rNBffZXp24ZurRPVaygMHCP1zKwOAX1V4 B/hmLqdtTkKtu/3yNl8KEzMvLd6Y+7jj+ZunLBhDBQelSucTSu/Pdq4Fw7HMbtRMx35z WWMlclijxAqeIoQYeMw12ALuPic3jhjyT+/9KNE8FlC9o8V3LpAtNhI2mLpzFLTwqTzm fijQ== X-Gm-Message-State: ANhLgQ2z0b/xEZttGyFBjdE4scdGwuGOfG99MPrRWeGbv/bGt7SPwoOa okQaZM+FKMG90LsFDjmyTLtX5sZ2 X-Google-Smtp-Source: ADFU+vvyx8iFKZsWEFSP1TV19yaqwIL8dM6+Asmo/3wv1Th8Ky/pjg2UPQ0qfHg/57/HW7joy7TS4g== X-Received: by 2002:a17:902:864c:: with SMTP id y12mr4395474plt.8.1585410393232; Sat, 28 Mar 2020 08:46:33 -0700 (PDT) Received: from local.opencloud.tech.localdomain ([115.171.63.184]) by smtp.gmail.com with ESMTPSA id q185sm6375218pfb.154.2020.03.28.08.46.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 28 Mar 2020 08:46:32 -0700 (PDT) From: xiangxia.m.yue@gmail.com To: pshelar@ovn.org Date: Mon, 23 Mar 2020 21:10:37 +0800 Message-Id: <1584969039-74113-1-git-send-email-xiangxia.m.yue@gmail.com> X-Mailer: git-send-email 1.8.3.1 Cc: dev@openvswitch.org, netdev@vger.kernel.org Subject: [ovs-dev] [PATCH net-next v1 1/3] net: openvswitch: expand the meters number supported X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" From: Tonghao Zhang In kernel datapath of Open vSwitch, there are only 1024 buckets of meter in one dp. If installing more than 1024 (e.g. 8192) meters, it may lead to the performance drop. But in some case, for example, Open vSwitch used as edge gateway, there should be 200,000+ at least, meters used for IP address bandwidth limitation. [Open vSwitch userspace datapath has this issue too.] For more scalable meter, this patch expands the buckets when necessary, so we can install more meters in the datapath. * Introducing the struct *dp_meter_instance*, it's easy to expand meter though change the *ti* point in the struct *dp_meter_table*. * Using kvmalloc_array instead of kmalloc_array. Cc: Pravin B Shelar Cc: Andy Zhou Signed-off-by: Tonghao Zhang --- net/openvswitch/datapath.h | 2 +- net/openvswitch/meter.c | 168 ++++++++++++++++++++++++++++++------- net/openvswitch/meter.h | 17 +++- 3 files changed, 153 insertions(+), 34 deletions(-) diff --git a/net/openvswitch/datapath.h b/net/openvswitch/datapath.h index e239a46c2f94..785105578448 100644 --- a/net/openvswitch/datapath.h +++ b/net/openvswitch/datapath.h @@ -82,7 +82,7 @@ struct datapath { u32 max_headroom; /* Switch meters. */ - struct hlist_head *meters; + struct dp_meter_table *meters; }; /** diff --git a/net/openvswitch/meter.c b/net/openvswitch/meter.c index 5010d1ddd4bd..98003b201b45 100644 --- a/net/openvswitch/meter.c +++ b/net/openvswitch/meter.c @@ -47,40 +47,136 @@ static void ovs_meter_free(struct dp_meter *meter) kfree_rcu(meter, rcu); } -static struct hlist_head *meter_hash_bucket(const struct datapath *dp, +static struct hlist_head *meter_hash_bucket(struct dp_meter_instance *ti, u32 meter_id) { - return &dp->meters[meter_id & (METER_HASH_BUCKETS - 1)]; + u32 hash = jhash_1word(meter_id, ti->hash_seed); + + return &ti->buckets[hash & (ti->n_buckets - 1)]; } /* Call with ovs_mutex or RCU read lock. */ -static struct dp_meter *lookup_meter(const struct datapath *dp, +static struct dp_meter *lookup_meter(const struct dp_meter_table *tbl, u32 meter_id) { + struct dp_meter_instance *ti = rcu_dereference_ovsl(tbl->ti); struct dp_meter *meter; struct hlist_head *head; - head = meter_hash_bucket(dp, meter_id); - hlist_for_each_entry_rcu(meter, head, dp_hash_node, - lockdep_ovsl_is_held()) { + head = meter_hash_bucket(ti, meter_id); + hlist_for_each_entry_rcu(meter, head, hash_node[ti->node_ver], + lockdep_ovsl_is_held()) { if (meter->id == meter_id) return meter; } + return NULL; } -static void attach_meter(struct datapath *dp, struct dp_meter *meter) +static struct dp_meter_instance *dp_meter_instance_alloc(const int size) +{ + struct dp_meter_instance *ti; + int i; + + ti = kmalloc(sizeof(*ti), GFP_KERNEL); + if (!ti) + return NULL; + + ti->buckets = kvmalloc_array(size, sizeof(struct hlist_head), + GFP_KERNEL); + if (!ti->buckets) { + kfree(ti); + return NULL; + } + + for (i = 0; i < size; i++) + INIT_HLIST_HEAD(&ti->buckets[i]); + + ti->n_buckets = size; + ti->node_ver = 0; + get_random_bytes(&ti->hash_seed, sizeof(u32)); + + return ti; +} + +static void dp_meter_instance_free_rcu(struct rcu_head *rcu) { - struct hlist_head *head = meter_hash_bucket(dp, meter->id); + struct dp_meter_instance *ti; - hlist_add_head_rcu(&meter->dp_hash_node, head); + ti = container_of(rcu, struct dp_meter_instance, rcu); + kvfree(ti->buckets); + kfree(ti); } -static void detach_meter(struct dp_meter *meter) +static void dp_meter_instance_insert(struct dp_meter_instance *ti, + struct dp_meter *meter) +{ + struct hlist_head *head = meter_hash_bucket(ti, meter->id); + + hlist_add_head_rcu(&meter->hash_node[ti->node_ver], head); +} + +static void dp_meter_instance_remove(struct dp_meter_instance *ti, + struct dp_meter *meter) { + hlist_del_rcu(&meter->hash_node[ti->node_ver]); +} + +static struct dp_meter_instance * +dp_meter_instance_expand(struct dp_meter_instance *ti) +{ + struct dp_meter_instance *new_ti; + int i; + + new_ti = dp_meter_instance_alloc(ti->n_buckets * 2); + if (!new_ti) + return NULL; + + new_ti->node_ver = !ti->node_ver; + + for (i = 0; i < ti->n_buckets; i++) { + struct hlist_head *head = &ti->buckets[i]; + struct dp_meter *meter; + + hlist_for_each_entry_rcu(meter, head, hash_node[ti->node_ver], + lockdep_ovsl_is_held()) + dp_meter_instance_insert(new_ti, meter); + } + + return new_ti; +} + +static void attach_meter(struct dp_meter_table *tbl, struct dp_meter *meter) +{ + struct dp_meter_instance *new_ti; + struct dp_meter_instance *ti; + + ti = rcu_dereference_ovsl(tbl->ti); + dp_meter_instance_insert(ti, meter); + + /* operate the counter safely, because called with ovs_lock. */ + tbl->count++; + + if (tbl->count > ti->n_buckets) { + new_ti = dp_meter_instance_expand(ti); + + if (new_ti) { + rcu_assign_pointer(tbl->ti, new_ti); + call_rcu(&ti->rcu, dp_meter_instance_free_rcu); + } + } +} + +static void detach_meter(struct dp_meter_table *tbl, struct dp_meter *meter) +{ + struct dp_meter_instance *ti = rcu_dereference_ovsl(tbl->ti); + ASSERT_OVSL(); - if (meter) - hlist_del_rcu(&meter->dp_hash_node); + if (meter) { + /* operate the counter safely, because called with ovs_lock. */ + tbl->count--; + dp_meter_instance_remove(ti, meter); + } } static struct sk_buff * @@ -303,9 +399,9 @@ static int ovs_meter_cmd_set(struct sk_buff *skb, struct genl_info *info) meter_id = nla_get_u32(a[OVS_METER_ATTR_ID]); /* Cannot fail after this. */ - old_meter = lookup_meter(dp, meter_id); - detach_meter(old_meter); - attach_meter(dp, meter); + old_meter = lookup_meter(dp->meters, meter_id); + detach_meter(dp->meters, old_meter); + attach_meter(dp->meters, meter); ovs_unlock(); /* Build response with the meter_id and stats from @@ -365,7 +461,7 @@ static int ovs_meter_cmd_get(struct sk_buff *skb, struct genl_info *info) } /* Locate meter, copy stats. */ - meter = lookup_meter(dp, meter_id); + meter = lookup_meter(dp->meters, meter_id); if (!meter) { err = -ENOENT; goto exit_unlock; @@ -416,13 +512,13 @@ static int ovs_meter_cmd_del(struct sk_buff *skb, struct genl_info *info) goto exit_unlock; } - old_meter = lookup_meter(dp, meter_id); + old_meter = lookup_meter(dp->meters, meter_id); if (old_meter) { spin_lock_bh(&old_meter->lock); err = ovs_meter_cmd_reply_stats(reply, meter_id, old_meter); WARN_ON(err); spin_unlock_bh(&old_meter->lock); - detach_meter(old_meter); + detach_meter(dp->meters, old_meter); } ovs_unlock(); ovs_meter_free(old_meter); @@ -452,7 +548,7 @@ bool ovs_meter_execute(struct datapath *dp, struct sk_buff *skb, int i, band_exceeded_max = -1; u32 band_exceeded_rate = 0; - meter = lookup_meter(dp, meter_id); + meter = lookup_meter(dp->meters, meter_id); /* Do not drop the packet when there is no meter. */ if (!meter) return false; @@ -570,32 +666,44 @@ struct genl_family dp_meter_genl_family __ro_after_init = { int ovs_meters_init(struct datapath *dp) { - int i; + struct dp_meter_instance *ti; + struct dp_meter_table *tbl; - dp->meters = kmalloc_array(METER_HASH_BUCKETS, - sizeof(struct hlist_head), GFP_KERNEL); + tbl = kmalloc(sizeof(*tbl), GFP_KERNEL); + if (!tbl) + return -ENOMEM; - if (!dp->meters) + tbl->count = 0; + + ti = dp_meter_instance_alloc(METER_HASH_BUCKETS); + if (!ti) { + kfree(tbl); return -ENOMEM; + } - for (i = 0; i < METER_HASH_BUCKETS; i++) - INIT_HLIST_HEAD(&dp->meters[i]); + rcu_assign_pointer(tbl->ti, ti); + dp->meters = tbl; return 0; } void ovs_meters_exit(struct datapath *dp) { + struct dp_meter_table *tbl = dp->meters; + struct dp_meter_instance *ti = rcu_dereference_ovsl(tbl->ti); int i; - for (i = 0; i < METER_HASH_BUCKETS; i++) { - struct hlist_head *head = &dp->meters[i]; + for (i = 0; i < ti->n_buckets; i++) { + struct hlist_head *head = &ti->buckets[i]; struct dp_meter *meter; struct hlist_node *n; - hlist_for_each_entry_safe(meter, n, head, dp_hash_node) - kfree(meter); + hlist_for_each_entry_safe(meter, n, head, + hash_node[ti->node_ver]) + ovs_meter_free(meter); } - kfree(dp->meters); + kvfree(ti->buckets); + kfree(ti); + kfree(tbl); } diff --git a/net/openvswitch/meter.h b/net/openvswitch/meter.h index f645913870bd..bc84796d7d4d 100644 --- a/net/openvswitch/meter.h +++ b/net/openvswitch/meter.h @@ -30,9 +30,7 @@ struct dp_meter_band { struct dp_meter { spinlock_t lock; /* Per meter lock */ struct rcu_head rcu; - struct hlist_node dp_hash_node; /*Element in datapath->meters - * hash table. - */ + struct hlist_node hash_node[2]; u32 id; u16 kbps:1, keep_stats:1; u16 n_bands; @@ -42,6 +40,19 @@ struct dp_meter { struct dp_meter_band bands[]; }; +struct dp_meter_instance { + struct hlist_head *buckets; + struct rcu_head rcu; + u32 n_buckets; + u32 hash_seed; + u8 node_ver; +}; + +struct dp_meter_table { + struct dp_meter_instance __rcu *ti; + u32 count; +}; + extern struct genl_family dp_meter_genl_family; int ovs_meters_init(struct datapath *dp); void ovs_meters_exit(struct datapath *dp); From patchwork Mon Mar 23 13:10:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tonghao Zhang X-Patchwork-Id: 1263256 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.138; helo=whitealder.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=f2asgbEI; dkim-atps=neutral Received: from whitealder.osuosl.org (smtp1.osuosl.org [140.211.166.138]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48qNPg1fjPz9sSQ for ; Sun, 29 Mar 2020 02:46:46 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by whitealder.osuosl.org (Postfix) with ESMTP id 8AB1C877E0; Sat, 28 Mar 2020 15:46:44 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from whitealder.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id nxY7iBtsbtcE; Sat, 28 Mar 2020 15:46:39 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by whitealder.osuosl.org (Postfix) with ESMTP id 6B89A87BC0; Sat, 28 Mar 2020 15:46:39 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 5BD12C1D87; Sat, 28 Mar 2020 15:46:39 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from hemlock.osuosl.org (smtp2.osuosl.org [140.211.166.133]) by lists.linuxfoundation.org (Postfix) with ESMTP id 1A5FBC0177 for ; Sat, 28 Mar 2020 15:46:37 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by hemlock.osuosl.org (Postfix) with ESMTP id 0ABB38826F for ; Sat, 28 Mar 2020 15:46:37 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from hemlock.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 4dIjnNgCsbnW for ; Sat, 28 Mar 2020 15:46:35 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-pj1-f67.google.com (mail-pj1-f67.google.com [209.85.216.67]) by hemlock.osuosl.org (Postfix) with ESMTPS id E7D3F84F80 for ; Sat, 28 Mar 2020 15:46:35 +0000 (UTC) Received: by mail-pj1-f67.google.com with SMTP id np9so5236859pjb.4 for ; Sat, 28 Mar 2020 08:46:35 -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 :mime-version:content-transfer-encoding; bh=7T0gpUg1KYguFzYbM6Ar8rC1c+eZds8dxyLFu0Xgo10=; b=f2asgbEI6ieB4Ellt7Dy+LgThprY4DwL6LP2pb1mHFUt/fhb1amGL5S1VHCf9IDkVY yDnTd0JDJcLG7InQoZcEysqBVWBbiHtb4BjYOzSk6z61B5GDJms6wurNd5/Cu86YTyTp R2nH70bBQUVyXAHkgBGI3s4BChjOTmn9IGL/59MYYI0ucy/qHpKGxx2l+eqqSSq81ZsJ MPjYobP5g2NNBZ8AvryADDiwV+q/XwbZZwG96hR3oIX1digTh8ABSep4AceewWNoO+0h WceB+ECejsuulKK2mQxvPPnVliGFAs3xUjzv6i0sSeQtfFBp6vKpuGVp/fD28NXeofq1 /nYQ== 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:mime-version:content-transfer-encoding; bh=7T0gpUg1KYguFzYbM6Ar8rC1c+eZds8dxyLFu0Xgo10=; b=YBPuRnLx3KGkt6woKNJ9sU4q2Viao259mqnrxQ7hzHHNce/mB6YxuhOHYLVehffGDY A+fFPUyp+AcBhVjZKsZ0fd4JiyCOYKSjM7T79VvD/liyfq7hMtcx2cf+NVuTjZsTI9vr wftYMppUaKuNea1hc3wfGTREiePfIb4IrBVMqF9KNGa5Jy3p18EeiiODbbg5+PoYIg2a 7n8nE5992Ck0zF6EUbJOYHKI6eDOH5cqau0nh1MmNr6qcNyGfGD/Ok4tzSypqA1f5Cw2 p41mH63HKoevMIIi/QHaOc6j59DdVBLfeyyme5gZHHxJIsW2NSP2L/fC151hKb0eh+fP RGiw== X-Gm-Message-State: ANhLgQ03Vp250jJo5ObslH+PJlFafSK+DkbBBHHRAHO6by8+yXF56Msk 6soCFzXW2/btbX2IgWNUh5c= X-Google-Smtp-Source: ADFU+vuXMBfgXsUSdwm9V8vkQDauHQHz2qMbbvGESUoHFAu2q7sZwPho4nmk6FJhUfBHgMWuMwfhmA== X-Received: by 2002:a17:90a:1784:: with SMTP id q4mr5387627pja.174.1585410395479; Sat, 28 Mar 2020 08:46:35 -0700 (PDT) Received: from local.opencloud.tech.localdomain ([115.171.63.184]) by smtp.gmail.com with ESMTPSA id q185sm6375218pfb.154.2020.03.28.08.46.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 28 Mar 2020 08:46:35 -0700 (PDT) From: xiangxia.m.yue@gmail.com To: pshelar@ovn.org Date: Mon, 23 Mar 2020 21:10:38 +0800 Message-Id: <1584969039-74113-2-git-send-email-xiangxia.m.yue@gmail.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1584969039-74113-1-git-send-email-xiangxia.m.yue@gmail.com> References: <1584969039-74113-1-git-send-email-xiangxia.m.yue@gmail.com> MIME-Version: 1.0 Cc: dev@openvswitch.org, netdev@vger.kernel.org Subject: [ovs-dev] [PATCH net-next v1 2/3] net: openvswitch: set max limitation to meters X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" From: Tonghao Zhang Don't allow user to create meter unlimitedly, which may cause to consume a large amount of kernel memory. The 200,000 meters may be fine in general case. Cc: Pravin B Shelar Cc: Andy Zhou Signed-off-by: Tonghao Zhang --- net/openvswitch/meter.c | 14 +++++++++----- net/openvswitch/meter.h | 3 ++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/net/openvswitch/meter.c b/net/openvswitch/meter.c index 98003b201b45..5efd48e024f0 100644 --- a/net/openvswitch/meter.c +++ b/net/openvswitch/meter.c @@ -256,7 +256,7 @@ static int ovs_meter_cmd_features(struct sk_buff *skb, struct genl_info *info) if (IS_ERR(reply)) return PTR_ERR(reply); - if (nla_put_u32(reply, OVS_METER_ATTR_MAX_METERS, U32_MAX) || + if (nla_put_u32(reply, OVS_METER_ATTR_MAX_METERS, DP_MAX_METERS) || nla_put_u32(reply, OVS_METER_ATTR_MAX_BANDS, DP_MAX_BANDS)) goto nla_put_failure; @@ -284,13 +284,17 @@ static int ovs_meter_cmd_features(struct sk_buff *skb, struct genl_info *info) static struct dp_meter *dp_meter_create(struct nlattr **a) { + u32 meter_id = nla_get_u32(a[OVS_METER_ATTR_ID]); + struct dp_meter_band *band; + struct dp_meter *meter; struct nlattr *nla; - int rem; u16 n_bands = 0; - struct dp_meter *meter; - struct dp_meter_band *band; + int rem; int err; + if (meter_id > DP_MAX_METERS) + return ERR_PTR(-EFBIG); + /* Validate attributes, count the bands. */ if (!a[OVS_METER_ATTR_BANDS]) return ERR_PTR(-EINVAL); @@ -304,7 +308,7 @@ static struct dp_meter *dp_meter_create(struct nlattr **a) if (!meter) return ERR_PTR(-ENOMEM); - meter->id = nla_get_u32(a[OVS_METER_ATTR_ID]); + meter->id = meter_id; meter->used = div_u64(ktime_get_ns(), 1000 * 1000); meter->kbps = a[OVS_METER_ATTR_KBPS] ? 1 : 0; meter->keep_stats = !a[OVS_METER_ATTR_CLEAR]; diff --git a/net/openvswitch/meter.h b/net/openvswitch/meter.h index bc84796d7d4d..9ff7a9200d0d 100644 --- a/net/openvswitch/meter.h +++ b/net/openvswitch/meter.h @@ -17,7 +17,8 @@ #include "flow.h" struct datapath; -#define DP_MAX_BANDS 1 +#define DP_MAX_METERS (200000ULL) +#define DP_MAX_BANDS 1 struct dp_meter_band { u32 type; From patchwork Mon Mar 23 13:10:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tonghao Zhang X-Patchwork-Id: 1263254 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.137; helo=fraxinus.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=fgT3adn0; dkim-atps=neutral Received: from fraxinus.osuosl.org (smtp4.osuosl.org [140.211.166.137]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48qNPd52k3z9sQt for ; Sun, 29 Mar 2020 02:46:44 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by fraxinus.osuosl.org (Postfix) with ESMTP id B8B06870D2; Sat, 28 Mar 2020 15:46:42 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from fraxinus.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id fcfnf4zA_19J; Sat, 28 Mar 2020 15:46:42 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by fraxinus.osuosl.org (Postfix) with ESMTP id 4C84387141; Sat, 28 Mar 2020 15:46:42 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 3701CC18DA; Sat, 28 Mar 2020 15:46:42 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from whitealder.osuosl.org (smtp1.osuosl.org [140.211.166.138]) by lists.linuxfoundation.org (Postfix) with ESMTP id 02939C0177 for ; Sat, 28 Mar 2020 15:46:41 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by whitealder.osuosl.org (Postfix) with ESMTP id 25A8387C74 for ; Sat, 28 Mar 2020 15:46:40 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from whitealder.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id UdyS6Do7NJAl for ; Sat, 28 Mar 2020 15:46:38 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-pj1-f66.google.com (mail-pj1-f66.google.com [209.85.216.66]) by whitealder.osuosl.org (Postfix) with ESMTPS id 41BC7810C0 for ; Sat, 28 Mar 2020 15:46:38 +0000 (UTC) Received: by mail-pj1-f66.google.com with SMTP id l36so5236211pjb.3 for ; Sat, 28 Mar 2020 08:46:38 -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=/F5soHZdX+upsGd/QwghyKOQFTKWDBPpvRaxS81m59o=; b=fgT3adn0fs7zZUrYAXaxjm17QmCkgA1prZ9HdbFtvgsnyMAVvvPQnlVzKvAu6Y9HrQ c51SHqnG4rcvxUqlK3lTF1QLJzSj8nKQkCS2XSqIzj7G/LcblWNEWUNuF/qJU3O1SIZR xCtTQjbVLmKlGngezpqy4rYKBqj0FE7HjzxYaRtjilWZSVpfdpFEojpFpHrSU58aDhtD rkXNrGqqcoC5IjUCSEgmwEuZoHfIIe3Y6jHwCzITUJycFoJDzDhmNI4IW+rTpKW1eYgt bWclqxfYm7yuhuYCV4ksFhQBH8kHbu+Z93nyuPe5SKX/3WNZ7DMJGNe2hP8wym2ZD+3n +MVg== 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=/F5soHZdX+upsGd/QwghyKOQFTKWDBPpvRaxS81m59o=; b=Mv/KxVa4R6OOJCmuzHpIj1EnhPC9hd2RDKtDOPVRWUHo2vypXdEYmy72lYQa8lQJ4i z+52lGGTvSUdwj8Sqa3bBNMM7aczlDT9I/O6hcGlyhax9pn6GBipQ35uyPxwqTx6HXGB XVoIHwkdPSE0uQQzeo5Vz0/54kyXfY4Y16gV5U9cp9dZBE/+kpZCo9gFfN2/ZNcUWQzF qr5AOZYt1pYCdgdHHFAvp9qnRx36qvHVjfARTa0bkzZ8jNIr1aPF29Fc15iJsTKPiTKu btRxsGFW18dZq4E2pxKDt8RD/7KjXVukSS/O1ywC8JQ2d/YsHNxuoDimXwJC73MBsyv8 p51g== X-Gm-Message-State: ANhLgQ1YroaAqnndUp9u8rg8dvuTuatAmi1YkZ0zoYfMs5aMQlBY4C99 oeIW4uHyeLosHVE2xFc3ykI= X-Google-Smtp-Source: ADFU+vsZNBWoCb5qSXtAEzHIqsSU7xXoLzKUGeFJMtO1JC865/HSednvyZjt2Nl4Y4b7qCy3YESQNQ== X-Received: by 2002:a17:90a:218b:: with SMTP id q11mr5534138pjc.163.1585410397779; Sat, 28 Mar 2020 08:46:37 -0700 (PDT) Received: from local.opencloud.tech.localdomain ([115.171.63.184]) by smtp.gmail.com with ESMTPSA id q185sm6375218pfb.154.2020.03.28.08.46.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 28 Mar 2020 08:46:37 -0700 (PDT) From: xiangxia.m.yue@gmail.com To: pshelar@ovn.org Date: Mon, 23 Mar 2020 21:10:39 +0800 Message-Id: <1584969039-74113-3-git-send-email-xiangxia.m.yue@gmail.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1584969039-74113-1-git-send-email-xiangxia.m.yue@gmail.com> References: <1584969039-74113-1-git-send-email-xiangxia.m.yue@gmail.com> Cc: dev@openvswitch.org, netdev@vger.kernel.org Subject: [ovs-dev] [PATCH net-next v1 3/3] net: openvswitch: remove the unnecessary check X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" From: Tonghao Zhang Before calling the ovs_meter_cmd_reply_stats, "meter" is checked, so don't check it agin in that function. Cc: Pravin B Shelar Cc: Andy Zhou Signed-off-by: Tonghao Zhang --- net/openvswitch/meter.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/net/openvswitch/meter.c b/net/openvswitch/meter.c index 5efd48e024f0..03b39b0eb4ea 100644 --- a/net/openvswitch/meter.c +++ b/net/openvswitch/meter.c @@ -212,12 +212,11 @@ static int ovs_meter_cmd_reply_stats(struct sk_buff *reply, u32 meter_id, if (nla_put_u32(reply, OVS_METER_ATTR_ID, meter_id)) goto error; - if (!meter) - return 0; - if (nla_put(reply, OVS_METER_ATTR_STATS, - sizeof(struct ovs_flow_stats), &meter->stats) || - nla_put_u64_64bit(reply, OVS_METER_ATTR_USED, meter->used, + sizeof(struct ovs_flow_stats), &meter->stats)) + goto error; + + if (nla_put_u64_64bit(reply, OVS_METER_ATTR_USED, meter->used, OVS_METER_ATTR_PAD)) goto error;