00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <l4/sys/types.h>
00015 #include <l4/sys/consts.h>
00016 #include <l4/env/errno.h>
00017 #include <l4/util/atomic.h>
00018 #include <l4/util/macros.h>
00019
00020
00021 #include <l4/dsi/dsi.h>
00022 #include "__socket.h"
00023 #include "__dataspace.h"
00024 #include "__thread.h"
00025 #include "__sync.h"
00026 #include "__event.h"
00027 #include "__config.h"
00028 #include "__debug.h"
00029
00030
00031
00032
00033
00034
00035
00036
00037 static dsi_socket_t sockets[DSI_MAX_SOCKETS];
00038
00039
00040
00041
00042
00043 static int next_socket = 0;
00044
00045
00046
00047
00048
00049 #define SOCKET_FLAGS_USER 0x0FFFFFFFUL
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 void
00062 dsi_init_sockets(void)
00063 {
00064 int i;
00065
00066 for (i = 0; i < DSI_MAX_SOCKETS; i++)
00067 sockets[i].flags = DSI_SOCKET_UNUSED;
00068 }
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 static int
00079 __allocate_socket(void)
00080 {
00081 int i = next_socket;
00082
00083
00084 do
00085 {
00086 if (l4util_cmpxchg32(&sockets[i].flags,
00087 DSI_SOCKET_UNUSED,DSI_SOCKET_USED))
00088 {
00089
00090 next_socket = (i + 1) % DSI_MAX_SOCKETS;
00091 break;
00092 }
00093
00094 i = (i + 1) % DSI_MAX_SOCKETS;
00095 }
00096 while (i != next_socket);
00097
00098 if (i == next_socket)
00099 return -1;
00100 else
00101 return i;
00102 }
00103
00104
00105
00106
00107
00108
00109
00110
00111 int
00112 dsi_is_valid_socket(dsi_socket_t * socket)
00113 {
00114 #if 0
00115 LOGdL(DEBUG_SOCKET,"socket at 0x%08x",(dword_t) socket);
00116 #endif
00117
00118 if (socket == NULL)
00119 return 0;
00120
00121 return ((socket >= sockets) &&
00122 (socket < &sockets[DSI_MAX_SOCKETS]) &&
00123 (socket->flags != DSI_SOCKET_UNUSED));
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 int
00157 dsi_socket_create(dsi_jcp_stream_t jcp_stream, dsi_stream_cfg_t cfg,
00158 l4dm_dataspace_t * ctrl_ds, l4dm_dataspace_t * data_ds,
00159 l4_threadid_t work_id, l4_threadid_t * sync_id,
00160 l4_uint32_t flags, dsi_socket_t ** socket)
00161 {
00162 dsi_socket_t * s;
00163 int sid,ret,i;
00164
00165 *socket = NULL;
00166
00167
00168 if (l4dm_is_invalid_ds(*data_ds))
00169 {
00170 LOG_Error("DSI: invalid data area!");
00171 return -L4_EINVAL;
00172 }
00173
00174 LOGdL(DEBUG_SOCKET,"data_ds: %d at "l4util_idfmt,
00175 data_ds->id, l4util_idstr(data_ds->manager));
00176
00177 if (l4_is_invalid_id(work_id))
00178 {
00179 LOG_Error("DSI: invalid work thread");
00180 return -L4_EINVAL;
00181 }
00182
00183
00184 sid = __allocate_socket();
00185 if (sid == -1)
00186 {
00187
00188 LOG_Error("DSI: no socket available");
00189 return -DSI_ENOSOCKET;
00190 }
00191 s = &sockets[sid];
00192 s->socket_id = sid;
00193
00194
00195 s->flags |= (flags & SOCKET_FLAGS_USER);
00196
00197
00198 if (l4_is_invalid_id(*sync_id))
00199 {
00200
00201 ret = dsi_create_sync_thread(s);
00202 if (ret)
00203 {
00204
00205 LOG_Error("DSI: create synchronization thread failed: %s (%d)",
00206 l4env_errstr(ret), ret);
00207 s->flags = DSI_SOCKET_UNUSED;
00208 return ret;
00209 }
00210 s->flags |= DSI_SOCKET_FREE_SYNC;
00211 *sync_id = s->sync_th;
00212 }
00213 else
00214
00215 s->sync_th = *sync_id;
00216
00217
00218 if (l4dm_is_invalid_ds(*ctrl_ds))
00219 {
00220
00221 LOGdL(DEBUG_SOCKET,"create control area");
00222
00223 ret = dsi_create_ctrl_area(s,jcp_stream,cfg);
00224 if(ret)
00225 {
00226 LOG_Error("DSI: error creating control area: %s (%d)",
00227 l4env_errstr(ret), ret);
00228 return ret;
00229 }
00230 s->flags |= DSI_SOCKET_FREE_CTRL;
00231 *ctrl_ds = s->ctrl_ds;
00232
00233 LOGdL(DEBUG_SOCKET,"control dataspace: %d at "l4util_idfmt,
00234 ctrl_ds->id, l4util_idstr(ctrl_ds->manager));
00235 }
00236 else
00237 {
00238
00239 ret = dsi_set_ctrl_area(s,*ctrl_ds,jcp_stream,cfg);
00240 }
00241
00242 if (ret)
00243 {
00244
00245 LOG_Error("DSI: create/setup control area failed: %s (%d)",
00246 l4env_errstr(ret), ret);
00247 if (s->flags & DSI_SOCKET_FREE_SYNC)
00248 dsi_shutdown_sync_thread(s);
00249 s->flags = DSI_SOCKET_UNUSED;
00250 return ret;
00251 }
00252
00253
00254 LOGdL(DEBUG_SOCKET,"setting data area (%d at "l4util_idfmt")...",
00255 data_ds->id, l4util_idstr(data_ds->manager));
00256
00257 ret = dsi_set_data_area(s,*data_ds);
00258 if (ret)
00259 {
00260
00261 LOG_Error("DSI: setup data area failed: %s (%d)",
00262 l4env_errstr(ret), ret);
00263 if (s->flags & DSI_SOCKET_FREE_SYNC)
00264 dsi_shutdown_sync_thread(s);
00265 if (s->flags & DSI_SOCKET_FREE_CTRL)
00266 dsi_release_ctrl_area(s);
00267 s->flags = DSI_SOCKET_UNUSED;
00268 return ret;
00269 }
00270
00271
00272 for (i = 0; i < DSI_MAX_EVENTS; i++)
00273 s->events[i] = 0;
00274
00275
00276 s->work_th = work_id;
00277 s->sync_callback = NULL;
00278 s->release_callback = NULL;
00279 s->packet_count = 0;
00280 s->next_packet = 0;
00281 s->next_sg_elem = 0;
00282 s->waiting = 0;
00283 s->clients = NULL;
00284
00285
00286 *socket = s;
00287
00288 return 0;
00289 }
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303 int
00304 dsi_socket_close(dsi_socket_t * socket)
00305 {
00306
00307 if (!dsi_is_valid_socket(socket))
00308 return -L4_EINVAL;
00309
00310
00311 if (socket->flags & DSI_SOCKET_FREE_SYNC)
00312 dsi_shutdown_sync_thread(socket);
00313
00314
00315 dsi_release_ctrl_area(socket);
00316
00317
00318 dsi_release_data_area(socket);
00319
00320
00321 socket->flags = DSI_SOCKET_UNUSED;
00322
00323
00324 return 0;
00325 }
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341 int
00342 dsi_socket_stop(dsi_socket_t * socket)
00343 {
00344
00345 if (!dsi_is_valid_socket(socket))
00346 return -L4_EINVAL;
00347
00348
00349 if (socket->flags & DSI_SOCKET_FREE_SYNC)
00350 dsi_shutdown_sync_thread(socket);
00351 socket->flags &= ~DSI_SOCKET_FREE_SYNC;
00352
00353
00354 return 0;
00355 }
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371 int
00372 dsi_socket_connect(dsi_socket_t * socket,
00373 const dsi_socket_ref_t * remote_socket)
00374 {
00375 int ret;
00376
00377
00378 if (!dsi_is_valid_socket(socket))
00379 {
00380 LOG_Error("DSI: invalid socket");
00381 return -L4_EINVAL;
00382 }
00383
00384 if (l4_is_invalid_id(remote_socket->work_th) ||
00385 l4_is_invalid_id(remote_socket->sync_th) ||
00386 !l4_task_equal(remote_socket->work_th,remote_socket->sync_th))
00387 {
00388 LOG_printf("work "l4util_idfmt", sync "l4util_idfmt"\n",
00389 l4util_idstr(remote_socket->work_th),
00390 l4util_idstr(remote_socket->sync_th));
00391 LOG_Error("DSI: invalid data in socket descriptor");
00392 return -L4_EINVAL;
00393 }
00394
00395 LOGdL(DEBUG_CONNECT,"connecting socket %d",socket->socket_id);
00396 LOGdL(DEBUG_CONNECT,"remote: %d, "l4util_idfmt", "l4util_idfmt,
00397 remote_socket->socket,
00398 l4util_idstr(remote_socket->work_th),
00399 l4util_idstr(remote_socket->sync_th));
00400
00401
00402 socket->remote_socket.socket = remote_socket->socket;
00403 socket->remote_socket.work_th = remote_socket->work_th;
00404 socket->remote_socket.sync_th = remote_socket->sync_th;
00405
00406
00407 ret = dsi_start_sync_thread(socket);
00408 if (ret)
00409 {
00410 LOG_Error("DSI: connect: wakeup synchronization thread failed!");
00411 return ret;
00412 }
00413
00414
00415 return 0;
00416 }
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431 int
00432 dsi_socket_set_sync_callback(dsi_socket_t * socket,
00433 dsi_sync_callback_fn_t func)
00434 {
00435
00436 if (!dsi_is_valid_socket(socket) || (func == NULL))
00437 return -L4_EINVAL;
00438
00439
00440 socket->sync_callback = func;
00441 socket->flags |= DSI_SOCKET_SYNC_CALLBACK;
00442
00443
00444 return 0;
00445 }
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461 int
00462 dsi_socket_set_release_callback(dsi_socket_t * socket,
00463 dsi_release_callback_fn_t func)
00464 {
00465
00466 if (!dsi_is_valid_socket(socket) || (func == NULL))
00467 return -L4_EINVAL;
00468
00469
00470 socket->release_callback = func;
00471 socket->flags |= DSI_SOCKET_RELEASE_CALLBACK;
00472
00473
00474 return 0;
00475 }
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490 int
00491 dsi_socket_get_ref(dsi_socket_t * socket, dsi_socket_ref_t * ref)
00492 {
00493
00494 if (!dsi_is_valid_socket(socket))
00495 return -L4_EINVAL;
00496
00497
00498 ref->socket = socket->socket_id;
00499 ref->work_th = socket->work_th;
00500 ref->sync_th = socket->sync_th;
00501 ref->event_th = dsi_get_event_thread_id();
00502
00503 return 0;
00504 }
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519 int
00520 dsi_socket_get_descriptor(dsi_socketid_t id, dsi_socket_t ** socket)
00521 {
00522
00523 if ((id < 0) || (id >= DSI_MAX_SOCKETS))
00524 return -L4_EINVAL;
00525
00526 if (sockets[id].flags == DSI_SOCKET_UNUSED)
00527 return -L4_EINVAL;
00528
00529
00530 *socket = &sockets[id];
00531 return 0;
00532 }
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547 int
00548 dsi_socket_get_data_area(dsi_socket_t * socket, void **data_area,
00549 l4_size_t * area_size)
00550 {
00551
00552 if (!dsi_is_valid_socket(socket))
00553 return -L4_EINVAL;
00554
00555
00556 *data_area = socket->data_area;
00557 *area_size = socket->data_size;
00558
00559 return 0;
00560 }
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574 int
00575 dsi_socket_set_flags(dsi_socket_t * socket, l4_uint32_t flags)
00576 {
00577
00578 if (!dsi_is_valid_socket(socket))
00579 return -L4_EINVAL;
00580
00581
00582 socket->flags |= (flags & DSI_SOCKET_USER_FLAGS);
00583
00584
00585 return 0;
00586 }
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600 int
00601 dsi_socket_clear_flags(dsi_socket_t * socket, l4_uint32_t flags)
00602 {
00603
00604 if (!dsi_is_valid_socket(socket))
00605 return -L4_EINVAL;
00606
00607
00608 socket->flags &= ~(flags & DSI_SOCKET_USER_FLAGS);
00609
00610
00611 return 0;
00612 }
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626 int
00627 dsi_socket_test_flag(dsi_socket_t * socket, l4_uint32_t flag)
00628 {
00629
00630 if (!dsi_is_valid_socket(socket))
00631 return 0;
00632
00633
00634 return (socket->flags & (flag & DSI_SOCKET_USER_FLAGS));
00635 }
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656 int
00657 dsi_socket_set_event(dsi_socket_t * socket, l4_uint32_t events)
00658 {
00659
00660 if (!dsi_is_valid_socket(socket))
00661 return -L4_EINVAL;
00662
00663 LOGdL(DEBUG_EVENT,"set events 0x%08x",events);
00664
00665
00666 return dsi_event_set(socket->socket_id,events);
00667 }
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680 int
00681 dsi_socket_get_packet_num(dsi_socket_t * socket)
00682 {
00683
00684 if (!dsi_is_valid_socket(socket))
00685 return -L4_EINVAL;
00686
00687 return socket->num_packets;
00688 }
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702 int
00703 dsi_socket_get_num_committed_packets(dsi_socket_t * socket)
00704 {
00705
00706 if (!dsi_is_valid_socket(socket))
00707 return -L4_EINVAL;
00708
00709 return socket->header->packets_committed;
00710 }