選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

tcp.c 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include <string.h>
  2. #include <assert.h>
  3. #include <byteswap.h>
  4. #include <latch.h>
  5. #include <errno.h>
  6. #include <gpxe/process.h>
  7. #include <gpxe/init.h>
  8. #include <gpxe/netdevice.h>
  9. #include <gpxe/pkbuff.h>
  10. #include <gpxe/ip.h>
  11. #include <gpxe/tcp.h>
  12. #include "uip/uip.h"
  13. /** @file
  14. *
  15. * TCP protocol
  16. *
  17. * The gPXE TCP stack is currently implemented on top of the uIP
  18. * protocol stack. This file provides wrappers around uIP so that
  19. * higher-level protocol implementations do not need to talk directly
  20. * to uIP (which has a somewhat baroque API).
  21. *
  22. * Basic operation is to create a #tcp_connection structure, call
  23. * tcp_connect() and then call run_tcpip() in a loop until the
  24. * operation has completed. The TCP stack will call the various
  25. * methods defined in the #tcp_operations structure in order to send
  26. * and receive data.
  27. *
  28. * See hello.c for a trivial example of a TCP protocol using this
  29. * API.
  30. *
  31. */
  32. /**
  33. * TCP transmit buffer
  34. *
  35. * When a tcp_operations::senddata() method is called, it is
  36. * guaranteed to be able to use this buffer as temporary space for
  37. * constructing the data to be sent. For example, code such as
  38. *
  39. * @code
  40. *
  41. * static void my_senddata ( struct tcp_connection *conn, void *buf,
  42. * size_t len ) {
  43. * len = snprintf ( buf, len, "FETCH %s\r\n", filename );
  44. * tcp_send ( conn, buf + already_sent, len - already_sent );
  45. * }
  46. *
  47. * @endcode
  48. *
  49. * is allowed, and is probably the best way to deal with
  50. * variably-sized data.
  51. *
  52. * Note that you cannot use this simple mechanism if you want to be
  53. * able to construct single data blocks of more than #len bytes.
  54. */
  55. static void *tcp_buffer = uip_buf + ( 40 + UIP_LLH_LEN );
  56. /** Size of #tcp_buffer */
  57. static size_t tcp_buflen = UIP_BUFSIZE - ( 40 + UIP_LLH_LEN );
  58. /**
  59. * Open a TCP connection
  60. *
  61. * @v conn TCP connection
  62. *
  63. * This sets up a new TCP connection to the remote host specified in
  64. * tcp_connection::sin.
  65. */
  66. void tcp_connect ( struct tcp_connection *conn ) {
  67. struct uip_conn *uip_conn;
  68. u16_t ipaddr[2];
  69. assert ( conn->sin.sin_addr.s_addr != 0 );
  70. assert ( conn->sin.sin_port != 0 );
  71. assert ( conn->tcp_op != NULL );
  72. assert ( sizeof ( uip_conn->appstate ) == sizeof ( conn ) );
  73. * ( ( uint32_t * ) ipaddr ) = conn->sin.sin_addr.s_addr;
  74. uip_conn = uip_connect ( ipaddr, conn->sin.sin_port );
  75. #warning "Use linked lists so that uip_connect() cannot fail"
  76. assert ( uip_conn != NULL );
  77. *( ( void ** ) uip_conn->appstate ) = conn;
  78. }
  79. /**
  80. * Send data via a TCP connection
  81. *
  82. * @v conn TCP connection
  83. * @v data Data to send
  84. * @v len Length of data
  85. *
  86. * Data will be automatically limited to the current TCP window size.
  87. *
  88. * If retransmission is required, the connection's
  89. * tcp_operations::senddata() method will be called again in order to
  90. * regenerate the data.
  91. */
  92. void tcp_send ( struct tcp_connection *conn __unused,
  93. const void *data, size_t len ) {
  94. assert ( conn = *( ( void ** ) uip_conn->appstate ) );
  95. if ( len > tcp_buflen )
  96. len = tcp_buflen;
  97. memmove ( tcp_buffer, data, len );
  98. uip_send ( tcp_buffer, len );
  99. }
  100. /**
  101. * Close a TCP connection
  102. *
  103. * @v conn TCP connection
  104. */
  105. void tcp_close ( struct tcp_connection *conn __unused ) {
  106. assert ( conn = *( ( void ** ) uip_conn->appstate ) );
  107. uip_close();
  108. }
  109. /**
  110. * uIP TCP application call interface
  111. *
  112. * This is the entry point of gPXE from the point of view of the uIP
  113. * protocol stack. This function calls the appropriate methods from
  114. * the connection's @tcp_operations table in order to process received
  115. * data, transmit new data etc.
  116. */
  117. void uip_tcp_appcall ( void ) {
  118. struct tcp_connection *conn = *( ( void ** ) uip_conn->appstate );
  119. struct tcp_operations *op = conn->tcp_op;
  120. if ( op->closed ) {
  121. if ( uip_aborted() )
  122. op->closed ( conn, -ECONNABORTED );
  123. if ( uip_timedout() )
  124. op->closed ( conn, -ETIMEDOUT );
  125. if ( uip_closed() )
  126. op->closed ( conn, 0 );
  127. }
  128. if ( uip_connected() && op->connected )
  129. op->connected ( conn );
  130. if ( uip_acked() && op->acked )
  131. op->acked ( conn, uip_conn->len );
  132. if ( uip_newdata() && op->newdata )
  133. op->newdata ( conn, ( void * ) uip_appdata, uip_len );
  134. if ( ( uip_rexmit() || uip_newdata() || uip_acked() ||
  135. uip_connected() || uip_poll() ) && op->senddata )
  136. op->senddata ( conn, tcp_buffer, tcp_buflen );
  137. }
  138. /* Present here to allow everything to link. Will go into separate
  139. * udp.c file
  140. */
  141. void uip_udp_appcall ( void ) {
  142. }
  143. /**
  144. * Perform periodic processing of all TCP connections
  145. *
  146. * This allows TCP connections to retransmit data if necessary.
  147. */
  148. static void tcp_periodic ( void ) {
  149. struct pk_buff *pkb;
  150. int i;
  151. for ( i = 0 ; i < UIP_CONNS ; i++ ) {
  152. uip_periodic ( i );
  153. if ( uip_len > 0 ) {
  154. pkb = alloc_pkb ( uip_len + MAX_LL_HEADER_LEN);
  155. if ( ! pkb )
  156. continue;
  157. pkb_reserve ( pkb, MAX_LL_HEADER_LEN );
  158. pkb_put ( pkb, uip_len );
  159. memcpy ( pkb->data, uip_buf, uip_len );
  160. ipv4_uip_tx ( pkb );
  161. }
  162. }
  163. }
  164. /**
  165. * Kick a connection into life
  166. *
  167. * @v conn TCP connection
  168. *
  169. * Call this function when you have new data to send and are not
  170. * already being called as part of TCP processing.
  171. */
  172. void tcp_kick ( struct tcp_connection *conn __unused ) {
  173. /* Just kick all the connections; this will work for now */
  174. tcp_periodic();
  175. }
  176. /**
  177. * Single-step the TCP stack
  178. *
  179. * @v process TCP process
  180. *
  181. * This calls tcp_periodic() at regular intervals.
  182. */
  183. static void tcp_step ( struct process *process ) {
  184. static unsigned long timeout = 0;
  185. if ( currticks() > timeout ) {
  186. timeout = currticks() + ( TICKS_PER_SEC / 10 );
  187. tcp_periodic ();
  188. }
  189. schedule ( process );
  190. }
  191. /** TCP stack process */
  192. static struct process tcp_process = {
  193. .step = tcp_step,
  194. };
  195. /** Initialise the TCP stack */
  196. static void init_tcp ( void ) {
  197. schedule ( &tcp_process );
  198. }
  199. INIT_FN ( INIT_PROCESS, init_tcp, NULL, NULL );