1/*
2 * Copyright 2016, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(D61_BSD)
11 */
12
13/*! @file
14    @brief Server client connection module implementation.
15
16    This module implements client related management on the server. It provides a generic
17    ID free list for recycling any ID as well as methods for cleaning up client related information.
18*/
19
20#ifndef _REFOS_NAMESERV_SERV_CLIENT_CONNECTION_IMPL_LIBRARY_H_
21#define _REFOS_NAMESERV_SERV_CLIENT_CONNECTION_IMPL_LIBRARY_H_
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <assert.h>
26#include <data_struct/cvector.h>
27#include <data_struct/coat.h>
28#include <refos/refos.h>
29#include <refos-rpc/rpc.h>
30
31#define SRC_CLIENT_LIST_MAGIC 0x26B7B92A
32#define SRC_CLIENT_INVALID_ID COAT_INVALID_ID
33
34/*! @brief Server client session structure,
35
36    Client session structure. The client ID is with respect to the server itself.
37    Liveness cap is a badged endpoint cap of the process server while session cap is a badged
38    endpoint cap of the fileserver.
39 */
40struct srv_client {
41    rpc_client_state_t rpcClient; /* Inherited, must be first. */
42    uint32_t magic;
43    uint32_t cID;
44
45    seL4_CPtr liveness;
46    seL4_CPtr session;
47    int32_t deathID;
48
49    uint32_t paramBufferStart;
50    seL4_CPtr paramBuffer;
51    seL4_CPtr paramBufferSize;
52};
53
54struct srv_client_table {
55    coat_t allocTable; /* Inherited struct, must be first. */
56    cvector_t pendingFreeList;
57    uint32_t magic;
58
59    uint32_t clientMagic;
60    int maxClients;
61    int badgeBase;
62    seL4_CPtr sessionSrcEP;
63};
64
65/*! @brief Initialise client allocation table. */
66void client_table_init(struct srv_client_table *ct, int maxClients, int badgeBase, uint32_t magic,
67                       seL4_CPtr sessionSrcEP);
68
69/*! @brief Release the client allocation table. */
70void client_table_release(struct srv_client_table *ct);
71
72/*! @brief Perform client table post IPC actions.
73
74    Actually frees the client IDs that need to be freed. Should be called
75    at the end of every IPC. The reason for having this is so that a client deletion syscall may
76    be implemented; there is no way to reply to the client if we delete the client in the middle of
77    the client's syscall to delete itself.
78*/
79void client_table_postaction(struct srv_client_table *ct);
80
81/*! @brief Assigns an client ID and creates a client structure. */
82struct srv_client* client_alloc(struct srv_client_table *ct, seL4_CPtr liveness);
83
84/*! @brief Gets the associated client structure given an ID. */
85struct srv_client* client_get(struct srv_client_table *ct, int id);
86
87/*! @brief Gets the associated client structure given an unwrapped badge. */
88struct srv_client* client_get_badge(struct srv_client_table *ct, int badge);
89
90/*! @brief Queue this client up for deletion at the end of syscall. */
91void client_queue_delete(struct srv_client_table *ct, int id);
92
93/*! @brief Queue client up for deletion based on deathID. */
94int client_queue_delete_deathID(struct srv_client_table *ct, int deathID);
95
96#endif /* _REFOS_NAMESERV_SERV_CLIENT_CONNECTION_IMPL_LIBRARY_H_ */