Browse Source

Moved uIP wrapper out of prototester.c and into proto/tcp.c.

tags/v0.9.3
Michael Brown 18 years ago
parent
commit
a4a0244aa3
3 changed files with 312 additions and 135 deletions
  1. 102
    0
      src/include/gpxe/tcp.h
  2. 206
    0
      src/proto/tcp.c
  3. 4
    135
      src/util/prototester.c

+ 102
- 0
src/include/gpxe/tcp.h View File

@@ -0,0 +1,102 @@
1
+#ifndef _TCP_H
2
+#define _TCP_H
3
+
4
+/** @file
5
+ *
6
+ * TCP protocol
7
+ *
8
+ * This file defines the gPXE TCP API.
9
+ *
10
+ */
11
+
12
+#include <stddef.h>
13
+#include <gpxe/in.h>
14
+
15
+struct tcp_connection;
16
+
17
+/**
18
+ * TCP operations
19
+ *
20
+ */
21
+struct tcp_operations {
22
+	/**
23
+	 * Connection aborted (RST received)
24
+	 *
25
+	 * @v conn	TCP connection
26
+	 */
27
+	void ( * aborted ) ( struct tcp_connection *conn );
28
+	/**
29
+	 * Connection timed out
30
+	 *
31
+	 * @v conn	TCP connection
32
+	 */
33
+	void ( * timedout ) ( struct tcp_connection *conn );
34
+	/**
35
+	 * Connection aborted (FIN received)
36
+	 *
37
+	 * @v conn	TCP connection
38
+	 *
39
+	 * Note that acked() and newdata() may be called after
40
+	 * closed(), if the packet containing the FIN also
41
+	 * acknowledged data or contained new data.
42
+	 */
43
+	void ( * closed ) ( struct tcp_connection *conn );
44
+	/**
45
+	 * Connection established (SYNACK received)
46
+	 *
47
+	 * @v conn	TCP connection
48
+	 */
49
+	void ( * connected ) ( struct tcp_connection *conn );
50
+	/**
51
+	 * Data acknowledged
52
+	 *
53
+	 * @v conn	TCP connection
54
+	 * @v len	Length of acknowledged data
55
+	 *
56
+	 * @c len is guaranteed to not exceed the outstanding amount
57
+	 * of unacknowledged data.
58
+	 */
59
+	void ( * acked ) ( struct tcp_connection *conn, size_t len );
60
+	/**
61
+	 * New data received
62
+	 *
63
+	 * @v conn	TCP connection
64
+	 * @v data	Data
65
+	 * @v len	Length of data
66
+	 */
67
+	void ( * newdata ) ( struct tcp_connection *conn,
68
+			     void *data, size_t len );
69
+	/**
70
+	 * Transmit data
71
+	 *
72
+	 * @v conn	TCP connection
73
+	 *
74
+	 * The application should transmit whatever it currently wants
75
+	 * to send using tcp_send().  If retransmissions are required,
76
+	 * senddata() will be called again and the application must
77
+	 * regenerate the data.  The easiest way to implement this is
78
+	 * to ensure that senddata() never changes the application's
79
+	 * state.
80
+	 */
81
+	void ( * senddata ) ( struct tcp_connection *conn );
82
+};
83
+
84
+/**
85
+ * A TCP connection
86
+ *
87
+ */
88
+struct tcp_connection {
89
+	/** Address of the remote end of the connection */
90
+	struct sockaddr_in sin;
91
+	/** Operations table for this connection */
92
+	struct tcp_operations *tcp_op;
93
+};
94
+
95
+extern int tcp_connect ( struct tcp_connection *conn );
96
+extern void tcp_send ( struct tcp_connection *conn, const void *data,
97
+		       size_t len );
98
+extern void tcp_close ( struct tcp_connection *conn );
99
+extern void init_tcpip ( void );
100
+extern void run_tcpip ( void );
101
+
102
+#endif /* _TCP_H */

+ 206
- 0
src/proto/tcp.c View File

