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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5441670 was 5441670, checked in by Martin Decky <martin@…>, 11 years ago

fix TCP locking issue and crash
connection needs to be locked before tcp_conn_sync() is called

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