Skip to contentSkip to conversation

bpf: bpf_{g,s}etsockopt for struct bpf_sock

PATCH Superseded Stanislav Fomichev 4 participants 4 messages Apr 28, 2020 – Apr 29, 2020

Subsystems (6)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: bpf_{g,s}etsockopt for struct bpf_sock

Currently, bpf_getsocktop and bpf_setsockopt helpers operate on the 'struct bpf_sock_ops' context in BPF_PROG_TYPE_CGROUP_SOCKOPT program. Let's generalize them and make the first argument be 'struct bpf_sock'. That way, in the future, we can allow those helpers in more places.

BPF_PROG_TYPE_CGROUP_SOCKOPT still has the existing helpers that operate on 'struct bpf_sock_ops', but we add new bpf_{g,s}etsockopt that work on 'struct bpf_sock'. [Alternatively, for BPF_PROG_TYPE_CGROUP_SOCKOPT, we can enable them both and teach verifier to pick the right one based on the context (bpf_sock_ops vs bpf_sock).]

As an example, let's allow those 'struct bpf_sock' based helpers to be called from the BPF_CGROUP_INET{4,6}_CONNECT hooks. That way we can override CC before the connection is made.

---
 include/uapi/linux/bpf.h                      |  12 +-
 net/core/filter.c                             | 113 ++++++++++++++----
 tools/include/uapi/linux/bpf.h                |  12 +-
 tools/testing/selftests/bpf/config            |   1 +
 .../selftests/bpf/progs/connect4_prog.c       |  46 +++++++
 5 files changed, 159 insertions(+), 25 deletions(-)