@@ -0,0 +1,206 @@
1
+#include <string.h>
2
+#include <assert.h>
3
+#include <byteswap.h>
4
+#include <gpxe/tcp.h>
5
+#include "uip/uip.h"
6
+#include "uip/uip_arp.h"
7
+
8
+/** @file
9
+ *
10
+ * TCP protocol
11
+ *
12
+ * The gPXE TCP stack is currently implemented on top of the uIP
13
+ * protocol stack.  This file provides wrappers around uIP so that
14
+ * higher-level protocol implementations do not need to talk directly
15
+ * to uIP (which has a somewhat baroque API).
16
+ *
17
+ * Basic operation is to create a #tcp_connection structure, call
18
+ * tcp_connect() and then call run_tcpip() in a loop until the
19
+ * operation has completed.  The TCP stack will call the various
20
+ * methods defined in the #tcp_operations structure in order to send
21
+ * and receive data.
22
+ *
23
+ * See hello.c for a trivial example of a TCP protocol using this
24
+ * API.
25
+ *
26
+ */
27
+
28
+/**
29
+ * Initialise TCP/IP stack
30
+ *
31
+ */
32
+void init_tcpip ( void ) {
33
+	uip_init();
34
+	uip_arp_init();
35
+}
36
+
37
+#define UIP_HLEN ( 40 + UIP_LLH_LEN )
38
+
39
+/**
40
+ * Transmit TCP data
41
+ *
42
+ * This is a wrapper around netdev_transmit().  It gathers up the
43
+ * packet produced by uIP, and then passes it to netdev_transmit() as
44
+ * a single buffer.
45
+ */
46
+static void uip_transmit ( void ) {
47
+	uip_arp_out();
48
+	if ( uip_len > UIP_HLEN ) {
49
+		memcpy ( uip_buf + UIP_HLEN, ( void * ) uip_appdata,
50
+			 uip_len - UIP_HLEN );
51
+	}
52
+	netdev_transmit ( uip_buf, uip_len );
53
+	uip_len = 0;
54
+}
55
+
56
+/**
57
+ * Run the TCP/IP stack
58
+ *
59
+ * Call this function in a loop in order to allow TCP/IP processing to
60
+ * take place.  This call takes the stack through a single iteration;
61
+ * it will typically be used in a loop such as
62
+ *
63
+ * @code
64
+ *
65
+ * struct tcp_connection *my_connection;
66
+ * ...
67
+ * tcp_connect ( my_connection );
68
+ * while ( ! my_connection->finished ) {
69
+ *   run_tcpip();
70
+ * }
71
+ *
72
+ * @endcode
73
+ *
74
+ * where @c my_connection->finished is set by one of the connection's
75
+ * #tcp_operations methods to indicate completion.
76
+ */
77
+void run_tcpip ( void ) {
78
+	void *data;
79
+	size_t len;
80
+	uint16_t type;
81
+	int i;
82
+	
83
+	if ( netdev_poll ( 1, &data, &len ) ) {
84
+		/* We have data */
85
+		memcpy ( uip_buf, data, len );
86
+		uip_len = len;
87
+		type = ntohs ( *( ( uint16_t * ) ( uip_buf + 12 ) ) );
88
+		if ( type == UIP_ETHTYPE_ARP ) {
89
+			uip_arp_arpin();
90
+		} else {
91
+			uip_arp_ipin();
92
+			uip_input();
93
+		}
94
+		if ( uip_len > 0 )
95
+			uip_transmit();
96
+	} else {
97
+		for ( i = 0 ; i < UIP_CONNS ; i++ ) {
98
+			uip_periodic ( i );
99
+			if ( uip_len > 0 )
100
+				uip_transmit();
101
+		}
102
+	}
103
+}
104
+
105
+/**
106
+ * Open a TCP connection
107
+ *
108
+ * @v conn	TCP connection
109
+ * @ret 0	Success
110
+ * @ret <0	Failure
111
+ * 
112
+ * This sets up a new TCP connection to the remote host specified in
113
+ * tcp_connection::sin.  The actual SYN packet will not be sent out
114
+ * until run_tcpip() is called for the first time.
115
+ *
116
+ * @todo Use linked lists instead of a static buffer, and thereby
117
+ *       remove the only potential failure case, giving this function
118
+ *       a void return type.
119
+ */
120
+int tcp_connect ( struct tcp_connection *conn ) {
121
+	struct uip_conn *uip_conn;
122
+	u16_t ipaddr[2];
123
+
124
+	assert ( conn->sin.sin_addr.s_addr != 0 );
125
+	assert ( conn->sin.sin_port != 0 );
126
+	assert ( conn->tcp_op != NULL );
127
+	assert ( sizeof ( uip_conn->appstate ) == sizeof ( conn ) );
128
+
129
+	* ( ( uint32_t * ) ipaddr ) = conn->sin.sin_addr.s_addr;
130
+	uip_conn = uip_connect ( ipaddr, conn->sin.sin_port );
131
+	if ( ! uip_conn )
132
+		return -1;
133
+
134
+	*( ( void ** ) uip_conn->appstate ) = conn;
135
+	return 0;
136
+}
137
+
138
+/**
139
+ * Send data via a TCP connection
140
+ *
141
+ * @v conn	TCP connection
142
+ * @v data	Data to send
143
+ * @v len	Length of data
144
+ *
145
+ * Data will be automatically limited to the current TCP window size.
146
+ *
147
+ * If retransmission is required, the connection's
148
+ * tcp_operations::newdata() method will be called again in order to
149
+ * regenerate the data.
150
+ */
151
+void tcp_send ( struct tcp_connection *conn __unused,
152
+		const void *data, size_t len ) {
153
+	assert ( conn = *( ( void ** ) uip_conn->appstate ) );
154
+	uip_send ( ( void * ) data, len );
155
+}
156
+
157
+/**
158
+ * Close a TCP connection
159
+ *
160
+ * @v conn	TCP connection
161
+ */
162
+void tcp_close ( struct tcp_connection *conn __unused ) {
163
+	assert ( conn = *( ( void ** ) uip_conn->appstate ) );
164
+	uip_close();
165
+}
166
+
167
+/**
168
+ * uIP TCP application call interface
169
+ *
170
+ * This is the entry point of gPXE from the point of view of the uIP
171
+ * protocol stack.  This function calls the appropriate methods from
172
+ * the connection's @tcp_operations table in order to process received
173
+ * data, transmit new data etc.
174
+ */
175
+void uip_tcp_appcall ( void ) {
176
+	struct tcp_connection *conn = *( ( void ** ) uip_conn->appstate );
177
+	struct tcp_operations *op = conn->tcp_op;
178
+
179
+	assert ( conn->tcp_op->closed != NULL );
180
+	assert ( conn->tcp_op->connected != NULL );
181
+	assert ( conn->tcp_op->acked != NULL );
182
+	assert ( conn->tcp_op->newdata != NULL );
183
+	assert ( conn->tcp_op->senddata != NULL );
184
+
185
+	if ( uip_aborted() && op->aborted ) /* optional method */
186
+		op->aborted ( conn );
187
+	if ( uip_timedout() && op->timedout ) /* optional method */
188
+		op->timedout ( conn );
189
+	if ( uip_closed() && op->closed ) /* optional method */
190
+		op->closed ( conn );
191
+	if ( uip_connected() )
192
+		op->connected ( conn );
193
+	if ( uip_acked() )
194
+		op->acked ( conn, uip_conn->len );
195
+	if ( uip_newdata() )
196
+		op->newdata ( conn, ( void * ) uip_appdata, uip_len );
197
+	if ( uip_rexmit() || uip_newdata() || uip_acked() ||
198
+	     uip_connected() || uip_poll() )
199
+		op->senddata ( conn );
200
+}
201
+
202
+/* Present here to allow everything to link.  Will go into separate
203
+ * udp.c file
204
+ */
205
+void uip_udp_appcall ( void ) {
206
+}

