Browse code

- tcp flags cleanup (preparation for bigger tls related changes)

Andrei Pelinescu-Onciul authored on 20/02/2008 14:37:29
Showing 3 changed files
... ...
@@ -78,7 +78,7 @@ MAIN_NAME=ser
78 78
 VERSION = 2
79 79
 PATCHLEVEL = 1
80 80
 SUBLEVEL =  0
81
-EXTRAVERSION = -dev17-tcp
81
+EXTRAVERSION = -dev18-tcp
82 82
 
83 83
 SER_VER = $(shell expr $(VERSION) \* 1000000 + $(PATCHLEVEL) \* 1000 + \
84 84
 			$(SUBLEVEL) )
... ...
@@ -68,9 +68,9 @@
68 68
 
69 69
 /* tcp connection flags */
70 70
 #define F_CONN_NON_BLOCKING 1
71
-#define F_CONN_REMOVED      2 /* no longer  in "main" listen fd list */
72
-#define F_CONN_READER       4 /* handled by a tcp reader */
73
-#define F_CONN_WRITE_W      8 /* watched for write (main) */
71
+#define F_CONN_READ_W       2 /* watched for READ ev. in main */
72
+#define F_CONN_WRITE_W      4 /* watched for WRITE (main) */
73
+#define F_CONN_READER       8 /* handled by a tcp reader */
74 74
 #define F_CONN_HASHED      16 /* in tcp_main hash */
75 75
 #define F_CONN_FD_CLOSED   32 /* fd was already closed */
76 76
 #define F_CONN_PENDING     64 /* pending connect  (fd not known yet in main) */
... ...
@@ -916,7 +916,6 @@ struct tcp_connection* tcpconn_new(int sock, union sockaddr_union* su,
916 916
 		c->rcv.proto=PROTO_TCP;
917 917
 		c->timeout=get_ticks_raw()+tcp_con_lifetime;
918 918
 	}
919
-	c->flags|=F_CONN_REMOVED;
920 919
 	
921 920
 	return c;
922 921
 	
... ...
@@ -1600,7 +1599,6 @@ no_id:
1600 1599
 #if defined(TCP_CONNECT_WAIT) && defined(TCP_BUF_WRITE)
