From 4381fe522fdaf31268c8eb0a19ee840798e21a56 Mon Sep 17 00:00:00 2001
From: Rich Megginson <rmeggins@redhat.com>
Date: Tue, 19 Mar 2013 16:43:44 -0600
Subject: [PATCH 4/5] Ticket #633 - allow nsslapd-nagle to be disabled, and also tcp cork

https://fedorahosted.org/389/ticket/633
Reviewed by: ???
Branch: master
Fix Description: Instead of hard-coded enable_nagle = 1 in the connection
code, use the server config nsslapd-nagle setting.  The default is on
on linux.  Also make the tcp cork in connection_dispatch_operation() depend
on the nagle setting - if nagle is enabled, then enable tcp cork, otherwise,
disable tcp cork too.
Platforms tested: RHEL6 x86_64
Flag Day: no
Doc impact: Yes - document new config parameter
---
 ldap/servers/slapd/connection.c |   36 +++++++++++++++++-------------------
 ldap/servers/slapd/daemon.c     |    6 ------
 ldap/servers/slapd/libglobs.c   |    5 +++++
 3 files changed, 22 insertions(+), 25 deletions(-)

diff --git a/ldap/servers/slapd/connection.c b/ldap/servers/slapd/connection.c
index 68cc044..f2685c6 100644
--- a/ldap/servers/slapd/connection.c
+++ b/ldap/servers/slapd/connection.c
@@ -555,7 +555,10 @@ connection_dispatch_operation(Connection *conn, Operation *op, Slapi_PBlock *pb)
 {
 	int minssf = config_get_minssf();
 	int minssf_exclude_rootdse = 0;
-
+#ifdef TCP_CORK
+	int enable_nagle = config_get_nagle();
+	int pop_cork = 0;
+#endif
 	/* If the minimum SSF requirements are not met, only allow
 	 * bind and extended operations through.  The bind and extop
 	 * code will ensure that only SASL binds and startTLS are
@@ -650,38 +653,33 @@ connection_dispatch_operation(Connection *conn, Operation *op, Slapi_PBlock *pb)
 
 	case LDAP_REQ_SEARCH:
 		operation_set_type(op,SLAPI_OPERATION_SEARCH);
-		
 
-	/* On Linux we can use TCP_CORK to get us 5-10% speed benefit when one entry is returned */
-	/* Nagle needs to be turned _off_, the default is off on linux, in daemon.c */
-#if defined(LINUX)
-	{
-		int i = 1;
-		int ret = 0;
-		/* Set TCP_CORK here but only if this is not LDAPI */
-		if(!conn->c_unix_local)
+		/* On Linux we can use TCP_CORK to get us 5-10% speed benefit when one entry is returned */
+		/* Nagle needs to be turned _on_, the default is _on_ on linux, in daemon.c */
+#ifdef TCP_CORK
+		if (enable_nagle && !conn->c_unix_local)
 		{
-			ret = setsockopt(conn->c_sd,IPPROTO_TCP,TCP_CORK,&i,sizeof(i));
+			int i = 1;
+			int ret = setsockopt(conn->c_sd,IPPROTO_TCP,TCP_CORK,&i,sizeof(i));
 			if (ret < 0) {
 				LDAPDebug(LDAP_DEBUG_ANY, "Failed to set TCP_CORK on connection %" NSPRIu64 "\n",conn->c_connid, 0, 0);
 			}
+			pop_cork = 1;
 		}
 #endif
-
 		do_search( pb );
 
-#if defined(LINUX)
-		/* Clear TCP_CORK to flush any unsent data but only if not LDAPI*/
-		i = 0;
-		if(!conn->c_unix_local)
-		{
-			ret = setsockopt(conn->c_sd,IPPROTO_TCP,TCP_CORK,&i,sizeof(i));
+#ifdef TCP_CORK
+		if (pop_cork) {
+			/* Clear TCP_CORK to flush any unsent data but only if not LDAPI*/
+			int i = 0;
+			int ret = setsockopt(conn->c_sd,IPPROTO_TCP,TCP_CORK,&i,sizeof(i));
 			if (ret < 0) {
 				LDAPDebug(LDAP_DEBUG_ANY, "Failed to clear TCP_CORK on connection %" NSPRIu64 "\n",conn->c_connid, 0, 0);
 			}
 		}
-	}
 #endif
+
 		break;
 
 	/* for some strange reason, the console is using this old obsolete
diff --git a/ldap/servers/slapd/daemon.c b/ldap/servers/slapd/daemon.c
index 857398d..962699a 100644
--- a/ldap/servers/slapd/daemon.c
+++ b/ldap/servers/slapd/daemon.c
@@ -3478,11 +3478,6 @@ int configure_pr_socket( PRFileDesc **pr_socket, int secure, int local )
 
 	PRSocketOptionData pr_socketoption;
   
-#if defined(LINUX)
-	/* On Linux we use TCP_CORK so we must enable nagle */
-	enable_nagle = 1;
-#endif
-
 	ns = PR_FileDesc2NativeHandle( *pr_socket );
 	
 #if !defined(_WIN32)
@@ -3561,7 +3556,6 @@ int configure_pr_socket( PRFileDesc **pr_socket, int secure, int local )
 
 	} /* else (secure) */
 
-
 	if ( !enable_nagle && !local ) {
 
 		 pr_socketoption.option = PR_SockOpt_NoDelay;
diff --git a/ldap/servers/slapd/libglobs.c b/ldap/servers/slapd/libglobs.c
index fbbb4f9..9780b2e 100644
--- a/ldap/servers/slapd/libglobs.c
+++ b/ldap/servers/slapd/libglobs.c
@@ -1343,7 +1343,12 @@ FrontendConfig_init () {
 #endif /* _WIN32 */
 
   init_accesscontrol = cfg->accesscontrol = LDAP_ON;
+#if defined(LINUX)
+  /* On Linux, by default, we use TCP_CORK so we must enable nagle */
+  init_nagle = cfg->nagle = LDAP_ON;
+#else
   init_nagle = cfg->nagle = LDAP_OFF;
+#endif
   init_security = cfg->security = LDAP_OFF;
   init_ssl_check_hostname = cfg->ssl_check_hostname = LDAP_ON;
   init_return_exact_case = cfg->return_exact_case = LDAP_ON;
-- 
1.7.1

