The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
tcp.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file tcp.h
5 * @brief TCP (RFC 9293): reliable byte streams between ports.
6 *
7 * a connection (tcp_conn_t) keeps the state of one end: the sequence
8 * numbers, a send buffer and a receive buffer, and the timers. the socket
9 * code (tcp_socket.c) puts data in and takes data out; this code sends
10 * and receives the segments.
11 *
12 * what it does:
13 * - the three-way handshake, both ways; a listener with a queue of
14 * connections to accept.
15 * - retransmission with a timeout from the measured round-trip time
16 * (RFC 6298), backing off to TCP_MAX_RETRIES; fast retransmit after
17 * three duplicate acks.
18 * - slow start and congestion avoidance (RFC 5681).
19 * - the receive window (no window scaling, so at most 64 KiB); a probe
20 * when the peer's window is zero.
21 * - the MSS option.
22 * - close with FIN (TIME_WAIT for TCP_TIME_WAIT_MS), reset with RST.
23 * what it does not do (yet): segments that come out of order are dropped
24 * (the sender sends them again), no selective acks, no timestamps, no
25 * keepalive, no urgent data. acks are sent at once (no delayed acks), and
26 * small writes go at once (no Nagle).
27 *
28 * locking: all of it runs with net_lock held. a connection is freed only
29 * by tcp_timer (closed and with no socket), never while the stack works on
30 * it, so a send that loops back through lo cannot free the connection
31 * under its caller.
32 */
33
34#include "ipv4.h"
36
37#define TCP_BUFFER_SIZE 65535 // each of the send and the receive buffer
38#define TCP_MAX_RETRIES 8 // timeouts in a row before the connection fails
39#define TCP_SYN_RETRIES 5
40#define TCP_TIME_WAIT_MS 30000
41#define TCP_BACKLOG_MAX 32
42
57
58typedef struct tcp_conn tcp_conn_t;
59
61{
62 tcp_conn_t *next; // on the list of all connections
66 uint16_t local_port; // host byte order
68
69 // sending: the buffer holds the bytes from snd_una on (sent and not
70 // acked, then not sent yet); SYN and FIN are not in it
78 uint32_t send_head; // where the byte at snd_una is
80 bool fin_wanted; // the user closed for writing: FIN after the data
83 uint16_t mss; // the most data in one segment to the peer
84
85 // receiving
91 uint32_t window_sent; // the window in the last segment sent
93 bool shut_read; // the user takes no more data: it is thrown away
94
95 // timers and congestion control (times in ticks)
98 uint32_t srtt_ms; // 0: no measurement yet
100 bool timing; // a segment is timed for the round-trip time
107 uint64_t closes_at; // TIME_WAIT, or an orphan in FIN_WAIT_2
108
109 int32_t error; // -ECONNREFUSED, -ECONNRESET, -ETIMEDOUT (for the socket)
110 bool ack_now; // send an ack with the next output
111 bool orphan; // no socket has it: freed when closed
112
113 // a listener's queue: connections that finished the handshake
114 tcp_conn_t *parent; // the listener of a connection not accepted yet
119 uint32_t children; // connections of this listener not accepted yet
120
121 wait_queue_t waiters; // readers, writers, connect and accept
122};
123
124/**
125 * set up TCP (from net_init): the timer.
126 */
127void tcp_init(void);
128
129/**
130 * a received segment (net_lock held).
131 */
132void tcp_receive(netif_t *netif, const ipv4_header_t *ip, const uint8_t *segment,
133 uint32_t length);
134
135/**
136 * start a connection: send a SYN (net_lock held).
137 * @return the connection in SYN_SENT (or already ESTABLISHED over lo), or
138 * NULL if there is no memory.
139 */
140tcp_conn_t *tcp_connect(uint32_t local, uint16_t local_port, uint32_t remote,
141 uint16_t remote_port);
142
143/**
144 * a listener on a port (net_lock held).
145 * @param local the address, or 0 for any.
146 * @return the connection in LISTEN, or NULL if there is no memory.
147 */
148tcp_conn_t *tcp_listen(uint32_t local, uint16_t local_port, uint32_t backlog);
149
150/**
151 * take the next connection from a listener's queue (net_lock held).
152 * @return it (not orphaned: the caller gives it a socket), or NULL.
153 */
155
156/**
157 * put data into the send buffer and send what the windows allow (net_lock
158 * held).
159 * @return the bytes taken (0 when the buffer is full).
160 */
161uint32_t tcp_write(tcp_conn_t *conn, const uint8_t *data, uint32_t length);
162
163/**
164 * take data from the receive buffer (net_lock held). this opens the window
165 * again, and the peer is told when that matters.
166 * @param peek leave the data in the buffer.
167 * @return the bytes taken.
168 */
169uint32_t tcp_read(tcp_conn_t *conn, uint8_t *out, uint32_t length, bool peek);
170
171/**
172 * no more data from the user: send FIN after the data (net_lock held).
173 */
175
176/**
177 * the socket lets the connection go (net_lock held): a listener resets
178 * the connections in its queue; a connection with data the user did not
179 * read is reset; any other is closed with FIN. the connection is freed
180 * when it is closed.
181 */
182void tcp_release(tcp_conn_t *conn);
183
184/**
185 * the room in the send buffer.
186 */
188
189/**
190 * true in the states where the user can send data.
191 */
192bool tcp_can_send(const tcp_conn_t *conn);
193
194/**
195 * the timers, each tick (net_lock is taken here).
196 */
197void tcp_timer(uint64_t now);
IPv4 (RFC 791): the header, the checks, and where a packet goes.
tcp_state_t
Definition tcp.h:44
@ TCP_ESTABLISHED
Definition tcp.h:49
@ TCP_LISTEN
Definition tcp.h:46
@ TCP_LAST_ACK
Definition tcp.h:54
@ TCP_FIN_WAIT_1
Definition tcp.h:50
@ TCP_CLOSE_WAIT
Definition tcp.h:52
@ TCP_CLOSING
Definition tcp.h:53
@ TCP_CLOSED
Definition tcp.h:45
@ TCP_FIN_WAIT_2
Definition tcp.h:51
@ TCP_TIME_WAIT
Definition tcp.h:55
@ TCP_SYN_SENT
Definition tcp.h:47
@ TCP_SYN_RECEIVED
Definition tcp.h:48
struct tcp_conn tcp_conn_t
Definition tcp.h:58
unsigned int uint32_t
Definition fb.h:17
unsigned char uint8_t
Definition fb.h:18
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
unsigned short uint16_t
Definition stdint.h:13
int int32_t
Definition stdint.h:14
struct netif netif_t
Definition netif.h:37
Definition tcp.h:61
uint32_t cwnd
Definition tcp.h:104
bool shut_read
Definition tcp.h:93
uint32_t window_sent
Definition tcp.h:91
uint32_t snd_nxt
Definition tcp.h:73
bool fin_received
Definition tcp.h:92
uint32_t irs
Definition tcp.h:86
uint32_t receive_head
Definition tcp.h:89
uint32_t receive_count
Definition tcp.h:90
uint32_t rcv_nxt
Definition tcp.h:87
uint32_t srtt_ms
Definition tcp.h:98
uint32_t backlog
Definition tcp.h:118
uint32_t remote
Definition tcp.h:65
uint32_t dup_acks
Definition tcp.h:106
uint64_t retransmit_at
Definition tcp.h:96
bool orphan
Definition tcp.h:111
uint32_t send_head
Definition tcp.h:78
uint64_t timed_at
Definition tcp.h:102
uint8_t * receive_buffer
Definition tcp.h:88
uint32_t rto_ms
Definition tcp.h:97
tcp_conn_t * queue_next
Definition tcp.h:117
uint8_t * send_buffer
Definition tcp.h:77
bool fin_acked
Definition tcp.h:82
int32_t error
Definition tcp.h:109
tcp_state_t state
Definition tcp.h:63
uint16_t remote_port
Definition tcp.h:67
bool ack_now
Definition tcp.h:110
wait_queue_t waiters
Definition tcp.h:121
uint32_t timed_seq
Definition tcp.h:101
bool timing
Definition tcp.h:100
uint32_t snd_una
Definition tcp.h:72
uint16_t local_port
Definition tcp.h:66
tcp_conn_t * queue_head
Definition tcp.h:115
uint64_t closes_at
Definition tcp.h:107
tcp_conn_t * next
Definition tcp.h:62
uint32_t ssthresh
Definition tcp.h:105
bool fin_wanted
Definition tcp.h:80
uint32_t children
Definition tcp.h:119
uint32_t iss
Definition tcp.h:71
uint32_t retries
Definition tcp.h:103
uint32_t rttvar_ms
Definition tcp.h:99
uint32_t local
Definition tcp.h:64
uint16_t mss
Definition tcp.h:83
uint32_t snd_wl1
Definition tcp.h:75
uint32_t snd_wnd
Definition tcp.h:74
uint32_t send_count
Definition tcp.h:79
tcp_conn_t * parent
Definition tcp.h:114
tcp_conn_t * queue_tail
Definition tcp.h:116
bool fin_sent
Definition tcp.h:81
uint32_t snd_wl2
Definition tcp.h:76
a queue of blocked processes, linked through process_t::wait_next.
Definition wait_queue.h:35
bool tcp_can_send(const tcp_conn_t *c)
true in the states where the user can send data.
Definition tcp.c:100
void tcp_timer(uint64_t now)
the timers, each tick (net_lock is taken here).
Definition tcp.c:1101
void tcp_shutdown_write(tcp_conn_t *c)
no more data from the user: send FIN after the data (net_lock held).
Definition tcp.c:987
void tcp_init(void)
set up TCP (from net_init): the timer.
Definition tcp.c:1129
void tcp_release(tcp_conn_t *c)
the socket lets the connection go (net_lock held): a listener resets the connections in its queue; a ...
Definition tcp.c:1015
uint32_t tcp_write(tcp_conn_t *c, const uint8_t *data, uint32_t length)
put data into the send buffer and send what the windows allow (net_lock held).
Definition tcp.c:949
tcp_conn_t * tcp_listen(uint32_t local, uint16_t local_port, uint32_t backlog)
a listener on a port (net_lock held).
Definition tcp.c:923
tcp_conn_t * tcp_accept(tcp_conn_t *listener)
take the next connection from a listener's queue (net_lock held).
Definition tcp.c:937
void tcp_receive(netif_t *netif, const ipv4_header_t *ip, const uint8_t *seg, uint32_t length)
a received segment (net_lock held).
Definition tcp.c:827
uint32_t tcp_send_room(const tcp_conn_t *c)
the room in the send buffer.
Definition tcp.c:95
uint32_t tcp_read(tcp_conn_t *c, uint8_t *out, uint32_t length, bool peek)
take data from the receive buffer (net_lock held).
Definition tcp.c:965
tcp_conn_t * tcp_connect(uint32_t local, uint16_t local_port, uint32_t remote, uint16_t remote_port)
start a connection: send a SYN (net_lock held).
Definition tcp.c:896