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

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

Must update amap when connection ident changes.

  • Property mode set to 100644
File size: 8.6 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 int 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 return TCP_ERESET;
111 }
112
113 tcp_conn_unlock(nconn);
114 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open: Connection was established.");
115
116 *conn = nconn;
117 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open -> %p", nconn);
118 return TCP_EOK;
119}
120
121/** SEND user call */
122tcp_error_t tcp_uc_send(tcp_conn_t *conn, void *data, size_t size,
123 xflags_t flags)
124{
125 size_t buf_free;
126 size_t xfer_size;
127
128 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_send()", conn->name);
129
130 tcp_conn_lock(conn);
131
132 if (conn->cstate == st_closed) {
133 tcp_conn_unlock(conn);
134 return TCP_ENOTEXIST;
135 }
136
137 if (conn->cstate == st_listen) {
138 /* Change connection to active */
139 tcp_conn_sync(conn);
140 }
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_remove(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
285/** STATUS user call */
286void tcp_uc_status(tcp_conn_t *conn, tcp_conn_status_t *cstatus)
287{
288 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_status()");
289 cstatus->cstate = conn->cstate;
290}
291
292/** Delete connection user call.
293 *
294 * (Not in spec.) Inform TCP that the user is done with this connection
295 * and will not make any further calls/references to it. TCP can deallocate
296 * the connection from now on.
297 */
298void tcp_uc_delete(tcp_conn_t *conn)
299{
300 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_delete()");
301 tcp_conn_delete(conn);
302}
303
304void tcp_uc_set_cb(tcp_conn_t *conn, tcp_cb_t *cb, void *arg)
305{
306 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_set_cb(%p, %p, %p)",
307 conn, cb, arg);
308
309 conn->cb = cb;
310 conn->cb_arg = arg;
311}
312
313void *tcp_uc_get_userptr(tcp_conn_t *conn)
314{
315 return conn->cb_arg;
316}
317
318/*
319 * Arriving segments
320 */
321
322/** Segment arrived */
323void tcp_as_segment_arrived(inet_ep2_t *epp, tcp_segment_t *seg)
324{
325 tcp_conn_t *conn;
326
327 log_msg(LOG_DEFAULT, LVL_DEBUG,
328 "tcp_as_segment_arrived(f:(%u), l:(%u))",
329 epp->remote.port, epp->local.port);
330
331 conn = tcp_conn_find_ref(epp);
332 if (conn == NULL) {
333 log_msg(LOG_DEFAULT, LVL_WARN, "No connection found.");
334 tcp_unexpected_segment(epp, seg);
335 return;
336 }
337
338 tcp_conn_segment_arrived(conn, epp, seg);
339 tcp_conn_delref(conn);
340}
341
342/*
343 * Timeouts
344 */
345
346/** User timeout */
347void tcp_to_user(void)
348{
349 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_to_user()");
350}
351
352/**
353 * @}
354 */
Note: See TracBrowser for help on using the repository browser.