diff mbox series

[mptcp-next,v2,20/36] mptcp: add loc and rem for set_flags

Message ID 0941887ad2ba9451d89c0409b40b945d57a4c56c.1729588019.git.tanggeliang@kylinos.cn (mailing list archive)
State Superseded, archived
Delegated to: Matthieu Baerts
Headers show
Series BPF path manager | expand

Checks

Context Check Description
matttbe/checkpatch success total: 0 errors, 0 warnings, 0 checks, 169 lines checked
matttbe/shellcheck success MPTCP selftests files have not been modified
matttbe/build warning Build error with: make C=1 net/mptcp/bpf.o
matttbe/KVM_Validation__normal success Success! ✅
matttbe/KVM_Validation__debug success Success! ✅
matttbe/KVM_Validation__btf-normal__only_bpftest_all_ success Success! ✅
matttbe/KVM_Validation__btf-debug__only_bpftest_all_ success Success! ✅

Commit Message

Geliang Tang Oct. 22, 2024, 9:14 a.m. UTC
From: Geliang Tang <tanggeliang@kylinos.cn>

This patch updates the interfaces set_flags to reduce repetitive code,
adds two more parameters "loc" and "rem" for them. These addresses are
parsed in public helper mptcp_pm_nl_set_flags_doit(), then pass them
to mptcp_pm_nl_set_flags() and mptcp_userspace_pm_set_flags().

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
 net/mptcp/pm_netlink.c   | 57 ++++++++++++++++++++++++----------------
 net/mptcp/pm_userspace.c | 28 ++++++--------------
 net/mptcp/protocol.h     |  4 ++-
 3 files changed, 46 insertions(+), 43 deletions(-)

Comments

Matthieu Baerts (NGI0) Nov. 4, 2024, 6:36 p.m. UTC | #1
Hi Geliang,

