source: mainline/uspace/srv/net/tcp/tcp_type.h@ 9520af7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9520af7 was 9520af7, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Allow TCP conn tests that involve transferring data by enabling an internal loopback. Add simple →SYN, ←RST test.

  • Property mode set to 100644
File size: 8.6 KB
Line 
1/*
2 * Copyright (c) 2015 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup tcp
30 * @{
31 */
32/** @file TCP type definitions
33 */
34
35#ifndef TCP_TYPE_H
36#define TCP_TYPE_H
37
38#include <adt/list.h>
39#include <async.h>
40#include <stdbool.h>
41#include <fibril.h>
42#include <fibril_synch.h>
43#include <stddef.h>
44#include <stdint.h>
45#include <inet/addr.h>
46#include <inet/endpoint.h>
47
48struct tcp_conn;
49
50/** Connection state */
51typedef enum {
52 /** Listen */
53 st_listen,
54 /** Syn-sent */
55 st_syn_sent,
56 /** Syn-received */
57 st_syn_received,
58 /** Established */
59 st_established,
60 /** Fin-wait-1 */
61 st_fin_wait_1,
62 /** Fin-wait-2 */
63 st_fin_wait_2,
64 /** Close-wait */
65 st_close_wait,
66 /** Closing */
67 st_closing,
68 /** Last-ack */
69 st_last_ack,
70 /** Time-wait */
71 st_time_wait,
72 /** Closed */
73 st_closed
74} tcp_cstate_t;
75
76/** Error codes returned by TCP user calls (per the spec). */
77typedef enum {
78 /* OK */
79 TCP_EOK,
80 /* Connection aborted due to user timeout */
81 TCP_EABORTED,
82 /* Connection already exists */
83 TCP_EEXISTS,
84 /* Connection closing */
85 TCP_ECLOSING,
86 /* Connection does not exist */
87 TCP_ENOTEXIST,
88 /* Connection illegal for this process */
89 TCP_EILLEGAL,
90 /* Connection not open */
91 TCP_ENOTOPEN,
92 /* Connection reset */
93 TCP_ERESET,
94 /* Remote endpoint unspecified */
95 TCP_EUNSPEC,
96 /* Insufficient resources */
97 TCP_ENORES,
98 /* Precedence not allowed */
99 TCP_EINVPREC,
100 /* Security/compartment not allowed */
101 TCP_EINVCOMP,
102 TCP_EAGAIN
103} tcp_error_t;
104
105/** Transfer flags */
106typedef enum {
107 XF_PUSH = 0x1,
108 XF_URGENT = 0x2
109} xflags_t;
110
111/** Control message bits
112 *
113 * Note this is not the actual on-the-wire encoding
114 */
115typedef enum {
116 CTL_SYN = 0x1,
117 CTL_FIN = 0x2,
118 CTL_RST = 0x4,
119 CTL_ACK = 0x8
120} tcp_control_t;
121
122/** Connection incoming segments queue */
123typedef struct {
124 struct tcp_conn *conn;
125 list_t list;
126} tcp_iqueue_t;
127
128/** Active or passive connection */
129typedef enum {
130 ap_active,
131 ap_passive
132} acpass_t;
133
134/** Flags for TCP open operation */
135typedef enum {
136 tcp_open_nonblock = 1
137} tcp_open_flags_t;
138
139typedef struct tcp_conn tcp_conn_t;
140
141/** Connection state change callback function */
142typedef void (*tcp_cstate_cb_t)(tcp_conn_t *, void *);
143
144/** Connection callbacks */
145typedef struct {
146 void (*cstate_change)(tcp_conn_t *, void *, tcp_cstate_t);
147 void (*recv_data)(tcp_conn_t *, void *);
148} tcp_cb_t;
149
150/** Data returned by Status user call */
151typedef struct {
152 /** Connection state */
153 tcp_cstate_t cstate;
154} tcp_conn_status_t;
155
156typedef struct {
157 /** SYN, FIN */
158 tcp_control_t ctrl;
159
160 /** Segment sequence number */
161 uint32_t seq;
162 /** Segment acknowledgement number */
163 uint32_t ack;
164 /** Segment length in sequence space */
165 uint32_t len;
166 /** Segment window */
167 uint32_t wnd;
168 /** Segment urgent pointer */
169 uint32_t up;
170
171 /** Segment data, may be moved when trimming segment */
172 void *data;
173 /** Segment data, original pointer used to free data */
174 void *dfptr;
175} tcp_segment_t;
176
177/** Receive queue entry */
178typedef struct {
179 link_t link;
180 inet_ep2_t epp;
181 tcp_segment_t *seg;
182} tcp_rqueue_entry_t;
183
184/** Receive queue callbacks */
185typedef struct {
186 /** Segment received */
187 void (*seg_received)(inet_ep2_t *, tcp_segment_t *);
188} tcp_rqueue_cb_t;
189
190/** NCSim queue entry */
191typedef struct {
192 link_t link;
193 suseconds_t delay;
194 inet_ep2_t epp;
195 tcp_segment_t *seg;
196} tcp_squeue_entry_t;
197
198/** Incoming queue entry */
199typedef struct {
200 link_t link;
201 tcp_segment_t *seg;
202} tcp_iqueue_entry_t;
203
204/** Retransmission queue entry */
205typedef struct {
206 link_t link;
207 tcp_conn_t *conn;
208 tcp_segment_t *seg;
209} tcp_tqueue_entry_t;
210
211/** Retransmission queue callbacks */
212typedef struct {
213 /** Segment received */
214 void (*transmit_seg)(inet_ep2_t *, tcp_segment_t *);
215} tcp_tqueue_cb_t;
216
217/** Retransmission queue */
218typedef struct {
219 struct tcp_conn *conn;
220 list_t list;
221
222 /** Retransmission timer */
223 fibril_timer_t *timer;
224
225 /** Callbacks */
226 tcp_tqueue_cb_t *cb;
227} tcp_tqueue_t;
228
229/** Connection */
230struct tcp_conn {
231 char *name;
232 link_t link;
233
234 /** Connection callbacks function */
235 tcp_cb_t *cb;
236 /** Argument to @c cstate_cb */
237 void *cb_arg;
238
239 /** Connection identification (local and remote endpoint) */
240 inet_ep2_t ident;
241 /** Connection is in association map */
242 bool mapped;
243
244 /** Active or passive connection */
245 acpass_t ap;
246
247 /** Protects access to connection structure */
248 fibril_mutex_t lock;
249 /** Reference count */
250 atomic_t refcnt;
251
252 /** Connection state */
253 tcp_cstate_t cstate;
254 /** True if connection was reset */
255 bool reset;
256 /** True if connection was deleted by user */
257 bool deleted;
258 /** Signalled when @c cstate changes */
259 fibril_condvar_t cstate_cv;
260
261 /** Set when FIN is removed from the retransmission queue */
262 bool fin_is_acked;
263
264 /** Queue of incoming segments */
265 tcp_iqueue_t incoming;
266
267 /** Retransmission queue */
268 tcp_tqueue_t retransmit;
269
270 /** Time-Wait timeout timer */
271 fibril_timer_t *tw_timer;
272
273 /** Receive buffer */
274 uint8_t *rcv_buf;
275 /** Receive buffer size */
276 size_t rcv_buf_size;
277 /** Receive buffer number of bytes used */
278 size_t rcv_buf_used;
279 /** Receive buffer contains FIN */
280 bool rcv_buf_fin;
281 /** Receive buffer CV. Broadcast when new data is inserted */
282 fibril_condvar_t rcv_buf_cv;
283
284 /** Send buffer */
285 uint8_t *snd_buf;
286 /** Send buffer size */
287 size_t snd_buf_size;
288 /** Send buffer number of bytes used */
289 size_t snd_buf_used;
290 /** Send buffer contains FIN */
291 bool snd_buf_fin;
292 /** Send buffer CV. Broadcast when space is made available in buffer */
293 fibril_condvar_t snd_buf_cv;
294
295 /** Send unacknowledged */
296 uint32_t snd_una;
297 /** Send next */
298 uint32_t snd_nxt;
299 /** Send window */
300 uint32_t snd_wnd;
301 /** Send urgent pointer */
302 uint32_t snd_up;
303 /** Segment sequence number used for last window update */
304 uint32_t snd_wl1;
305 /** Segment acknowledgement number used for last window update */
306 uint32_t snd_wl2;
307 /** Initial send sequence number */
308 uint32_t iss;
309
310 /** Receive next */
311 uint32_t rcv_nxt;
312 /** Receive window */
313 uint32_t rcv_wnd;
314 /** Receive urgent pointer */
315 uint32_t rcv_up;
316 /** Initial receive sequence number */
317 uint32_t irs;
318};
319
320/** Continuation of processing.
321 *
322 * When processing incoming segment, are we done or should we continue
323 * processing it?
324 */
325typedef enum {
326 cp_continue,
327 cp_done
328} cproc_t;
329
330/** Encoded PDU */
331typedef struct {
332 /** Source address */
333 inet_addr_t src;
334 /** Destination address */
335 inet_addr_t dest;
336 /** Encoded header */
337 void *header;
338 /** Encoded header size */
339 size_t header_size;
340 /** Text */
341 void *text;
342 /** Text size */
343 size_t text_size;
344} tcp_pdu_t;
345
346/** TCP client connection */
347typedef struct tcp_cconn {
348 /** Connection */
349 tcp_conn_t *conn;
350 /** Connection ID for the client */
351 sysarg_t id;
352 /** Client */
353 struct tcp_client *client;
354 link_t lclient;
355} tcp_cconn_t;
356
357/** TCP client listener */
358typedef struct tcp_clst {
359 /** Local endpoint */
360 inet_ep_t elocal;
361 /** Connection */
362 tcp_conn_t *conn;
363 /** Listener ID for the client */
364 sysarg_t id;
365 /** Client */
366 struct tcp_client *client;
367 /** Link to tcp_client_t.clst */
368 link_t lclient;
369} tcp_clst_t;
370
371/** TCP client */
372typedef struct tcp_client {
373 /** Client callback session */
374 async_sess_t *sess;
375 /** Client's connections */
376 list_t cconn; /* of tcp_cconn_t */
377 /** Client's listeners */
378 list_t clst;
379} tcp_client_t;
380
381/** Internal loopback type */
382typedef enum {
383 /** No loopback */
384 tcp_lb_none,
385 /** Segment loopback */
386 tcp_lb_segment,
387 /** PDU loopback */
388 tcp_lb_pdu
389} tcp_lb_t;
390
391#endif
392
393/** @}
394 */
Note: See TracBrowser for help on using the repository browser.