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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3bacee1 was b7fd2a0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

Although this is a massive commit, it is a simple text replacement, and thus
is very easy to verify. Simply do the following:

`
git checkout <this commit's hash>
git reset HEAD
git add .
tools/srepl '\berrno_t\b' int
git add .
tools/srepl '\bsys_errno_t\b' sysarg_t
git reset
git diff
`

While this doesn't ensure that the replacements are correct, it does ensure
that the commit doesn't do anything except those replacements. Since errno_t
is typedef'd to int in the usual case (and sys_errno_t to sysarg_t), even if
incorrect, this commit cannot change behavior.

  • 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
144 if (conn->snd_buf_fin) {
145 tcp_conn_unlock(conn);
146 return TCP_ECLOSING;
147 }
148
149 while (size > 0) {
150 buf_free = conn->snd_buf_size - conn->snd_buf_used;
151 while (buf_free == 0 && !conn->reset) {
152 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: buf_free == 0, waiting.",
153 conn->name);
154 fibril_condvar_wait(&conn->snd_buf_cv, &conn->lock);
155 buf_free = conn->snd_buf_size - conn->snd_buf_used;
156 }
157
158 if (conn->reset) {
159 tcp_conn_unlock(conn);
160 return TCP_ERESET;
161 }
162
163 xfer_size = min(size, buf_free);
164
165 /* Copy data to buffer */
166 memcpy(conn->snd_buf + conn->snd_buf_used, data, xfer_size);
167 data += xfer_size;
168 conn->snd_buf_used += xfer_size;
169 size -= xfer_size;
170
171 tcp_tqueue_new_data(conn);
172 }
173
174 tcp_tqueue_new_data(conn);
175 tcp_conn_unlock(conn);
176
177 return TCP_EOK;
178}
179
180/** RECEIVE user call */
181tcp_error_t tcp_uc_receive(tcp_conn_t *conn, void *buf, size_t size,
182 size_t *rcvd, xflags_t *xflags)
183{
184 size_t xfer_size;
185
186 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_receive()", conn->name);
187
188 tcp_conn_lock(conn);
189
190 if (conn->cstate == st_closed) {
191 tcp_conn_unlock(conn);
192 return TCP_ENOTEXIST;
193 }
194
195 /* Wait for data to become available */
196 while (conn->rcv_buf_used == 0 && !conn->rcv_buf_fin && !conn->reset) {
197 tcp_conn_unlock(conn);
198 return TCP_EAGAIN;
199 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_receive() - wait for data");
200 fibril_condvar_wait(&conn->rcv_buf_cv, &conn->lock);
201 }
202
203 if (conn->rcv_buf_used == 0) {
204 *rcvd = 0;
205 *xflags = 0;
206
207 if (conn->rcv_buf_fin) {
208 /* End of data, peer closed connection */
209 tcp_conn_unlock(conn);
210 return TCP_ECLOSING;
211 } else {
212 /* Connection was reset */
213 assert(conn->reset);
214 tcp_conn_unlock(conn);
215 return TCP_ERESET;
216 }
217 }
218
219 /* Copy data from receive buffer to user buffer */
220 xfer_size = min(size, conn->rcv_buf_used);
221 memcpy(buf, conn->rcv_buf, xfer_size);
222 *rcvd = xfer_size;
223
224 /* Remove data from receive buffer */
225 memmove(conn->rcv_buf, conn->rcv_buf + xfer_size, conn->rcv_buf_used -
226 xfer_size);
227 conn->rcv_buf_used -= xfer_size;
228 conn->rcv_wnd += xfer_size;
229
230 /* TODO */
231 *xflags = 0;
232
233 /* Send new size of receive window */
234 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
235
236 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_receive() - returning %zu bytes",
237 conn->name, xfer_size);
238
239 tcp_conn_unlock(conn);
240
241 return TCP_EOK;
242}
243
244/** CLOSE user call */
245tcp_error_t tcp_uc_close(tcp_conn_t *conn)
246{
247 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_close(%p)", conn->name,
248 conn);
249
250 tcp_conn_lock(conn);
251
252 if (conn->cstate == st_closed) {
253 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - ENOTEXIST");
254 tcp_conn_unlock(conn);
255 return TCP_ENOTEXIST;
256 }
257
258 if (conn->cstate == st_listen || conn->cstate == st_syn_sent) {
259 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - listen/syn_sent");
260 tcp_conn_reset(conn);
261 tcp_conn_unlock(conn);
262 return TCP_EOK;
263 }
264
265 if (conn->snd_buf_fin) {
266 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - ECLOSING");
267 tcp_conn_unlock(conn);
268 return TCP_ECLOSING;
269 }
270
271 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - set snd_buf_fin");
272 conn->snd_buf_fin = true;
273 tcp_tqueue_new_data(conn);
274
275 tcp_conn_unlock(conn);
276 return TCP_EOK;
277}
278
279/** ABORT user call */
280void tcp_uc_abort(tcp_conn_t *conn)
281{
282 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_abort()");
283
284 tcp_conn_lock(conn);
285 tcp_conn_reset(conn);
286 tcp_conn_unlock(conn);
287}
288
289/** STATUS user call */
290void tcp_uc_status(tcp_conn_t *conn, tcp_conn_status_t *cstatus)
291{
292 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_status()");
293 cstatus->cstate = conn->cstate;
294}
295
296/** Delete connection user call.
297 *
298 * (Not in spec.) Inform TCP that the user is done with this connection
299 * and will not make any further calls/references to it. TCP can deallocate
300 * the connection from now on.
301 */
302void tcp_uc_delete(tcp_conn_t *conn)
303{
304 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_delete()");
305 tcp_conn_delete(conn);
306}
307
308void tcp_uc_set_cb(tcp_conn_t *conn, tcp_cb_t *cb, void *arg)
309{
310 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_set_cb(%p, %p, %p)",
311 conn, cb, arg);
312
313 conn->cb = cb;
314 conn->cb_arg = arg;
315}
316
317void *tcp_uc_get_userptr(tcp_conn_t *conn)
318{
319 return conn->cb_arg;
320}
321
322/*
323 * Arriving segments
324 */
325
326/** Segment arrived */
327void tcp_as_segment_arrived(inet_ep2_t *epp, tcp_segment_t *seg)
328{
329 tcp_conn_t *conn;
330
331 log_msg(LOG_DEFAULT, LVL_DEBUG,
332 "tcp_as_segment_arrived(f:(%u), l:(%u))",
333 epp->remote.port, epp->local.port);
334
335 conn = tcp_conn_find_ref(epp);
336 if (conn == NULL) {
337 log_msg(LOG_DEFAULT, LVL_WARN, "No connection found.");
338 tcp_unexpected_segment(epp, seg);
339 return;
340 }
341
342 tcp_conn_segment_arrived(conn, epp, seg);
343 tcp_conn_delref(conn);
344}
345
346/*
347 * Timeouts
348 */
349
350/** User timeout */
351void tcp_to_user(void)
352{
353 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_to_user()");
354}
355
356/**
357 * @}
358 */
Note: See TracBrowser for help on using the repository browser.