Changeset 32105348 in mainline
- Timestamp:
- 2011-10-04T18:12:41Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- d9ce049
- Parents:
- 032bbe7
- Location:
- uspace/srv/net/tl/tcp
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/tl/tcp/conn.c
r032bbe7 r32105348 48 48 49 49 #define RCV_BUF_SIZE 4096 50 #define SND_BUF_SIZE 4096 50 51 51 52 LIST_INITIALIZE(conn_list); … … 77 78 } 78 79 80 /** Allocate send buffer */ 81 conn->snd_buf_size = SND_BUF_SIZE; 82 conn->snd_buf_used = 0; 83 conn->snd_buf = calloc(1, conn->snd_buf_size); 84 if (conn->snd_buf == NULL) { 85 free(conn); 86 return NULL; 87 } 88 79 89 /* Set up receive window. */ 80 90 conn->rcv_wnd = conn->rcv_buf_size; … … 171 181 172 182 return NULL; 183 } 184 185 /** Determine if SYN has been received. 186 * 187 * @param conn Connection 188 * @return @c true if SYN has been received, @c false otherwise. 189 */ 190 bool tcp_conn_got_syn(tcp_conn_t *conn) 191 { 192 switch (conn->cstate) { 193 case st_listen: 194 case st_syn_sent: 195 return false; 196 case st_syn_received: 197 case st_established: 198 case st_fin_wait_1: 199 case st_fin_wait_2: 200 case st_close_wait: 201 case st_closing: 202 case st_last_ack: 203 case st_time_wait: 204 return true; 205 case st_closed: 206 assert(false); 207 } 208 209 assert(false); 173 210 } 174 211 … … 273 310 274 311 log_msg(LVL_DEBUG, "Sent SYN, got SYN."); 312 313 /* 314 * Surprisingly the spec does not deal with initial window setting. 315 * Set SND.WND = SEG.WND and set SND.WL1 so that next segment 316 * will always be accepted as new window setting. 317 */ 318 log_msg(LVL_DEBUG, "SND.WND := %" PRIu32 ", SND.WL1 := %" PRIu32 ", " 319 "SND.WL2 = %" PRIu32, seg->wnd, seg->seq, seg->seq); 320 conn->snd_wnd = seg->wnd; 321 conn->snd_wl1 = seg->seq; 322 conn->snd_wl2 = seg->seq; 275 323 276 324 if (seq_no_syn_acked(conn)) { … … 618 666 tcp_segment_text_copy(seg, conn->rcv_buf, xfer_size); 619 667 668 log_msg(LVL_DEBUG, "Received %zu bytes of data.", xfer_size); 669 620 670 /* Advance RCV.NXT */ 621 671 conn->rcv_nxt += xfer_size; … … 764 814 /** Compute flipped socket pair for response. 765 815 * 766 * Flipped socket pair has local and foreign socke s exchanged.816 * Flipped socket pair has local and foreign sockets exchanged. 767 817 * 768 818 * @param sp Socket pair -
uspace/srv/net/tl/tcp/conn.h
r032bbe7 r32105348 36 36 #define CONN_H 37 37 38 #include <bool.h> 38 39 #include "tcp_type.h" 39 40 … … 42 43 extern void tcp_conn_sync(tcp_conn_t *); 43 44 extern tcp_conn_t *tcp_conn_find(tcp_sockpair_t *); 45 extern bool tcp_conn_got_syn(tcp_conn_t *); 44 46 extern void tcp_conn_segment_arrived(tcp_conn_t *, tcp_segment_t *); 45 47 extern void tcp_conn_trim_seg_to_wnd(tcp_conn_t *, tcp_segment_t *); -
uspace/srv/net/tl/tcp/segment.c
r032bbe7 r32105348 53 53 } 54 54 55 /** Create a control segment.56 * 57 * @return Control segment55 /** Create a control-only segment. 56 * 57 * @return Segment 58 58 */ 59 59 tcp_segment_t *tcp_segment_make_ctrl(tcp_control_t ctrl) … … 89 89 return rseg; 90 90 } 91 92 /** Create a control segment. 93 * 94 * @return Segment 95 */ 96 tcp_segment_t *tcp_segment_make_data(tcp_control_t ctrl, void *data, 97 size_t size) 98 { 99 tcp_segment_t *seg; 100 101 assert(size > 0); 102 103 seg = tcp_segment_new(); 104 if (seg == NULL) 105 return NULL; 106 107 seg->ctrl = ctrl; 108 seg->len = seq_no_control_len(ctrl) + size; 109 110 seg->dfptr = seg->data = malloc(size); 111 if (seg->dfptr == NULL) { 112 free(seg); 113 return NULL; 114 } 115 116 memcpy(seg->data, data, size); 117 118 return seg; 119 } 120 91 121 92 122 /** Trim segment from left and right by the specified amount. -
uspace/srv/net/tl/tcp/segment.h
r032bbe7 r32105348 43 43 extern tcp_segment_t *tcp_segment_make_ctrl(tcp_control_t); 44 44 extern tcp_segment_t *tcp_segment_make_rst(tcp_segment_t *); 45 extern tcp_segment_t *tcp_segment_make_data(tcp_control_t, void *, size_t); 45 46 extern void tcp_segment_trim(tcp_segment_t *, uint32_t, uint32_t); 46 47 extern void tcp_segment_text_copy(tcp_segment_t *, void *, size_t); -
uspace/srv/net/tl/tcp/state.c
r032bbe7 r32105348 36 36 37 37 #include <io/log.h> 38 #include <macros.h> 39 #include <mem.h> 38 40 #include "conn.h" 39 41 #include "state.h" 40 42 #include "tcp_type.h" 43 #include "tqueue.h" 41 44 42 45 /* … … 78 81 void tcp_uc_send(tcp_conn_t *conn, void *data, size_t size, xflags_t flags) 79 82 { 83 size_t buf_free; 84 size_t xfer_size; 85 80 86 log_msg(LVL_DEBUG, "tcp_uc_send()"); 87 88 while (size > 0) { 89 buf_free = conn->snd_buf_size - conn->snd_buf_used; 90 while (buf_free == 0) 91 tcp_tqueue_new_data(conn); 92 93 xfer_size = min(size, buf_free); 94 95 /* Copy data to buffer */ 96 memcpy(conn->snd_buf + conn->snd_buf_used, data, xfer_size); 97 data += xfer_size; 98 conn->snd_buf_used += xfer_size; 99 size -= xfer_size; 100 } 101 102 tcp_tqueue_new_data(conn); 81 103 } 82 104 -
uspace/srv/net/tl/tcp/tcp_type.h
r032bbe7 r32105348 115 115 size_t rcv_buf_used; 116 116 117 /** Send buffer */ 118 uint8_t *snd_buf; 119 /** Send buffer size */ 120 size_t snd_buf_size; 121 size_t snd_buf_used; 122 117 123 /** Send unacknowledged */ 118 124 uint32_t snd_una; -
uspace/srv/net/tl/tcp/test.c
r032bbe7 r32105348 39 39 #include <stdio.h> 40 40 #include <thread.h> 41 #include <str.h> 41 42 #include "state.h" 42 43 #include "tcp_type.h" … … 59 60 tcp_conn_t *conn; 60 61 tcp_sock_t sock; 62 const char *msg = "Hello World!"; 61 63 62 64 printf("test_cli()\n"); … … 67 69 async_usleep(1000*1000*3); 68 70 tcp_uc_open(1024, &sock, ap_active, &conn); 71 72 async_usleep(1000*1000*10); 73 tcp_uc_send(conn, (void *)msg, str_size(msg), 0); 69 74 } 70 75 -
uspace/srv/net/tl/tcp/tqueue.c
r032bbe7 r32105348 37 37 #include <byteorder.h> 38 38 #include <io/log.h> 39 #include <macros.h> 40 #include <mem.h> 41 #include "conn.h" 39 42 #include "header.h" 40 43 #include "rqueue.h" … … 50 53 51 54 seg = tcp_segment_make_ctrl(ctrl); 52 seg->seq = conn->snd_nxt;53 seg->ack = conn->rcv_nxt;54 55 tcp_tqueue_seg(conn, seg); 55 56 } … … 60 61 /* XXX queue */ 61 62 63 /* 64 * Always send ACK once we have received SYN, except for RST segments. 65 * (Spec says we should always send ACK once connection has been 66 * established.) 67 */ 68 if (tcp_conn_got_syn(conn) && (seg->ctrl & CTL_RST) == 0) 69 seg->ctrl |= CTL_ACK; 70 71 seg->seq = conn->snd_nxt; 72 seg->wnd = conn->rcv_wnd; 73 74 if ((seg->ctrl & CTL_ACK) != 0) 75 seg->ack = conn->rcv_nxt; 76 else 77 seg->ack = 0; 78 62 79 conn->snd_nxt += seg->len; 80 63 81 tcp_transmit_segment(&conn->ident, seg); 82 } 83 84 /** Transmit data from the send buffer. 85 * 86 * @param conn Connection 87 */ 88 void tcp_tqueue_new_data(tcp_conn_t *conn) 89 { 90 size_t data_size; 91 tcp_segment_t *seg; 92 93 log_msg(LVL_DEBUG, "tcp_tqueue_new_data()"); 94 95 data_size = min(conn->snd_buf_used, conn->snd_wnd); 96 log_msg(LVL_DEBUG, "conn->snd_buf_used = %zu, SND.WND = %zu, " 97 "data_size = %zu", conn->snd_buf_used, conn->snd_wnd, data_size); 98 99 if (data_size == 0) 100 return; 101 102 /* XXX Do not always send immediately */ 103 104 seg = tcp_segment_make_data(0, conn->snd_buf, data_size); 105 if (seg == NULL) { 106 log_msg(LVL_ERROR, "Memory allocation failure."); 107 return; 108 } 109 110 /* Remove data from send buffer */ 111 memmove(conn->snd_buf, conn->snd_buf + data_size, 112 conn->snd_buf_used - data_size); 113 conn->snd_buf_used -= data_size; 114 115 tcp_tqueue_seg(conn, seg); 64 116 } 65 117 -
uspace/srv/net/tl/tcp/tqueue.h
r032bbe7 r32105348 41 41 extern void tcp_tqueue_ctrl_seg(tcp_conn_t *, tcp_control_t); 42 42 extern void tcp_tqueue_seg(tcp_conn_t *, tcp_segment_t *); 43 extern void tcp_tqueue_new_data(tcp_conn_t *); 43 44 extern void tcp_tqueue_remove_acked(tcp_conn_t *); 44 45 extern void tcp_transmit_segment(tcp_sockpair_t *, tcp_segment_t *);
Note:
See TracChangeset
for help on using the changeset viewer.