Hi hope this little patch will find its use.

Thanks.
Regards.
From 02dc058b4f0f41ad1deeb581653e1c3cfb2b2432 Mon Sep 17 00:00:00 2001
From: David Carlier <devne...@gmail.com>
Date: Sat, 6 Feb 2021 12:11:11 +0000
Subject: [PATCH] BUILD/MEDIUM: proto_tcp defer-accept flag support for
 FreeBSD.

FreeBSD has a kernel feature (accf) and a sockopt flag similar to the Linux's TCP_DEFER_ACCEPT to filter incoming data upon ACK. The main difference is the filter needs to be placed when the socket actually listens.
---
 src/cfgparse-tcp.c |  4 ++--
 src/proto_tcp.c    | 12 ++++++++++++
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/src/cfgparse-tcp.c b/src/cfgparse-tcp.c
index 4dc39d547..e7868e6bf 100644
--- a/src/cfgparse-tcp.c
+++ b/src/cfgparse-tcp.c
@@ -61,7 +61,7 @@ static int bind_parse_transparent(char **args, int cur_arg, struct proxy *px, st
 }
 #endif
 
-#ifdef TCP_DEFER_ACCEPT
+#if defined(TCP_DEFER_ACCEPT) || defined(SO_ACCEPTFILTER)
 /* parse the "defer-accept" bind keyword */
 static int bind_parse_defer_accept(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
 {
@@ -243,7 +243,7 @@ static int srv_parse_tcp_ut(char **args, int *cur_arg, struct proxy *px, struct
  * not enabled.
  */
 static struct bind_kw_list bind_kws = { "TCP", { }, {
-#ifdef TCP_DEFER_ACCEPT
+#if defined(TCP_DEFER_ACCEPT) || defined(SO_ACCEPTFILTER)
 	{ "defer-accept",  bind_parse_defer_accept, 0 }, /* wait for some data for 1 second max before doing accept */
 #endif
 #ifdef SO_BINDTODEVICE
diff --git a/src/proto_tcp.c b/src/proto_tcp.c
index 485603d57..85cd56360 100644
--- a/src/proto_tcp.c
+++ b/src/proto_tcp.c
@@ -711,6 +711,18 @@ int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen)
 		goto tcp_close_return;
 	}
 
+#if defined(SO_ACCEPTFILTER)
+	/* the socket needs to listen first */
+	if (listener->options & LI_O_DEF_ACCEPT) {
+		struct accept_filter_arg accept;
+		memset(&accept, 0, sizeof(accept));
+		strcpy(accept.af_name, "dataready");
+		if (setsockopt(fd, SOL_SOCKET, SO_ACCEPTFILTER, &accept, sizeof(accept)) == -1) {
+			msg = "cannot enable ACCEPT_FILTER";
+			err |= ERR_WARN;
+		}
+	}
+#endif
 #if defined(TCP_QUICKACK)
 	if (listener->options & LI_O_NOQUICKACK)
 		setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero));
-- 
2.30.0

Reply via email to