]> git.meshlink.io Git - utcp/blob - utcp_priv.h
Measure RTT and calculate RTO.
[utcp] / utcp_priv.h
1 /*
2     utcp.h -- Userspace TCP
3     Copyright (C) 2014 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef UTCP_PRIV_H
21 #define UTCP_PRIV_H
22
23 #define UTCP_INTERNAL
24 #include "utcp.h"
25
26 #define PREP(l) char pkt[(l) + sizeof struct hdr]; struct hdr *hdr = &pkt;
27
28 #define SYN 1
29 #define ACK 2
30 #define FIN 4
31 #define RST 8
32
33 #define NSACKS 4
34 #define DEFAULT_SNDBUFSIZE 4096
35 #define DEFAULT_MAXSNDBUFSIZE 131072
36 #define DEFAULT_RCVBUFSIZE 0
37 #define DEFAULT_MAXRCVBUFSIZE 131072
38
39 #define DEFAULT_MTU 1000
40
41 #define DEFAULT_USER_TIMEOUT 60 // s
42 #define CLOCK_GRANULARITY 1000 // us
43 #define START_RTO 1000000 // us
44 #define MAX_RTO 3000000 // us
45
46 struct hdr {
47         uint16_t src; // Source port
48         uint16_t dst; // Destination port
49         uint32_t seq; // Sequence number
50         uint32_t ack; // Acknowledgement number
51         uint32_t wnd; // Window size
52         uint16_t ctl; // Flags (SYN, ACK, FIN, RST)
53         uint16_t aux; // other stuff
54 };
55
56 enum state {
57         CLOSED,
58         LISTEN,
59         SYN_SENT,
60         SYN_RECEIVED,
61         ESTABLISHED,
62         FIN_WAIT_1,
63         FIN_WAIT_2,
64         CLOSE_WAIT,
65         CLOSING,
66         LAST_ACK,
67         TIME_WAIT
68 };
69
70 static const char *strstate[] __attribute__((unused)) = {
71         [CLOSED] = "CLOSED",
72         [LISTEN] = "LISTEN",
73         [SYN_SENT] = "SYN_SENT",
74         [SYN_RECEIVED] = "SYN_RECEIVED",
75         [ESTABLISHED] = "ESTABLISHED",
76         [FIN_WAIT_1] = "FIN_WAIT_1",
77         [FIN_WAIT_2] = "FIN_WAIT_2",
78         [CLOSE_WAIT] = "CLOSE_WAIT",
79         [CLOSING] = "CLOSING",
80         [LAST_ACK] = "LAST_ACK",
81         [TIME_WAIT] = "TIME_WAIT"
82 };
83
84 struct buffer {
85         char *data;
86         uint32_t used;
87         uint32_t size;
88         uint32_t maxsize;
89 };
90
91 struct sack {
92         uint32_t offset;
93         uint32_t len;
94 };
95
96 struct utcp_connection {
97         void *priv;
98         struct utcp *utcp;
99
100         bool reapable;
101
102         // Callbacks
103
104         utcp_recv_t recv;
105         utcp_poll_t poll;
106
107         // TCP State
108
109         uint16_t src;
110         uint16_t dst;
111         enum state state;
112
113         struct {
114                 uint32_t una;
115                 uint32_t nxt;
116                 uint32_t wnd;
117                 uint32_t iss;
118
119                 uint32_t last;
120                 uint32_t cwnd;
121         } snd;
122
123         struct {
124                 uint32_t nxt;
125                 uint32_t wnd;
126                 uint32_t irs;
127         } rcv;
128
129         int dupack;
130
131         // Timers
132
133         struct timeval conn_timeout;
134         struct timeval rtrx_timeout;
135         struct timeval rtt_start;
136         uint32_t rtt_seq;
137
138         // Buffers
139
140         struct buffer sndbuf;
141         struct buffer rcvbuf;
142         struct sack sacks[NSACKS];
143
144         // Per-socket options
145
146         bool nodelay;
147         bool keepalive;
148
149         // Congestion avoidance state
150
151         struct timeval tlast;
152         uint64_t bandwidth;
153 };
154
155 struct utcp {
156         void *priv;
157
158         // Callbacks
159
160         utcp_accept_t accept;
161         utcp_pre_accept_t pre_accept;
162         utcp_send_t send;
163
164         // Global socket options
165
166         uint16_t mtu;
167         int timeout; // s
168
169         // RTT variables
170
171         uint32_t srtt; // us
172         uint32_t rttvar; // us
173         uint32_t rto; // us
174
175         // Connection management
176
177         struct utcp_connection **connections;
178         int nconnections;
179         int nallocated;
180 };
181
182 #endif