From nobody Wed Dec 4 08:21:26 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0751E82C60 for ; Thu, 7 Nov 2024 06:45:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730961953; cv=none; b=LqyIW7e0Zxysb93yEDcQsZ20Qlx08347f1I3Vdo1r98bmZkanHHUkLT2UMGlRLY/HQob2Y3PE6RAQTuAYz568CRike9ZgVFtsNmpESCR9VwfPn0MuWLDbmM8fr5lOz+MBMR4h5DwXyE/iXqjYek8cSNRC14Qzv2v9VtfOtvXAiA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730961953; c=relaxed/simple; bh=WfIZ9uToCcB1m4Rnuq5v917T8euXNkcfx3sAa9zHLSc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QdoO5FFBYi4FTfoBSWt38W0BXhWo1lAJz7g4d2wGeULfP7QgbMWsxJujXMhgDgpudlhx7jCas8BiNqYP8zlzkmH3TOCjxVueIUUyMmDBgMGEvcoO9Hop9Bqzy/U3nt/186bE8ZCiDJ4j2/uku2KSy6NbKQ6phkW8Z3puyGLyRXo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=C5RaWm9h; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="C5RaWm9h" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5A109C4CED2; Thu, 7 Nov 2024 06:45:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1730961952; bh=WfIZ9uToCcB1m4Rnuq5v917T8euXNkcfx3sAa9zHLSc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=C5RaWm9hJ90vwiPKJN/LYnbqJHp9lPEFBZqqEtFfKvq5W+agvM59J8YqZE9JVX4VM 6gukhQVIEbbif9BiBRvJrRX89astw0LN+2xed9eGk/6sO1GsRBhRB4IVQ0ciWW2zHT cUsMCrG4NFH6N5KC0riBoe2hnOPfAY9xgU7aNi8GPqA6z3BK/1PgNfHjuZvUxdb9nB +hyCElgfeZN1ivKSfS+RmNI8W8SSxgLL1spc2h81nN+VcZbzGkKxnHkwo/WtECKmV7 xBwOZUlQ1+UTwAZILiywc5tnuNIC/ENuHtjNZhiEvxWTnbDDLt9dUDGR/1C7grANMG GslTiumREpzLA== From: Geliang Tang To: mptcp@lists.linux.dev Cc: Geliang Tang Subject: [PATCH mptcp-next v3 1/9] mptcp: add mptcp_userspace_pm_lookup_addr helper Date: Thu, 7 Nov 2024 14:45:35 +0800 Message-ID: <3e3198879157a2b94642537d95028643da257c0d.1730961809.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Geliang Tang Like __lookup_addr() helper in pm_netlink.c, a new helper mptcp_userspace_pm_lookup_addr() is also defined in pm_userspace.c. It looks up the corresponding mptcp_pm_addr_entry address in userspace_pm_local_addr_list through the passed "addr" parameter and returns the found address entry. This helper can be used in mptcp_userspace_pm_delete_local_addr(), mptcp_userspace_pm_set_flags(), mptcp_userspace_pm_get_local_id() and mptcp_userspace_pm_is_backup() to simplify the code. Please note that with this change now list_for_each_entry() is used in mptcp_userspace_pm_append_new_local_addr(), not list_for_each_entry_safe(), but that's OK to do so because mptcp_userspace_pm_lookup_addr() only returns an entry from the list, the list hasn't been modified here. Signed-off-by: Geliang Tang --- net/mptcp/pm_userspace.c | 71 ++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/net/mptcp/pm_userspace.c b/net/mptcp/pm_userspace.c index e35178f5205f..3664f3c1572e 100644 --- a/net/mptcp/pm_userspace.c +++ b/net/mptcp/pm_userspace.c @@ -26,6 +26,19 @@ void mptcp_free_local_addr_list(struct mptcp_sock *msk) } } =20 +static struct mptcp_pm_addr_entry * +mptcp_userspace_pm_lookup_addr(struct mptcp_sock *msk, + const struct mptcp_addr_info *addr) +{ + struct mptcp_pm_addr_entry *entry; + + list_for_each_entry(entry, &msk->pm.userspace_pm_local_addr_list, list) { + if (mptcp_addresses_equal(&entry->addr, addr, false)) + return entry; + } + return NULL; +} + static int mptcp_userspace_pm_append_new_local_addr(struct mptcp_sock *msk, struct mptcp_pm_addr_entry *entry, bool needs_id) @@ -90,22 +103,20 @@ static int mptcp_userspace_pm_append_new_local_addr(st= ruct mptcp_sock *msk, static int mptcp_userspace_pm_delete_local_addr(struct mptcp_sock *msk, struct mptcp_pm_addr_entry *addr) { - struct mptcp_pm_addr_entry *entry, *tmp; struct sock *sk =3D (struct sock *)msk; + struct mptcp_pm_addr_entry *entry; =20 - list_for_each_entry_safe(entry, tmp, &msk->pm.userspace_pm_local_addr_lis= t, list) { - if (mptcp_addresses_equal(&entry->addr, &addr->addr, false)) { - /* TODO: a refcount is needed because the entry can - * be used multiple times (e.g. fullmesh mode). - */ - list_del_rcu(&entry->list); - sock_kfree_s(sk, entry, sizeof(*entry)); - msk->pm.local_addr_used--; - return 0; - } - } - - return -EINVAL; + entry =3D mptcp_userspace_pm_lookup_addr(msk, &addr->addr); + if (!entry) + return -EINVAL; + + /* TODO: a refcount is needed because the entry can + * be used multiple times (e.g. fullmesh mode). + */ + list_del_rcu(&entry->list); + sock_kfree_s(sk, entry, sizeof(*entry)); + msk->pm.local_addr_used--; + return 0; } =20 static struct mptcp_pm_addr_entry * @@ -123,17 +134,12 @@ mptcp_userspace_pm_lookup_addr_by_id(struct mptcp_soc= k *msk, unsigned int id) int mptcp_userspace_pm_get_local_id(struct mptcp_sock *msk, struct mptcp_addr_info *skc) { - struct mptcp_pm_addr_entry *entry =3D NULL, *e, new_entry; + struct mptcp_pm_addr_entry *entry =3D NULL, new_entry; __be16 msk_sport =3D ((struct inet_sock *) inet_sk((struct sock *)msk))->inet_sport; =20 spin_lock_bh(&msk->pm.lock); - list_for_each_entry(e, &msk->pm.userspace_pm_local_addr_list, list) { - if (mptcp_addresses_equal(&e->addr, skc, false)) { - entry =3D e; - break; - } - } + entry =3D mptcp_userspace_pm_lookup_addr(msk, skc); spin_unlock_bh(&msk->pm.lock); if (entry) return entry->addr.id; @@ -153,15 +159,11 @@ bool mptcp_userspace_pm_is_backup(struct mptcp_sock *= msk, struct mptcp_addr_info *skc) { struct mptcp_pm_addr_entry *entry; - bool backup =3D false; + bool backup; =20 spin_lock_bh(&msk->pm.lock); - list_for_each_entry(entry, &msk->pm.userspace_pm_local_addr_list, list) { - if (mptcp_addresses_equal(&entry->addr, skc, false)) { - backup =3D !!(entry->flags & MPTCP_PM_ADDR_FLAG_BACKUP); - break; - } - } + entry =3D mptcp_userspace_pm_lookup_addr(msk, skc); + backup =3D entry && !!(entry->flags & MPTCP_PM_ADDR_FLAG_BACKUP); spin_unlock_bh(&msk->pm.lock); =20 return backup; @@ -606,13 +608,12 @@ int mptcp_userspace_pm_set_flags(struct sk_buff *skb,= struct genl_info *info) bkup =3D 1; =20 spin_lock_bh(&msk->pm.lock); - list_for_each_entry(entry, &msk->pm.userspace_pm_local_addr_list, list) { - if (mptcp_addresses_equal(&entry->addr, &loc.addr, false)) { - if (bkup) - entry->flags |=3D MPTCP_PM_ADDR_FLAG_BACKUP; - else - entry->flags &=3D ~MPTCP_PM_ADDR_FLAG_BACKUP; - } + entry =3D mptcp_userspace_pm_lookup_addr(msk, &loc.addr); + if (entry) { + if (bkup) + entry->flags |=3D MPTCP_PM_ADDR_FLAG_BACKUP; + else + entry->flags &=3D ~MPTCP_PM_ADDR_FLAG_BACKUP; } spin_unlock_bh(&msk->pm.lock); =20 --=20 2.45.2