source: mainline/uspace/lib/packet/generic/packet_server.c@ 0b4a67a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0b4a67a was 1bfd3d3, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Replace @returns with @return.

  • Property mode set to 100644
File size: 11.0 KB
RevLine 
[21580dd]1/*
2 * Copyright (c) 2009 Lukas Mejdrech
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
[515a00da]29/** @addtogroup libpacket
[21580dd]30 * @{
31 */
32
33/** @file
[515a00da]34 * Packet server implementation.
[21580dd]35 */
36
[515a00da]37#include <packet_server.h>
38#include <packet_local.h>
39
[21580dd]40#include <align.h>
41#include <assert.h>
42#include <async.h>
43#include <errno.h>
44#include <fibril_synch.h>
45#include <unistd.h>
[d52fbaf]46#include <sys/mman.h>
[21580dd]47
48#include <ipc/ipc.h>
[d52fbaf]49#include <ipc/packet.h>
[514ee46]50#include <ipc/net.h>
51
[c69d327]52#include <net/packet.h>
53#include <net/packet_header.h>
[21580dd]54
55#define FREE_QUEUES_COUNT 7
56
[515a00da]57/** The default address length reserved for new packets. */
[21580dd]58#define DEFAULT_ADDR_LEN 32
59
[515a00da]60/** The default prefix reserved for new packets. */
[21580dd]61#define DEFAULT_PREFIX 64
62
[515a00da]63/** The default suffix reserved for new packets. */
[21580dd]64#define DEFAULT_SUFFIX 64
65
[515a00da]66/** Packet server global data. */
67static struct {
68 /** Safety lock. */
[21580dd]69 fibril_mutex_t lock;
[515a00da]70 /** Free packet queues. */
[aadf01e]71 packet_t free[FREE_QUEUES_COUNT];
[515a00da]72
73 /**
74 * Packet length upper bounds of the free packet queues. The maximal
75 * lengths of packets in each queue in the ascending order. The last
76 * queue is not limited.
[21580dd]77 */
[aadf01e]78 size_t sizes[FREE_QUEUES_COUNT];
[515a00da]79
80 /** Total packets allocated. */
[21580dd]81 unsigned int count;
82} ps_globals = {
[e721462]83 .lock = FIBRIL_MUTEX_INITIALIZER(ps_globals.lock),
[515a00da]84 .free = {
85 NULL,
86 NULL,
87 NULL,
88 NULL,
89 NULL,
90 NULL,
91 NULL
92 },
93 .sizes = {
94 PAGE_SIZE,
95 PAGE_SIZE * 2,
96 PAGE_SIZE * 4,
97 PAGE_SIZE * 8,
98 PAGE_SIZE * 16,
99 PAGE_SIZE * 32,
100 PAGE_SIZE * 64
101 },
[21580dd]102 .count = 0
103};
104
[f772bc55]105int packet_translate_local(int phone, packet_t *packet, packet_id_t packet_id)
[14f1db0]106{
107 if (!packet)
[849ed54]108 return EINVAL;
[14f1db0]109
[849ed54]110 *packet = pm_find(packet_id);
111 return (*packet) ? EOK : ENOENT;
112}
[21580dd]113
[849ed54]114/** Clears and initializes the packet according to the given dimensions.
[515a00da]115 *
116 * @param[in] packet The packet to be initialized.
117 * @param[in] addr_len The source and destination addresses maximal length in
118 * bytes.
119 * @param[in] max_prefix The maximal prefix length in bytes.
120 * @param[in] max_content The maximal content length in bytes.
121 * @param[in] max_suffix The maximal suffix length in bytes.
[21580dd]122 */
[515a00da]123static void
124packet_init(packet_t packet, size_t addr_len, size_t max_prefix,
125 size_t max_content, size_t max_suffix)
126{
[849ed54]127 // clear the packet content
[515a00da]128 bzero(((void *) packet) + sizeof(struct packet),
129 packet->length - sizeof(struct packet));
130
[849ed54]131 // clear the packet header
132 packet->order = 0;
133 packet->metric = 0;
134 packet->previous = 0;
135 packet->next = 0;
136 packet->addr_len = 0;
137 packet->src_addr = sizeof(struct packet);
138 packet->dest_addr = packet->src_addr + addr_len;
139 packet->max_prefix = max_prefix;
140 packet->max_content = max_content;
141 packet->data_start = packet->dest_addr + addr_len + packet->max_prefix;
142 packet->data_end = packet->data_start;
143}
[21580dd]144
[515a00da]145/** Creates a new packet of dimensions at least as given.
146 *
147 * Should be used only when the global data are locked.
148 *
149 * @param[in] length The total length of the packet, including the header,
150 * the addresses and the data of the packet.
151 * @param[in] addr_len The source and destination addresses maximal length in
152 * bytes.
153 * @param[in] max_prefix The maximal prefix length in bytes.
154 * @param[in] max_content The maximal content length in bytes.
155 * @param[in] max_suffix The maximal suffix length in bytes.
[1bfd3d3]156 * @return The packet of dimensions at least as given.
157 * @return NULL if there is not enough memory left.
[21580dd]158 */
[515a00da]159static packet_t
160packet_create(size_t length, size_t addr_len, size_t max_prefix,
161 size_t max_content, size_t max_suffix)
162{
[849ed54]163 packet_t packet;
[88b127b]164 int rc;
[849ed54]165
166 // already locked
[515a00da]167 packet = (packet_t) mmap(NULL, length, PROTO_READ | PROTO_WRITE,
168 MAP_SHARED | MAP_ANONYMOUS, 0, 0);
169 if (packet == MAP_FAILED)
[849ed54]170 return NULL;
[515a00da]171
[9880813f]172 ps_globals.count++;
[849ed54]173 packet->packet_id = ps_globals.count;
174 packet->length = length;
175 packet_init(packet, addr_len, max_prefix, max_content, max_suffix);
176 packet->magic_value = PACKET_MAGIC_VALUE;
[88b127b]177 rc = pm_add(packet);
178 if (rc != EOK) {
[849ed54]179 munmap(packet, packet->length);
180 return NULL;
181 }
[515a00da]182
[849ed54]183 return packet;
184}
185
[14f1db0]186/** Return the packet of dimensions at least as given.
187 *
188 * Try to reuse free packets first.
189 * Create a new packet aligned to the memory page size if none available.
190 * Lock the global data during its processing.
191 *
[515a00da]192 * @param[in] addr_len The source and destination addresses maximal length in
193 * bytes.
194 * @param[in] max_prefix The maximal prefix length in bytes.
[14f1db0]195 * @param[in] max_content The maximal content length in bytes.
[515a00da]196 * @param[in] max_suffix The maximal suffix length in bytes.
197 * @return The packet of dimensions at least as given.
198 * @return NULL if there is not enough memory left.
[21580dd]199 */
[515a00da]200static packet_t
201packet_get_local(size_t addr_len, size_t max_prefix, size_t max_content,
202 size_t max_suffix)
[14f1db0]203{
[515a00da]204 size_t length = ALIGN_UP(sizeof(struct packet) + 2 * addr_len +
205 max_prefix + max_content + max_suffix, PAGE_SIZE);
[14f1db0]206
[849ed54]207 fibril_mutex_lock(&ps_globals.lock);
[14f1db0]208
209 packet_t packet;
210 unsigned int index;
211
[279b80c]212 for (index = 0; index < FREE_QUEUES_COUNT; index++) {
[376a7fd]213 if ((length > ps_globals.sizes[index]) &&
214 (index < FREE_QUEUES_COUNT - 1))
[515a00da]215 continue;
216
217 packet = ps_globals.free[index];
218 while (packet_is_valid(packet) && (packet->length < length))
219 packet = pm_find(packet->next);
220
221 if (packet_is_valid(packet)) {
222 if (packet == ps_globals.free[index])
223 ps_globals.free[index] = pq_detach(packet);
224 else
225 pq_detach(packet);
[14f1db0]226
[515a00da]227 packet_init(packet, addr_len, max_prefix, max_content,
228 max_suffix);
229 fibril_mutex_unlock(&ps_globals.lock);
[14f1db0]230
[515a00da]231 return packet;
[849ed54]232 }
[aadf01e]233 }
[14f1db0]234
235 packet = packet_create(length, addr_len, max_prefix, max_content,
236 max_suffix);
237
[849ed54]238 fibril_mutex_unlock(&ps_globals.lock);
[14f1db0]239
[849ed54]240 return packet;
[21580dd]241}
242
[14f1db0]243packet_t packet_get_4_local(int phone, size_t max_content, size_t addr_len,
244 size_t max_prefix, size_t max_suffix)
245{
246 return packet_get_local(addr_len, max_prefix, max_content, max_suffix);
[21580dd]247}
248
[14f1db0]249packet_t packet_get_1_local(int phone, size_t content)
250{
251 return packet_get_local(DEFAULT_ADDR_LEN, DEFAULT_PREFIX, content,
252 DEFAULT_SUFFIX);
[21580dd]253}
254
[14f1db0]255/** Release the packet and returns it to the appropriate free packet queue.
256 *
[515a00da]257 * Should be used only when the global data are locked.
[14f1db0]258 *
[515a00da]259 * @param[in] packet The packet to be released.
[14f1db0]260 *
[849ed54]261 */
[515a00da]262static void packet_release(packet_t packet)
263{
[849ed54]264 int index;
265 int result;
266
[515a00da]267 for (index = 0; (index < FREE_QUEUES_COUNT - 1) &&
268 (packet->length > ps_globals.sizes[index]); index++) {
269 ;
270 }
271
272 result = pq_add(&ps_globals.free[index], packet, packet->length,
273 packet->length);
[849ed54]274 assert(result == EOK);
275}
276
277/** Releases the packet queue.
[515a00da]278 *
279 * @param[in] packet_id The first packet identifier.
[1bfd3d3]280 * @return EOK on success.
281 * @return ENOENT if there is no such packet.
[849ed54]282 */
[515a00da]283static int packet_release_wrapper(packet_id_t packet_id)
284{
[849ed54]285 packet_t packet;
286
287 packet = pm_find(packet_id);
[515a00da]288 if (!packet_is_valid(packet))
[849ed54]289 return ENOENT;
[515a00da]290
[849ed54]291 fibril_mutex_lock(&ps_globals.lock);
292 pq_destroy(packet, packet_release);
293 fibril_mutex_unlock(&ps_globals.lock);
[515a00da]294
[849ed54]295 return EOK;
296}
297
[14f1db0]298void pq_release_local(int phone, packet_id_t packet_id)
299{
[aadf01e]300 (void) packet_release_wrapper(packet_id);
[21580dd]301}
302
[849ed54]303/** Shares the packet memory block.
[515a00da]304 * @param[in] packet The packet to be shared.
[1bfd3d3]305 * @return EOK on success.
306 * @return EINVAL if the packet is not valid.
307 * @return EINVAL if the calling module does not accept the memory.
308 * @return ENOMEM if the desired and actual sizes differ.
309 * @return Other error codes as defined for the
[515a00da]310 * async_share_in_finalize() function.
[849ed54]311 */
[515a00da]312static int packet_reply(const packet_t packet)
313{
[849ed54]314 ipc_callid_t callid;
315 size_t size;
316
[515a00da]317 if (!packet_is_valid(packet))
[849ed54]318 return EINVAL;
[515a00da]319
[57f737a]320 if (!async_share_in_receive(&callid, &size)) {
321 ipc_answer_0(callid, EINVAL);
[515a00da]322 return EINVAL;
[57f737a]323 }
[a649e73f]324
325 if (size != packet->length) {
326 ipc_answer_0(callid, ENOMEM);
327 return ENOMEM;
328 }
[515a00da]329
330 return async_share_in_finalize(callid, packet,
331 PROTO_READ | PROTO_WRITE);
[849ed54]332}
333
[515a00da]334/** Processes the packet server message.
335 *
336 * @param[in] callid The message identifier.
337 * @param[in] call The message parameters.
338 * @param[out] answer The message answer parameters.
339 * @param[out] answer_count The last parameter for the actual answer in the
340 * answer parameter.
[1bfd3d3]341 * @return EOK on success.
342 * @return ENOMEM if there is not enough memory left.
343 * @return ENOENT if there is no such packet as in the packet
[515a00da]344 * message parameter.
[1bfd3d3]345 * @return ENOTSUP if the message is not known.
346 * @return Other error codes as defined for the
[515a00da]347 * packet_release_wrapper() function.
348 */
349int
350packet_server_message(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer,
351 int *answer_count)
352{
[21580dd]353 packet_t packet;
354
[aadf01e]355 *answer_count = 0;
[515a00da]356 switch (IPC_GET_METHOD(*call)) {
357 case IPC_M_PHONE_HUNGUP:
358 return EOK;
359
360 case NET_PACKET_CREATE_1:
361 packet = packet_get_local(DEFAULT_ADDR_LEN, DEFAULT_PREFIX,
362 IPC_GET_CONTENT(call), DEFAULT_SUFFIX);
363 if (!packet)
364 return ENOMEM;
365 *answer_count = 2;
366 IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id);
367 IPC_SET_ARG2(*answer, (ipcarg_t) packet->length);
368 return EOK;
369
370 case NET_PACKET_CREATE_4:
371 packet = packet_get_local(
372 ((DEFAULT_ADDR_LEN < IPC_GET_ADDR_LEN(call)) ?
373 IPC_GET_ADDR_LEN(call) : DEFAULT_ADDR_LEN),
374 DEFAULT_PREFIX + IPC_GET_PREFIX(call),
375 IPC_GET_CONTENT(call),
376 DEFAULT_SUFFIX + IPC_GET_SUFFIX(call));
377 if (!packet)
378 return ENOMEM;
379 *answer_count = 2;
380 IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id);
381 IPC_SET_ARG2(*answer, (ipcarg_t) packet->length);
382 return EOK;
383
384 case NET_PACKET_GET:
385 packet = pm_find(IPC_GET_ID(call));
386 if (!packet_is_valid(packet))
387 return ENOENT;
388 return packet_reply(packet);
389
390 case NET_PACKET_GET_SIZE:
391 packet = pm_find(IPC_GET_ID(call));
392 if (!packet_is_valid(packet))
393 return ENOENT;
394 IPC_SET_ARG1(*answer, (ipcarg_t) packet->length);
395 *answer_count = 1;
396 return EOK;
397
398 case NET_PACKET_RELEASE:
399 return packet_release_wrapper(IPC_GET_ID(call));
[21580dd]400 }
[515a00da]401
[21580dd]402 return ENOTSUP;
403}
404
405/** @}
406 */
Note: See TracBrowser for help on using the repository browser.