diff mbox series

[bpf-next,v2] bpf: add tcp_notsent_lowat bpf setsockopt

Message ID 20201009070325.226855-1-tehnerd@tehnerd.com (mailing list archive)
State Accepted
Delegated to: BPF
Headers show
Series [bpf-next,v2] bpf: add tcp_notsent_lowat bpf setsockopt | expand

Commit Message

Nikita V. Shirokov Oct. 9, 2020, 7:03 a.m. UTC
Adding support for TCP_NOTSENT_LOWAT sockoption
(https://lwn.net/Articles/560082/) in tcpbpf

v1->v2:
- addressing yhs@ comments. explicitly defining TCP_NOTSENT_LOWAT in
  selftests if it is not defined in the system

Signed-off-by: Nikita V. Shirokov <tehnerd@tehnerd.com>
---
 include/uapi/linux/bpf.h                      |  2 +-
 net/core/filter.c                             |  4 ++++
 .../selftests/bpf/progs/connect4_prog.c       | 19 +++++++++++++++++++
 3 files changed, 24 insertions(+), 1 deletion(-)

Comments

Yonghong Song Oct. 9, 2020, 3:06 p.m. UTC | #1
On 10/9/20 12:03 AM, Nikita V. Shirokov wrote:
> Adding support for TCP_NOTSENT_LOWAT sockoption
> (https://lwn.net/Articles/560082/ ) in tcpbpf
> 
> v1->v2:
> - addressing yhs@ comments. explicitly defining TCP_NOTSENT_LOWAT in
>    selftests if it is not defined in the system
> 
> Signed-off-by: Nikita V. Shirokov <tehnerd@tehnerd.com>

Acked-by: Yonghong Song <yhs@fb.com>
Daniel Borkmann Oct. 9, 2020, 3:17 p.m. UTC | #2
On 10/9/20 5:06 PM, Yonghong Song wrote:
> On 10/9/20 12:03 AM, Nikita V. Shirokov wrote:
>> Adding support for TCP_NOTSENT_LOWAT sockoption
>> (https://lwn.net/Articles/560082/ ) in tcpbpf
>>
>> v1->v2:
>> - addressing yhs@ comments. explicitly defining TCP_NOTSENT_LOWAT in
>>    selftests if it is not defined in the system
>>
>> Signed-off-by: Nikita V. Shirokov <tehnerd@tehnerd.com>
> 
> Acked-by: Yonghong Song <yhs@fb.com>

Applied & also copied the include/uapi/linux/bpf.h over to the
tools/include/uapi/linux/bpf.h as well, thanks!
diff mbox series

Patch

diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index d83561e8cd2c..42d2df799397 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -1698,7 +1698,7 @@  union bpf_attr {
  * 		  **TCP_CONGESTION**, **TCP_BPF_IW**,
  * 		  **TCP_BPF_SNDCWND_CLAMP**, **TCP_SAVE_SYN**,
  * 		  **TCP_KEEPIDLE**, **TCP_KEEPINTVL**, **TCP_KEEPCNT**,
- * 		  **TCP_SYNCNT**, **TCP_USER_TIMEOUT**.
+ *		  **TCP_SYNCNT**, **TCP_USER_TIMEOUT**, **TCP_NOTSENT_LOWAT**.
  * 		* **IPPROTO_IP**, which supports *optname* **IP_TOS**.
  * 		* **IPPROTO_IPV6**, which supports *optname* **IPV6_TCLASS**.
  * 	Return
diff --git a/net/core/filter.c b/net/core/filter.c
index 05df73780dd3..5da44b11e1ec 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -4827,6 +4827,10 @@  static int _bpf_setsockopt(struct sock *sk, int level, int optname,
 				else
 					icsk->icsk_user_timeout = val;
 				break;
+			case TCP_NOTSENT_LOWAT:
+				tp->notsent_lowat = val;
+				sk->sk_write_space(sk);
+				break;
 			default:
 				ret = -EINVAL;
 			}
diff --git a/tools/testing/selftests/bpf/progs/connect4_prog.c b/tools/testing/selftests/bpf/progs/connect4_prog.c
index b1b2773c0b9d..a943d394fd3a 100644
--- a/tools/testing/selftests/bpf/progs/connect4_prog.c
+++ b/tools/testing/selftests/bpf/progs/connect4_prog.c
@@ -23,6 +23,10 @@ 
 #define TCP_CA_NAME_MAX 16
 #endif
 
+#ifndef TCP_NOTSENT_LOWAT
+#define TCP_NOTSENT_LOWAT 25
+#endif
+
 #ifndef IFNAMSIZ
 #define IFNAMSIZ 16
 #endif
@@ -128,6 +132,18 @@  static __inline int set_keepalive(struct bpf_sock_addr *ctx)
 	return 0;
 }
 
+static __inline int set_notsent_lowat(struct bpf_sock_addr *ctx)
+{
+	int lowat = 65535;
+
+	if (ctx->type == SOCK_STREAM) {
+		if (bpf_setsockopt(ctx, SOL_TCP, TCP_NOTSENT_LOWAT, &lowat, sizeof(lowat)))
+			return 1;
+	}
+
+	return 0;
+}
+
 SEC("cgroup/connect4")
 int connect_v4_prog(struct bpf_sock_addr *ctx)
 {
@@ -148,6 +164,9 @@  int connect_v4_prog(struct bpf_sock_addr *ctx)
 	if (set_keepalive(ctx))
 		return 0;
 
+	if (set_notsent_lowat(ctx))
+		return 0;
+
 	if (ctx->type != SOCK_STREAM && ctx->type != SOCK_DGRAM)
 		return 0;
 	else if (ctx->type == SOCK_STREAM)