[7907cf9] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Jakub Jermar
|
---|
| 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 libc
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | /**
|
---|
| 36 | * This file implements simple session support for the async framework.
|
---|
| 37 | *
|
---|
| 38 | * By the term 'session', we mean a logical data path between a client and a
|
---|
[d3a1ade3] | 39 | * server over which the client can perform multiple concurrent exchanges.
|
---|
| 40 | * Each exchange consists of one or more requests (IPC calls) which can
|
---|
[7907cf9] | 41 | * be potentially blocking.
|
---|
| 42 | *
|
---|
| 43 | * Clients and servers are naturally connected using IPC phones, thus an IPC
|
---|
| 44 | * phone represents a session between a client and a server. In one
|
---|
[d3a1ade3] | 45 | * session, there can be many outstanding exchanges. In the current
|
---|
| 46 | * implementation each concurrent exchanges takes place over a different
|
---|
| 47 | * connection (there can be at most one active exchage per connection).
|
---|
[7907cf9] | 48 | *
|
---|
| 49 | * Sessions make it useful for a client or client API to support concurrent
|
---|
| 50 | * requests, independent of the actual implementation. Sessions provide
|
---|
| 51 | * an abstract interface to concurrent IPC communication. This is especially
|
---|
| 52 | * useful for client API stubs that aim to be reentrant (i.e. that allow
|
---|
| 53 | * themselves to be called from different fibrils and threads concurrently).
|
---|
| 54 | *
|
---|
| 55 | * There are several possible implementations of sessions. This implementation
|
---|
| 56 | * uses additional phones to represent sessions. Using phones both for the
|
---|
[d3a1ade3] | 57 | * session and also for its exchages/connections has several advantages:
|
---|
[7907cf9] | 58 | *
|
---|
[d3a1ade3] | 59 | * - to make a series of exchanges over a session, the client can continue to
|
---|
[7907cf9] | 60 | * use the existing async framework APIs
|
---|
| 61 | * - the server supports sessions by the virtue of spawning a new connection
|
---|
| 62 | * fibril, just as it does for every new connection even without sessions
|
---|
| 63 | * - the implementation is pretty straightforward; a very naive implementation
|
---|
[d3a1ade3] | 64 | * would be to make each exchage using a fresh phone (that is what we
|
---|
[7907cf9] | 65 | * have done in the past); a slightly better approach would be to cache
|
---|
[d3a1ade3] | 66 | * connections so that they can be reused by a later exchange within
|
---|
[7907cf9] | 67 | * the same session (that is what this implementation does)
|
---|
| 68 | *
|
---|
| 69 | * The main disadvantages of using phones to represent sessions are:
|
---|
| 70 | *
|
---|
[d3a1ade3] | 71 | * - if there are too many exchanges (even cached ones), the task may hit its
|
---|
[7907cf9] | 72 | * limit on the maximum number of connected phones, which could prevent the
|
---|
| 73 | * task from making new IPC connections to other tasks
|
---|
| 74 | * - if there are too many IPC connections already, it may be impossible to
|
---|
[d3a1ade3] | 75 | * create an exchange by connecting a new phone thanks to the task's limit on
|
---|
[7907cf9] | 76 | * the maximum number of connected phones
|
---|
| 77 | *
|
---|
| 78 | * These problems can be alleviated by increasing the limit on the maximum
|
---|
| 79 | * number of connected phones to some reasonable value and by limiting the number
|
---|
| 80 | * of cached connections to some fraction of this limit.
|
---|
| 81 | *
|
---|
| 82 | * The cache itself has a mechanism to close some number of unused phones if a
|
---|
| 83 | * new phone cannot be connected, but the outer world currently does not have a
|
---|
| 84 | * way to ask the phone cache to shrink.
|
---|
| 85 | *
|
---|
| 86 | * To minimize the confusion stemming from the fact that we use phones for two
|
---|
| 87 | * things (the session itself and also one for each data connection), this file
|
---|
| 88 | * makes the distinction by using the term 'session phone' for the former and
|
---|
| 89 | * 'data phone' for the latter. Under the hood, all phones remain equal,
|
---|
| 90 | * of course.
|
---|
| 91 | *
|
---|
| 92 | * There is a small inefficiency in that the cache repeatedly allocates and
|
---|
| 93 | * deallocates the conn_node_t structures when in fact it could keep the
|
---|
| 94 | * allocated structures around and reuse them later. But such a solution would
|
---|
| 95 | * be effectively implementing a poor man's slab allocator while it would be
|
---|
| 96 | * better to have the slab allocator ported to uspace so that everyone could
|
---|
| 97 | * benefit from it.
|
---|
| 98 | */
|
---|
| 99 |
|
---|
| 100 | #include <async_sess.h>
|
---|
| 101 | #include <fibril_synch.h>
|
---|
| 102 | #include <adt/list.h>
|
---|
| 103 | #include <adt/hash_table.h>
|
---|
| 104 | #include <malloc.h>
|
---|
| 105 | #include <errno.h>
|
---|
| 106 | #include <assert.h>
|
---|
[d161715] | 107 | #include <async.h>
|
---|
[47b7006] | 108 | #include "private/async_sess.h"
|
---|
[7907cf9] | 109 |
|
---|
[8cd21d16] | 110 | /** An inactive open connection. */
|
---|
[7907cf9] | 111 | typedef struct {
|
---|
[8cd21d16] | 112 | link_t sess_link; /**< Link for the session list of inactive connections. */
|
---|
[8b5c8ae] | 113 | link_t global_link; /**< Link for the global list of inactive connections. */
|
---|
[7907cf9] | 114 | int data_phone; /**< Connected data phone. */
|
---|
| 115 | } conn_node_t;
|
---|
| 116 |
|
---|
| 117 | /**
|
---|
[8b5c8ae] | 118 | * Mutex protecting the inactive_conn_head list, the session list and the
|
---|
| 119 | * avail_phone condition variable.
|
---|
[7907cf9] | 120 | */
|
---|
| 121 | static fibril_mutex_t async_sess_mutex;
|
---|
| 122 |
|
---|
| 123 | /**
|
---|
| 124 | * List of all currently inactive connections.
|
---|
| 125 | */
|
---|
| 126 | static LIST_INITIALIZE(inactive_conn_head);
|
---|
| 127 |
|
---|
| 128 | /**
|
---|
[a1dc7fe] | 129 | * List of all open sessions.
|
---|
[7907cf9] | 130 | */
|
---|
[a1dc7fe] | 131 | static LIST_INITIALIZE(session_list_head);
|
---|
[7907cf9] | 132 |
|
---|
[8b5c8ae] | 133 | /**
|
---|
| 134 | * Condition variable used to wait for a phone to become available.
|
---|
| 135 | */
|
---|
| 136 | static FIBRIL_CONDVAR_INITIALIZE(avail_phone_cv);
|
---|
| 137 |
|
---|
[7907cf9] | 138 | /** Initialize the async_sess subsystem.
|
---|
| 139 | *
|
---|
| 140 | * Needs to be called prior to any other interface in this file.
|
---|
[47b7006] | 141 | *
|
---|
[7907cf9] | 142 | */
|
---|
[47b7006] | 143 | void __async_sess_init(void)
|
---|
[7907cf9] | 144 | {
|
---|
| 145 | fibril_mutex_initialize(&async_sess_mutex);
|
---|
| 146 | list_initialize(&inactive_conn_head);
|
---|
[a1dc7fe] | 147 | list_initialize(&session_list_head);
|
---|
[7907cf9] | 148 | }
|
---|
| 149 |
|
---|
[8cd21d16] | 150 | /** Create a session.
|
---|
| 151 | *
|
---|
| 152 | * Session is a logical datapath from a client task to a server task.
|
---|
| 153 | * One session can accomodate multiple concurrent exchanges. Here
|
---|
| 154 | * @a phone is a phone connected to the desired server task.
|
---|
| 155 | *
|
---|
| 156 | * This function always succeeds.
|
---|
| 157 | *
|
---|
| 158 | * @param sess Session structure provided by caller, will be filled in.
|
---|
| 159 | * @param phone Phone connected to the desired server task.
|
---|
[9d12059] | 160 | * @param arg1 Value to pass as first argument upon creating a new
|
---|
| 161 | * connection. Typical use is to identify a resource within
|
---|
| 162 | * the server that the caller wants to access (port ID,
|
---|
| 163 | * interface ID, device ID, etc.).
|
---|
[8cd21d16] | 164 | */
|
---|
[9d12059] | 165 | void async_session_create(async_sess_t *sess, int phone, sysarg_t arg1)
|
---|
[7907cf9] | 166 | {
|
---|
[c1c0184] | 167 | sess->sess_phone = phone;
|
---|
[9d12059] | 168 | sess->connect_arg1 = arg1;
|
---|
[7907cf9] | 169 | list_initialize(&sess->conn_head);
|
---|
[a1dc7fe] | 170 |
|
---|
| 171 | /* Add to list of sessions. */
|
---|
| 172 | fibril_mutex_lock(&async_sess_mutex);
|
---|
| 173 | list_append(&sess->sess_link, &session_list_head);
|
---|
| 174 | fibril_mutex_unlock(&async_sess_mutex);
|
---|
[7907cf9] | 175 | }
|
---|
| 176 |
|
---|
[8cd21d16] | 177 | /** Destroy a session.
|
---|
| 178 | *
|
---|
| 179 | * Dismantle session structure @a sess and release any resources (connections)
|
---|
| 180 | * held by the session.
|
---|
| 181 | *
|
---|
| 182 | * @param sess Session to destroy.
|
---|
| 183 | */
|
---|
[c1c0184] | 184 | void async_session_destroy(async_sess_t *sess)
|
---|
| 185 | {
|
---|
[d1c041a] | 186 | conn_node_t *conn;
|
---|
| 187 |
|
---|
[a1dc7fe] | 188 | /* Remove from list of sessions. */
|
---|
| 189 | fibril_mutex_lock(&async_sess_mutex);
|
---|
| 190 | list_remove(&sess->sess_link);
|
---|
| 191 | fibril_mutex_unlock(&async_sess_mutex);
|
---|
| 192 |
|
---|
[d1c041a] | 193 | /* We did not connect the phone so we do not hang it up either. */
|
---|
[c1c0184] | 194 | sess->sess_phone = -1;
|
---|
[d1c041a] | 195 |
|
---|
| 196 | /* Tear down all data connections. */
|
---|
| 197 | while (!list_empty(&sess->conn_head)) {
|
---|
| 198 | conn = list_get_instance(sess->conn_head.next, conn_node_t,
|
---|
[8cd21d16] | 199 | sess_link);
|
---|
[d1c041a] | 200 |
|
---|
[8cd21d16] | 201 | list_remove(&conn->sess_link);
|
---|
[d1c041a] | 202 | list_remove(&conn->global_link);
|
---|
| 203 |
|
---|
[64d2b10] | 204 | async_hangup(conn->data_phone);
|
---|
[d1c041a] | 205 | free(conn);
|
---|
| 206 | }
|
---|
[8b5c8ae] | 207 |
|
---|
| 208 | fibril_condvar_broadcast(&avail_phone_cv);
|
---|
[c1c0184] | 209 | }
|
---|
| 210 |
|
---|
[7907cf9] | 211 | static void conn_node_initialize(conn_node_t *conn)
|
---|
| 212 | {
|
---|
[8cd21d16] | 213 | link_initialize(&conn->sess_link);
|
---|
[7907cf9] | 214 | link_initialize(&conn->global_link);
|
---|
| 215 | conn->data_phone = -1;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
[d3a1ade3] | 218 | /** Start new exchange in a session.
|
---|
[7907cf9] | 219 | *
|
---|
[c1c0184] | 220 | * @param sess_phone Session.
|
---|
[d3a1ade3] | 221 | * @return Phone representing the new exchange or a negative error
|
---|
[7907cf9] | 222 | * code.
|
---|
| 223 | */
|
---|
[d3a1ade3] | 224 | int async_exchange_begin(async_sess_t *sess)
|
---|
[7907cf9] | 225 | {
|
---|
| 226 | conn_node_t *conn;
|
---|
| 227 | int data_phone;
|
---|
| 228 |
|
---|
| 229 | fibril_mutex_lock(&async_sess_mutex);
|
---|
| 230 |
|
---|
| 231 | if (!list_empty(&sess->conn_head)) {
|
---|
| 232 | /*
|
---|
| 233 | * There are inactive connections in the session.
|
---|
| 234 | */
|
---|
| 235 | conn = list_get_instance(sess->conn_head.next, conn_node_t,
|
---|
[8cd21d16] | 236 | sess_link);
|
---|
| 237 | list_remove(&conn->sess_link);
|
---|
[7907cf9] | 238 | list_remove(&conn->global_link);
|
---|
| 239 |
|
---|
| 240 | data_phone = conn->data_phone;
|
---|
| 241 | free(conn);
|
---|
| 242 | } else {
|
---|
| 243 | /*
|
---|
| 244 | * There are no available connections in the session.
|
---|
| 245 | * Make a one-time attempt to connect a new data phone.
|
---|
| 246 | */
|
---|
| 247 | retry:
|
---|
[339dfc5] | 248 | data_phone = async_connect_me_to(sess->sess_phone,
|
---|
| 249 | sess->connect_arg1, 0, 0);
|
---|
[7907cf9] | 250 | if (data_phone >= 0) {
|
---|
| 251 | /* success, do nothing */
|
---|
| 252 | } else if (!list_empty(&inactive_conn_head)) {
|
---|
| 253 | /*
|
---|
| 254 | * We did not manage to connect a new phone. But we can
|
---|
| 255 | * try to close some of the currently inactive
|
---|
| 256 | * connections in other sessions and try again.
|
---|
| 257 | */
|
---|
| 258 | conn = list_get_instance(inactive_conn_head.next,
|
---|
| 259 | conn_node_t, global_link);
|
---|
| 260 | list_remove(&conn->global_link);
|
---|
[8cd21d16] | 261 | list_remove(&conn->sess_link);
|
---|
[7907cf9] | 262 | data_phone = conn->data_phone;
|
---|
| 263 | free(conn);
|
---|
[64d2b10] | 264 | async_hangup(data_phone);
|
---|
[7907cf9] | 265 | goto retry;
|
---|
| 266 | } else {
|
---|
| 267 | /*
|
---|
[8b5c8ae] | 268 | * Wait for a phone to become available.
|
---|
[7907cf9] | 269 | */
|
---|
[8b5c8ae] | 270 | fibril_condvar_wait(&avail_phone_cv, &async_sess_mutex);
|
---|
| 271 | goto retry;
|
---|
[7907cf9] | 272 | }
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | fibril_mutex_unlock(&async_sess_mutex);
|
---|
| 276 | return data_phone;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[d3a1ade3] | 279 | /** Finish an exchange.
|
---|
[7907cf9] | 280 | *
|
---|
[c1c0184] | 281 | * @param sess Session.
|
---|
[d3a1ade3] | 282 | * @param data_phone Phone representing the exchange within the session.
|
---|
[7907cf9] | 283 | */
|
---|
[d3a1ade3] | 284 | void async_exchange_end(async_sess_t *sess, int data_phone)
|
---|
[7907cf9] | 285 | {
|
---|
| 286 | conn_node_t *conn;
|
---|
| 287 |
|
---|
| 288 | fibril_mutex_lock(&async_sess_mutex);
|
---|
[8b5c8ae] | 289 | fibril_condvar_signal(&avail_phone_cv);
|
---|
[7907cf9] | 290 | conn = (conn_node_t *) malloc(sizeof(conn_node_t));
|
---|
| 291 | if (!conn) {
|
---|
| 292 | /*
|
---|
| 293 | * Being unable to remember the connected data phone here
|
---|
| 294 | * means that we simply hang up.
|
---|
| 295 | */
|
---|
[64d2b10] | 296 | async_hangup(data_phone);
|
---|
[8b5c8ae] | 297 | fibril_mutex_unlock(&async_sess_mutex);
|
---|
[7907cf9] | 298 | return;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | conn_node_initialize(conn);
|
---|
| 302 | conn->data_phone = data_phone;
|
---|
[8cd21d16] | 303 | list_append(&conn->sess_link, &sess->conn_head);
|
---|
[7907cf9] | 304 | list_append(&conn->global_link, &inactive_conn_head);
|
---|
| 305 | fibril_mutex_unlock(&async_sess_mutex);
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | /** @}
|
---|
| 309 | */
|
---|