Browse Source

Use auto-colourisation for debug messages.

Truncate TX length to TCP window at time of transmission rather than at
time of adding to TX packet; this is conceptually cleaner and also allows
the application to call tcp_send() multiple times to build up a single
packet.
tags/v0.9.3
Michael Brown 18 years ago
parent
commit
feb4f65d1e
1 changed files with 44 additions and 45 deletions
  1. 44
    45
      src/net/tcp.c

+ 44
- 45
src/net/tcp.c View File

112
 tcp_dump_state ( struct tcp_connection *conn ) {
112
 tcp_dump_state ( struct tcp_connection *conn ) {
113
 
113
 
114
 	if ( conn->tcp_state != conn->prev_tcp_state ) {
114
 	if ( conn->tcp_state != conn->prev_tcp_state ) {
115
-		DBG ( "TCP %p transitioned from %s to %s\n", conn,
116
-		      tcp_state ( conn->prev_tcp_state ),
117
-		      tcp_state ( conn->tcp_state ) );
115
+		DBGC ( conn, "TCP %p transitioned from %s to %s\n", conn,
116
+		       tcp_state ( conn->prev_tcp_state ),
117
+		       tcp_state ( conn->tcp_state ) );
118
 	}
118
 	}
119
 	conn->prev_tcp_state = conn->tcp_state;
119
 	conn->prev_tcp_state = conn->tcp_state;
120
 }
120
 }
125
  * @v flags		TCP flags
125
  * @v flags		TCP flags
126
  */
126
  */
127
 static inline __attribute__ (( always_inline )) void
127
 static inline __attribute__ (( always_inline )) void
128
-tcp_dump_flags ( unsigned int flags ) {
128
+tcp_dump_flags ( struct tcp_connection *conn, unsigned int flags ) {
129
 	if ( flags & TCP_RST )
129
 	if ( flags & TCP_RST )
130
-		DBG ( " RST" );
130
+		DBGC ( conn, " RST" );
131
 	if ( flags & TCP_SYN )
131
 	if ( flags & TCP_SYN )
132
-		DBG ( " SYN" );
132
+		DBGC ( conn, " SYN" );
133
 	if ( flags & TCP_PSH )
133
 	if ( flags & TCP_PSH )
134
-		DBG ( " PSH" );
134
+		DBGC ( conn, " PSH" );
135
 	if ( flags & TCP_FIN )
135
 	if ( flags & TCP_FIN )
136
-		DBG ( " FIN" );
136
+		DBGC ( conn, " FIN" );
137
 	if ( flags & TCP_ACK )
137
 	if ( flags & TCP_ACK )
138
-		DBG ( " ACK" );
138
+		DBGC ( conn, " ACK" );
139
 }
139
 }
140
 
140
 
141
 /**
141
 /**
150
 
150
 
151
 	conn = calloc ( 1, sizeof ( *conn ) );
151
 	conn = calloc ( 1, sizeof ( *conn ) );
152
 	if ( conn ) {
152
 	if ( conn ) {
153
-		DBG ( "TCP %p allocated\n", conn );
153
+		DBGC ( conn, "TCP %p allocated\n", conn );
154
 		conn->tcp_state = conn->prev_tcp_state = TCP_CLOSED;
154
 		conn->tcp_state = conn->prev_tcp_state = TCP_CLOSED;
155
 		conn->snd_seq = random();
155
 		conn->snd_seq = random();
156
 		conn->timer.expired = tcp_expired;
156
 		conn->timer.expired = tcp_expired;
176
 	stop_timer ( &conn->timer );
176
 	stop_timer ( &conn->timer );
177
 	list_del ( &conn->list );
177
 	list_del ( &conn->list );
178
 	free ( conn );
178
 	free ( conn );
179
-	DBG ( "TCP %p freed\n", conn );
179
+	DBGC ( conn, "TCP %p freed\n", conn );
180
 }
180
 }
181
 
181
 
182
 /**
182
 /**
191
 	assert ( app->conn == NULL );
191
 	assert ( app->conn == NULL );
192
 	conn->app = app;
192
 	conn->app = app;
193
 	app->conn = conn;
193
 	app->conn = conn;
194
-	DBG ( "TCP %p associated with application %p\n", conn, app );
194
+	DBGC ( conn, "TCP %p associated with application %p\n", conn, app );
195
 }
195
 }
196
 
196
 
197
 /**
197
 /**
206
 		assert ( app->conn == conn );
206
 		assert ( app->conn == conn );
207
 		conn->app = NULL;
207
 		conn->app = NULL;
208
 		app->conn = NULL;
208
 		app->conn = NULL;
209
-		DBG ( "TCP %p disassociated from application %p\n",
210
-		      conn, app );
209
+		DBGC ( conn, "TCP %p disassociated from application %p\n",
210
+		       conn, app );
211
 	}
211
 	}
212
 }
212
 }
213
 
213
 
236
 	/* Allocate space to the TX buffer */
