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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7e69e0e 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
RevLine 
[c5808b41]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/**
[032bbe7]34 * @file TCP entry points (close to those defined in the RFC)
[c5808b41]35 */
36
[d9ce049]37#include <fibril_synch.h>
[c5808b41]38#include <io/log.h>
[32105348]39#include <macros.h>
40#include <mem.h>
[c5808b41]41#include "conn.h"
42#include "tcp_type.h"
[32105348]43#include "tqueue.h"
[762b48a]44#include "ucall.h"
[c5808b41]45
46/*
47 * User calls
48 */
49
50/** OPEN user call
51 *
[0ac2158]52 * @param lsock Local socket
[c5808b41]53 * @param fsock Foreign socket
54 * @param acpass Active/passive
[ae481e0]55 * @param oflags Open flags
[c5808b41]56 * @param conn Connection
[854e79a6]57 *
[0ac2158]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 *
[854e79a6]62 * XXX We should be able to call active open on an existing listening
63 * connection.
[a4ee3ab2]64 * XXX We should be able to get connection structure immediately, before
65 * establishment.
[c5808b41]66 */
[0ac2158]67tcp_error_t tcp_uc_open(tcp_sock_t *lsock, tcp_sock_t *fsock, acpass_t acpass,
[ae481e0]68 tcp_open_flags_t oflags, tcp_conn_t **conn)
[c5808b41]69{
70 tcp_conn_t *nconn;
71
[a1a101d]72 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open(%p, %p, %s, %s, %p)",
[0ac2158]73 lsock, fsock, acpass == ap_active ? "active" : "passive",
[ae481e0]74 oflags == tcp_open_nonblock ? "nonblock" : "none", conn);
[c5808b41]75
[0ac2158]76 nconn = tcp_conn_new(lsock, fsock);
[c5808b41]77 tcp_conn_add(nconn);
[5441670]78 tcp_conn_lock(nconn);
[c5808b41]79
80 if (acpass == ap_active) {
81 /* Synchronize (initiate) connection */
82 tcp_conn_sync(nconn);
[a4ee3ab2]83 }
84
[ae481e0]85 if (oflags == tcp_open_nonblock) {
[5441670]86 tcp_conn_unlock(nconn);
[b243da3]87 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open -> %p", nconn);
[ae481e0]88 *conn = nconn;
89 return TCP_EOK;
90 }
91
[a4ee3ab2]92 /* Wait for connection to be established or reset */
[a1a101d]93 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open: Wait for connection.");
[a4ee3ab2]94 while (nconn->cstate == st_listen ||
95 nconn->cstate == st_syn_sent ||
96 nconn->cstate == st_syn_received) {
[0edaf0f6]97 fibril_condvar_wait(&nconn->cstate_cv, &nconn->lock);
[a4ee3ab2]98 }
[004a5fe]99
[a4ee3ab2]100 if (nconn->cstate != st_established) {
[a1a101d]101 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open: Connection was reset.");
[a4ee3ab2]102 assert(nconn->cstate == st_closed);
[78192cc7]103 tcp_conn_unlock(nconn);
[a4ee3ab2]104 return TCP_ERESET;
[c5808b41]105 }
106
[78192cc7]107 tcp_conn_unlock(nconn);
[a1a101d]108 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open: Connection was established.");
[a4ee3ab2]109
[c5808b41]110 *conn = nconn;
[a1a101d]111 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open -> %p", nconn);
[004a5fe]112 return TCP_EOK;
[c5808b41]113}
114
115/** SEND user call */
[854e79a6]116tcp_error_t tcp_uc_send(tcp_conn_t *conn, void *data, size_t size,
117 xflags_t flags)
[c5808b41]118{
[32105348]119 size_t buf_free;
120 size_t xfer_size;
121
[a1a101d]122 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_send()", conn->name);
[32105348]123
[78192cc7]124 tcp_conn_lock(conn);
[0edaf0f6]125
126 if (conn->cstate == st_closed) {
[78192cc7]127 tcp_conn_unlock(conn);
[854e79a6]128 return TCP_ENOTEXIST;
[0edaf0f6]129 }
[854e79a6]130
131 if (conn->cstate == st_listen) {
132 /* Change connection to active */
133 tcp_conn_sync(conn);
134 }
135
[bbf159a]136
137 if (conn->snd_buf_fin) {
[78192cc7]138 tcp_conn_unlock(conn);
[854e79a6]139 return TCP_ECLOSING;
[bbf159a]140 }
[854e79a6]141
[32105348]142 while (size > 0) {
143 buf_free = conn->snd_buf_size - conn->snd_buf_used;
[0d29e0cd]144 while (buf_free == 0 && !conn->reset) {
[a1a101d]145 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: buf_free == 0, waiting.",
[bbf159a]146 conn->name);
[0edaf0f6]147 fibril_condvar_wait(&conn->snd_buf_cv, &conn->lock);
[0d29e0cd]148 buf_free = conn->snd_buf_size - conn->snd_buf_used;
149 }
[32105348]150
[bbf159a]151 if (conn->reset) {
[78192cc7]152 tcp_conn_unlock(conn);
[d9e14fa4]153 return TCP_ERESET;
[bbf159a]154 }
[d9e14fa4]155
[32105348]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;
[bbf159a]163
164 tcp_tqueue_new_data(conn);
[32105348]165 }
166
167 tcp_tqueue_new_data(conn);
[78192cc7]168 tcp_conn_unlock(conn);
[854e79a6]169
170 return TCP_EOK;
[c5808b41]171}
172
173/** RECEIVE user call */
[854e79a6]174tcp_error_t tcp_uc_receive(tcp_conn_t *conn, void *buf, size_t size,
175 size_t *rcvd, xflags_t *xflags)
[c5808b41]176{
[d9ce049]177 size_t xfer_size;
178
[a1a101d]179 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_receive()", conn->name);
[d9ce049]180
[78192cc7]181 tcp_conn_lock(conn);
[7cf7ded]182
[0edaf0f6]183 if (conn->cstate == st_closed) {
[78192cc7]184 tcp_conn_unlock(conn);
[0edaf0f6]185 return TCP_ENOTEXIST;
186 }
[d9ce049]187
188 /* Wait for data to become available */
[d9e14fa4]189 while (conn->rcv_buf_used == 0 && !conn->rcv_buf_fin && !conn->reset) {
[a1a101d]190 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_receive() - wait for data");
[0edaf0f6]191 fibril_condvar_wait(&conn->rcv_buf_cv, &conn->lock);
[d9ce049]192 }
193
[8c7a054]194 if (conn->rcv_buf_used == 0) {
195 *rcvd = 0;
196 *xflags = 0;
[d9e14fa4]197
198 if (conn->rcv_buf_fin) {
199 /* End of data, peer closed connection */
[78192cc7]200 tcp_conn_unlock(conn);
[d9e14fa4]201 return TCP_ECLOSING;
202 } else {
203 /* Connection was reset */
204 assert(conn->reset);
[78192cc7]205 tcp_conn_unlock(conn);
[d9e14fa4]206 return TCP_ERESET;
207 }
[8c7a054]208 }
209
[d9ce049]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
[a1a101d]227 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_receive() - returning %zu bytes",
[23fe06c]228 conn->name, xfer_size);
[854e79a6]229
[78192cc7]230 tcp_conn_unlock(conn);
[0edaf0f6]231
[854e79a6]232 return TCP_EOK;
[c5808b41]233}
234
235/** CLOSE user call */
[854e79a6]236tcp_error_t tcp_uc_close(tcp_conn_t *conn)
[c5808b41]237{
[b243da3]238 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_close(%p)", conn->name,
239 conn);
[8c7a054]240
[78192cc7]241 tcp_conn_lock(conn);
[0edaf0f6]242
243 if (conn->cstate == st_closed) {
[b243da3]244 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - ENOTEXIST");
[78192cc7]245 tcp_conn_unlock(conn);
[854e79a6]246 return TCP_ENOTEXIST;
[0edaf0f6]247 }
[854e79a6]248
[b243da3]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);
[78192cc7]253 tcp_conn_unlock(conn);
[b243da3]254 return TCP_EOK;
255 }
256
[0edaf0f6]257 if (conn->snd_buf_fin) {
[b243da3]258 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - ECLOSING");
[78192cc7]259 tcp_conn_unlock(conn);
[854e79a6]260 return TCP_ECLOSING;
[0edaf0f6]261 }
[854e79a6]262
[b243da3]263 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - set snd_buf_fin");
[8c7a054]264 conn->snd_buf_fin = true;
265 tcp_tqueue_new_data(conn);
[854e79a6]266
[78192cc7]267 tcp_conn_unlock(conn);
[854e79a6]268 return TCP_EOK;
[c5808b41]269}
270
271/** ABORT user call */
272void tcp_uc_abort(tcp_conn_t *conn)
273{
[a1a101d]274 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_abort()");
[c5808b41]275}
276
277/** STATUS user call */
278void tcp_uc_status(tcp_conn_t *conn, tcp_conn_status_t *cstatus)
279{
[a1a101d]280 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_status()");
[ae481e0]281 cstatus->cstate = conn->cstate;
[c5808b41]282}
283
[7a8c1c4e]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{
[a1a101d]292 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_delete()");
[7a8c1c4e]293 tcp_conn_delete(conn);
294}
[c5808b41]295
[ae481e0]296void tcp_uc_set_cstate_cb(tcp_conn_t *conn, tcp_cstate_cb_t cb, void *arg)
297{
[a1a101d]298 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_set_ctate_cb(%p, %p, %p)",
[ae481e0]299 conn, cb, arg);
300
301 conn->cstate_cb = cb;
302 conn->cstate_cb_arg = arg;
303}
304
[c5808b41]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
[c0f3460]314 log_msg(LOG_DEFAULT, LVL_DEBUG,
315 "tcp_as_segment_arrived(f:(%u), l:(%u))",
316 sp->foreign.port, sp->local.port);
317
[0edaf0f6]318 conn = tcp_conn_find_ref(sp);
319 if (conn == NULL) {
[a1a101d]320 log_msg(LOG_DEFAULT, LVL_WARN, "No connection found.");
[c5808b41]321 tcp_unexpected_segment(sp, seg);
[0edaf0f6]322 return;
[c5808b41]323 }
[0edaf0f6]324
[78192cc7]325 tcp_conn_lock(conn);
[0edaf0f6]326
327 if (conn->cstate == st_closed) {
[a1a101d]328 log_msg(LOG_DEFAULT, LVL_WARN, "Connection is closed.");
[0edaf0f6]329 tcp_unexpected_segment(sp, seg);
[78192cc7]330 tcp_conn_unlock(conn);
[0edaf0f6]331 tcp_conn_delref(conn);
332 return;
333 }
334
[a2e3ee6]335 if (inet_addr_is_any(&conn->ident.foreign.addr))
336 conn->ident.foreign.addr = sp->foreign.addr;
337
[0edaf0f6]338 if (conn->ident.foreign.port == TCP_PORT_ANY)
339 conn->ident.foreign.port = sp->foreign.port;
[a2e3ee6]340
341 if (inet_addr_is_any(&conn->ident.local.addr))
342 conn->ident.local.addr = sp->local.addr;
[0edaf0f6]343
344 tcp_conn_segment_arrived(conn, seg);
345
[78192cc7]346 tcp_conn_unlock(conn);
[0edaf0f6]347 tcp_conn_delref(conn);
[c5808b41]348}
349
350/*
351 * Timeouts
352 */
353
354/** User timeout */
355void tcp_to_user(void)
356{
[a1a101d]357 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_to_user()");
[c5808b41]358}
359
360/**
361 * @}
362 */
Note: See TracBrowser for help on using the repository browser.