From: zhangmingyi <zhangmingyi5@huawei.com>
To: <ast@kernel.org>, <daniel@iogearbox.net>, <andrii@kernel.org>,
<martin.lau@linux.dev>, <song@kernel.org>, <yhs@fb.com>,
<john.fastabend@gmail.com>, <kpsingh@kernel.org>,
<sdf@google.com>, <haoluo@google.com>, <jolsa@kernel.org>
Cc: <bpf@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
<yanan@huawei.com>, <wuchangye@huawei.com>,
<xiesongyang@huawei.com>, <liuxin350@huawei.com>,
<liwei883@huawei.com>, <tianmuyang@huawei.com>,
<zhangmingyi5@huawei.com>
Subject: [PATCH bpf-next v3 1/2] Introduced to support the ULP to get or set sockets
Date: Fri, 28 Feb 2025 15:06:27 +0800 [thread overview]
Message-ID: <20250228070628.3219087-2-zhangmingyi5@huawei.com> (raw)
In-Reply-To: <20250228070628.3219087-1-zhangmingyi5@huawei.com>
From: Mingyi Zhang <zhangmingyi5@huawei.com>
Note that tcp_getsockopt and tcp_setsockopt support TCP_ULP, while
bpf_getsockopt and bpf_setsockopt do not support TCP_ULP.
I think we can add the handling of this case.
We want call bpf_setsockopt to replace the kernel module in the TCP_ULP
case. The purpose is to customize the behavior in connect and sendmsg.
We have an open source community project kmesh (kmesh.net). Based on
this, we refer to some processes of tcp fastopen to implement delayed
connet and perform HTTP DNAT when sendmsg.In this case, we need to parse
HTTP packets in the bpf program and set TCP_ULP for the specified socket.
Signed-off-by: Mingyi Zhang <zhangmingyi5@huawei.com>
Signed-off-by: Xin Liu <liuxin350@huawei.com>
---
include/net/tcp.h | 2 +-
net/core/filter.c | 1 +
net/ipv4/tcp.c | 2 +-
net/ipv4/tcp_ulp.c | 28 +++++++++++++++-------------
net/mptcp/subflow.c | 2 +-
5 files changed, 19 insertions(+), 16 deletions(-)
diff --git a/include/net/tcp.h b/include/net/tcp.h
index e9b37b76e894..f26e92099b86 100644
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -2582,7 +2582,7 @@ struct tcp_ulp_ops {
};
int tcp_register_ulp(struct tcp_ulp_ops *type);
void tcp_unregister_ulp(struct tcp_ulp_ops *type);
-int tcp_set_ulp(struct sock *sk, const char *name);
+int tcp_set_ulp(struct sock *sk, const char *name, bool load);
void tcp_get_available_ulp(char *buf, size_t len);
void tcp_cleanup_ulp(struct sock *sk);
void tcp_update_ulp(struct sock *sk, struct proto *p,
diff --git a/net/core/filter.c b/net/core/filter.c
index 713d6f454df3..bdb5c43d6fb0 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -5380,6 +5380,7 @@ static int sol_tcp_sockopt(struct sock *sk, int optname,
case TCP_CONGESTION:
return sol_tcp_sockopt_congestion(sk, optval, optlen, getopt);
case TCP_SAVED_SYN:
+ case TCP_ULP:
if (*optlen < 1)
return -EINVAL;
break;
diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index 0d704bda6c41..88ccd0e211f9 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -3744,7 +3744,7 @@ int do_tcp_setsockopt(struct sock *sk, int level, int optname,
name[val] = 0;
sockopt_lock_sock(sk);
- err = tcp_set_ulp(sk, name);
+ err = tcp_set_ulp(sk, name, !has_current_bpf_ctx());
sockopt_release_sock(sk);
return err;
}
diff --git a/net/ipv4/tcp_ulp.c b/net/ipv4/tcp_ulp.c
index 2aa442128630..9e828d4c2932 100644
--- a/net/ipv4/tcp_ulp.c
+++ b/net/ipv4/tcp_ulp.c
@@ -33,10 +33,7 @@ static struct tcp_ulp_ops *tcp_ulp_find(const char *name)
static const struct tcp_ulp_ops *__tcp_ulp_find_autoload(const char *name)
{
- const struct tcp_ulp_ops *ulp = NULL;
-
- rcu_read_lock();
- ulp = tcp_ulp_find(name);
+ const struct tcp_ulp_ops *ulp = tcp_ulp_find(name);
#ifdef CONFIG_MODULES
if (!ulp && capable(CAP_NET_ADMIN)) {
@@ -46,10 +43,6 @@ static const struct tcp_ulp_ops *__tcp_ulp_find_autoload(const char *name)
ulp = tcp_ulp_find(name);
}
#endif
- if (!ulp || !try_module_get(ulp->owner))
- ulp = NULL;
-
- rcu_read_unlock();
return ulp;
}
@@ -154,15 +147,24 @@ static int __tcp_set_ulp(struct sock *sk, const struct tcp_ulp_ops *ulp_ops)
return err;
}
-int tcp_set_ulp(struct sock *sk, const char *name)
+int tcp_set_ulp(struct sock *sk, const char *name, bool load)
{
const struct tcp_ulp_ops *ulp_ops;
+ int err = 0;
sock_owned_by_me(sk);
- ulp_ops = __tcp_ulp_find_autoload(name);
- if (!ulp_ops)
- return -ENOENT;
+ rcu_read_lock();
+ if (!load)
+ ulp_ops = tcp_ulp_find(name);
+ else
+ ulp_ops = __tcp_ulp_find_autoload(name);
+
+ if (!ulp_ops || !try_module_get(ulp_ops->owner))
+ err = -ENOENT;
+ rcu_read_unlock();
- return __tcp_set_ulp(sk, ulp_ops);
+ if (!err)
+ err = __tcp_set_ulp(sk, ulp_ops);
+ return err;
}
diff --git a/net/mptcp/subflow.c b/net/mptcp/subflow.c
index fd021cf8286e..fb936d280b83 100644
--- a/net/mptcp/subflow.c
+++ b/net/mptcp/subflow.c
@@ -1776,7 +1776,7 @@ int mptcp_subflow_create_socket(struct sock *sk, unsigned short family,
sf->sk->sk_net_refcnt = 1;
get_net_track(net, &sf->sk->ns_tracker, GFP_KERNEL);
sock_inuse_add(net, 1);
- err = tcp_set_ulp(sf->sk, "mptcp");
+ err = tcp_set_ulp(sf->sk, "mptcp", true);
if (err)
goto err_free;
--
2.43.0
next prev parent reply other threads:[~2025-02-28 7:09 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-28 7:06 [PATCH bpf-next v3 0/2] " zhangmingyi
2025-02-28 7:06 ` zhangmingyi [this message]
2025-02-28 7:06 ` [PATCH bpf-next v3 2/2] selftest for TCP_ULP in bpf_setsockopt zhangmingyi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250228070628.3219087-2-zhangmingyi5@huawei.com \
--to=zhangmingyi5@huawei.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=liuxin350@huawei.com \
--cc=liwei883@huawei.com \
--cc=martin.lau@linux.dev \
--cc=sdf@google.com \
--cc=song@kernel.org \
--cc=tianmuyang@huawei.com \
--cc=wuchangye@huawei.com \
--cc=xiesongyang@huawei.com \
--cc=yanan@huawei.com \
--cc=yhs@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®