source: mainline/uspace/srv/net/tcp/ucall.c@ ca48672

Last change on this file since ca48672 was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 8.7 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
33/**
34 * @file TCP entry points (close to those defined in the RFC)
35 */
36
37#include <errno.h>
38#include <fibril_synch.h>
39#include <io/log.h>
40#include <macros.h>
41#include <mem.h>
42#include "conn.h"
43#include "tcp_type.h"
44#include "tqueue.h"
45#include "ucall.h"
46
47/*
48 * User calls
49 */
50
51/** OPEN user call
52 *
53 * @param epp Endpoint pair
54 * @param acpass Active/passive
55 * @param oflags Open flags
56 * @param conn Connection
57 *
58 * Unlike in the spec we allow specifying the local address. This means
59 * the implementation does not need to magically guess it, especially
60 * considering there can be more than one local address.
61 *
62 * XXX We should be able to call active open on an existing listening
63 * connection.
64 * XXX We should be able to get connection structure immediately, before
65 * establishment.
66 */
67tcp_error_t tcp_uc_open(inet_ep2_t *epp, acpass_t acpass,
68 tcp_open_flags_t oflags, tcp_conn_t **conn)
69{
70 tcp_conn_t *nconn;
71 errno_t rc;
72
73 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open(%p, %s, %s, %p)",
74 epp, acpass == ap_active ? "active" : "passive",
75 oflags == tcp_open_nonblock ? "nonblock" : "none", conn);
76
77 nconn = tcp_conn_new(epp);
78 rc = tcp_conn_add(nconn);
79 if (rc != EOK) {
80 tcp_conn_delete(nconn);
81 return TCP_EEXISTS;
82 }
83
84 tcp_conn_lock(nconn);
85
86 if (acpass == ap_active) {
87 /* Synchronize (initiate) connection */
88 tcp_conn_sync(nconn);
89 }
90
91 if (oflags == tcp_open_nonblock) {
92 tcp_conn_unlock(nconn);
93 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open -> %p", nconn);
94 *conn = nconn;
95 return TCP_EOK;
96 }
97
98 /* Wait for connection to be established or reset */
99 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open: Wait for connection.");
100 while (nconn->cstate == st_listen ||
101 nconn->cstate == st_syn_sent ||
102 nconn->cstate == st_syn_received) {
103 fibril_condvar_wait(&nconn->cstate_cv, &nconn->lock);
104 }
105
106 if (nconn->cstate != st_established) {
107 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open: Connection was reset.");
108 assert(nconn->cstate == st_closed);
109 tcp_conn_unlock(nconn);
110 tcp_conn_delete(nconn);
111 return TCP_ERESET;
112 }
113
114 tcp_conn_unlock(nconn);
115 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open: Connection was established.");
116
117 *conn = nconn;
118 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open -> %p", nconn);
119 return TCP_EOK;
120}
121
122/** SEND user call */
123tcp_error_t tcp_uc_send(tcp_conn_t *conn, void *data, size_t size,
124 xflags_t flags)
125{
126 size_t buf_free;
127 size_t xfer_size;
128
129 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_send()", conn->name);
130
131 tcp_conn_lock(conn);
132
133 if (conn->cstate == st_closed) {
134 tcp_conn_unlock(conn);
135 return TCP_ENOTEXIST;
136 }
137
138 if (conn->cstate == st_listen) {
139 /* Change connection to active */
140 tcp_conn_sync(conn);
141 }
142
143 if (conn->snd_buf_fin) {
144 tcp_conn_unlock(conn);
145 return TCP_ECLOSING;
146 }
147
148 while (size > 0) {
149 buf_free = conn->snd_buf_size - conn->snd_buf_used;
150 while (buf_free == 0 && !conn->reset) {
151 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: buf_free == 0, waiting.",
152 conn->name);
153 fibril_condvar_wait(&conn->snd_buf_cv, &conn->lock);
154 buf_free = conn->snd_buf_size - conn->snd_buf_used;
155 }
156
157 if (conn->reset) {
158 tcp_conn_unlock(conn);
159 return TCP_ERESET;
160 }
161
162 xfer_size = min(size, buf_free);
163
164 /* Copy data to buffer */
165 memcpy(conn->snd_buf + conn->snd_buf_used, data, xfer_size);
166 data += xfer_size;
167 conn->snd_buf_used += xfer_size;
168 size -= xfer_size;
169
170 tcp_tqueue_new_data(conn);
171 }
172
173 tcp_tqueue_new_data(conn);
174 tcp_conn_unlock(conn);
175
176 return TCP_EOK;
177}
178
179/** RECEIVE user call */
180tcp_error_t tcp_uc_receive(tcp_conn_t *conn, void *buf, size_t size,
181 size_t *rcvd, xflags_t *xflags)
182{
183 size_t xfer_size;
184
185 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_receive()", conn->name);
186
187 tcp_conn_lock(conn);
188
189 if (conn->cstate == st_closed) {
190 tcp_conn_unlock(conn);
191 return TCP_ENOTEXIST;
192 }
193
194 /* Wait for data to become available */
195 while (conn->rcv_buf_used == 0 && !conn->rcv_buf_fin && !conn->reset) {
196 tcp_conn_unlock(conn);
197 return TCP_EAGAIN;
198 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_receive() - wait for data");
199 fibril_condvar_wait(&conn->rcv_buf_cv, &conn->lock);
200 }
201
202 if (conn->rcv_buf_used == 0) {
203 *rcvd = 0;
204 *xflags = 0;
205
206 if (conn->rcv_buf_fin) {
207 /* End of data, peer closed connection */
208 tcp_conn_unlock(conn);
209 return TCP_ECLOSING;
210 } else {
211 /* Connection was reset */
212 assert(conn->reset);
213 tcp_conn_unlock(conn);
214 return TCP_ERESET;
215 }
216 }
217
218 /* Copy data from receive buffer to user buffer */
219 xfer_size = min(size, conn->rcv_buf_used);
220 memcpy(buf, conn->rcv_buf, xfer_size);
221 *rcvd = xfer_size;
222
223 /* Remove data from receive buffer */
224 memmove(conn->rcv_buf, conn->rcv_buf + xfer_size, conn->rcv_buf_used -
225 xfer_size);
226 conn->rcv_buf_used -= xfer_size;
227 conn->rcv_wnd += xfer_size;
228
229 /* TODO */
230 *xflags = 0;
231
232 /* Send new size of receive window */
233 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
234
235 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_receive() - returning %zu bytes",
236 conn->name, xfer_size);
237
238 tcp_conn_unlock(conn);
239
240 return TCP_EOK;
241}
242
243/** CLOSE user call */
244tcp_error_t tcp_uc_close(tcp_conn_t *conn)
245{
246 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_close(%p)", conn->name,
247 conn);
248
249 tcp_conn_lock(conn);
250
251 if (conn->cstate == st_closed) {
252 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - ENOTEXIST");
253 tcp_conn_unlock(conn);
254 return TCP_ENOTEXIST;
255 }
256
257 if (conn->cstate == st_listen || conn->cstate == st_syn_sent) {
258 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - listen/syn_sent");
259 tcp_conn_reset(conn);
260 tcp_conn_unlock(conn);
261 return TCP_EOK;
262 }
263
264 if (conn->snd_buf_fin) {
265 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - ECLOSING");
266 tcp_conn_unlock(conn);
267 return TCP_ECLOSING;
268 }
269
270 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - set snd_buf_fin");
271 conn->snd_buf_fin = true;
272 tcp_tqueue_new_data(conn);
273
274 tcp_conn_unlock(conn);
275 return TCP_EOK;
276}
277
278/** ABORT user call */
279void tcp_uc_abort(tcp_conn_t *conn)
280{
281 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_abort()");
282
283 tcp_conn_lock(conn);
284 tcp_conn_reset(conn);
285 tcp_conn_unlock(conn);
286}
287
288/** STATUS user call */
289void tcp_uc_status(tcp_conn_t *conn, tcp_conn_status_t *cstatus)
290{
291 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_status()");
292 cstatus->cstate = conn->cstate;
293}
294
295/** Delete connection user call.
296 *
297 * (Not in spec.) Inform TCP that the user is done with this connection
298 * and will not make any further calls/references to it. TCP can deallocate
299 * the connection from now on.
300 */
301void tcp_uc_delete(tcp_conn_t *conn)
302{
303 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_delete()");
304 tcp_conn_delete(conn);
305}
306
307void tcp_uc_set_cb(tcp_conn_t *conn, tcp_cb_t *cb, void *arg)
308{
309 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_set_cb(%p, %p, %p)",
310 conn, cb, arg);
311
312 conn->cb = cb;
313 conn->cb_arg = arg;
314}
315
316void *tcp_uc_get_userptr(tcp_conn_t *conn)
317{
318 return conn->cb_arg;
319}
320
321/*
322 * Arriving segments
323 */
324
325/** Segment arrived */
326void tcp_as_segment_arrived(inet_ep2_t *epp, tcp_segment_t *seg)
327{
328 tcp_conn_t *conn;
329
330 log_msg(LOG_DEFAULT, LVL_DEBUG,
331 "tcp_as_segment_arrived(f:(%u), l:(%u))",
332 epp->remote.port, epp->local.port);
333
334 conn = tcp_conn_find_ref(epp);
335 if (conn == NULL) {
336 log_msg(LOG_DEFAULT, LVL_WARN, "No connection found.");
337 tcp_unexpected_segment(epp, seg);
338 return;
339 }
340
341 tcp_conn_segment_arrived(conn, epp, seg);
342 tcp_conn_delref(conn);
343}
344
345/*
346 * Timeouts
347 */
348
349/** User timeout */
350void tcp_to_user(void)
351{
352 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_to_user()");
353}
354
355/**
356 * @}
357 */
Note: See TracBrowser for help on using the repository browser.