236
 	/* Allocate space to the TX buffer */
237
 	pkb = alloc_pkb ( MAX_PKB_LEN );
237
 	pkb = alloc_pkb ( MAX_PKB_LEN );
238
 	if ( ! pkb ) {
238
 	if ( ! pkb ) {
239
-		DBG ( "TCP %p could not allocate senddata buffer\n", conn );
239
+		DBGC ( conn, "TCP %p could not allocate data buffer\n", conn );
240
 		/* Start the retry timer so that we attempt to
240
 		/* Start the retry timer so that we attempt to
241
 		 * retransmit this packet later.  (Start it
241
 		 * retransmit this packet later.  (Start it
242
 		 * unconditionally, since without a packet buffer we
242
 		 * unconditionally, since without a packet buffer we
243
-		 * can't can the senddata() callback, and so may not
243
+		 * can't call the senddata() callback, and so may not
244
 		 * be able to tell whether or not we have something
244
 		 * be able to tell whether or not we have something
245
 		 * that actually needs to be retransmitted).
245
 		 * that actually needs to be retransmitted).
246
 		 */
246
 		 */
259
 		conn->tx_pkb = NULL;
259
 		conn->tx_pkb = NULL;
260
 	}
260
 	}
261
 
261
 
262
+	/* Truncate payload length to fit transmit window */
263
+	len = pkb_len ( pkb );
264
+	if ( len > conn->snd_win )
265
+		len = conn->snd_win;
266
+
262
 	/* Calculate amount of sequence space that this transmission
267
 	/* Calculate amount of sequence space that this transmission
263
 	 * consumes.  (SYN or FIN consume one byte, and we can never
268
 	 * consumes.  (SYN or FIN consume one byte, and we can never
264
 	 * send both at once).
269
 	 * send both at once).
265
 	 */
270
 	 */
266
-	len = pkb_len ( pkb );
267
 	seq_len = len;
271
 	seq_len = len;
268
 	flags = TCP_FLAGS_SENDING ( conn->tcp_state );
272
 	flags = TCP_FLAGS_SENDING ( conn->tcp_state );
269
 	assert ( ! ( ( flags & TCP_SYN ) && ( flags & TCP_FIN ) ) );
273
 	assert ( ! ( ( flags & TCP_SYN ) && ( flags & TCP_FIN ) ) );
297
 	tcphdr->csum = tcpip_chksum ( pkb->data, pkb_len ( pkb ) );
301
 	tcphdr->csum = tcpip_chksum ( pkb->data, pkb_len ( pkb ) );
298
 
302
 
299
 	/* Dump header */
303
 	/* Dump header */
300
-	DBG ( "TCP %p TX %d->%d %08lx..%08lx           %08lx %4zd", conn,
301
-	      ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
302
-	      ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) + seq_len ),
303
-	      ntohl ( tcphdr->ack ), len );
304
-	tcp_dump_flags ( tcphdr->flags );
305
-	DBG ( "\n" );
304
+	DBGC ( conn, "TCP %p TX %d->%d %08lx..%08lx           %08lx %4zd",
305
+	       conn, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
306
+	       ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) + seq_len ),
307
+	       ntohl ( tcphdr->ack ), len );
308
+	tcp_dump_flags ( conn, tcphdr->flags );
309
+	DBGC ( conn, "\n" );
306
 
310
 
307
 	/* Transmit packet */
311
 	/* Transmit packet */
308
 	return tcpip_tx ( pkb, &tcp_protocol, &conn->peer );
312
 	return tcpip_tx ( pkb, &tcp_protocol, &conn->peer );
359
 		return -EINVAL;
363
 		return -EINVAL;
360
 	}
364
 	}
361
 
365
 
362
-	/* Truncate length to fit transmit window */
363
-	if ( len > conn->snd_win )
364
-		len = conn->snd_win;
365
-
366
 	/* Truncate length to fit packet buffer */
366
 	/* Truncate length to fit packet buffer */
367
 	if ( len > pkb_available ( pkb ) )
367
 	if ( len > pkb_available ( pkb ) )
368
 		len = pkb_available ( pkb );
368
 		len = pkb_available ( pkb );
385
 	struct tcp_application *app = conn->app;
385
 	struct tcp_application *app = conn->app;
386
 	int graceful_close = TCP_CLOSED_GRACEFULLY ( conn->tcp_state );
386
 	int graceful_close = TCP_CLOSED_GRACEFULLY ( conn->tcp_state );
387
 
387
 
388
-	DBG ( "TCP %p timer %s in %s\n", conn,
389
-	      ( over ? "expired" : "fired" ), tcp_state ( conn->tcp_state ) );
388
+	DBGC ( conn, "TCP %p timer %s in %s\n", conn,
389
+	       ( over ? "expired" : "fired" ), tcp_state ( conn->tcp_state ) );
390
 
