The manual
ginnOS
The reference: libc, the libraries and the kernel
Toggle main menu visibility
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
"
35
#include "
kernel/sync/wait_queue.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
43
typedef
enum
44
{
45
TCP_CLOSED
,
46
TCP_LISTEN
,
47
TCP_SYN_SENT
,
48
TCP_SYN_RECEIVED
,
49
TCP_ESTABLISHED
,
50
TCP_FIN_WAIT_1
,
51
TCP_FIN_WAIT_2
,
52
TCP_CLOSE_WAIT
,
53
TCP_CLOSING
,
54
TCP_LAST_ACK
,
55
TCP_TIME_WAIT
,
56
}
tcp_state_t
;
57
58
typedef
struct
tcp_conn
tcp_conn_t
;
59
60
struct
tcp_conn
61
{
62
tcp_conn_t
*
next
;
// on the list of all connections
63
tcp_state_t
state
;
64
uint32_t
local
;
65
uint32_t
remote
;
66
uint16_t
local_port
;
// host byte order
67
uint16_t
remote_port
;
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
71
uint32_t
iss
;
72
uint32_t
snd_una
;
73
uint32_t
snd_nxt
;
74
uint32_t
snd_wnd
;
75
uint32_t
snd_wl1
;
76
uint32_t
snd_wl2
;
77
uint8_t
*
send_buffer
;
78
uint32_t
send_head
;
// where the byte at snd_una is
79
uint32_t
send_count
;
80
bool
fin_wanted
;
// the user closed for writing: FIN after the data
81
bool
fin_sent
;
82
bool
fin_acked
;
83
uint16_t
mss
;
// the most data in one segment to the peer
84
85
// receiving
86
uint32_t
irs
;
87
uint32_t
rcv_nxt
;
88
uint8_t
*
receive_buffer
;
89
uint32_t
receive_head
;
90
uint32_t
receive_count
;
91
uint32_t
window_sent
;
// the window in the last segment sent
92
bool
fin_received
;
93
bool
shut_read
;
// the user takes no more data: it is thrown away
94
95
// timers and congestion control (times in ticks)
96
uint64_t
retransmit_at
;
// 0: off
97
uint32_t
rto_ms
;
98
uint32_t
srtt_ms
;
// 0: no measurement yet
99
uint32_t
rttvar_ms
;
100
bool
timing
;
// a segment is timed for the round-trip time
101
uint32_t
timed_seq
;
102
uint64_t
timed_at
;
103
uint32_t
retries
;
104
uint32_t
cwnd
;
105
uint32_t
ssthresh
;
106
uint32_t
dup_acks
;
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
115
tcp_conn_t
*
queue_head
;
116
tcp_conn_t
*
queue_tail
;
117
tcp_conn_t
*
queue_next
;
118
uint32_t
backlog
;
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
*/
127
void
tcp_init
(
void
);
128
129
/**
130
* a received segment (net_lock held).
131
*/
132
void
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
*/
140
tcp_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
*/
148
tcp_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
*/
154
tcp_conn_t
*
tcp_accept
(
tcp_conn_t
*listener);
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
*/
161
uint32_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
*/
169
uint32_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
*/
174
void
tcp_shutdown_write
(
tcp_conn_t
*conn);
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
*/
182
void
tcp_release
(
tcp_conn_t
*conn);
183
184
/**
185
* the room in the send buffer.
186
*/
187
uint32_t
tcp_send_room
(
const
tcp_conn_t
*conn);
188
189
/**
190
* true in the states where the user can send data.
191
*/
192
bool
tcp_can_send
(
const
tcp_conn_t
*conn);
193
194
/**
195
* the timers, each tick (net_lock is taken here).
196
*/
197
void
tcp_timer
(
uint64_t
now);
ipv4.h
IPv4 (RFC 791): the header, the checks, and where a packet goes.
tcp_state_t
tcp_state_t
Definition
tcp.h:44
TCP_ESTABLISHED
@ TCP_ESTABLISHED
Definition
tcp.h:49
TCP_LISTEN
@ TCP_LISTEN
Definition
tcp.h:46
TCP_LAST_ACK
@ TCP_LAST_ACK
Definition
tcp.h:54
TCP_FIN_WAIT_1
@ TCP_FIN_WAIT_1
Definition
tcp.h:50
TCP_CLOSE_WAIT
@ TCP_CLOSE_WAIT
Definition
tcp.h:52
TCP_CLOSING
@ TCP_CLOSING
Definition
tcp.h:53
TCP_CLOSED
@ TCP_CLOSED
Definition
tcp.h:45
TCP_FIN_WAIT_2
@ TCP_FIN_WAIT_2
Definition
tcp.h:51
TCP_TIME_WAIT
@ TCP_TIME_WAIT
Definition
tcp.h:55
TCP_SYN_SENT
@ TCP_SYN_SENT
Definition
tcp.h:47
TCP_SYN_RECEIVED
@ TCP_SYN_RECEIVED
Definition
tcp.h:48
tcp_conn_t
struct tcp_conn tcp_conn_t
Definition
tcp.h:58
uint32_t
unsigned int uint32_t
Definition
fb.h:17
uint8_t
unsigned char uint8_t
Definition
fb.h:18
uint64_t
__UINT64_TYPE__ uint64_t
Definition
stdint.h:17
uint16_t
unsigned short uint16_t
Definition
stdint.h:13
int32_t
int int32_t
Definition
stdint.h:14
netif_t
struct netif netif_t
netif
Definition
netif.h:37
tcp_conn
Definition
tcp.h:61
tcp_conn::cwnd
uint32_t cwnd
Definition
tcp.h:104
tcp_conn::shut_read
bool shut_read
Definition
tcp.h:93
tcp_conn::window_sent
uint32_t window_sent
Definition
tcp.h:91
tcp_conn::snd_nxt
uint32_t snd_nxt
Definition
tcp.h:73
tcp_conn::fin_received
bool fin_received
Definition
tcp.h:92
tcp_conn::irs
uint32_t irs
Definition
tcp.h:86
tcp_conn::receive_head
uint32_t receive_head
Definition
tcp.h:89
tcp_conn::receive_count
uint32_t receive_count
Definition
tcp.h:90
tcp_conn::rcv_nxt
uint32_t rcv_nxt
Definition
tcp.h:87
tcp_conn::srtt_ms
uint32_t srtt_ms
Definition
tcp.h:98
tcp_conn::backlog
uint32_t backlog
Definition
tcp.h:118
tcp_conn::remote
uint32_t remote
Definition
tcp.h:65
tcp_conn::dup_acks
uint32_t dup_acks
Definition
tcp.h:106
tcp_conn::retransmit_at
uint64_t retransmit_at
Definition
tcp.h:96
tcp_conn::orphan
bool orphan
Definition
tcp.h:111
tcp_conn::send_head
uint32_t send_head
Definition
tcp.h:78
tcp_conn::timed_at
uint64_t timed_at
Definition
tcp.h:102
tcp_conn::receive_buffer
uint8_t * receive_buffer
Definition
tcp.h:88
tcp_conn::rto_ms
uint32_t rto_ms
Definition
tcp.h:97
tcp_conn::queue_next
tcp_conn_t * queue_next
Definition
tcp.h:117
tcp_conn::send_buffer
uint8_t * send_buffer
Definition
tcp.h:77
tcp_conn::fin_acked
bool fin_acked
Definition
tcp.h:82
tcp_conn::error
int32_t error
Definition
tcp.h:109
tcp_conn::state
tcp_state_t state
Definition
tcp.h:63
tcp_conn::remote_port
uint16_t remote_port
Definition
tcp.h:67
tcp_conn::ack_now
bool ack_now
Definition
tcp.h:110
tcp_conn::waiters
wait_queue_t waiters
Definition
tcp.h:121
tcp_conn::timed_seq
uint32_t timed_seq
Definition
tcp.h:101
tcp_conn::timing
bool timing
Definition
tcp.h:100
tcp_conn::snd_una
uint32_t snd_una
Definition
tcp.h:72
tcp_conn::local_port
uint16_t local_port
Definition
tcp.h:66
tcp_conn::queue_head
tcp_conn_t * queue_head
Definition
tcp.h:115
tcp_conn::closes_at
uint64_t closes_at
Definition
tcp.h:107
tcp_conn::next
tcp_conn_t * next
Definition
tcp.h:62
tcp_conn::ssthresh
uint32_t ssthresh
Definition
tcp.h:105
tcp_conn::fin_wanted
bool fin_wanted
Definition
tcp.h:80
tcp_conn::children
uint32_t children
Definition
tcp.h:119
tcp_conn::iss
uint32_t iss
Definition
tcp.h:71
tcp_conn::retries
uint32_t retries
Definition
tcp.h:103
tcp_conn::rttvar_ms
uint32_t rttvar_ms
Definition
tcp.h:99
tcp_conn::local
uint32_t local
Definition
tcp.h:64
tcp_conn::mss
uint16_t mss
Definition
tcp.h:83
tcp_conn::snd_wl1
uint32_t snd_wl1
Definition
tcp.h:75
tcp_conn::snd_wnd
uint32_t snd_wnd
Definition
tcp.h:74
tcp_conn::send_count
uint32_t send_count
Definition
tcp.h:79
tcp_conn::parent
tcp_conn_t * parent
Definition
tcp.h:114
tcp_conn::queue_tail
tcp_conn_t * queue_tail
Definition
tcp.h:116
tcp_conn::fin_sent
bool fin_sent
Definition
tcp.h:81
tcp_conn::snd_wl2
uint32_t snd_wl2
Definition
tcp.h:76
wait_queue_t
a queue of blocked processes, linked through process_t::wait_next.
Definition
wait_queue.h:35
tcp_can_send
bool tcp_can_send(const tcp_conn_t *c)
true in the states where the user can send data.
Definition
tcp.c:100
tcp_timer
void tcp_timer(uint64_t now)
the timers, each tick (net_lock is taken here).
Definition
tcp.c:1101
tcp_shutdown_write
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
tcp_init
void tcp_init(void)
set up TCP (from net_init): the timer.
Definition
tcp.c:1129
tcp_release
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
tcp_write
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_listen
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_accept
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
tcp_receive
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
tcp_send_room
uint32_t tcp_send_room(const tcp_conn_t *c)
the room in the send buffer.
Definition
tcp.c:95
tcp_read
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_connect
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
wait_queue.h
src
kernel
net
tcp.h
Generated by
1.18.0