964723
From a58f31659a924c59f6342d79d2c19ee956453d82 Mon Sep 17 00:00:00 2001
964723
From: Mark Andrews <marka@isc.org>
964723
Date: Sat, 18 Oct 2014 12:40:13 +1100
964723
Subject: [PATCH 2/2] 3980.   [bug]           Improve --with-tuning=large by
964723
 self tuning of SO_RCVBUF                         size. [RT #37187]
964723
964723
(cherry picked from commit 871f3c8beeb2134b17414ec167b90a57adb8e122)
964723
---
964723
 lib/isc/unix/socket.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++----
964723
 1 file changed, 61 insertions(+), 5 deletions(-)
964723
964723
diff --git a/lib/isc/unix/socket.c b/lib/isc/unix/socket.c
964723
index af0c3bc..90953ff 100644
964723
--- a/lib/isc/unix/socket.c
964723
+++ b/lib/isc/unix/socket.c
964723
@@ -2245,6 +2245,62 @@ free_socket(isc__socket_t **socketp) {
964723
 	*socketp = NULL;
964723
 }
964723
 
964723
+#ifdef SO_RCVBUF
964723
+static isc_once_t	rcvbuf_once = ISC_ONCE_INIT;
964723
+static int		rcvbuf = RCVBUFSIZE;
964723
+
964723
+static void
964723
+set_rcvbuf(void) {
964723
+	int fd;
964723
+	int max = rcvbuf, min;
964723
+	ISC_SOCKADDR_LEN_T len;
964723
+
964723
+	fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
964723
+#if defined(ISC_PLATFORM_HAVEIPV6)
964723
+	if (fd == -1) {
964723
+		switch (errno) {
964723
+		case EPROTONOSUPPORT:
964723
+		case EPFNOSUPPORT:
964723
+		case EAFNOSUPPORT:
964723
+		/*
964723
+		 * Linux 2.2 (and maybe others) return EINVAL instead of
964723
+		 * EAFNOSUPPORT.
964723
+		 */
964723
+		case EINVAL:
964723
+			fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
964723
+			break;
964723
+		}
964723
+	}
964723
+#endif
964723
+	if (fd == -1)
964723
+		return;
964723
+
964723
+	len = sizeof(min);
964723
+	if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *)&min, &len) >= 0 &&
964723
+	    min < rcvbuf) {
964723
+ again:
964723
+		if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *)&rcvbuf,
964723
+			       sizeof(rcvbuf)) == -1) {
964723
+			if (errno == ENOBUFS && rcvbuf > min) {
964723
+				max = rcvbuf - 1;
964723
+				rcvbuf = (rcvbuf + min) / 2;
964723
+				goto again;
964723
+			} else {
964723
+				rcvbuf = min;
964723
+				goto cleanup;
964723
+			}
964723
+		} else
964723
+			min = rcvbuf;
964723
+		if (min != max) {
964723
+			rcvbuf = max;
964723
+			goto again;
964723
+		}
964723
+	}
964723
+ cleanup:
964723
+	close (fd);
964723
+}
964723
+#endif
964723
+
964723
 #ifdef SO_BSDCOMPAT
964723
 /*
964723
  * This really should not be necessary to do.  Having to workout
964723
@@ -2609,15 +2665,15 @@ opensocket(isc__socketmgr_t *manager, isc__socket_t *sock,
964723
 #if defined(SO_RCVBUF)
964723
 		optlen = sizeof(size);
964723
 		if (getsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
964723
-			       (void *)&size, &optlen) >= 0 &&
964723
-		     size < RCVBUFSIZE) {
964723
-			size = RCVBUFSIZE;
964723
+			       (void *)&size, &optlen) >= 0 && size < rcvbuf) {
964723
+			RUNTIME_CHECK(isc_once_do(&rcvbuf_once,
964723
+						  set_rcvbuf) == ISC_R_SUCCESS);
964723
 			if (setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
964723
-				       (void *)&size, sizeof(size)) == -1) {
964723
+			       (void *)&rcvbuf, sizeof(rcvbuf)) == -1) {
964723
 				isc__strerror(errno, strbuf, sizeof(strbuf));
964723
 				UNEXPECTED_ERROR(__FILE__, __LINE__,
964723
 					"setsockopt(%d, SO_RCVBUF, %d) %s: %s",
964723
-					sock->fd, size,
964723
+					sock->fd, rcvbuf,
964723
 					isc_msgcat_get(isc_msgcat,
964723
 						       ISC_MSGSET_GENERAL,
964723
 						       ISC_MSG_FAILED,
964723
-- 
964723
2.9.5
964723