Browse code

tcp: use dynamic config framework, part 2

- added tcp_connect_timeout, tcp_send_timeout,
tcp_connection_lifetime, tcp_max_connections, tcp_accept_aliases
to the config framework.

- added 2 new tcp related variables: tcp alias_flags and tcp
new_conn_alias_flags

Andrei Pelinescu-Onciul authored on 05/03/2009 17:20:42
Showing 9 changed files
... ...
@@ -764,7 +764,7 @@ assign_stm:
764 764
 	| DISABLE_TCP EQUAL error { yyerror("boolean value expected"); }
765 765
 	| TCP_ACCEPT_ALIASES EQUAL NUMBER {
766 766
 		#ifdef USE_TCP
767
-			tcp_accept_aliases=$3;
767
+			tcp_default_cfg.accept_aliases=$3;
768 768
 		#else
769 769
 			warn("tcp support not compiled in");
770 770
 		#endif
... ...
@@ -780,7 +780,7 @@ assign_stm:
780 780
 	| TCP_CHILDREN EQUAL error { yyerror("number expected"); }
781 781
 	| TCP_CONNECT_TIMEOUT EQUAL NUMBER {
782 782
 		#ifdef USE_TCP
783
-			tcp_connect_timeout=$3;
783
+			tcp_default_cfg.connect_timeout_s=$3;
784 784
 		#else
785 785
 			warn("tcp support not compiled in");
786 786
 		#endif
... ...
@@ -788,7 +788,7 @@ assign_stm:
788 788
 	| TCP_CONNECT_TIMEOUT EQUAL error { yyerror("number expected"); }
789 789
 	| TCP_SEND_TIMEOUT EQUAL NUMBER {
790 790
 		#ifdef USE_TCP
791
-			tcp_send_timeout=$3;
791
+			tcp_default_cfg.send_timeout_s=$3;
792 792
 		#else
793 793
 			warn("tcp support not compiled in");
794 794
 		#endif
... ...
@@ -796,7 +796,7 @@ assign_stm:
796 796
 	| TCP_SEND_TIMEOUT EQUAL error { yyerror("number expected"); }
797 797
 	| TCP_CON_LIFETIME EQUAL NUMBER {
798 798
 		#ifdef USE_TCP
799
-			tcp_con_lifetime=$3;
799
+			tcp_default_cfg.con_lifetime_s=$3;
800 800
 		#else
801 801
 			warn("tcp support not compiled in");
802 802
 		#endif
... ...
@@ -87,12 +87,8 @@ extern int children_no;
87 87
 extern int tcp_main_pid;
88 88
 extern int tcp_children_no;
89 89
 extern int tcp_disable;
90
-extern int tcp_accept_aliases;
91
-extern int tcp_connect_timeout;
92
-extern int tcp_send_timeout;
93
-extern int tcp_con_lifetime; /* connection lifetime */
94 90
 extern enum poll_types tcp_poll_method;
95
-extern int tcp_max_connections;
91
+extern int tcp_max_connections; /* maximum connections, hard limit */
96 92
 #endif
97 93
 #ifdef USE_TLS
98 94
 extern int tls_disable;
... ...
@@ -62,7 +62,8 @@
62 62
 #include "select_buf.h"
63 63
 
64 64
 #include "tcp_server.h" /* for tcpconn_add_alias */
65
-
65
+#include "tcp_options.h" /* for access to tcp_accept_aliases*/
66
+#include "cfg/cfg.h"
66 67
 
67 68
 #ifdef DEBUG_DMALLOC
68 69
 #include <mem/dmalloc.h>
... ...
@@ -138,7 +139,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
138 138
 		/* check if necessary to add receive?->moved to forward_req */
139 139
 		/* check for the alias stuff */
140 140
 #ifdef USE_TCP
141
-		if (msg->via1->alias && tcp_accept_aliases && 
141
+		if (msg->via1->alias && cfg_get(tcp, tcp_cfg, accept_aliases) && 
142 142
 				(((rcv_info->proto==PROTO_TCP) && !tcp_disable)
143 143
 #ifdef USE_TLS
144 144
 					|| ((rcv_info->proto==PROTO_TLS) && !tls_disable)
... ...
@@ -42,6 +42,7 @@
42 42
 #ifndef _tcp_conn_h
43 43
 #define _tcp_conn_h
44 44
 
45
+#include "tcp_init.h"
45 46
 #include "tcp_options.h"
46 47
 
47 48
 #include "ip_addr.h"
... ...
@@ -54,12 +55,6 @@
54 54
 #define TCP_CON_MAX_ALIASES (4*3) 
55 55
 
56 56
 #define TCP_BUF_SIZE	4096 
57
-#define DEFAULT_TCP_CONNECTION_LIFETIME 120 /* in  seconds */
58
-#define DEFAULT_TCP_SEND_TIMEOUT 10 /* if a send can't write for more then 10s,
59
-									   timeout */
60
-#define DEFAULT_TCP_CONNECT_TIMEOUT 10 /* if a connect doesn't complete in this
61
-										  time, timeout */
62
-#define DEFAULT_TCP_MAX_CONNECTIONS 2048 /* maximum connections */
63 57
 #define TCP_CHILD_TIMEOUT 5 /* after 5 seconds, the child "returns" 
64 58
 							 the connection to the tcp master process */
65 59
 #define TCP_MAIN_SELECT_TIMEOUT 5 /* how often "tcp main" checks for timeout*/
... ...
@@ -33,7 +33,7 @@
33 33
 
34 34
 struct tcp_gen_info{
35 35
 	int tcp_readers;
36
-	int tcp_max_connections;
36
+	int tcp_max_connections; /* startup connection limit, cannot be exceeded*/
37 37
 	int tcp_connections_no; /* crt. number */
38 38
 	int tcp_write_queued; /* total bytes queued for write, 0 if no
39 39
 							 write queued support is enabled */
... ...
@@ -29,6 +29,16 @@
29 29
 #define tcp_init_h
30 30
 #include "ip_addr.h"
31 31
 
32
+#define DEFAULT_TCP_CONNECTION_LIFETIME_S 120 /* in  seconds */
33
+/* maximum accepted lifetime in ticks (maximum possible is  ~ MAXINT/2) */
34
+#define MAX_TCP_CON_LIFETIME	((1U<<(sizeof(ticks_t)*8-1))-1)
35
+
36
+#define DEFAULT_TCP_SEND_TIMEOUT 10 /* if a send can't write for more then 10s,
37
+									   timeout */
38
+#define DEFAULT_TCP_CONNECT_TIMEOUT 10 /* if a connect doesn't complete in this
39
+										  time, timeout */
40
+#define DEFAULT_TCP_MAX_CONNECTIONS 2048 /* maximum connections */
41
+
32 42
 struct tcp_child{
33 43
 	pid_t pid;
34 44
 	int proc_no; /* ser proc_no, for debugging */
... ...
@@ -203,8 +203,6 @@
203 203
 #define SEND_FD_QUEUE_TIMEOUT	MS_TO_TICKS(2000)  /* 2 s */
204 204
 #endif
205 205
 
206
-/* maximum accepted lifetime (maximum possible is  ~ MAXINT/2) */
207
-#define MAX_TCP_CON_LIFETIME	((1U<<(sizeof(ticks_t)*8-1))-1)
208 206
 /* minimum interval local_timer_run() is allowed to run, in ticks */
209 207
 #define TCPCONN_TIMEOUT_MIN_RUN 1  /* once per tick */
210 208
 #define TCPCONN_WAIT_TIMEOUT 1 /* 1 tick */
... ...
@@ -235,17 +233,10 @@ static struct fd_cache_entry fd_cache[TCP_FD_CACHE_SIZE];
235 235
 
236 236
 static int is_tcp_main=0;
237 237
 
238
-int tcp_accept_aliases=0; /* by default don't accept aliases */
239
-/* flags used for adding new aliases */
240
-int tcp_alias_flags=TCP_ALIAS_FORCE_ADD;
241
-/* flags used for adding the default aliases of a new tcp connection */
242
-int tcp_new_conn_alias_flags=TCP_ALIAS_REPLACE;
243
-int tcp_connect_timeout=DEFAULT_TCP_CONNECT_TIMEOUT;
244
-int tcp_send_timeout=DEFAULT_TCP_SEND_TIMEOUT;
245
-int tcp_con_lifetime=DEFAULT_TCP_CONNECTION_LIFETIME;
238
+
246 239
 enum poll_types tcp_poll_method=0; /* by default choose the best method */
247
-int tcp_max_connections=DEFAULT_TCP_MAX_CONNECTIONS;
248 240
 int tcp_main_max_fd_no=0;
241
+int tcp_max_connections=DEFAULT_TCP_MAX_CONNECTIONS;
249 242
 
250 243
 static union sockaddr_union tcp_source_ipv4_addr; /* saved bind/srv v4 addr. */
251 244
 static union sockaddr_union* tcp_source_ipv4=0;
... ...
@@ -500,7 +491,7 @@ static int tcp_blocking_connect(int fd, int type,
500 500
 	int poll_err;
501 501
 	
502 502
 	poll_err=0;
503
-	to=tcp_connect_timeout;
503
+	to=cfg_get(tcp, tcp_cfg, connect_timeout_s);
504 504
 	ticks=get_ticks();
505 505
 again:
506 506
 	n=connect(fd, servaddr, addrlen);
... ...
@@ -594,7 +585,7 @@ error_timeout:
594 594
 #endif /* USE_DST_BLACKLIST */
595 595
 	LOG(L_ERR, "ERROR: tcp_blocking_connect %s: timeout %d s elapsed "
596 596
 				"from %d s\n", su2a((union sockaddr_union*)servaddr, addrlen),
597
-				elapsed, tcp_connect_timeout);
597
+				elapsed, cfg_get(tcp, tcp_cfg, connect_timeout_s));
598 598
 error:
599 599
 	return -1;
600 600
 end:
... ...
@@ -976,7 +967,7 @@ struct tcp_connection* tcpconn_new(int sock, union sockaddr_union* su,
976 976
 	{
977 977
 		c->type=PROTO_TCP;
978 978
 		c->rcv.proto=PROTO_TCP;
979
-		c->timeout=get_ticks_raw()+tcp_con_lifetime;
979
+		c->timeout=get_ticks_raw()+cfg_get(tcp, tcp_cfg, con_lifetime);
980 980
 	}
981 981
 	
982 982
 	return c;
... ...
@@ -1118,10 +1109,11 @@ struct tcp_connection* tcpconn_connect( union sockaddr_union* server,
1118 1118
 
1119 1119
 	s=-1;
1120 1120
 	
1121
-	if (*tcp_connections_no >= tcp_max_connections){
1121
+	if (*tcp_connections_no >= cfg_get(tcp, tcp_cfg, max_connections)){
1122 1122
 		LOG(L_ERR, "ERROR: tcpconn_connect: maximum number of connections"
1123 1123
 					" exceeded (%d/%d)\n",
1124
-					*tcp_connections_no, tcp_max_connections);
1124
+					*tcp_connections_no,
1125
+					cfg_get(tcp, tcp_cfg, max_connections));
1125 1126
 		goto error;
1126 1127
 	}
1127 1128
 	s=tcp_do_connect(server, from, type, &my_name, &si, &state);
... ...
@@ -1155,6 +1147,7 @@ int tcpconn_finish_connect( struct tcp_connection* c,
1155 1155
 	struct socket_info* si;
1156 1156
 	enum tcp_conn_states state;
1157 1157
 	struct tcp_conn_alias* a;
1158
+	int new_conn_alias_flags;
1158 1159
 	
1159 1160
 	s=tcp_do_connect(&c->rcv.src_su, from, c->type, &local_addr, &si, &state);
1160 1161
 	if (unlikely(s==-1)){
... ...
@@ -1168,14 +1161,16 @@ int tcpconn_finish_connect( struct tcp_connection* c,
1168 1168
 	c->rcv.dst_port=su_getport(&local_addr);
1169 1169
 	/* update aliases if needed */
1170 1170
 	if (likely(from==0)){
1171
+		new_conn_alias_flags=cfg_get(tcp, tcp_cfg, new_conn_alias_flags);
1171 1172
 		/* add aliases */
1172 1173
 		TCPCONN_LOCK;
1173 1174
 		_tcpconn_add_alias_unsafe(c, c->rcv.src_port, &c->rcv.dst_ip, 0,
1174
-													tcp_new_conn_alias_flags);
1175
+													new_conn_alias_flags);
1175 1176
 		_tcpconn_add_alias_unsafe(c, c->rcv.src_port, &c->rcv.dst_ip,
1176
-									c->rcv.dst_port, tcp_new_conn_alias_flags);
1177
+									c->rcv.dst_port, new_conn_alias_flags);
1177 1178
 		TCPCONN_UNLOCK;
1178 1179
 	}else if (su_cmp(from, &local_addr)!=1){
1180
+		new_conn_alias_flags=cfg_get(tcp, tcp_cfg, new_conn_alias_flags);
1179 1181
 		TCPCONN_LOCK;
1180 1182
 			/* remove all the aliases except the first one and re-add them
1181 1183
 			 * (there shouldn't be more then the 3 default aliases at this 
... ...
@@ -1187,9 +1182,9 @@ int tcpconn_finish_connect( struct tcp_connection* c,
1187 1187
 			c->aliases=1;
1188 1188
 			/* add the local_ip:0 and local_ip:local_port aliases */
1189 1189
 			_tcpconn_add_alias_unsafe(c, c->rcv.src_port, &c->rcv.dst_ip,
1190
-												0, tcp_new_conn_alias_flags);
1190
+												0, new_conn_alias_flags);
1191 1191
 			_tcpconn_add_alias_unsafe(c, c->rcv.src_port, &c->rcv.dst_ip,
1192
-									c->rcv.dst_port, tcp_new_conn_alias_flags);
1192
+									c->rcv.dst_port, new_conn_alias_flags);
1193 1193
 		TCPCONN_UNLOCK;
1194 1194
 	}
1195 1195
 	
... ...
@@ -1204,11 +1199,13 @@ int tcpconn_finish_connect( struct tcp_connection* c,
1204 1204
 inline static struct tcp_connection*  tcpconn_add(struct tcp_connection *c)
1205 1205
 {
1206 1206
 	struct ip_addr zero_ip;
1207
+	int new_conn_alias_flags;
1207 1208
 
1208 1209
 	if (likely(c)){
1209 1210
 		ip_addr_mk_any(c->rcv.src_ip.af, &zero_ip);
1210 1211
 		c->id_hash=tcp_id_hash(c->id);
1211 1212
 		c->aliases=0;
1213
+		new_conn_alias_flags=cfg_get(tcp, tcp_cfg, new_conn_alias_flags);
1212 1214
 		TCPCONN_LOCK;
1213 1215
 		c->flags|=F_CONN_HASHED;
1214 1216
 		/* add it at the begining of the list*/
... ...
@@ -1221,12 +1218,12 @@ inline static struct tcp_connection*  tcpconn_add(struct tcp_connection *c)
1221 1221
 		 * the third alias is for (peer_ip, peer_port, local_addr, local_port) 
1222 1222
 		 *   -- for finding if a fully specified connection exists */
1223 1223
 		_tcpconn_add_alias_unsafe(c, c->rcv.src_port, &zero_ip, 0,
1224
-													tcp_new_conn_alias_flags);
1224
+													new_conn_alias_flags);
1225 1225
 		if (likely(c->rcv.dst_ip.af && ! ip_addr_any(&c->rcv.dst_ip))){
1226 1226
 			_tcpconn_add_alias_unsafe(c, c->rcv.src_port, &c->rcv.dst_ip, 0,
1227
-													tcp_new_conn_alias_flags);
1227
+													new_conn_alias_flags);
1228 1228
 			_tcpconn_add_alias_unsafe(c, c->rcv.src_port, &c->rcv.dst_ip,
1229
-									c->rcv.dst_port, tcp_new_conn_alias_flags);
1229
+									c->rcv.dst_port, new_conn_alias_flags);
1230 1230
 		}
1231 1231
 		/* ignore add_alias errors, there are some valid cases when one
1232 1232
 		 *  of the add_alias would fail (e.g. first add_alias for 2 connections
... ...
@@ -1501,6 +1498,7 @@ int tcpconn_add_alias(int id, int port, int proto)
1501 1501
 	int ret;
1502 1502
 	struct ip_addr zero_ip;
1503 1503
 	int r;
1504
+	int alias_flags;
1504 1505
 	
1505 1506
 	/* fix the port */
1506 1507
 	port=port?port:((proto==PROTO_TLS)?SIPS_PORT:SIP_PORT);
... ...
@@ -1509,18 +1507,18 @@ int tcpconn_add_alias(int id, int port, int proto)
1509 1509
 	c=_tcpconn_find(id, 0, 0, 0, 0);
1510 1510
 	if (likely(c)){
1511 1511
 		ip_addr_mk_any(c->rcv.src_ip.af, &zero_ip);
1512
-		
1512
+		alias_flags=cfg_get(tcp, tcp_cfg, alias_flags);
1513 1513
 		/* alias src_ip:port, 0, 0 */
1514 1514
 		ret=_tcpconn_add_alias_unsafe(c, port,  &zero_ip, 0, 
1515
-										tcp_alias_flags);
1515
+										alias_flags);
1516 1516
 		if (ret<0 && ret!=-3) goto error;
1517 1517
 		/* alias src_ip:port, local_ip, 0 */
1518 1518
 		ret=_tcpconn_add_alias_unsafe(c, port,  &c->rcv.dst_ip, 0, 
1519
-										tcp_alias_flags);
1519
+										alias_flags);
1520 1520
 		if (ret<0 && ret!=-3) goto error;
1521 1521
 		/* alias src_ip:port, local_ip, local_port */
1522 1522
 		ret=_tcpconn_add_alias_unsafe(c, port, &c->rcv.dst_ip, c->rcv.dst_port,
1523
-										tcp_alias_flags);
1523
+										alias_flags);
1524 1524
 		if (unlikely(ret<0)) goto error;
1525 1525
 	}else goto error_not_found;
1526 1526
 	TCPCONN_UNLOCK;
... ...
@@ -1622,6 +1620,7 @@ int tcp_send(struct dest_info* dst, union sockaddr_union* from,
1622 1622
 	long response[2];
1623 1623
 	int n;
1624 1624
 	int do_close_fd;
1625
+	ticks_t con_lifetime;
1625 1626
 #ifdef TCP_BUF_WRITE
1626 1627
 	int enable_write_watch;
1627 1628
 #endif /* TCP_BUF_WRITE */
... ...
@@ -1634,11 +1633,12 @@ int tcp_send(struct dest_info* dst, union sockaddr_union* from,
1634 1634
 #endif /* TCP_FD_CACHE */
1635 1635
 	do_close_fd=1; /* close the fd on exit */
1636 1636
 	port=su_getport(&dst->to);
1637
+	con_lifetime=cfg_get(tcp, tcp_cfg, con_lifetime);
1637 1638
 	if (likely(port)){
1638 1639
 		su2ip_addr(&ip, &dst->to);
1639
-		c=tcpconn_get(dst->id, &ip, port, from, tcp_con_lifetime); 
1640
+		c=tcpconn_get(dst->id, &ip, port, from, con_lifetime); 
1640 1641
 	}else if (likely(dst->id)){
1641
-		c=tcpconn_get(dst->id, 0, 0, 0, tcp_con_lifetime);
1642
+		c=tcpconn_get(dst->id, 0, 0, 0, con_lifetime);
1642 1643
 	}else{
1643 1644
 		LOG(L_CRIT, "BUG: tcp_send called with null id & to\n");
1644 1645
 		return -1;
... ...
@@ -1648,7 +1648,7 @@ int tcp_send(struct dest_info* dst, union sockaddr_union* from,
1648 1648
 		if (unlikely(c==0)) {
1649 1649
 			if (likely(port)){
1650 1650
 				/* try again w/o id */
1651
-				c=tcpconn_get(0, &ip, port, from, tcp_con_lifetime);
1651
+				c=tcpconn_get(0, &ip, port, from, con_lifetime);
1652 1652
 				goto no_id;
1653 1653
 			}else{
1654 1654
 				LOG(L_ERR, "ERROR: tcp_send: id %d not found, dropping\n",
... ...
@@ -1680,11 +1680,13 @@ no_id:
1680 1680
 #if defined(TCP_CONNECT_WAIT) && defined(TCP_BUF_WRITE)
1681 1681
 			if (likely(cfg_get(tcp, tcp_cfg, tcp_connect_wait) && 
1682 1682
 						cfg_get(tcp, tcp_cfg, tcp_buf_write) )){
1683
-				if (unlikely(*tcp_connections_no >= tcp_max_connections)){
1683
+				if (unlikely(*tcp_connections_no >=
1684
+								cfg_get(tcp, tcp_cfg, max_connections))){
1684 1685
 					LOG(L_ERR, "ERROR: tcp_send %s: maximum number of"
1685 1686
 								" connections exceeded (%d/%d)\n",
1686 1687
 								su2a(&dst->to, sizeof(dst->to)),
1687
-								*tcp_connections_no, tcp_max_connections);
1688
+								*tcp_connections_no,
1689
+								cfg_get(tcp, tcp_cfg, max_connections));
1688 1690
 					return -1;
1689 1691
 				}
1690 1692
 				c=tcpconn_new(-1, &dst->to, from, 0, dst->proto,
... ...
@@ -1932,7 +1934,8 @@ send_it:
1932 1932
 	else
1933 1933
 #endif
1934 1934
 		/* n=tcp_blocking_write(c, fd, buf, len); */
1935
-		n=tsend_stream(fd, buf, len, tcp_send_timeout*1000); 
1935
+		n=tsend_stream(fd, buf, len,
1936
+							cfg_get(tcp, tcp_cfg, send_timeout_s)*1000);
1936 1937
 #ifdef TCP_BUF_WRITE
1937 1938
 	}
1938 1939
 #else /* ! TCP_BUF_WRITE */
... ...
@@ -2574,6 +2577,7 @@ inline static int handle_tcp_child(struct tcp_child* tcp_c, int fd_i)
2574 2574
 	int n;
2575 2575
 	ticks_t t;
2576 2576
 	ticks_t crt_timeout;
2577
+	ticks_t con_lifetime;
2577 2578
 	
2578 2579
 	if (unlikely(tcp_c->unix_sock<=0)){
2579 2580
 		/* (we can't have a fd==0, 0 is never closed )*/
... ...
@@ -2650,8 +2654,9 @@ inline static int handle_tcp_child(struct tcp_child* tcp_c, int fd_i)
2650 2650
 			}
2651 2651
 			/* update the timeout*/
2652 2652
 			t=get_ticks_raw();
2653
-			tcpconn->timeout=t+tcp_con_lifetime;
2654
-			crt_timeout=tcp_con_lifetime;
2653
+			con_lifetime=cfg_get(tcp, tcp_cfg, con_lifetime);
2654
+			tcpconn->timeout=t+con_lifetime;
2655
+			crt_timeout=con_lifetime;
2655 2656
 #ifdef TCP_BUF_WRITE
2656 2657
 			if (unlikely(cfg_get(tcp, tcp_cfg, tcp_buf_write) && 
2657 2658
 							_wbufq_non_empty(tcpconn) )){
... ...
@@ -2676,7 +2681,7 @@ inline static int handle_tcp_child(struct tcp_child* tcp_c, int fd_i)
2676 2676
 						tcpconn_put_destroy(tcpconn);
2677 2677
 					break;
2678 2678
 				}else{
2679
-					crt_timeout=MIN_unsigned(tcp_con_lifetime,
2679
+					crt_timeout=MIN_unsigned(con_lifetime,
2680 2680
 											tcpconn->wbuf_q.wr_timeout-t);
2681 2681
 				}
2682 2682
 			}
... ...
@@ -2766,6 +2771,7 @@ inline static int handle_ser_child(struct process_table* p, int fd_i)
2766 2766
 	int fd;
2767 2767
 	int flags;
2768 2768
 	ticks_t t;
2769
+	ticks_t con_lifetime;
2769 2770
 #ifdef TCP_BUF_WRITE
2770 2771
 	ticks_t nxt_timeout;
2771 2772
 #endif /* TCP_BUF_WRITE */
... ...
@@ -2874,11 +2880,12 @@ inline static int handle_ser_child(struct process_table* p, int fd_i)
2874 2874
 			tcpconn_add(tcpconn);
2875 2875
 			/* update the timeout*/
2876 2876
 			t=get_ticks_raw();
2877
-			tcpconn->timeout=t+tcp_con_lifetime;
2877
+			con_lifetime=cfg_get(tcp, tcp_cfg, con_lifetime);
2878
+			tcpconn->timeout=t+con_lifetime;
2878 2879
 			/* activate the timer (already properly init. in tcpconn_new())
2879 2880
 			 * no need for reinit */
2880 2881
 			local_timer_add(&tcp_main_ltimer, &tcpconn->timer, 
2881
-								tcp_con_lifetime, t);
2882
+								con_lifetime, t);
2882 2883
 			tcpconn->flags|=(F_CONN_MAIN_TIMER|F_CONN_READ_W|F_CONN_WANTS_RD)
2883 2884
 #ifdef TCP_BUF_WRITE
2884 2885
 					/* not used for now, the connection is sent to tcp_main
... ...
@@ -2989,8 +2996,9 @@ inline static int handle_ser_child(struct process_table* p, int fd_i)
2989 2989
 			tcpconn->s=fd;
2990 2990
 			/* update the timeout*/
2991 2991
 			t=get_ticks_raw();
2992
-			tcpconn->timeout=t+tcp_con_lifetime;
2993
-			nxt_timeout=tcp_con_lifetime;
2992
+			con_lifetime=cfg_get(tcp, tcp_cfg, con_lifetime);
2993
+			tcpconn->timeout=t+con_lifetime;
2994
+			nxt_timeout=con_lifetime;
2994 2995
 			if (unlikely(cmd==CONN_NEW_COMPLETE)){
2995 2996
 				tcpconn->state=S_CONN_OK;
2996 2997
 				/* check if needs to be watched for write */
... ...
@@ -3157,9 +3165,10 @@ static inline int handle_new_connect(struct socket_info* si)
3157 3157
 				" connection(%d): %s\n", errno, strerror(errno));
3158 3158
 		return -1;
3159 3159
 	}
3160
-	if (unlikely(*tcp_connections_no>=tcp_max_connections)){
3160
+	if (unlikely(*tcp_connections_no>=cfg_get(tcp, tcp_cfg, max_connections))){
3161 3161
 		LOG(L_ERR, "ERROR: maximum number of connections exceeded: %d/%d\n",
3162
-					*tcp_connections_no, tcp_max_connections);
3162
+					*tcp_connections_no,
3163
+					cfg_get(tcp, tcp_cfg, max_connections));
3163 3164
 		close(new_sock);
3164 3165
 		return 1; /* success, because the accept was succesfull */
3165 3166
 	}
... ...
@@ -3192,7 +3201,8 @@ static inline int handle_new_connect(struct socket_info* si)
3192 3192
 		tcpconn_add(tcpconn);
3193 3193
 		/* activate the timer */
3194 3194
 		local_timer_add(&tcp_main_ltimer, &tcpconn->timer, 
3195
-								tcp_con_lifetime, get_ticks_raw());
3195
+								cfg_get(tcp, tcp_cfg, con_lifetime),
3196
+								get_ticks_raw());
3196 3197
 		tcpconn->flags|=(F_CONN_MAIN_TIMER|F_CONN_READ_W|F_CONN_WANTS_RD);
3197 3198
 		if (unlikely(io_watch_add(&io_h, tcpconn->s, POLLIN, 
3198 3199
 													F_TCPCONN, tcpconn)<0)){
... ...
@@ -3843,26 +3853,7 @@ int init_tcp()
3843 3843
 			TCP_ID_HASH_SIZE * sizeof(struct tcp_connection*));
3844 3844
 	
3845 3845
 	/* fix config variables */
3846
-	if (tcp_connect_timeout<0)
3847
-		tcp_connect_timeout=DEFAULT_TCP_CONNECT_TIMEOUT;
3848
-	if (tcp_send_timeout<0)
3849
-		tcp_send_timeout=DEFAULT_TCP_SEND_TIMEOUT;
3850
-	if (tcp_con_lifetime<0){
3851
-		/* set to max value (~ 1/2 MAX_INT) */
3852
-		tcp_con_lifetime=MAX_TCP_CON_LIFETIME;
3853
-	}else{
3854
-		if ((unsigned)tcp_con_lifetime > 
3855
-				(unsigned)TICKS_TO_S(MAX_TCP_CON_LIFETIME)){
3856
-			LOG(L_WARN, "init_tcp: tcp_con_lifetime too big (%u s), "
3857
-					" the maximum value is %u\n", tcp_con_lifetime,
3858
-					TICKS_TO_S(MAX_TCP_CON_LIFETIME));
3859
-			tcp_con_lifetime=MAX_TCP_CON_LIFETIME;
3860
-		}else{
3861
-			tcp_con_lifetime=S_TO_TICKS(tcp_con_lifetime);
3862
-		}
3863
-	}
3864
-	
3865
-		poll_err=check_poll_method(tcp_poll_method);
3846
+	poll_err=check_poll_method(tcp_poll_method);
3866 3847
 	
3867 3848
 	/* set an appropriate poll method */
3868 3849
 	if (poll_err || (tcp_poll_method==0)){
... ...
@@ -29,6 +29,7 @@
29 29
 #include "globals.h"
30 30
 #include "timer_ticks.h"
31 31
 #include "cfg/cfg.h"
32
+#include "tcp_init.h" /* DEFAULT* */
32 33
 
33 34
 
34 35
 
... ...
@@ -68,7 +69,18 @@ struct cfg_group_tcp tcp_default_cfg;
68 68
 static cfg_def_t tcp_cfg_def[] = {
69 69
 	/*   name        , type |input type| chg type, min, max, fixup, proc. cbk 
70 70
 	      description */
71
-	{ "fd_cache",     CFG_VAR_INT | CFG_READONLY,    0,   1,     0,         0,
71
+	{ "connect_timeout", CFG_VAR_INT | CFG_READONLY, -1,
72
+						TICKS_TO_S(MAX_TCP_CON_LIFETIME),         0,         0,
73
+		"used only in non-async mode, in seconds"},
74
+	{ "send_timeout", CFG_VAR_INT | CFG_READONLY,   -1,
75
+						TICKS_TO_S(MAX_TCP_CON_LIFETIME),         0,         0,
76
+		"in seconds"},
77
+	{ "connection_lifetime", CFG_VAR_INT | CFG_READONLY,   -1,
78
+						TICKS_TO_S(MAX_TCP_CON_LIFETIME),         0,         0,
79
+		"connection lifetime (in seconds)"},
80
+	{ "max_connections", CFG_VAR_INT | CFG_READONLY, 0,  (1U<<31)-1, 0,      0,
81
+		"maximum connection number, soft limit"},
82
+	{ "fd_cache",     CFG_VAR_INT | CFG_READONLY,    0,   1,      0,         0,
72 83
 		"file descriptor cache for tcp_send"},
73 84
 	/* tcp async options */
74 85
 	{ "async",        CFG_VAR_INT | CFG_READONLY,    0,   1,      0,         0,
... ...
@@ -79,8 +91,7 @@ static cfg_def_t tcp_cfg_def[] = {
79 79
 		"maximum bytes queued for write per connection (depends on async)"},
80 80
 	{ "wq_max",       CFG_VAR_INT | CFG_READONLY,    0,  1<<30,    0,        0,
81 81
 		"maximum bytes queued for write allowed globally (depends on async)"},
82
-	{ "wq_timeout",   CFG_VAR_INT | CFG_READONLY,    1,  1<<30,    0,        0,
83
-		"timeout for queued writes (in ticks, use send_timeout for seconds)"},
82
+	/* see also wq_timeout below */
84 83
 	/* tcp socket options */
85 84
 	{ "defer_accept", CFG_VAR_INT | CFG_READONLY,    0,   3600,   0,         0,
86 85
 		"0/1 on linux, seconds on freebsd (see docs)"},
... ...
@@ -101,6 +112,20 @@ static cfg_def_t tcp_cfg_def[] = {
101 101
 	/* other options */
102 102
 	{ "crlf_ping",   CFG_VAR_INT | CFG_READONLY,    0,        1,  0,         0,
103 103
 		"enable responding to CRLF SIP-level keepalives "},
104
+	{ "accept_aliases", CFG_VAR_INT | CFG_READONLY, 0,        1,  0,         0,
105
+		"turn on/off tcp aliases (see tcp_accept_aliases) "},
106
+	{ "alias_flags", CFG_VAR_INT | CFG_READONLY,    0,        0,  0,         0,
107
+		"flags used for adding new aliases (FORCE_ADD:1 , REPLACE:2) "},
108
+	{ "new_conn_alias_flags", CFG_VAR_INT | CFG_READONLY, 0,  0,  0,         0,
109
+		"flags for the def. aliases for a new conn. (FORCE_ADD:1, REPLACE:2 "},
110
+	/* internal and/or "fixed" versions of some vars
111
+	   (not supposed to be writeable, read will provide only debugging value*/
112
+	{ "wq_timeout_ticks",   CFG_VAR_INT | CFG_READONLY, 0,
113
+									MAX_TCP_CON_LIFETIME,         0,         0,
114
+		"internal send_timeout value in ticks, used in async. mode"},
115
+	{ "con_lifetime_ticks", CFG_VAR_INT | CFG_READONLY, 0,
116
+									MAX_TCP_CON_LIFETIME,         0,         0,
117
+		"internal connection_lifetime value, converted to ticks"},
104 118
 	{0, 0, 0, 0, 0, 0, 0}
105 119
 };
106 120
 
... ...
@@ -110,11 +135,15 @@ void* tcp_cfg; /* tcp config handle */
110 110
 /* set defaults */
111 111
 void init_tcp_options()
112 112
 {
113
+	tcp_default_cfg.connect_timeout_s=DEFAULT_TCP_CONNECT_TIMEOUT;
114
+	tcp_default_cfg.send_timeout_s=DEFAULT_TCP_SEND_TIMEOUT;
115
+	tcp_default_cfg.con_lifetime_s=DEFAULT_TCP_CONNECTION_LIFETIME_S;
116
+	tcp_default_cfg.max_connections=tcp_max_connections;
113 117
 #ifdef TCP_BUF_WRITE
114 118
 	tcp_default_cfg.tcp_buf_write=0;
115 119
 	tcp_default_cfg.tcpconn_wq_max=32*1024; /* 32 k */
116 120
 	tcp_default_cfg.tcp_wq_max=10*1024*1024; /* 10 MB */
117
-	tcp_default_cfg.tcp_wq_timeout=S_TO_TICKS(tcp_send_timeout);
121
+	tcp_default_cfg.tcp_wq_timeout=S_TO_TICKS(tcp_default_cfg.send_timeout_s);
118 122
 #ifdef TCP_CONNECT_WAIT
119 123
 	tcp_default_cfg.tcp_connect_wait=1;
120 124
 #endif /* TCP_CONNECT_WAIT */
... ...
@@ -134,6 +163,11 @@ void init_tcp_options()
134 134
 	tcp_default_cfg.delayed_ack=1;
135 135
 #endif
136 136
 	tcp_default_cfg.crlf_ping=1;
137
+	tcp_default_cfg.accept_aliases=0; /* don't accept aliases by default */
138
+	/* flags used for adding new aliases */
139
+	tcp_default_cfg.alias_flags=TCP_ALIAS_FORCE_ADD;
140
+	/* flags used for adding the default aliases of a new tcp connection */
141
+	tcp_default_cfg.new_conn_alias_flags=TCP_ALIAS_REPLACE;
137 142
 }
138 143
 
139 144
 
... ...
@@ -155,6 +189,20 @@ void init_tcp_options()
155 155
 	}
156 156
 
157 157
 
158
+
159
+/* if *to<0 to=default_val, else if to>max_val to=max_val */
160
+static void fix_timeout(char* name, int* to, int default_val, unsigned max_val)
161
+{
162
+	if (*to < 0) *to=default_val;
163
+	else if ((unsigned)*to > max_val){
164
+		WARN("%s: timeout too big (%u), the maximum value is %u\n",
165
+				name, *to, max_val);
166
+		*to=max_val;
167
+	}
168
+}
169
+
170
+
171
+
158 172
 /* checks & warns if some tcp_option cannot be enabled */
159 173
 void tcp_options_check()
160 174
 {
... ...
@@ -196,7 +244,7 @@ void tcp_options_check()
196 196
 #ifndef HAVE_TCP_KEEPCNT
197 197
 	W_OPT_NS(keepcnt);
198 198
 #endif
199
-	if (tcp_default_cfg.keepintvl || tcp_default_cfg.keepidle || 
199
+	if (tcp_default_cfg.keepintvl || tcp_default_cfg.keepidle ||
200 200
 			tcp_default_cfg.keepcnt){
201 201
 		tcp_default_cfg.keepalive=1; /* force on */
202 202
 	}
... ...
@@ -206,6 +254,22 @@ void tcp_options_check()
206 206
 #ifndef HAVE_TCP_QUICKACK
207 207
 	W_OPT_NS(delayed_ack);
208 208
 #endif
209
+	/* fix various timeouts */
210
+	fix_timeout("tcp_connect_timeout", &tcp_default_cfg.connect_timeout_s,
211
+						DEFAULT_TCP_CONNECT_TIMEOUT,
212
+						TICKS_TO_S(MAX_TCP_CON_LIFETIME));
213
+	fix_timeout("tcp_send_timeout", &tcp_default_cfg.send_timeout_s,
214
+						DEFAULT_TCP_SEND_TIMEOUT,
215
+						TICKS_TO_S(MAX_TCP_CON_LIFETIME));
216
+	fix_timeout("tcp_connection_lifetime", &tcp_default_cfg.con_lifetime_s,
217
+						TICKS_TO_S(MAX_TCP_CON_LIFETIME),
218
+						TICKS_TO_S(MAX_TCP_CON_LIFETIME));
219
+	/* compute timeout in ticks */
220
+#ifdef TCP_BUF_WRITE
221
+	tcp_default_cfg.tcp_wq_timeout=S_TO_TICKS(tcp_default_cfg.send_timeout_s);
222
+#endif /* TCP_BUF_WRITE */
223
+	tcp_default_cfg.con_lifetime=S_TO_TICKS(tcp_default_cfg.con_lifetime_s);
224
+	tcp_default_cfg.max_connections=tcp_max_connections;
209 225
 }
210 226
 
211 227
 
... ...
@@ -110,14 +110,17 @@
110 110
 #endif /* USE_TCP */
111 111
 
112 112
 struct cfg_group_tcp{
113
-	/* ser tcp options */
113
+	/* ser tcp options, low level */
114
+	int connect_timeout_s; /* in s, used only in non-async mode */
115
+	int send_timeout_s; /* in s */
116
+	int con_lifetime_s; /* in s */
117
+	int max_connections;
114 118
 	int fd_cache; /* on /off */
115 119
 	/* tcp async options */
116 120
 	int tcp_buf_write; /* on / off */
117 121
 	int tcp_connect_wait; /* on / off, depends on tcp_buf_write */
118 122
 	unsigned int tcpconn_wq_max; /* maximum queue len per connection */
119 123
 	unsigned int tcp_wq_max; /* maximum overall queued bytes */
120
-	unsigned int tcp_wq_timeout;      /* timeout for queued writes */
121 124
 
122 125
 	/* tcp socket options */
123 126
 	int defer_accept; /* on / off */
... ...
@@ -131,6 +134,12 @@ struct cfg_group_tcp{
131 131
 	
132 132
 	/* other options */
133 133
 	int crlf_ping;  /* on/off - reply to double CRLF keepalives */
134
+	int accept_aliases;
135
+	int alias_flags;
136
+	int new_conn_alias_flags;
137
+	/* internal, "fixed" vars */
138
+	unsigned int tcp_wq_timeout; /* in ticks, timeout for queued writes */
139
+	unsigned int con_lifetime; /* in ticks, see con_lifetime_s */
134 140
 };
135 141
 
136 142
 extern struct cfg_group_tcp tcp_default_cfg;