include/uapi/linux/bpf.h +10 -2
index 4a6c47f3febe..68900c32d05f 100644
@@ -1564,7 +1564,7 @@ union bpf_attr {
  * 	Return
  * 		0
  *
- * int bpf_setsockopt(struct bpf_sock_ops *bpf_socket, int level, int optname, void *optval, int optlen)
+ * int bpf_setsockopt(void *bpf_socket, int level, int optname, void *optval, int optlen)
  * 	Description
  * 		Emulate a call to **setsockopt()** on the socket associated to
  * 		*bpf_socket*, which must be a full socket. The *level* at
@@ -1572,6 +1572,10 @@ union bpf_attr {
  * 		must be specified, see **setsockopt(2)** for more information.
  * 		The option value of length *optlen* is pointed by *optval*.
  *
+ * 		*bpf_socket* should be one of the following:
+ * 		* **struct bpf_sock_ops** for **BPF_PROG_TYPE_CGROUP_SOCKOPT**.
+ * 		* **struct bpf_sock** for the other types.
+ *
  * 		This helper actually implements a subset of **setsockopt()**.
  * 		It supports the following *level*\ s:
  *
@@ -1766,7 +1770,7 @@ union bpf_attr {
  * 	Return
  * 		0 on success, or a negative error in case of failure.
  *
- * int bpf_getsockopt(struct bpf_sock_ops *bpf_socket, int level, int optname, void *optval, int optlen)
+ * int bpf_getsockopt(void *bpf_socket, int level, int optname, void *optval, int optlen)
  * 	Description
  * 		Emulate a call to **getsockopt()** on the socket associated to
  * 		*bpf_socket*, which must be a full socket. The *level* at
@@ -1775,6 +1779,10 @@ union bpf_attr {
  * 		The retrieved value is stored in the structure pointed by
  * 		*opval* and of length *optlen*.
  *
+ * 		*bpf_socket* should be one of the following:
+ * 		* **struct bpf_sock_ops** for **BPF_PROG_TYPE_CGROUP_SOCKOPT**.
+ * 		* **struct bpf_sock** for the other types.
+ *
  * 		This helper actually implements a subset of **getsockopt()**.
  * 		It supports the following *level*\ s:
  *
net/core/filter.c +92 -21

164 lines not on this page · raw message

tools/include/uapi/linux/bpf.h +10 -2
index 4a6c47f3febe..68900c32d05f 100644
@@ -1564,7 +1564,7 @@ union bpf_attr {
  * 	Return
  * 		0
  *
- * int bpf_setsockopt(struct bpf_sock_ops *bpf_socket, int level, int optname, void *optval, int optlen)
+ * int bpf_setsockopt(void *bpf_socket, int level, int optname, void *optval, int optlen)
  * 	Description
  * 		Emulate a call to **setsockopt()** on the socket associated to
  * 		*bpf_socket*, which must be a full socket. The *level* at
@@ -1572,6 +1572,10 @@ union bpf_attr {
  * 		must be specified, see **setsockopt(2)** for more information.
  * 		The option value of length *optlen* is pointed by *optval*.
  *
+ * 		*bpf_socket* should be one of the following:
+ * 		* **struct bpf_sock_ops** for **BPF_PROG_TYPE_CGROUP_SOCKOPT**.
+ * 		* **struct bpf_sock** for the other types.
+ *
  * 		This helper actually implements a subset of **setsockopt()**.
  * 		It supports the following *level*\ s:
  *
@@ -1766,7 +1770,7 @@ union bpf_attr {
  * 	Return
  * 		0 on success, or a negative error in case of failure.
  *
- * int bpf_getsockopt(struct bpf_sock_ops *bpf_socket, int level, int optname, void *optval, int optlen)
+ * int bpf_getsockopt(void *bpf_socket, int level, int optname, void *optval, int optlen)
  * 	Description
  * 		Emulate a call to **getsockopt()** on the socket associated to
  * 		*bpf_socket*, which must be a full socket. The *level* at
@@ -1775,6 +1779,10 @@ union bpf_attr {
  * 		The retrieved value is stored in the structure pointed by
  * 		*opval* and of length *optlen*.
  *
+ * 		*bpf_socket* should be one of the following:
+ * 		* **struct bpf_sock_ops** for **BPF_PROG_TYPE_CGROUP_SOCKOPT**.
+ * 		* **struct bpf_sock** for the other types.
+ *
  * 		This helper actually implements a subset of **getsockopt()**.
  * 		It supports the following *level*\ s:
  *
tools/testing/selftests/bpf/config +1 -0
index 60e3ae5d4e48..6e5b94c036ca 100644
@@ -37,3 +37,4 @@ CONFIG_IPV6_SIT=m
 CONFIG_BPF_JIT=y
 CONFIG_BPF_LSM=y
 CONFIG_SECURITY=y
+CONFIG_TCP_CONG_DCTCP=y
tools/testing/selftests/bpf/progs/connect4_prog.c +46 -0

70 lines not on this page · raw message

Signature · 1 line
2.26.2.303.gf8c07b1a785-goog
#1 lore raw
Quoted 11 lines · Stanislav Fomichev

Currently, bpf_getsocktop and bpf_setsockopt helpers operate on the 'struct bpf_sock_ops' context in BPF_PROG_TYPE_CGROUP_SOCKOPT program. Let's generalize them and make the first argument be 'struct bpf_sock'. That way, in the future, we can allow those helpers in more places.

BPF_PROG_TYPE_CGROUP_SOCKOPT still has the existing helpers that operate on 'struct bpf_sock_ops', but we add new bpf_{g,s}etsockopt that work on 'struct bpf_sock'. [Alternatively, for BPF_PROG_TYPE_CGROUP_SOCKOPT, we can enable them both and teach verifier to pick the right one based on the context (bpf_sock_ops vs bpf_sock).]

Quoted 5 lines

As an example, let's allow those 'struct bpf_sock' based helpers to be called from the BPF_CGROUP_INET{4,6}_CONNECT hooks. That way we can override CC before the connection is made.

Looks good to me and safety checks seem to be correct. Martin,
could you please help review as well?

reviewer Acked-by on v1 1/1
#2 lore raw
In reply to Stanislav Fomichev
Quoted 11 lines · Stanislav Fomichev

Currently, bpf_getsocktop and bpf_setsockopt helpers operate on the 'struct bpf_sock_ops' context in BPF_PROG_TYPE_CGROUP_SOCKOPT program. Let's generalize them and make the first argument be 'struct bpf_sock'. That way, in the future, we can allow those helpers in more places.

BPF_PROG_TYPE_CGROUP_SOCKOPT still has the existing helpers that operate on 'struct bpf_sock_ops', but we add new bpf_{g,s}etsockopt that work on 'struct bpf_sock'. [Alternatively, for BPF_PROG_TYPE_CGROUP_SOCKOPT, we can enable them both and teach verifier to pick the right one based on the context (bpf_sock_ops vs bpf_sock).]

Quoted 6 lines

As an example, let's allow those 'struct bpf_sock' based helpers to be called from the BPF_CGROUP_INET{4,6}_CONNECT hooks. That way we can override CC before the connection is made.

---

LGTM.

#3 lore raw
In reply to Stanislav Fomichev
Quoted 4 lines · Stanislav Fomichev

Currently, bpf_getsocktop and bpf_setsockopt helpers operate on the 'struct bpf_sock_ops' context in BPF_PROG_TYPE_CGROUP_SOCKOPT program. Let's generalize them and make the first argument be 'struct bpf_sock'. That way, in the future, we can allow those helpers in more places.

s/BPF_PROG_TYPE_CGROUP_SOCKOPT/BPF_PROG_TYPE_SOCK_OPS/

Same for the other uses in the commit message and also the document comment in the uapi (and tools) bpf.h.

Others LGTM.

Quoted 12 lines

BPF_PROG_TYPE_CGROUP_SOCKOPT still has the existing helpers that operate on 'struct bpf_sock_ops', but we add new bpf_{g,s}etsockopt that work on 'struct bpf_sock'. [Alternatively, for BPF_PROG_TYPE_CGROUP_SOCKOPT, we can enable them both and teach verifier to pick the right one based on the context (bpf_sock_ops vs bpf_sock).]

As an example, let's allow those 'struct bpf_sock' based helpers to be called from the BPF_CGROUP_INET{4,6}_CONNECT hooks. That way we can override CC before the connection is made.