Browse code

- fix: don't extend the timeout on tcp_send() if the connection is handled by a tcp_reader process

Andrei Pelinescu-Onciul authored on 28/11/2007 13:39:55
Showing 1 changed files
... ...
@@ -791,7 +791,7 @@ struct tcp_connection* _tcpconn_find(int id, struct ip_addr* ip, int port,
791 791
 	DBG("tcpconn_find: %d  port %d\n",id, port);
792 792
 	if (ip) print_ip("tcpconn_find: ip ", ip, "\n");
793 793
 #endif
794
-	if (id){
794
+	if (likely(id)){
795 795
 		hash=tcp_id_hash(id);
796 796
 		for (c=tcpconn_id_hash[hash]; c; c=c->id_next){
797 797
 #ifdef EXTRA_DEBUG
... ...
@@ -800,7 +800,7 @@ struct tcp_connection* _tcpconn_find(int id, struct ip_addr* ip, int port,
800 800
 #endif
801 801
 			if ((id==c->id)&&(c->state!=S_CONN_BAD)) return c;
802 802
 		}
803
-	}else if (ip){
803
+	}else if (likely(ip)){
804 804
 		hash=tcp_addr_hash(ip, port, l_ip, l_port);
805 805
 		is_local_ip_any=ip_addr_any(l_ip);
806 806
 		for (a=tcpconn_aliases_hash[hash]; a; a=a->next){
... ...
@@ -836,8 +836,8 @@ struct tcp_connection* tcpconn_get(int id, struct ip_addr* ip, int port,
836 836
 	int local_port;
837 837
 	
838 838
 	local_port=0;
839
-	if (ip){
840
-		if (local_addr){
839
+	if (likely(ip)){
840
+		if (unlikely(local_addr)){
841 841
 			su2ip_addr(&local_ip, local_addr);
842 842
 			local_port=su_getport(local_addr);
843 843
 		}else{
... ...
@@ -847,9 +847,14 @@ struct tcp_connection* tcpconn_get(int id, struct ip_addr* ip, int port,
847 847
 	}
848 848
 	TCPCONN_LOCK;
849 849
 	c=_tcpconn_find(id, ip, port, &local_ip, local_port);
850
-	if (c){ 
850
+	if (likely(c)){ 
851 851
 			atomic_inc(&c->refcnt);
852
-			c->timeout=get_ticks_raw()+timeout;
852
+			/* update the timeout only if the connection is not handled
853
+			 * by a tcp reader (the tcp reader process uses c->timeout for 
854
+			 * its own internal timeout and c->timeout will be overwritten
855
+			 * anyway on return to tcp_main) */
856
+			if (likely(c->reader_pid==0))
857
+				c->timeout=get_ticks_raw()+timeout;
853 858
 	}
854 859
 	TCPCONN_UNLOCK;
855 860
 	return c;
... ...
@@ -875,7 +880,7 @@ inline static int _tcpconn_add_alias_unsafe(struct tcp_connection* c, int port,
875 875
 	
876 876
 	a=0;
877 877
 	is_local_ip_any=ip_addr_any(l_ip);
878
-	if (c){
878
+	if (likely(c)){
879 879
 		hash=tcp_addr_hash(&c->rcv.src_ip, port, l_ip, l_port);
880 880
 		/* search the aliases for an already existing one */
881 881
 		for (a=tcpconn_aliases_hash[hash], nxt=0; a; a=nxt){
... ...
@@ -887,7 +892,7 @@ inline static int _tcpconn_add_alias_unsafe(struct tcp_connection* c, int port,
887 887
 					  ip_addr_cmp(&a->parent->rcv.dst_ip, l_ip))
888 888
 					){
889 889
 				/* found */
890
-				if (a->parent!=c){
890
+				if (unlikely(a->parent!=c)){
891 891
 					if (flags & TCP_ALIAS_FORCE_ADD)
892 892
 						/* still have to walk the whole list to check if
893 893
 						 * the alias was not already added */
... ...
@@ -903,7 +908,7 @@ inline static int _tcpconn_add_alias_unsafe(struct tcp_connection* c, int port,
903 903
 				}else goto ok;
904 904
 			}
905 905
 		}
906
-		if (c->aliases>=TCP_CON_MAX_ALIASES) goto error_aliases;
906
+		if (unlikely(c->aliases>=TCP_CON_MAX_ALIASES)) goto error_aliases;
907 907
 		c->con_aliases[c->aliases].parent=c;
908 908
 		c->con_aliases[c->aliases].port=port;
909 909
 		c->con_aliases[c->aliases].hash=hash;
... ...
@@ -945,7 +950,7 @@ int tcpconn_add_alias(int id, int port, int proto)
945 945
 	TCPCONN_LOCK;
946 946
 	/* check if alias already exists */
947 947
 	c=_tcpconn_find(id, 0, 0, 0, 0);
948
-	if (c){
948
+	if (likely(c)){
949 949
 		ip_addr_mk_any(c->rcv.src_ip.af, &zero_ip);
950 950
 		
951 951
 		/* alias src_ip:port, 0, 0 */
... ...
@@ -959,7 +964,7 @@ int tcpconn_add_alias(int id, int port, int proto)
959 959
 		/* alias src_ip:port, local_ip, local_port */
960 960
 		ret=_tcpconn_add_alias_unsafe(c, port, &c->rcv.dst_ip, c->rcv.dst_port,
961 961
 										tcp_alias_flags);
962
-		if (ret<0) goto error;
962
+		if (unlikely(ret<0)) goto error;
963 963
 	}else goto error_not_found;
964 964
 	TCPCONN_UNLOCK;
965 965
 	return 0;
... ...
@@ -1021,7 +1026,7 @@ inline static void tcp_fd_cache_add(struct tcp_connection *c, int fd)
1021 1021
 	int h;
1022 1022
 	
1023 1023
 	h=c->id%TCP_FD_CACHE_SIZE;
1024
-	if (fd_cache[h].fd>0)
1024
+	if (likely(fd_cache[h].fd>0))
1025 1025
 		close(fd_cache[h].fd);
1026 1026
 	fd_cache[h].fd=fd;
1027 1027
 	fd_cache[h].id=c->id;
... ...
@@ -1120,7 +1125,7 @@ get_fd:
1120 1120
 		/* check if this is not the same reader process holding
1121 1121
 		 *  c  and if so send directly on c->fd */
1122 1122
 		if (c->reader_pid==my_pid()){
1123
-			WARN("tcp_send: FIXME: send from reader (%d (%d)), reusing fd\n",
1123
+			DBG("tcp_send: send from reader (%d (%d)), reusing fd\n",
1124 1124
 					my_pid(), process_no);
1125 1125
 			fd=c->fd;
1126 1126
 			do_close_fd=0; /* don't close the fd on exit, it's in use */
... ...
@@ -1128,7 +1133,7 @@ get_fd:
1128 1128
 		}else if (likely((fd_cache_e=tcp_fd_cache_get(c))!=0)){
1129 1129
 			fd=fd_cache_e->fd;
1130 1130
 			do_close_fd=0;
1131
-			WARN("tcp_send: FIXME: found fd in cache ( %d, %p, %d)\n",
1131
+			DBG("tcp_send: found fd in cache ( %d, %p, %d)\n",
1132 1132
 					fd, c, fd_cache_e->id);
1133 1133
 #endif /* TCP_FD_CACHE */
1134 1134
 		}else{