[PATCH mptcp-next 03/17] bpf: add bpf_mptcp_sock_from_subflow helper

Geliang Tang posted 17 patches 3 years, 4 months ago
There is a newer version of this series
[PATCH mptcp-next 03/17] bpf: add bpf_mptcp_sock_from_subflow helper
Posted by Geliang Tang 3 years, 4 months ago
This patch implements the helper bpf_mptcp_sock_from_subflow(), to get
the MPTCP socket from a given TCP subflow socket.

Move the inline function mptcp_sk() from net/mptcp/protocol.h to
include/net/mptcp.h.

Signed-off-by: Geliang Tang <geliang.tang@suse.com>
---
 include/net/mptcp.h  | 18 ++++++++++++++++++
 net/mptcp/protocol.h |  5 -----
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/include/net/mptcp.h b/include/net/mptcp.h
index 9422b6d2a268..a267cff36197 100644
--- a/include/net/mptcp.h
+++ b/include/net/mptcp.h
@@ -287,6 +287,11 @@ mptcp_subflow_ctx(const struct sock *sk)
 	/* Use RCU on icsk_ulp_data only for sock diag code */
 	return (__force struct mptcp_subflow_context *)icsk->icsk_ulp_data;
 }
+
+static inline struct mptcp_sock *mptcp_sk(const struct sock *sk)
+{
+	return (struct mptcp_sock *)sk;
+}
 #else
 
 static inline void mptcp_init(void)
@@ -366,6 +371,7 @@ static inline int mptcp_subflow_init_cookie_req(struct request_sock *req,
 static inline __be32 mptcp_reset_option(const struct sk_buff *skb)  { return htonl(0u); }
 static inline struct mptcp_subflow_context *
 mptcp_subflow_ctx(const struct sock *sk) { return NULL; }
+static inline struct mptcp_sock *mptcp_sk(const struct sock *sk) { return NULL; }
 #endif /* CONFIG_MPTCP */
 
 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
@@ -376,4 +382,16 @@ static inline int mptcpv6_init(void) { return 0; }
 static inline void mptcpv6_handle_mapped(struct sock *sk, bool mapped) { }
 #endif
 
+#if defined(CONFIG_MPTCP) && defined(CONFIG_BPF_SYSCALL)
+static inline struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk)
+{
+	if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP && sk_is_mptcp(sk))
+		return mptcp_sk(mptcp_subflow_ctx(sk)->conn);
+
+	return NULL;
+}
+#else
+static inline struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk) { return NULL; }
+#endif
+
 #endif /* __NET_MPTCP_H */
diff --git a/net/mptcp/protocol.h b/net/mptcp/protocol.h
index cc24756cedfe..2fc8aaf5cb05 100644
--- a/net/mptcp/protocol.h
+++ b/net/mptcp/protocol.h
@@ -318,11 +318,6 @@ static inline void msk_owned_by_me(const struct mptcp_sock *msk)
 	sock_owned_by_me((const struct sock *)msk);
 }
 
-static inline struct mptcp_sock *mptcp_sk(const struct sock *sk)
-{
-	return (struct mptcp_sock *)sk;
-}
-
 /* the msk socket don't use the backlog, also account for the bulk
  * free memory
  */
-- 
2.34.1