source: mainline/uspace/srv/ns/service.c@ 2732c94

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2732c94 was 0ca7286, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

Added resizing to user space (single-threaded) hash_table. Resizes in a way to mitigate effects of bad hash functions. Change of interface affected many files.

  • Property mode set to 100644
File size: 6.8 KB
Line 
1/*
2 * Copyright (c) 2009 Martin Decky
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 ns
30 * @{
31 */
32
33#include <ipc/ipc.h>
34#include <adt/hash_table.h>
35#include <assert.h>
36#include <errno.h>
37#include <stdio.h>
38#include <malloc.h>
39#include "service.h"
40#include "ns.h"
41
42
43/** Service hash table item. */
44typedef struct {
45 link_t link;
46 sysarg_t service; /**< Service ID. */
47 sysarg_t phone; /**< Phone registered with the service. */
48 sysarg_t in_phone_hash; /**< Incoming phone hash. */
49} hashed_service_t;
50
51/** Compute hash index into service hash table.
52 *
53 * @param key Pointer keys. However, only the first key (i.e. service number)
54 * is used to compute the hash index.
55 *
56 * @return Hash index corresponding to key[0].
57 *
58 */
59static size_t service_key_hash(unsigned long key[])
60{
61 assert(key);
62 return key[0];
63}
64
65static size_t service_hash(const link_t *item)
66{
67 hashed_service_t *hs = hash_table_get_instance(item, hashed_service_t, link);
68 unsigned long key = hs->service;
69 return service_key_hash(&key);
70}
71
72/** Compare a key with hashed item.
73 *
74 * This compare function always ignores the third key.
75 * It exists only to make it possible to remove records
76 * originating from connection with key[1] in_phone_hash
77 * value. Note that this is close to being classified
78 * as a nasty hack.
79 *
80 * @param key Array of keys.
81 * @param keys Must be lesser or equal to 3.
82 * @param item Pointer to a hash table item.
83 *
84 * @return Non-zero if the key matches the item, zero otherwise.
85 *
86 */
87static bool service_match(unsigned long key[], size_t keys, const link_t *item)
88{
89 assert(key);
90 assert(keys <= 3);
91 assert(item);
92
93 hashed_service_t *hs = hash_table_get_instance(item, hashed_service_t, link);
94
95 if (keys == 2)
96 return ((key[0] == hs->service) && (key[1] == hs->in_phone_hash));
97 else
98 return (key[0] == hs->service);
99}
100
101/** Perform actions after removal of item from the hash table.
102 *
103 * @param item Item that was removed from the hash table.
104 *
105 */
106static void service_remove(link_t *item)
107{
108 assert(item);
109 free(hash_table_get_instance(item, hashed_service_t, link));
110}
111
112/** Operations for service hash table. */
113static hash_table_ops_t service_hash_table_ops = {
114 .hash = service_hash,
115 .key_hash = service_key_hash,
116 .match = service_match,
117 .equal = 0,
118 .remove_callback = service_remove
119};
120
121/** Service hash table structure. */
122static hash_table_t service_hash_table;
123
124/** Pending connection structure. */
125typedef struct {
126 link_t link;
127 sysarg_t service; /**< Number of the service. */
128 ipc_callid_t callid; /**< Call ID waiting for the connection */
129 sysarg_t arg2; /**< Second argument */
130 sysarg_t arg3; /**< Third argument */
131} pending_conn_t;
132
133static list_t pending_conn;
134
135int service_init(void)
136{
137 if (!hash_table_create(&service_hash_table, 0, 3, &service_hash_table_ops)) {
138 printf(NAME ": No memory available for services\n");
139 return ENOMEM;
140 }
141
142 list_initialize(&pending_conn);
143
144 return EOK;
145}
146
147/** Process pending connection requests */
148void process_pending_conn(void)
149{
150loop:
151 list_foreach(pending_conn, cur) {
152 pending_conn_t *pr = list_get_instance(cur, pending_conn_t, link);
153
154 unsigned long keys[3] = {
155 pr->service,
156 0,
157 0
158 };
159
160 link_t *link = hash_table_find(&service_hash_table, keys);
161 if (!link)
162 continue;
163
164 hashed_service_t *hs = hash_table_get_instance(link, hashed_service_t, link);
165 (void) ipc_forward_fast(pr->callid, hs->phone, pr->arg2,
166 pr->arg3, 0, IPC_FF_NONE);
167
168 list_remove(cur);
169 free(pr);
170 goto loop;
171 }
172}
173
174/** Register service.
175 *
176 * @param service Service to be registered.
177 * @param phone Phone to be used for connections to the service.
178 * @param call Pointer to call structure.
179 *
180 * @return Zero on success or a value from @ref errno.h.
181 *
182 */
183int register_service(sysarg_t service, sysarg_t phone, ipc_call_t *call)
184{
185 unsigned long keys[3] = {
186 service,
187 call->in_phone_hash,
188 0
189 };
190
191 if (hash_table_find(&service_hash_table, keys))
192 return EEXISTS;
193
194 hashed_service_t *hs = (hashed_service_t *) malloc(sizeof(hashed_service_t));
195 if (!hs)
196 return ENOMEM;
197
198 link_initialize(&hs->link);
199 hs->service = service;
200 hs->phone = phone;
201 hs->in_phone_hash = call->in_phone_hash;
202 hash_table_insert(&service_hash_table, &hs->link);
203
204 return EOK;
205}
206
207/** Connect client to service.
208 *
209 * @param service Service to be connected to.
210 * @param call Pointer to call structure.
211 * @param callid Call ID of the request.
212 *
213 * @return Zero on success or a value from @ref errno.h.
214 *
215 */
216void connect_to_service(sysarg_t service, ipc_call_t *call, ipc_callid_t callid)
217{
218 sysarg_t retval;
219 unsigned long keys[3] = {
220 service,
221 0,
222 0
223 };
224
225 link_t *link = hash_table_find(&service_hash_table, keys);
226 if (!link) {
227 if (IPC_GET_ARG4(*call) & IPC_FLAG_BLOCKING) {
228 /* Blocking connection, add to pending list */
229 pending_conn_t *pr =
230 (pending_conn_t *) malloc(sizeof(pending_conn_t));
231 if (!pr) {
232 retval = ENOMEM;
233 goto out;
234 }
235
236 link_initialize(&pr->link);
237 pr->service = service;
238 pr->callid = callid;
239 pr->arg2 = IPC_GET_ARG2(*call);
240 pr->arg3 = IPC_GET_ARG3(*call);
241 list_append(&pr->link, &pending_conn);
242 return;
243 }
244 retval = ENOENT;
245 goto out;
246 }
247
248 hashed_service_t *hs = hash_table_get_instance(link, hashed_service_t, link);
249 (void) ipc_forward_fast(callid, hs->phone, IPC_GET_ARG2(*call),
250 IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
251 return;
252
253out:
254 if (!(callid & IPC_CALLID_NOTIFICATION))
255 ipc_answer_0(callid, retval);
256}
257
258/**
259 * @}
260 */
Note: See TracBrowser for help on using the repository browser.