1601 1600
 			if (likely(tcp_options.tcp_connect_wait && 
1602 1601
 						tcp_options.tcp_buf_write )){
1603
-				/* FIXME: flag for timer? + check again */
1604 1602
 				if (unlikely(*tcp_connections_no >= tcp_max_connections)){
1605 1603
 					LOG(L_ERR, "ERROR: tcp_send: maximum number of connections"
1606 1604
 								" exceeded (%d/%d)\n",
... ...
@@ -2123,9 +2121,8 @@ inline static int tcpconn_chld_put(struct tcp_connection* tcpconn)
2123 2121
 		/* sanity checks */
2124 2122
 		membar_read_atomic_op(); /* make sure we see the current flags */
2125 2123
 		if (unlikely(!(tcpconn->flags & F_CONN_FD_CLOSED) || 
2126
-					 !(tcpconn->flags & F_CONN_REMOVED) || 
2127
-					(tcpconn->flags & 
2128
-					 	(F_CONN_HASHED|F_CONN_MAIN_TIMER| F_CONN_WRITE_W)) )){
2124
+			(tcpconn->flags & 
2125
+				(F_CONN_HASHED|F_CONN_MAIN_TIMER|F_CONN_READ_W|F_CONN_WRITE_W)) )){
2129 2126
 			LOG(L_CRIT, "BUG: tcpconn_chld_put: %p bad flags = %0x\n",
2130 2127
 					tcpconn, tcpconn->flags);
2131 2128
 			abort();
... ...
@@ -2179,8 +2176,7 @@ inline static void tcpconn_destroy(struct tcp_connection* tcpconn)
2179 2176
 inline static int tcpconn_put_destroy(struct tcp_connection* tcpconn)
2180 2177
 {
2181 2178
 	if (unlikely((tcpconn->flags & 
2182
-						(F_CONN_WRITE_W|F_CONN_HASHED|F_CONN_MAIN_TIMER)) ||
2183
-				!(tcpconn->flags & F_CONN_REMOVED) )){
2179
+			(F_CONN_WRITE_W|F_CONN_HASHED|F_CONN_MAIN_TIMER|F_CONN_READ_W)) )){
2184 2180
 		/* sanity check */
2185 2181
 		if (unlikely(tcpconn->flags & F_CONN_HASHED)){
2186 2182
 			LOG(L_CRIT, "BUG: tcpconn_destroy: called with hashed and/or"
... ...
@@ -2559,8 +2555,8 @@ inline static int handle_tcp_child(struct tcp_child* tcp_c, int fd_i)
2559 2555
 			local_timer_reinit(&tcpconn->timer);
2560 2556
 			local_timer_add(&tcp_main_ltimer, &tcpconn->timer, crt_timeout, t);
2561 2557
 			/* must be after the de-ref*/
2562
-			tcpconn->flags|=F_CONN_MAIN_TIMER;
2563
-			tcpconn->flags&=~(F_CONN_REMOVED|F_CONN_READER|F_CONN_OOB_DATA);
2558
+			tcpconn->flags|=(F_CONN_MAIN_TIMER|F_CONN_READ_W);
2559
+			tcpconn->flags&=~(F_CONN_READER|F_CONN_OOB_DATA);
2564 2560
 #ifdef TCP_BUF_WRITE
2565 2561
 			if (unlikely(tcpconn->flags & F_CONN_WRITE_W))
2566 2562
 				n=io_watch_chg(&io_h, tcpconn->s, POLLIN| POLLOUT, -1);
... ...
@@ -2570,7 +2566,7 @@ inline static int handle_tcp_child(struct tcp_child* tcp_c, int fd_i)
2570 2566
 			if (unlikely(n<0)){
2571 2567
 				LOG(L_CRIT, "ERROR: tcp_main: handle_tcp_child: failed to add"
2572 2568
 						" new socket to the fd list\n");
2573
-				tcpconn->flags|=F_CONN_REMOVED;
2569
+				tcpconn->flags&=~F_CONN_READ_W;
2574 2570
 #ifdef TCP_BUF_WRITE
2575 2571
 				if (unlikely(tcpconn->flags & F_CONN_WRITE_W)){
2576 2572
 					io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING);
... ...
@@ -2711,11 +2707,10 @@ inline static int handle_ser_child(struct process_table* p, int fd_i)
2711 2707
 			if ( tcpconn_try_unhash(tcpconn) )
2712 2708
 				tcpconn_put(tcpconn);
2713 2709
 #endif /* TCP_CONNECT_WAIT */
2714
-			if ( (!(tcpconn->flags & F_CONN_REMOVED) ||
2715
-					(tcpconn->flags & F_CONN_WRITE_W) ) && (tcpconn->s!=-1)){
2710
+			if ( ((tcpconn->flags & (F_CONN_WRITE_W|F_CONN_READ_W)) ) &&
2711
+					(tcpconn->s!=-1)){
2716 2712
 				io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING);
2717
-				tcpconn->flags|=F_CONN_REMOVED;
2718
-				tcpconn->flags&=~F_CONN_WRITE_W;
2713
+				tcpconn->flags&=~(F_CONN_WRITE_W|F_CONN_READ_W);
2719 2714
 			}
2720 2715
 			tcpconn_put_destroy(tcpconn); /* dec refcnt & destroy on 0 */
2721 2716
 			break;
... ...
@@ -2750,8 +2745,8 @@ inline static int handle_ser_child(struct process_table* p, int fd_i)
2750 2745
 			 * no need for reinit */
2751 2746
 			local_timer_add(&tcp_main_ltimer, &tcpconn->timer, 
2752 2747
 								tcp_con_lifetime, t);
2753
-			tcpconn->flags|=F_CONN_MAIN_TIMER;
2754
-			tcpconn->flags&=~(F_CONN_REMOVED|F_CONN_FD_CLOSED);
2748
+			tcpconn->flags|=(F_CONN_MAIN_TIMER|F_CONN_READ_W);
2749
+			tcpconn->flags&=~F_CONN_FD_CLOSED;
2755 2750
 			flags=POLLIN 
2756 2751
 #ifdef TCP_BUF_WRITE
2757 2752
 					/* not used for now, the connection is sent to tcp_main
... ...
@@ -2765,8 +2760,7 @@ inline static int handle_ser_child(struct process_table* p, int fd_i)
2765 2760
 												F_TCPCONN, tcpconn)<0)){
2766 2761
 				LOG(L_CRIT, "ERROR: tcp_main: handle_ser_child: failed to add"
2767 2762
 						" new socket to the fd list\n");
2768
-				tcpconn->flags|=F_CONN_REMOVED;
2769
-				tcpconn->flags&=~F_CONN_WRITE_W;
2763
+				tcpconn->flags&=~(F_CONN_WRITE_W|F_CONN_READ_W);
2770 2764
 				tcpconn_try_unhash(tcpconn); /*  unhash & dec refcnt */
2771 2765
 				tcpconn_put_destroy(tcpconn);
2772 2766
 			}
... ...
@@ -2796,7 +2790,7 @@ inline static int handle_ser_child(struct process_table* p, int fd_i)
2796 2790
 							"timeout adjusted to %d s\n", tcpconn, 
2797 2791
 							TICKS_TO_S(tcpconn->wbuf_q.wr_timeout-t));
2798 2792
 				}
2799
-				if (tcpconn->flags& F_CONN_REMOVED){
2793
+				if (!(tcpconn->flags & F_CONN_READ_W)){
2800 2794
 					if (unlikely(io_watch_add(&io_h, tcpconn->s, POLLOUT,
2801 2795
 												F_TCPCONN, tcpconn)<0)){
2802 2796
 						LOG(L_CRIT, "ERROR: tcp_main: handle_ser_child: failed"
... ...
@@ -2811,7 +2805,7 @@ inline static int handle_ser_child(struct process_table* p, int fd_i)
2811 2805
 						LOG(L_CRIT, "ERROR: tcp_main: handle_ser_child: failed"
2812 2806
 								    " to change socket watch events\n");
2813 2807
 						io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING);
2814
-						tcpconn->flags|=F_CONN_REMOVED;
2808
+						tcpconn->flags&=~F_CONN_READ_W;
2815 2809
 						if (tcpconn_try_unhash(tcpconn))
2816 2810
 							tcpconn_put_destroy(tcpconn);
2817 2811
 						break;
... ...
@@ -2831,7 +2825,7 @@ inline static int handle_ser_child(struct process_table* p, int fd_i)
2831 2825
 				 * the connection is already in the hash with S_CONN_PENDING
2832 2826
 				 * state (added by tcp_send()) and refcnt at least 1 (for the
2833 2827
 				 *  hash)*/
2834
-			tcpconn->flags&=~(F_CONN_PENDING|F_CONN_FD_CLOSED|F_CONN_REMOVED);
2828
+			tcpconn->flags&=~(F_CONN_PENDING|F_CONN_FD_CLOSED);
2835 2829
 			if (unlikely((tcpconn->state==S_CONN_BAD) || (fd==-1))){
2836 2830
 				if (unlikely(fd==-1))
2837 2831
 					LOG(L_CRIT, "BUG: handle_ser_child: CONN_NEW_COMPLETE:"
... ...
@@ -2855,7 +2849,7 @@ inline static int handle_ser_child(struct process_table* p, int fd_i)
2855 2849
 			 * no need for reinit */
2856 2850
 			local_timer_add(&tcp_main_ltimer, &tcpconn->timer, 
2857 2851
 								tcp_con_lifetime, t);
2858
-			tcpconn->flags|=F_CONN_MAIN_TIMER;
2852
+			tcpconn->flags|=F_CONN_MAIN_TIMER|F_CONN_READ_W;
2859 2853
 			if (unlikely(cmd==CONN_NEW_COMPLETE)){
2860 2854
 				/* check if needs to be watched for write */
2861 2855
 				lock_get(&tcpconn->write_lock);
... ...
@@ -2875,8 +2869,7 @@ inline static int handle_ser_child(struct process_table* p, int fd_i)
2875 2869
 												F_TCPCONN, tcpconn)<0)){
2876 2870
 				LOG(L_CRIT, "ERROR: tcp_main: handle_ser_child: failed to add"
2877 2871
 						" new socket to the fd list\n");
2878
-				tcpconn->flags|=F_CONN_REMOVED;
2879
-				tcpconn->flags&=~F_CONN_WRITE_W;
2872
+				tcpconn->flags&=~(F_CONN_WRITE_W|F_CONN_READ_W);
2880 2873
 				tcpconn_try_unhash(tcpconn); /*  unhash & dec refcnt */
2881 2874
 				tcpconn_put_destroy(tcpconn);
2882 2875
 			}
... ...
@@ -3034,13 +3027,12 @@ static inline int handle_new_connect(struct socket_info* si)
3034 3027
 		/* activate the timer */
3035 3028
 		local_timer_add(&tcp_main_ltimer, &tcpconn->timer, 
3036 3029
 								tcp_con_lifetime, get_ticks_raw());
3037
-		tcpconn->flags|=F_CONN_MAIN_TIMER;
3038
-		tcpconn->flags&=~F_CONN_REMOVED;
3030
+		tcpconn->flags|=(F_CONN_MAIN_TIMER|F_CONN_READ_W);
3039 3031
 		if (unlikely(io_watch_add(&io_h, tcpconn->s, POLLIN, 
3040 3032
 													F_TCPCONN, tcpconn)<0)){
3041 3033
 			LOG(L_CRIT, "ERROR: tcp_main: handle_new_connect: failed to add"
3042 3034
 						" new socket to the fd list\n");
3043
-			tcpconn->flags|=F_CONN_REMOVED;
3035
+			tcpconn->flags&=~F_CONN_READ_W;
3044 3036
 			if (tcpconn_try_unhash(tcpconn))
3045 3037
 				tcpconn_put_destroy(tcpconn);
3046 3038
 		}
... ...
@@ -3117,7 +3109,7 @@ inline static int handle_tcpconn_ev(struct tcp_connection* tcpconn, short ev,
3117 3109
 				LOG(L_ERR, "ERROR: handle_tcpconn_ev: io_watch_del(1) failed:"
3118 3110
 							" for %p, fd %d\n", tcpconn, tcpconn->s);
3119 3111
 			}
3120
-			if (!(tcpconn->flags & F_CONN_REMOVED) && (ev & POLLIN)){
3112
+			if ((tcpconn->flags & F_CONN_READ_W) && (ev & POLLIN)){
3121 3113
 				/* connection is watched for read and there is a read event
3122 3114
 				 * (unfortunately if we have POLLIN here we don't know if 
3123 3115
 				 * there's really any data in the read buffer or the POLLIN
... ...
@@ -3129,15 +3121,13 @@ inline static int handle_tcpconn_ev(struct tcp_connection* tcpconn, short ev,
3129 3121
 				 * conn.  to a a child only if needed (another syscall + at 
3130 3122
 				 * least 2 * syscalls in the reader + ...) */
3131 3123
 				if ((ioctl(tcpconn->s, FIONREAD, &bytes)>=0) && (bytes>0)){
3132
-					tcpconn->flags&=~F_CONN_WRITE_W;
3133
-					tcpconn->flags|=F_CONN_REMOVED;
3124
+					tcpconn->flags&=~(F_CONN_WRITE_W|F_CONN_READ_W);
3134 3125
 					tcpconn->flags|=F_CONN_FORCE_EOF|F_CONN_WR_ERROR;
3135 3126
 					goto send_to_child;
3136 3127
 				}
3137 3128
 				/* if bytes==0 or ioctl failed, destroy the connection now */
3138 3129
 			}
3139
-			tcpconn->flags&=~F_CONN_WRITE_W;
3140
-			tcpconn->flags|=F_CONN_REMOVED;
3130
+			tcpconn->flags&=~(F_CONN_WRITE_W|F_CONN_READ_W);
3141 3131
 			if (unlikely(!tcpconn_try_unhash(tcpconn))){
3142 3132
 				LOG(L_CRIT, "BUG: tcpconn_ev: unhashed connection %p\n",
3143 3133
 							tcpconn);
... ...
@@ -3146,7 +3136,7 @@ inline static int handle_tcpconn_ev(struct tcp_connection* tcpconn, short ev,
3146 3136
 			goto error;
3147 3137
 		}
3148 3138
 		if (empty_q){
3149
-			if (tcpconn->flags & F_CONN_REMOVED){
3139
+			if (!(tcpconn->flags & F_CONN_READ_W)){
3150 3140
 				if (unlikely(io_watch_del(&io_h, tcpconn->s, fd_i, 0)==-1)){
3151 3141
 					LOG(L_ERR, "ERROR: handle_tcpconn_ev: io_watch_del(2)"
3152 3142
 								" failed:" " for %p, fd %d\n",
... ...
@@ -3166,7 +3156,7 @@ inline static int handle_tcpconn_ev(struct tcp_connection* tcpconn, short ev,
3166 3156
 		}
3167 3157
 		ev&=~POLLOUT; /* clear POLLOUT */
3168 3158
 	}
3169
-	if (likely(ev && !(tcpconn->flags & F_CONN_REMOVED))){
3159
+	if (likely(ev && (tcpconn->flags & F_CONN_READ_W))){
3170 3160
 		/* if still some other IO event (POLLIN|POLLHUP|POLLERR) and
3171 3161
 		 * connection is still watched in tcp_main for reads, send it to a
3172 3162
 		 * child and stop watching it for input (but continue watching for
... ...
@@ -3199,9 +3189,9 @@ send_to_child:
3199 3189
 		tcpconn->flags|=((int)!(ev & (POLLHUP|POLLERR)) -1) & F_CONN_EOF_SEEN;
3200 3190
 #endif /* POLLRDHUP */
3201 3191
 		tcpconn->flags|= ((int)!(ev & POLLPRI) -1)  & F_CONN_OOB_DATA;
3202
-		tcpconn->flags|=F_CONN_REMOVED|F_CONN_READER;
3192
+		tcpconn->flags|=F_CONN_READER;
3203 3193
 		local_timer_del(&tcp_main_ltimer, &tcpconn->timer);
3204
-		tcpconn->flags&=~F_CONN_MAIN_TIMER;
3194
+		tcpconn->flags&=~(F_CONN_MAIN_TIMER|F_CONN_READ_W);
3205 3195
 		tcpconn_ref(tcpconn); /* refcnt ++ */
3206 3196
 		if (unlikely(send2child(tcpconn)<0)){
3207 3197
 			LOG(L_ERR,"ERROR: handle_tcpconn_ev: no children available\n");
... ...
@@ -3323,16 +3313,9 @@ static ticks_t tcpconn_main_timeout(ticks_t t, struct timer_ln* tl, void* data)
3323 3313
 	}
3324 3314
 	fd=c->s;
3325 3315
 	if (likely(fd>0)){
3326
-		if (likely(!(c->flags & F_CONN_REMOVED)
3327
-#ifdef TCP_BUF_WRITE
3328
-			|| (c->flags & F_CONN_WRITE_W)
3329
-#endif /* TCP_BUF_WRITE */
3330
-			)){
3316
+		if (likely(c->flags & (F_CONN_READ_W|F_CONN_WRITE_W))){
3331 3317
 			io_watch_del(&io_h, fd, -1, IO_FD_CLOSING);
3332
-			c->flags|=F_CONN_REMOVED;
3333
-#ifdef TCP_BUF_WRITE
3334
-			c->flags&=~F_CONN_WRITE_W;
3335
-#endif /* TCP_BUF_WRITE */
3318
+			c->flags&=~(F_CONN_READ_W|F_CONN_WRITE_W);
3336 3319
 		}
3337 3320
 	}
3338 3321
 	tcpconn_put_destroy(c);
... ...
@@ -3379,16 +3362,9 @@ static inline void tcpconn_destroy_all()
3379 3362
 						c->flags&=~F_CONN_MAIN_TIMER;
3380 3363
 					} /* else still in some reader */
3381 3364
 					fd=c->s;
3382
-					if (fd>0 && (!(c->flags & F_CONN_REMOVED)
3383
-#ifdef TCP_BUF_WRITE
3384
-								|| (c->flags & F_CONN_WRITE_W)
3385
-#endif /* TCP_BUF_WRITE */
3386
-								)){
3365
+					if (fd>0 && (c->flags & (F_CONN_READ_W|F_CONN_WRITE_W))){
3387 3366
 						io_watch_del(&io_h, fd, -1, IO_FD_CLOSING);
3388
-						c->flags|=F_CONN_REMOVED;
3389
-#ifdef TCP_BUF_WRITE
3390
-						c->flags&=~F_CONN_WRITE_W;
3391
-#endif /* TCP_BUF_WRITE */
3367
+						c->flags&=~(F_CONN_READ_W|F_CONN_WRITE_W);
3392 3368
 					}
3393 3369
 				}else{
3394 3370
 					fd=-1;