+ 4
- 135
src/util/prototester.c View File

@@ -8,11 +8,11 @@
8 8
 #include <sys/un.h>
9 9
 #include <net/if.h>
10 10
 #include <net/ethernet.h>
11
-#include <netinet/in.h>
12
-#include <arpa/inet.h>
13 11
 #include <getopt.h>
14 12
 #include <assert.h>
15 13
 
14
+#include <gpxe/tcp.h>
15
+
16 16
 typedef int irq_action_t;
17 17
 
18 18
 struct nic {
@@ -68,14 +68,14 @@ static inline void free_netdevice ( struct nic *nic ) {
68 68
 	/* Do nothing */
69 69
 }
70 70
 
71
-static int netdev_poll ( int retrieve, void **data, size_t *len ) {
71
+int netdev_poll ( int retrieve, void **data, size_t *len ) {
72 72
 	int rc = static_nic.nic_op->poll ( &static_nic, retrieve );
73 73
 	*data = static_nic.packet;
74 74
 	*len = static_nic.packetlen;
75 75
 	return rc;
76 76
 }
77 77
 
78
-static void netdev_transmit ( const void *data, size_t len ) {
78
+void netdev_transmit ( const void *data, size_t len ) {
79 79
 	uint16_t type = ntohs ( *( ( uint16_t * ) ( data + 12 ) ) );
80 80
 	static_nic.nic_op->transmit ( &static_nic, data, type,
81 81
 				      len - ETH_HLEN,
@@ -245,137 +245,6 @@ static void hijack_disable ( struct hijack_device *hijack_dev ) {
245 245
 	close ( hijack->fd );
246 246
 }
247 247
 
248
-/*****************************************************************************
249
- *
250
- * uIP wrapper layer
251
- *
252
- */
253
-
254
-#include "../proto/uip/uip.h"
255
-#include "../proto/uip/uip_arp.h"
256
-
257
-struct tcp_connection;
258
-
259
-struct tcp_operations {
260
-	void ( * aborted ) ( struct tcp_connection *conn );
261
-	void ( * timedout ) ( struct tcp_connection *conn );
262
-	void ( * closed ) ( struct tcp_connection *conn );
263
-	void ( * connected ) ( struct tcp_connection *conn );
264
-	void ( * acked ) ( struct tcp_connection *conn, size_t len );
265
-	void ( * newdata ) ( struct tcp_connection *conn,
266
-			     void *data, size_t len );
267
-	void ( * senddata ) ( struct tcp_connection *conn );
268
-};
269
-
270
-struct tcp_connection {
271
-	struct sockaddr_in sin;
272
-	struct tcp_operations *tcp_op;
273
-};
274
-
275
-int tcp_connect ( struct tcp_connection *conn ) {
276
-	struct uip_conn *uip_conn;
277
-	u16_t ipaddr[2];
278
-
279
-	assert ( conn->sin.sin_addr.s_addr != 0 );
280
-	assert ( conn->sin.sin_port != 0 );
281
-	assert ( conn->tcp_op != NULL );
282
-	assert ( sizeof ( uip_conn->appstate ) == sizeof ( conn ) );
283
-
284
-	* ( ( uint32_t * ) ipaddr ) = conn->sin.sin_addr.s_addr;
285
-	uip_conn = uip_connect ( ipaddr, conn->sin.sin_port );
286
-	if ( ! uip_conn )
287
-		return -1;
288
-
289
-	*( ( void ** ) uip_conn->appstate ) = conn;
290
-	return 0;
291
-}
292
-
293
-void tcp_send ( struct tcp_connection *conn, const void *data,
294
-		       size_t len ) {
295
-	assert ( conn = *( ( void ** ) uip_conn->appstate ) );
296
-	uip_send ( ( void * ) data, len );
297
-}
298
-
299
-void tcp_close ( struct tcp_connection *conn ) {
300
-	assert ( conn = *( ( void ** ) uip_conn->appstate ) );
301
-	uip_close();
302
-}
303
-
304
-void uip_tcp_appcall ( void ) {
305
-	struct tcp_connection *conn = *( ( void ** ) uip_conn->appstate );
306
-	struct tcp_operations *op = conn->tcp_op;
307
-
308
-	assert ( conn->tcp_op->closed != NULL );
309
-	assert ( conn->tcp_op->connected != NULL );
310
-	assert ( conn->tcp_op->acked != NULL );
311
-	assert ( conn->tcp_op->newdata != NULL );
312
-	assert ( conn->tcp_op->senddata != NULL );
313
-
314
-	if ( uip_aborted() && op->aborted ) /* optional method */
315
-		op->aborted ( conn );
316
-	if ( uip_timedout() && op->timedout ) /* optional method */
317
-		op->timedout ( conn );
318
-	if ( uip_closed() && op->closed ) /* optional method */
319
-		op->closed ( conn );
320
-	if ( uip_connected() )
321
-		op->connected ( conn );
322
-	if ( uip_acked() )
323
-		op->acked ( conn, uip_conn->len );
324
-	if ( uip_newdata() )
325
-		op->newdata ( conn, ( void * ) uip_appdata, uip_len );
326
-	if ( uip_rexmit() || uip_newdata() || uip_acked() ||
327
-	     uip_connected() || uip_poll() )
328
-		op->senddata ( conn );
329
-}
330
-
331
-void uip_udp_appcall ( void ) {
332
-}
333
-
334
-static void init_tcpip ( void ) {
335
-	uip_init();
336
-	uip_arp_init();
337
-}
338
-
339
-#define UIP_HLEN ( 40 + UIP_LLH_LEN )
340
-
341
-static void uip_transmit ( void ) {
342
-	uip_arp_out();
343
-	if ( uip_len > UIP_HLEN ) {
344
-		memcpy ( uip_buf + UIP_HLEN, ( void * ) uip_appdata,
345
-			 uip_len - UIP_HLEN );
346
-	}
347
-	netdev_transmit ( uip_buf, uip_len );
348
-	uip_len = 0;
349
-}
350
-
351
-static void run_tcpip ( void ) {
352
-	void *data;
353
-	size_t len;
354
-	uint16_t type;
355
-	int i;
356
-	
357
-	if ( netdev_poll ( 1, &data, &len ) ) {
358
-		/* We have data */
359
-		memcpy ( uip_buf, data, len );
360
-		uip_len = len;
361
-		type = ntohs ( *( ( uint16_t * ) ( uip_buf + 12 ) ) );
362
-		if ( type == ETHERTYPE_ARP ) {
363
-			uip_arp_arpin();
364
-		} else {
365
-			uip_arp_ipin();
366
-			uip_input();
367
-		}
368
-		if ( uip_len > 0 )
369
-			uip_transmit();
370
-	} else {
371
-		for ( i = 0 ; i < UIP_CONNS ; i++ ) {
372
-			uip_periodic ( i );
373
-			if ( uip_len > 0 )
374
-				uip_transmit();
375
-		}
376
-	}
377
-}
378
-
379 248
 /*****************************************************************************
380 249
  *
381 250
  * "Hello world" protocol tester

Loading…
Cancel
Save