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

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

Fibril timer locking improvements.

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