AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
tcp.h
Go to the documentation of this file.
1#ifndef _NET_TCP_H
2#define _NET_TCP_H
3
4#include <core/system.h>
5#include <ds/hashmap.h>
6#include <ds/queue.h>
7#include <ds/ringbuf.h>
8#include <net/inet.h>
9#include <sys/callout.h>
10
11/*
12 * TCP (RFC 9293), in net/inet/tcp_*.c. See docs/kernel/networking.md.
13 */
14
16struct tcp_hdr {
19 uint8_t off; /* the header's length in 32-bit words, shifted left by 4 */
24} __packed;
25
26/* A segment's flags. */
27#define TH_FIN 0x01
28#define TH_SYN 0x02
29#define TH_RST 0x04
30#define TH_PSH 0x08
31#define TH_ACK 0x10
32#define TH_URG 0x20
33
34/* A connection's states (RFC 9293 3.3.2). */
48
49/* Compares sequence numbers, which wrap around (modulo 2^32). */
50#define SEQ_LT(a, b) ((int32_t) ((a) - (b)) < 0)
51#define SEQ_LEQ(a, b) ((int32_t) ((a) - (b)) <= 0)
52#define SEQ_GT(a, b) ((int32_t) ((a) - (b)) > 0)
53#define SEQ_GEQ(a, b) ((int32_t) ((a) - (b)) >= 0)
54
55/* The buffers' size, each way: 65535 bytes, the most a window can say. */
56#define TCP_BUFSIZE 65536
57
58/* The peer's maximum segment size, if it doesn't say (RFC 9293). */
59#define TCP_MSS_DEFAULT 536
60
66struct tcp_pcb {
67 int state;
68
69 /* in the network's byte order */
72
73 struct tcp_pcb *next; /* the next PCB of all of them (for ports, listeners) */
74 struct hashmap_node *hnode; /* its node in the hash, by addresses and ports, once it has them */
75
76 /* sending: snd_una is the first byte of sndbuf, and snd_max the end
77 * of what was sent (snd_nxt goes back from there to retransmit) */
80 uint16_t mss; /* the most data a segment carries */
81 struct ringbuf *sndbuf;
82 int fin_queued; /* close() or shutdown() came: send a FIN after the data */
83
84 /* receiving: rcvbuf holds the data before rcv_nxt; ooo holds the
85 * segments that came out of order */
87 struct ringbuf *rcvbuf;
88 struct netbuf *ooo;
89 int fin_received; /* the peer's FIN came: the end is after rcvbuf */
90
91 /* the retransmission timeout (RFC 6298), in ns */
93 int rtt_timing; /* a segment is being timed (one not retransmitted) */
97 int persist; /* the peer's zero window is being probed (rexmt) */
98
99 /* congestion control (RFC 5681, 6582) */
104
105 int ack_pending; /* how many segments aren't acknowledged yet (delayed ACK) */
106 int nodelay; /* TCP_NODELAY: no Nagle algorithm */
107 int keepalive; /* SO_KEEPALIVE */
110
111 struct callout rexmt; /* the retransmission, or persist, timer */
112 struct callout dack; /* the delayed ACK */
113 struct callout timer2; /* TIME_WAIT, FIN_WAIT_2, keepalive */
114
115 /* for a listener: the handshakes going on, and the connections done
116 * (for accept()); for a connection, its listener until it is
117 * accepted */
121
122 int detached; /* its socket was closed: it goes once it's done */
123 int connected; /* whether it ever was (ENOTCONN, EPIPE) */
124 int error; /* an error to report */
126 uint64_t rcvtimeo, sndtimeo; /* in ns; 0 means none */
127 struct waitq wait; /* connect, accept, send and recv sleep here */
128};
129
130/* tcp_input.c: a segment that came */
131void tcp_input(struct netif *netif, struct netbuf *nb, const struct ipv4_hdr *ih);
132
133/* tcp_output.c: sending segments, and the timers */
134void tcp_send_keepalive(struct tcp_pcb *pcb);
135void tcp_output(struct tcp_pcb *pcb);
136void tcp_send_ack(struct tcp_pcb *pcb);
137void tcp_send_syn(struct tcp_pcb *pcb);
138void tcp_send_rst(struct tcp_pcb *pcb);
139void tcp_reply_rst(struct netif *netif, const struct ipv4_hdr *ih, const struct tcp_hdr *th, size_t len);
140void tcp_rexmt_start(struct tcp_pcb *pcb);
141void tcp_rtt_update(struct tcp_pcb *pcb, uint64_t rtt);
142uint16_t tcp_rcv_window(struct tcp_pcb *pcb);
143void tcp_dack(struct tcp_pcb *pcb);
144
145/* tcp_socket.c: the connections, and their sockets */
146extern struct tcp_pcb *tcp_pcbs;
147struct tcp_pcb *tcp_pcb_new(void);
148void tcp_pcb_free(struct tcp_pcb *pcb);
151int tcp_insert(struct tcp_pcb *pcb);
152uint32_t tcp_iss(struct tcp_pcb *pcb);
153void tcp_fin_wait_2(struct tcp_pcb *pcb);
154void tcp_keepalive_reset(struct tcp_pcb *pcb);
155void tcp_established(struct tcp_pcb *pcb);
156void tcp_drop(struct tcp_pcb *pcb, int err);
157void tcp_closed(struct tcp_pcb *pcb);
158void tcp_time_wait(struct tcp_pcb *pcb);
159void tcp_wakeup(struct tcp_pcb *pcb);
160
161#endif /* ! _NET_TCP_H */
#define __packed
Definition system.h:30
unsigned int uint32_t
Definition stddef.h:22
unsigned char uint8_t
Definition stddef.h:20
unsigned long long int uint64_t
Definition stddef.h:23
unsigned short int uint16_t
Definition stddef.h:21
A function to call later, with its argument.
Definition callout.h:13
A hash table's node: an entry, with its key's hash.
Definition hashmap.h:43
An IPv4 packet's header.
Definition inet.h:46
A packet (net/netbuf.c).
Definition netbuf.h:16
A network interface (net/netif.c): a device that sends and receives Ethernet frames,...
Definition netif.h:16
A ring buffer.
Definition ringbuf.h:18
A TCP segment's header.
Definition tcp.h:16
uint16_t urg
Definition tcp.h:23
uint32_t seq
Definition tcp.h:18
uint16_t dst
Definition tcp.h:17
uint16_t src
Definition tcp.h:17
uint16_t checksum
Definition tcp.h:22
uint16_t win
Definition tcp.h:21
uint8_t flags
Definition tcp.h:20
uint32_t ack
Definition tcp.h:18
uint8_t off
Definition tcp.h:19
A connection (its control block, or PCB).
Definition tcp.h:66
int nodelay
Definition tcp.h:106
uint32_t laddr
Definition tcp.h:70
uint16_t mss
Definition tcp.h:80
int in_recovery
Definition tcp.h:102
struct callout rexmt
Definition tcp.h:111
uint32_t cwnd
Definition tcp.h:100
uint64_t rttvar
Definition tcp.h:92
int detached
Definition tcp.h:122
uint64_t rto
Definition tcp.h:92
uint32_t snd_wl1
Definition tcp.h:79
int backlog
Definition tcp.h:120
struct tcp_pcb * next
Definition tcp.h:73
struct tcp_pcb * parent
Definition tcp.h:118
int error
Definition tcp.h:124
uint32_t snd_una
Definition tcp.h:78
uint32_t iss
Definition tcp.h:78
struct callout timer2
Definition tcp.h:113
int retries
Definition tcp.h:96
uint32_t snd_wl2
Definition tcp.h:79
int shut_rd
Definition tcp.h:125
uint64_t rtt_start
Definition tcp.h:95
uint32_t snd_nxt
Definition tcp.h:78
uint32_t irs
Definition tcp.h:86
int fin_received
Definition tcp.h:89
struct tcp_pcb * accept_next
Definition tcp.h:119
uint32_t rcv_adv
Definition tcp.h:86
int reuseaddr
Definition tcp.h:109
uint16_t rport
Definition tcp.h:71
uint16_t lport
Definition tcp.h:71
int qlen
Definition tcp.h:120
uint32_t snd_wnd
Definition tcp.h:79
struct netbuf * ooo
Definition tcp.h:88
struct waitq wait
Definition tcp.h:127
uint64_t srtt
Definition tcp.h:92
int rtt_timing
Definition tcp.h:93
uint32_t ssthresh
Definition tcp.h:100
int ack_pending
Definition tcp.h:105
uint64_t sndtimeo
Definition tcp.h:126
struct ringbuf * rcvbuf
Definition tcp.h:87
int state
Definition tcp.h:67
uint32_t rtt_seq
Definition tcp.h:94
int dupacks
Definition tcp.h:101
uint64_t rcvtimeo
Definition tcp.h:126
int fin_queued
Definition tcp.h:82
struct tcp_pcb * accept_head
Definition tcp.h:119
struct tcp_pcb * accept_tail
Definition tcp.h:119
struct hashmap_node * hnode
Definition tcp.h:74
int keep_probes
Definition tcp.h:108
uint32_t rcv_nxt
Definition tcp.h:86
int persist
Definition tcp.h:97
int keepalive
Definition tcp.h:107
struct ringbuf * sndbuf
Definition tcp.h:81
uint32_t raddr
Definition tcp.h:70
uint32_t recover
Definition tcp.h:103
struct callout dack
Definition tcp.h:112
int connected
Definition tcp.h:123
uint32_t snd_max
Definition tcp.h:78
Definition waitq.h:19
void tcp_established(struct tcp_pcb *pcb)
Moves a connection to ESTABLISHED: its congestion window starts, and a listener's connection is queue...
Definition tcp_socket.c:335
struct tcp_pcb * tcp_lookup(uint32_t laddr, uint16_t lport, uint32_t raddr, uint16_t rport)
Finds a connection by both ends' addresses and ports.
Definition tcp_socket.c:96
void tcp_send_syn(struct tcp_pcb *pcb)
Sends the SYN (or, in SYN_RECEIVED, the SYN and ACK), the first time or again, and starts the retrans...
Definition tcp_output.c:135
void tcp_dack(struct tcp_pcb *pcb)
Acknowledges a received segment: at once if it's the second one not acknowledged yet,...
Definition tcp_output.c:344
struct tcp_pcb * tcp_pcb_new(void)
Makes a connection, CLOSED, with its send and receive buffers, on the list of connections.
Definition tcp_socket.c:162
void tcp_reply_rst(struct netif *netif, const struct ipv4_hdr *ih, const struct tcp_hdr *th, size_t len)
Answers a segment that is for no connection with a reset (RFC 9293 3.10.7.1).
Definition tcp_output.c:192
void tcp_closed(struct tcp_pcb *pcb)
Moves a connection to CLOSED: it leaves the hashmap, its timers stop, and its waiters wake up.
Definition tcp_socket.c:295
struct tcp_pcb * tcp_listener(uint32_t laddr, uint16_t lport)
Finds the listener for a local address and port: one bound to that address, or else one bound to any ...
Definition tcp_socket.c:138
void tcp_output(struct tcp_pcb *pcb)
Sends as much of the send buffer as the windows allow, in segments of at most mss bytes.
Definition tcp_output.c:249
void tcp_wakeup(struct tcp_pcb *pcb)
Wakes up the threads waiting on a connection (in accept(), connect(), recv() or send()).
Definition tcp_socket.c:257
void tcp_fin_wait_2(struct tcp_pcb *pcb)
Handles our FIN being acknowledged (FIN_WAIT_2): if the socket is closed, the connection waits for th...
Definition tcp_socket.c:387
void tcp_send_rst(struct tcp_pcb *pcb)
Sends a reset on a connection.
Definition tcp_output.c:178
void tcp_send_ack(struct tcp_pcb *pcb)
Sends an ACK, now.
Definition tcp_output.c:157
void tcp_time_wait(struct tcp_pcb *pcb)
Moves a connection to TIME_WAIT, which it leaves after 2 MSL.
Definition tcp_socket.c:373
void tcp_rtt_update(struct tcp_pcb *pcb, uint64_t rtt)
Updates the round trip estimates with a new measurement, and the retransmission timeout from them (RF...
Definition tcp_output.c:316
int tcp_insert(struct tcp_pcb *pcb)
Adds a connection to the hashmap of connections, by its addresses and ports, which it must have now.
Definition tcp_socket.c:112
struct tcp_pcb * tcp_pcbs
Definition tcp_socket.c:55
void tcp_input(struct netif *netif, struct netbuf *nb, const struct ipv4_hdr *ih)
Takes in a TCP segment that IPv4 received, and passes it to its connection or listener.
Definition tcp_input.c:585
tcp_state
Definition tcp.h:35
@ TCP_ESTABLISHED
Definition tcp.h:40
@ TCP_LISTEN
Definition tcp.h:37
@ TCP_LAST_ACK
Definition tcp.h:45
@ TCP_FIN_WAIT_1
Definition tcp.h:41
@ TCP_CLOSE_WAIT
Definition tcp.h:43
@ TCP_CLOSING
Definition tcp.h:44
@ TCP_CLOSED
Definition tcp.h:36
@ TCP_FIN_WAIT_2
Definition tcp.h:42
@ TCP_TIME_WAIT
Definition tcp.h:46
@ TCP_SYN_SENT
Definition tcp.h:38
@ TCP_SYN_RECEIVED
Definition tcp.h:39
uint16_t tcp_rcv_window(struct tcp_pcb *pcb)
Computes the window to advertise: the room in the receive buffer.
Definition tcp_output.c:49
void tcp_rexmt_start(struct tcp_pcb *pcb)
Starts the retransmission timer, if it isn't running.
Definition tcp_output.c:122
uint32_t tcp_iss(struct tcp_pcb *pcb)
Chooses a connection's initial sequence number (RFC 6528): a clock that ticks every 4 us,...
Definition tcp_socket.c:231
void tcp_drop(struct tcp_pcb *pcb, int err)
Drops a connection that is lost, giving its socket an error.
Definition tcp_socket.c:323
void tcp_send_keepalive(struct tcp_pcb *pcb)
Sends a keepalive probe: a segment one byte before what the peer has acknowledged (with no data),...
Definition tcp_output.c:168
void tcp_keepalive_reset(struct tcp_pcb *pcb)
Restarts the keepalive timer, when the peer was heard from.
Definition tcp_socket.c:417
void tcp_pcb_free(struct tcp_pcb *pcb)
Frees a connection: its timers stop, it leaves the lists, and its buffers and waiting segments are fr...
Definition tcp_socket.c:196