390
 
391
 	assert ( ( conn->tcp_state == TCP_SYN_SENT ) ||
391
 	assert ( ( conn->tcp_state == TCP_SYN_SENT ) ||
392
 		 ( conn->tcp_state == TCP_SYN_RCVD ) ||
392
 		 ( conn->tcp_state == TCP_SYN_RCVD ) ||
485
 
485
 
486
 	/* Ignore duplicate or out-of-range ACK */
486
 	/* Ignore duplicate or out-of-range ACK */
487
 	if ( ack_len > conn->snd_sent ) {
487
 	if ( ack_len > conn->snd_sent ) {
488
-		DBG ( "TCP %p received ACK for [%08lx,%08lx), sent only "
489
-		      "[%08lx,%08lx)\n", conn, conn->snd_seq,
490
-		      ( conn->snd_seq + ack_len ), conn->snd_seq,
491
-		      ( conn->snd_seq + conn->snd_sent ) );
488
+		DBGC ( conn, "TCP %p received ACK for [%08lx,%08lx), "
489
+		       "sent only [%08lx,%08lx)\n", conn, conn->snd_seq,
490
+		       ( conn->snd_seq + ack_len ), conn->snd_seq,
491
+		       ( conn->snd_seq + conn->snd_sent ) );
492
 		return -EINVAL;
492
 		return -EINVAL;
493
 	}
493
 	}
494
 
494
 
643
 	len = pkb_len ( pkb );
643
 	len = pkb_len ( pkb );
644
 
644
 
645
 	/* Dump header */
645
 	/* Dump header */
646
-	DBG ( "TCP %p RX %d<-%d %08lx           %08lx..%08lx %4zd", conn,
647
-	      ntohs ( tcphdr->dest ), ntohs ( tcphdr->src ),
648
-	      ntohl ( tcphdr->ack ), ntohl ( tcphdr->seq ),
649
-	      ( ntohl ( tcphdr->seq ) + len +
650
-		( ( tcphdr->flags & ( TCP_SYN | TCP_FIN ) ) ? 1 : 0 ) ), len );
651
-	tcp_dump_flags ( tcphdr->flags );
652
-	DBG ( "\n" );
646
+	DBGC ( conn, "TCP %p RX %d<-%d           %08lx %08lx..%08lx %4zd",
647
+	       conn, ntohs ( tcphdr->dest ), ntohs ( tcphdr->src ),
648
+	       ntohl ( tcphdr->ack ), ntohl ( tcphdr->seq ),
649
+	       ( ntohl ( tcphdr->seq ) + len +
650
+		 ( ( tcphdr->flags & ( TCP_SYN | TCP_FIN ) ) ? 1 : 0 ) ), len);
651
+	tcp_dump_flags ( conn, tcphdr->flags );
652
+	DBGC ( conn, "\n" );
653
 
653
 
654
 	/* If no connection was found, create dummy connection for
654
 	/* If no connection was found, create dummy connection for
655
 	 * sending RST
655
 	 * sending RST
731
 			if ( tcp_bind ( conn, htons ( try_port ) ) == 0 )
731
 			if ( tcp_bind ( conn, htons ( try_port ) ) == 0 )
732
 				return 0;
732
 				return 0;
733
 		}
733
 		}
734
-		DBG ( "TCP %p could not bind: no free ports remaining\n",
735
-		      conn );
734
+		DBGC ( conn, "TCP %p could not bind: no free ports\n", conn );
736
 		return -EADDRINUSE;
735
 		return -EADDRINUSE;
737
 	}
736
 	}
738
 
737
 
739
 	/* Attempt bind to local port */
738
 	/* Attempt bind to local port */
740
 	list_for_each_entry ( existing, &tcp_conns, list ) {
739
 	list_for_each_entry ( existing, &tcp_conns, list ) {
741
 		if ( existing->local_port == local_port ) {
740
 		if ( existing->local_port == local_port ) {
742
-			DBG ( "TCP %p could not bind: port %d in use\n",
743
-			      conn, ntohs ( local_port ) );
741
+			DBGC ( conn, "TCP %p could not bind: port %d in use\n",
742
+			       conn, ntohs ( local_port ) );
744
 			return -EADDRINUSE;
743
 			return -EADDRINUSE;
745
 		}
744
 		}
746
 	}
745
 	}
747
 	conn->local_port = local_port;
746
 	conn->local_port = local_port;
748
 
747
 
749
-	DBG ( "TCP %p bound to port %d\n", conn, ntohs ( local_port ) );
748
+	DBGC ( conn, "TCP %p bound to port %d\n", conn, ntohs ( local_port ) );
750
 	return 0;
749
 	return 0;
751
 }
750
 }
752
 
751
 

Loading…
Cancel
Save