On 22/10/2024 11:14, Geliang Tang wrote:
> From: Geliang Tang <tanggeliang@kylinos.cn>
> 
> This patch updates the interfaces set_flags to reduce repetitive code,
> adds two more parameters "loc" and "rem" for them. These addresses are
> parsed in public helper mptcp_pm_nl_set_flags_doit(), then pass them
> to mptcp_pm_nl_set_flags() and mptcp_userspace_pm_set_flags().
> 
> Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
> ---
>  net/mptcp/pm_netlink.c   | 57 ++++++++++++++++++++++++----------------
>  net/mptcp/pm_userspace.c | 28 ++++++--------------
>  net/mptcp/protocol.h     |  4 ++-
>  3 files changed, 46 insertions(+), 43 deletions(-)
> 
> diff --git a/net/mptcp/pm_netlink.c b/net/mptcp/pm_netlink.c
> index 5fb1d5d8bf57..5cd5241da3c6 100644
> --- a/net/mptcp/pm_netlink.c
> +++ b/net/mptcp/pm_netlink.c
> @@ -2045,10 +2045,10 @@ static int mptcp_nl_set_flags(struct net *net,
>  	return ret;
>  }
>  
> -static int mptcp_pm_nl_set_flags(struct genl_info *info)
> +static int mptcp_pm_nl_set_flags(struct mptcp_pm_addr_entry *loc,
> +				 struct mptcp_addr_info *rem,
> +				 struct genl_info *info)
>  {
> -	struct mptcp_pm_addr_entry addr = { .addr = { .family = AF_UNSPEC }, };
> -	struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
>  	u8 changed, mask = MPTCP_PM_ADDR_FLAG_BACKUP |
>  			   MPTCP_PM_ADDR_FLAG_FULLMESH;
>  	struct net *net = genl_info_net(info);
> @@ -2056,59 +2056,72 @@ static int mptcp_pm_nl_set_flags(struct genl_info *info)
>  	struct pm_nl_pernet *pernet;
>  	u8 lookup_by_id = 0;
>  	u8 bkup = 0;
> -	int ret;
>  
>  	pernet = pm_nl_get_pernet(net);
>  
> -	ret = mptcp_pm_parse_entry(attr, info, false, &addr);
> -	if (ret < 0)
> -		return ret;
> -
> -	if (addr.addr.family == AF_UNSPEC) {
> +	if (loc->addr.family == AF_UNSPEC) {
>  		lookup_by_id = 1;
> -		if (!addr.addr.id) {
> +		if (!loc->addr.id) {
>  			GENL_SET_ERR_MSG(info, "missing required inputs");
>  			return -EOPNOTSUPP;
>  		}
>  	}
>  
> -	if (addr.flags & MPTCP_PM_ADDR_FLAG_BACKUP)
> +	if (loc->flags & MPTCP_PM_ADDR_FLAG_BACKUP)
>  		bkup = 1;
>  
>  	spin_lock_bh(&pernet->lock);
> -	entry = lookup_by_id ? __lookup_addr_by_id(pernet, addr.addr.id) :
> -			       __lookup_addr(pernet, &addr.addr);
> +	entry = lookup_by_id ? __lookup_addr_by_id(pernet, loc->addr.id) :
> +			       __lookup_addr(pernet, &loc->addr);
>  	if (!entry) {
>  		spin_unlock_bh(&pernet->lock);
>  		GENL_SET_ERR_MSG(info, "address not found");
>  		return -EINVAL;
>  	}
> -	if ((addr.flags & MPTCP_PM_ADDR_FLAG_FULLMESH) &&
> +	if ((loc->flags & MPTCP_PM_ADDR_FLAG_FULLMESH) &&
>  	    (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) {
>  		spin_unlock_bh(&pernet->lock);
>  		GENL_SET_ERR_MSG(info, "invalid addr flags");
>  		return -EINVAL;
>  	}
>  
> -	changed = (addr.flags ^ entry->flags) & mask;
> -	entry->flags = (entry->flags & ~mask) | (addr.flags & mask);
> -	addr = *entry;
> +	changed = (loc->flags ^ entry->flags) & mask;
> +	entry->flags = (entry->flags & ~mask) | (loc->flags & mask);
> +	*loc = *entry;
>  	spin_unlock_bh(&pernet->lock);
>  
> -	mptcp_nl_set_flags(net, &addr.addr, bkup, changed);
> +	mptcp_nl_set_flags(net, &loc->addr, bkup, changed);
>  	return 0;
>  }
>  
> -static int mptcp_pm_set_flags(struct genl_info *info)
> +static int mptcp_pm_set_flags(struct mptcp_pm_addr_entry *loc,
> +			      struct mptcp_addr_info *rem,
> +			      struct genl_info *info)
>  {
>  	if (info->attrs[MPTCP_PM_ATTR_TOKEN])
> -		return mptcp_userspace_pm_set_flags(info);
> -	return mptcp_pm_nl_set_flags(info);
> +		return mptcp_userspace_pm_set_flags(loc, rem, info);
> +	return mptcp_pm_nl_set_flags(loc, rem, info);
>  }
>  
>  int mptcp_pm_nl_set_flags_doit(struct sk_buff *skb, struct genl_info *info)
>  {
> -	return mptcp_pm_set_flags(info);
> +	struct mptcp_pm_addr_entry loc = { .addr = { .family = AF_UNSPEC }, };
> +	struct nlattr *attr_rem = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE];
> +	struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];

(detail: maybe clearer to call it "attr_loc", similar to "attr_rem")

> +	struct mptcp_addr_info rem = { .family = AF_UNSPEC, };
> +	int ret;
> +
> +	ret = mptcp_pm_parse_entry(attr, info, false, &loc);
> +	if (ret < 0)
> +		return ret;
> +
> +	if (attr_rem) {
> +		ret = mptcp_pm_parse_addr(attr_rem, info, &rem);
> +		if (ret < 0)
> +			return ret;
> +	}
> +
> +	return mptcp_pm_set_flags(&loc, &rem, info);

Nothing to change here, but linked to a previous comment for another
patch of this series: I think it would be clearer to move
mptcp_pm_nl_set_flags_doit() and similar (mptcp_pm_nl_get_addr_dumpit()
+ mptcp_pm_nl_get_addr_doit()) to pm.c: these helpers are generic,
implemented for each PM.

Similarly, we can move the content of their linked function
(mptcp_pm_set_flags(), mptcp_pm_dump_addr() and mptcp_pm_get_addr()) in
there. If this last step makes sense for the future changes of course,
but for the moment, these helpers are only used in one place, just next
to, and only 3 basic lines: 'if (X) return A; return B'.

>  }
>  
>  static void mptcp_nl_mcast_send(struct net *net, struct sk_buff *nlskb, gfp_t gfp)
(...)

Cheers,
Matt
diff mbox series

Patch

diff --git a/net/mptcp/pm_netlink.c b/net/mptcp/pm_netlink.c
index 5fb1d5d8bf57..5cd5241da3c6 100644
--- a/net/mptcp/pm_netlink.c
+++ b/net/mptcp/pm_netlink.c
@@ -2045,10 +2045,10 @@  static int mptcp_nl_set_flags(struct net *net,
 	return ret;
 }
 
-static int mptcp_pm_nl_set_flags(struct genl_info *info)
+static int mptcp_pm_nl_set_flags(struct mptcp_pm_addr_entry *loc,
+				 struct mptcp_addr_info *rem,
+				 struct genl_info *info)
 {
-	struct mptcp_pm_addr_entry addr = { .addr = { .family = AF_UNSPEC }, };
-	struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
 	u8 changed, mask = MPTCP_PM_ADDR_FLAG_BACKUP |
 			   MPTCP_PM_ADDR_FLAG_FULLMESH;
 	struct net *net = genl_info_net(info);
@@ -2056,59 +2056,72 @@  static int mptcp_pm_nl_set_flags(struct genl_info *info)
 	struct pm_nl_pernet *pernet;
 	u8 lookup_by_id = 0;
 	u8 bkup = 0;
-	int ret;
 
 	pernet = pm_nl_get_pernet(net);
 
-	ret = mptcp_pm_parse_entry(attr, info, false, &addr);
-	if (ret < 0)
-		return ret;
-
-	if (addr.addr.family == AF_UNSPEC) {
+	if (loc->addr.family == AF_UNSPEC) {
 		lookup_by_id = 1;
-		if (!addr.addr.id) {
+		if (!loc->addr.id) {
 			GENL_SET_ERR_MSG(info, "missing required inputs");
 			return -EOPNOTSUPP;
 		}
 	}
 
-	if (addr.flags & MPTCP_PM_ADDR_FLAG_BACKUP)
+	if (loc->flags & MPTCP_PM_ADDR_FLAG_BACKUP)
 		bkup = 1;
 
 	spin_lock_bh(&pernet->lock);
-	entry = lookup_by_id ? __lookup_addr_by_id(pernet, addr.addr.id) :
-			       __lookup_addr(pernet, &addr.addr);
+	entry = lookup_by_id ? __lookup_addr_by_id(pernet, loc->addr.id) :
+			       __lookup_addr(pernet, &loc->addr);
 	if (!entry) {
 		spin_unlock_bh(&pernet->lock);
 		GENL_SET_ERR_MSG(info, "address not found");
 		return -EINVAL;
 	}
-	if ((addr.flags & MPTCP_PM_ADDR_FLAG_FULLMESH) &&
+	if ((loc->flags & MPTCP_PM_ADDR_FLAG_FULLMESH) &&
 	    (entry->flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) {
 		spin_unlock_bh(&pernet->lock);
 		GENL_SET_ERR_MSG(info, "invalid addr flags");
 		return -EINVAL;
 	}
 
-	changed = (addr.flags ^ entry->flags) & mask;
-	entry->flags = (entry->flags & ~mask) | (addr.flags & mask);
-	addr = *entry;
+	changed = (loc->flags ^ entry->flags) & mask;
+	entry->flags = (entry->flags & ~mask) | (loc->flags & mask);
+	*loc = *entry;
 	spin_unlock_bh(&pernet->lock);
 
-	mptcp_nl_set_flags(net, &addr.addr, bkup, changed);
+	mptcp_nl_set_flags(net, &loc->addr, bkup, changed);
 	return 0;
 }
 
-static int mptcp_pm_set_flags(struct genl_info *info)
+static int mptcp_pm_set_flags(struct mptcp_pm_addr_entry *loc,
+			      struct mptcp_addr_info *rem,
+			      struct genl_info *info)
 {
 	if (info->attrs[MPTCP_PM_ATTR_TOKEN])
-		return mptcp_userspace_pm_set_flags(info);
-	return mptcp_pm_nl_set_flags(info);
+		return mptcp_userspace_pm_set_flags(loc, rem, info);
+	return mptcp_pm_nl_set_flags(loc, rem, info);
 }
 
 int mptcp_pm_nl_set_flags_doit(struct sk_buff *skb, struct genl_info *info)
 {
-	return mptcp_pm_set_flags(info);
+	struct mptcp_pm_addr_entry loc = { .addr = { .family = AF_UNSPEC }, };
+	struct nlattr *attr_rem = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE];
+	struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
+	struct mptcp_addr_info rem = { .family = AF_UNSPEC, };
+	int ret;
+
+	ret = mptcp_pm_parse_entry(attr, info, false, &loc);
+	if (ret < 0)
+		return ret;
+
+	if (attr_rem) {
+		ret = mptcp_pm_parse_addr(attr_rem, info, &rem);
+		if (ret < 0)
+			return ret;
+	}
+
+	return mptcp_pm_set_flags(&loc, &rem, info);
 }
 
 static void mptcp_nl_mcast_send(struct net *net, struct sk_buff *nlskb, gfp_t gfp)
diff --git a/net/mptcp/pm_userspace.c b/net/mptcp/pm_userspace.c
index 4db39bf26cf4..aa4a5d110376 100644
--- a/net/mptcp/pm_userspace.c
+++ b/net/mptcp/pm_userspace.c
@@ -537,12 +537,10 @@  int mptcp_pm_nl_subflow_destroy_doit(struct sk_buff *skb, struct genl_info *info
 	return err;
 }
 
-int mptcp_userspace_pm_set_flags(struct genl_info *info)
+int mptcp_userspace_pm_set_flags(struct mptcp_pm_addr_entry *loc,
+				 struct mptcp_addr_info *rem,
+				 struct genl_info *info)
 {
-	struct mptcp_pm_addr_entry loc = { .addr = { .family = AF_UNSPEC }, };
-	struct nlattr *attr_rem = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE];
-	struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
-	struct mptcp_addr_info rem = { .family = AF_UNSPEC, };
 	struct mptcp_pm_addr_entry *entry;
 	struct mptcp_sock *msk;
 	int ret = -EINVAL;
@@ -555,28 +553,18 @@  int mptcp_userspace_pm_set_flags(struct genl_info *info)
 
 	sk = (struct sock *)msk;
 
-	ret = mptcp_pm_parse_entry(attr, info, false, &loc);
-	if (ret < 0)
-		goto set_flags_err;
-
-	if (attr_rem) {
-		ret = mptcp_pm_parse_addr(attr_rem, info, &rem);
-		if (ret < 0)
-			goto set_flags_err;
-	}
-
-	if (loc.addr.family == AF_UNSPEC ||
-	    rem.family == AF_UNSPEC) {
+	if (loc->addr.family == AF_UNSPEC ||
+	    rem->family == AF_UNSPEC) {
 		GENL_SET_ERR_MSG(info, "invalid address families");
 		ret = -EINVAL;
 		goto set_flags_err;
 	}
 
-	if (loc.flags & MPTCP_PM_ADDR_FLAG_BACKUP)
+	if (loc->flags & MPTCP_PM_ADDR_FLAG_BACKUP)
 		bkup = 1;
 
 	spin_lock_bh(&msk->pm.lock);
-	entry = mptcp_userspace_pm_lookup_addr(msk, &loc.addr);
+	entry = mptcp_userspace_pm_lookup_addr(msk, &loc->addr);
 	if (entry) {
 		if (bkup)
 			entry->flags |= MPTCP_PM_ADDR_FLAG_BACKUP;
@@ -586,7 +574,7 @@  int mptcp_userspace_pm_set_flags(struct genl_info *info)
 	spin_unlock_bh(&msk->pm.lock);
 
 	lock_sock(sk);
-	ret = mptcp_pm_nl_mp_prio_send_ack(msk, &loc.addr, &rem, bkup);
+	ret = mptcp_pm_nl_mp_prio_send_ack(msk, &loc->addr, rem, bkup);
 	release_sock(sk);
 
 set_flags_err:
diff --git a/net/mptcp/protocol.h b/net/mptcp/protocol.h
index 29e39a790409..25d2d3def17a 100644
--- a/net/mptcp/protocol.h
+++ b/net/mptcp/protocol.h
@@ -1040,7 +1040,9 @@  mptcp_pm_del_add_timer(struct mptcp_sock *msk,
 struct mptcp_pm_add_entry *
 mptcp_lookup_anno_list_by_saddr(const struct mptcp_sock *msk,
 				const struct mptcp_addr_info *addr);
-int mptcp_userspace_pm_set_flags(struct genl_info *info);
+int mptcp_userspace_pm_set_flags(struct mptcp_pm_addr_entry *loc,
+				 struct mptcp_addr_info *rem,
+				 struct genl_info *info);
 int mptcp_pm_announce_addr(struct mptcp_sock *msk,
 			   const struct mptcp_addr_info *addr,
 			   bool echo);